From c220cb9216f99fa4ed8fbaa5675dd367cd79fcab Mon Sep 17 00:00:00 2001 From: sebaszm Date: Wed, 11 Dec 2024 23:58:49 +0100 Subject: [PATCH] Facilitate automatic JSON-RPC object lookup --- Source/core/JSONRPC.h | 69 ++++++++++++++++++- Source/plugins/JSONRPC.h | 140 +++++++++++++++++++++++++++++++++++---- 2 files changed, 194 insertions(+), 15 deletions(-) diff --git a/Source/core/JSONRPC.h b/Source/core/JSONRPC.h index 0139a3a9c..6ec43da58 100644 --- a/Source/core/JSONRPC.h +++ b/Source/core/JSONRPC.h @@ -778,9 +778,13 @@ namespace Core { { using ARG0 = typename std::decay::template argument<0>::type>::type; using ARG1 = typename std::decay::template argument<1>::type>::type; + using ARG2 = typename std::decay::template argument<2>::type>::type; + constexpr auto HAS_CONTEXT = std::is_same::value; + constexpr auto HAS_INDEX = (std::is_same::value | std::is_same::value | std::is_same::value); + constexpr auto HAS_INSTANCEID = (std::is_same::value | std::is_same::value); InternalRegister( - ::TemplateIntToType::value | (std::is_same::value << 1) | (std::is_same::value << 1) | (std::is_same::value << 2)>(), + ::TemplateIntToType(), ::TemplateIntToType::value>(), ::TemplateIntToType::value>(), methodName, @@ -791,9 +795,13 @@ namespace Core { { using ARG0 = typename std::decay::template argument<0>::type>::type; using ARG1 = typename std::decay::template argument<1>::type>::type; + using ARG2 = typename std::decay::template argument<2>::type>::type; + constexpr auto HAS_CONTEXT = std::is_same::value; + constexpr auto HAS_INDEX = (std::is_same::value | std::is_same::value | std::is_same::value); + constexpr auto HAS_INSTANCEID = (std::is_same::value | std::is_same::value); InternalRegister( - ::TemplateIntToType::value | (std::is_same::value << 1) | (std::is_same::value << 1) | (std::is_same::value << 2)>(), + ::TemplateIntToType(), ::TemplateIntToType::value>(), ::TemplateIntToType::value>(), methodName, @@ -1029,6 +1037,10 @@ namespace Core { Register(methodName, implementation); } + // To reduce the number of possible permutations parameters to the native implementation method are always expected in particular order: + // - context, instance id, property index, json parameters, json result (all optional). + // If index is used then either parameters or result must be present. + private: template void InternalRegister(const ::TemplateIntToType<0>&, const ::TemplateIntToType<1>&, const ::TemplateIntToType<1>&, const string& methodName, const METHOD& method) @@ -1165,6 +1177,36 @@ namespace Core { }); } + private: + template // context+id + void InternalRegister(const ::TemplateIntToType<5>&, const ::TemplateIntToType<1>&, const ::TemplateIntToType<1>&, const string& methodName, const METHOD& method) + { + Register(methodName, [method](const Context& context, const string& methodName, const string& /* parameters */, string& /* result */) -> uint32_t { + return (InternalRegisterImpl(method, context, Message::InstanceId(methodName))); + }); + } + template // context+id+inbound + void InternalRegister(const ::TemplateIntToType<5>&, const ::TemplateIntToType<0>&, const ::TemplateIntToType<1>&, const string& methodName, const METHOD& method) + { + Register(methodName, [method](const Context& context, const string& methodName, const string& parameters, string& result) -> uint32_t { + return (InternalRegisterImpl(parameters, result, method, context, Message::InstanceId(methodName))); + }); + } + template // context+id+outbound + void InternalRegister(const ::TemplateIntToType<5>&, const ::TemplateIntToType<1>&, const ::TemplateIntToType<0>&, const string& methodName, const METHOD& method) + { + Register(methodName, [method](const Context& context, const string& methodName, const string&, string& result) -> uint32_t { + return (InternalRegisterImpl(result, method, context, Message::InstanceId(methodName))); + }); + } + template // context+id+inbound+outbound + void InternalRegister(const ::TemplateIntToType<5>&, const ::TemplateIntToType<0>&, const ::TemplateIntToType<0>&, const string& methodName, const METHOD& method) + { + Register(methodName, [method](const Context& context, const string& methodName, const string& parameters, string& result) -> uint32_t { + return (InternalRegisterImplIO(parameters, result, method, context, Message::InstanceId(methodName))); + }); + } + private: template // id+index+inbound void InternalRegister(const ::TemplateIntToType<6>&, const ::TemplateIntToType<0>&, const ::TemplateIntToType<1>&, const string& methodName, const METHOD& method) @@ -1188,6 +1230,29 @@ namespace Core { }); } + private: + template // context+id+index+inbound + void InternalRegister(const ::TemplateIntToType<7>&, const ::TemplateIntToType<0>&, const ::TemplateIntToType<1>&, const string& methodName, const METHOD& method) + { + Register(methodName, [method](const Context& context, const string& methodName, const string& parameters, string& result) -> uint32_t { + return (InternalRegisterImpl(parameters, result, method, context, Message::InstanceId(methodName), Message::Index(methodName))); + }); + } + template // context+id+index+outbound + void InternalRegister(const ::TemplateIntToType<7>&, const ::TemplateIntToType<1>&, const ::TemplateIntToType<0>&, const string& methodName, const METHOD& method) + { + Register(methodName, [method](const Context& context, const string& methodName, const string&, string& result) -> uint32_t { + return (InternalRegisterImpl(result, method, context, Message::InstanceId(methodName), Message::Index(methodName))); + }); + } + template // context+id+index+inbound+outbound + void InternalRegister(const ::TemplateIntToType<7>&, const ::TemplateIntToType<0>&, const ::TemplateIntToType<0>&, const string& methodName, const METHOD& method) + { + Register(methodName, [method](const Context& context, const string& methodName, const string& parameters, string& result) -> uint32_t { + return (InternalRegisterImplIO(parameters, result, method, context, Message::InstanceId(methodName), Message::Index(methodName))); + }); + } + private: template void InternalRegister(const ::TemplateIntToType<0>&, const ::TemplateIntToType<1>&, const ::TemplateIntToType<1>&, const string& methodName, const METHOD& method, REALOBJECT* objectPtr) diff --git a/Source/plugins/JSONRPC.h b/Source/plugins/JSONRPC.h index b08dc7d29..dda5a72ee 100644 --- a/Source/plugins/JSONRPC.h +++ b/Source/plugins/JSONRPC.h @@ -30,9 +30,9 @@ namespace Thunder { namespace PluginHost { namespace { - + template - uint32_t InvokeOnHandler(const Core::JSONRPC::Context& context, const string& method, const string& parameters, string& response, Core::JSONRPC::Handler& handler, JSONRPCERRORASSESSORTYPE errorhandler) + uint32_t InvokeOnHandler(const Core::JSONRPC::Context& context, const string& method, const string& parameters, string& response, Core::JSONRPC::Handler& handler, JSONRPCERRORASSESSORTYPE errorhandler) { uint32_t result = handler.Invoke(context, method, parameters, response); if(result != Core::ERROR_NONE) { @@ -460,7 +460,7 @@ namespace PluginHost { while ((index != _handlers.end()) && (result == nullptr)) { if (index->HasVersionSupport(version) == true) { result = &(*index); - } + } else { index++; } @@ -509,8 +509,8 @@ namespace PluginHost { { _handlers.front().Register(methodName, method); } - void Register(const string& methodName, const Core::JSONRPC::InvokeFunction& lambda) - { + void Register(const string& methodName, const Core::JSONRPC::InvokeFunction& lambda) + { _handlers.front().Register(methodName, lambda); } void Register(const string& methodName, const Core::JSONRPC::CallbackFunction& lambda) @@ -615,7 +615,7 @@ namespace PluginHost { // Inherited via IDispatcher // --------------------------------------------------------------------------------- - uint32_t Invoke(const uint32_t channelId, const uint32_t id, const string& token, const string& method, const string& parameters, string& response) override + uint32_t Invoke(const uint32_t channelId, const uint32_t id, const string& token, const string& method, const string& parameters, string& response) override { return InvokeHandler(channelId, id, token, method, parameters, response); } @@ -683,7 +683,7 @@ namespace PluginHost { } else { Core::JSONRPC::Handler* handler(Handler(realMethod)); - + if (handler != nullptr) { Core::JSONRPC::Context context(channelId, id, token); result = InvokeOnHandler(context, Core::JSONRPC::Message::FullMethod(method), parameters, response, *handler, errorhandler); @@ -804,7 +804,7 @@ namespace PluginHost { std::vector versions({ version }); _handlers.emplace_front(versions); index = _handlers.begin(); - } + } index->Register(methodName, Core::JSONRPC::InvokeFunction()); _adminLock.Unlock(); @@ -1048,7 +1048,7 @@ namespace JSONRPCErrorAssessorTypes { class EXTERNAL JSONRPCErrorAssessor : public JSONRPC { public: - JSONRPCErrorAssessor(JSONRPCERRORASSESSORTYPE errorhandler) + JSONRPCErrorAssessor(JSONRPCERRORASSESSORTYPE errorhandler) : JSONRPC() , _errorhandler(errorhandler) { @@ -1061,7 +1061,7 @@ namespace JSONRPCErrorAssessorTypes { JSONRPCErrorAssessor(JSONRPCErrorAssessor&&) = delete; JSONRPCErrorAssessor &operator=(JSONRPCErrorAssessor&&) = delete; - uint32_t Invoke(const uint32_t channelId, const uint32_t id, const string& token, const string& method, const string& parameters, string& response) override + uint32_t Invoke(const uint32_t channelId, const uint32_t id, const string& token, const string& method, const string& parameters, string& response) override { return JSONRPC::InvokeHandler(channelId, id, token, method, parameters, response, _errorhandler); } @@ -1075,13 +1075,13 @@ namespace JSONRPCErrorAssessorTypes { class EXTERNAL JSONRPCErrorAssessor : public JSONRPC { public: - JSONRPCErrorAssessor(const JSONRPCErrorAssessorTypes::StdFunctionCallbackType& errorhandler) + JSONRPCErrorAssessor(const JSONRPCErrorAssessorTypes::StdFunctionCallbackType& errorhandler) : JSONRPC() , _errorhandler(errorhandler) { } - JSONRPCErrorAssessor(JSONRPCErrorAssessorTypes::StdFunctionCallbackType&& errorhandler) + JSONRPCErrorAssessor(JSONRPCErrorAssessorTypes::StdFunctionCallbackType&& errorhandler) : JSONRPC() , _errorhandler(std::move(errorhandler)) { @@ -1094,7 +1094,7 @@ namespace JSONRPCErrorAssessorTypes { JSONRPCErrorAssessor(JSONRPCErrorAssessor&&) = delete; JSONRPCErrorAssessor &operator=(JSONRPCErrorAssessor&&) = delete; - uint32_t Invoke(const uint32_t channelId, const uint32_t id, const string& token, const string& method, const string& parameters, string& response) override + uint32_t Invoke(const uint32_t channelId, const uint32_t id, const string& token, const string& method, const string& parameters, string& response) override { return JSONRPC::InvokeHandler(channelId, id, token, method, parameters, response, _errorhandler); } @@ -1106,6 +1106,120 @@ namespace JSONRPCErrorAssessorTypes { #endif // __DISABLE_USE_COMPLEMENTARY_CODE_SET__ + template + class LookupStorageType { + public: + LookupStorageType() + : _storage() + , _nextId(1) + { + } + ~LookupStorageType() = default; + + LookupStorageType(const LookupStorageType&) = delete; + LookupStorageType(LookupStorageType&&) = delete; + LookupStorageType& operator=(const LookupStorageType&) = delete; + LookupStorageType& operator=(LookupStorageType&&) = delete; + + public: + uint32_t Store(T* obj, const Core::JSONRPC::Context& context) + { + ID id = 0; + + ASSERT(obj != nullptr); + + if (obj != nullptr) { + + obj->AddRef(); + + id = _nextId++; + + _storage.emplace(std::piecewise_construct, + std::forward_as_tuple(id), + std::forward_as_tuple(context.ChannelId(), obj)); + + } + + return (id); + } + + T* Lookup(const Core::JSONRPC::Context& context, const ID id) + { + T* obj{}; + + auto it = _storage.find(id); + + if ((it != _storage.end()) && ((*it).second.first == context.ChannelId())) { + obj = (*it).second.second; + obj->AddRef(); + } + + return (obj); + } + + const T* Lookup(const Core::JSONRPC::Context& context, const ID id) const + { + const T* obj{}; + + auto const it = _storage.cfind(id); + + if ((it != _storage.cend()) && ((*it).second.first == context.ChannelId())) { + obj = (*it).second.second; + obj->AddRef(); + } + + return (obj); + } + + T* Dispose(const Core::JSONRPC::Context& context, const ID id) + { + T* obj{}; + + auto it = _storage.find(id); + + if (it != _storage.end() && ((*it).second.first == context.ChannelId())) { + obj = (*it).second.second; + _storage.erase(it); + } + + return (obj); + } + + public: + using OnCloseCallback = std::function; + void Closed(const uint32_t& channel, const OnCloseCallback& callback = nullptr) + { + auto it = _storage.begin(); + + while (it != _storage.end()) { + if ((*it).second.first == channel) { + + T* obj = (*it).second.second; + ASSERT(obj != nullptr); + + if (callback) { + callback((*it).first, T::ID, obj); + } + + obj->Release(); + it = _storage.erase(it); + } + else { + ++it; + } + } + } + + void Closed(const Core::JSONRPC::Context& context, const std::function& callback = nullptr) + { + Closed(context.ChannelId()); + } + + private: + std::map> _storage; + ID _nextId; + }; + } // namespace Thunder::PluginHost }