112 lines
2.9 KiB
C++
112 lines
2.9 KiB
C++
#pragma once
|
|
|
|
#include "componentbasic_global.h"
|
|
#include <simsbasic.h>
|
|
|
|
|
|
/// <summary>
|
|
/// 消息处理单元基类
|
|
/// </summary>
|
|
/// <typeparam name="MsgA">输入类型</typeparam>
|
|
/// <typeparam name="MsgB">输出类型</typeparam>
|
|
template<typename MsgA, typename MsgB> class WsRespond {
|
|
public:
|
|
using SelfType = WsRespond<MsgA, MsgB>;
|
|
|
|
virtual ~WsRespond() = default;
|
|
/// <summary>
|
|
/// Respond的类型签名
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
static WsRespondSignatureType signature()
|
|
{
|
|
return std::make_pair<QString, QString>(MsgA().topicString(), MsgB().topicString());
|
|
}
|
|
|
|
/// <summary>
|
|
/// 子类中实际处理函数
|
|
/// </summary>
|
|
/// <param name="in"></param>
|
|
/// <returns></returns>
|
|
virtual std::shared_ptr<MsgB> execute(std::shared_ptr<const MsgA> in) = 0;
|
|
/// <summary>
|
|
/// WsRespond构造函数
|
|
/// </summary>
|
|
/// <param name="map">注册函数</param>
|
|
explicit WsRespond(QHash<WsRespondSignatureType, WsRespondEntry>& map) {
|
|
WsRespondEntry ins = [=](std::shared_ptr<const WsMessage> in) -> std::shared_ptr<WsMessage> {
|
|
return this->execute(std::static_pointer_cast<const MsgA>(in));
|
|
};
|
|
map[SelfType::signature()] = ins;
|
|
}
|
|
};
|
|
|
|
|
|
template <typename... ProcSigs>struct _ProcRoute_;
|
|
template <> struct COMPONENTBASIC_EXPORT _ProcRoute_<>
|
|
{
|
|
QHash<WsRespondSignatureType, WsRespondEntry> _execute_map;
|
|
|
|
_ProcRoute_() = default;
|
|
};
|
|
template <typename ProcSig, typename... ProcSigs>
|
|
struct _ProcRoute_<ProcSig, ProcSigs...> : public _ProcRoute_<ProcSigs...>, public ProcSig
|
|
{
|
|
_ProcRoute_() : _ProcRoute_<ProcSigs...>(), ProcSig(_ProcRoute_<>::_execute_map) {}
|
|
};
|
|
|
|
template <typename... Procs>
|
|
struct ProcList : public _ProcRoute_<Procs...>, public WsComponent
|
|
{
|
|
using SuperType = ProcList<Procs...>;
|
|
|
|
ProcList() : _ProcRoute_<Procs...>() {}
|
|
|
|
// 通过 WsComponent 继承
|
|
QList<QString> inputTypes() const override
|
|
{
|
|
QList<QString> list;
|
|
for (auto key : _ProcRoute_<>::_execute_map.keys()) {
|
|
list.append(key.first);
|
|
}
|
|
return list;
|
|
}
|
|
WsRespondEntry getEntryWithSignature(const WsRespondSignatureType& t) const override
|
|
{
|
|
for (auto key : _ProcRoute_<>::_execute_map.keys()) {
|
|
if (key == t)
|
|
return _ProcRoute_<>::_execute_map[key];
|
|
}
|
|
}
|
|
QList<WsRespondEntry> getEntrysWithInType(const QString& msg_type) const override
|
|
{
|
|
QList<WsRespondEntry> list;
|
|
for (auto key : _ProcRoute_<>::_execute_map.keys()) {
|
|
if (key.first == msg_type)
|
|
list.append(_ProcRoute_<>::_execute_map[key]);
|
|
}
|
|
return list;
|
|
}
|
|
};
|
|
|
|
#include <messagebasic.h>
|
|
class COMPONENTBASIC_EXPORT Visible3DPlugin : public ProcList<
|
|
WsRespond<Get3DVolume, Volume3DDesc>
|
|
>
|
|
{
|
|
public:
|
|
|
|
// 通过 ProcList 继承
|
|
std::shared_ptr<Volume3DDesc> execute(std::shared_ptr<const Get3DVolume> in) override;
|
|
|
|
void recoveryFrom(const QJsonObject& obj) override;
|
|
|
|
void saveTo(QJsonObject& obj) const override;
|
|
|
|
std::shared_ptr<WsComponent> defaultNew() const override;
|
|
|
|
void bindEntity(uint64_t entity_id) override;
|
|
|
|
QString name() const override;
|
|
|
|
}; |