diff --git a/libmexclass/cpp/source/include/libmexclass/proxy/Proxy.h b/libmexclass/cpp/source/include/libmexclass/proxy/Proxy.h index 00303af..8cfc825 100644 --- a/libmexclass/cpp/source/include/libmexclass/proxy/Proxy.h +++ b/libmexclass/cpp/source/include/libmexclass/proxy/Proxy.h @@ -28,6 +28,9 @@ namespace libmexclass::proxy { class Proxy { public: Proxy() : methods{} { } + + virtual ~Proxy() {} + std::optional invoke(libmexclass::proxy::method::Method& method); protected: std::unordered_map methods; diff --git a/libmexclass/cpp/source/include/libmexclass/proxy/ProxyManager.h b/libmexclass/cpp/source/include/libmexclass/proxy/ProxyManager.h index 507a329..cc94541 100755 --- a/libmexclass/cpp/source/include/libmexclass/proxy/ProxyManager.h +++ b/libmexclass/cpp/source/include/libmexclass/proxy/ProxyManager.h @@ -2,11 +2,17 @@ #include "libmexclass/proxy/ID.h" #include "libmexclass/proxy/Proxy.h" +#include "libmexclass/error/Error.h" #include #include +#include namespace libmexclass::proxy { + +template +using ProxyResult = std::variant, libmexclass::error::Error>; + // ProxyMangager // Manages Proxy instances by placing them inside of a map which maps unique IDs // to Proxy instances. NOTE: ProxyManager uses the "Singleton" design pattern to @@ -17,6 +23,23 @@ class ProxyManager { static void unmanageProxy(ID id); static std::shared_ptr getProxy(ID id); + template + static ProxyResult getTypedProxy(ID id) { + auto proxy = libmexclass::proxy::ProxyManager::getProxy(id); + if (!proxy) { + std::string msg = "Invalid Proxy ID: " + std::to_string(id); + return libmexclass::error::Error{"libmexclass:proxy:InvalidID", msg}; + } + + auto typed_proxy = std::dynamic_pointer_cast(proxy); + if (!typed_proxy) { + std::string msg = "Failed to cast proxy to expected type"; + return libmexclass::error::Error{"libmexclass:proxy:InvalidProxyType", msg}; + } + + return typed_proxy; + } + private: static ProxyManager singleton; // The internal map used to associate Proxy instances with unique IDs. @@ -31,4 +54,5 @@ class ProxyManager { // increment it. ID current_proxy_id = 0; }; + } // namespace libmexclass::proxy diff --git a/libmexclass/cpp/source/libmexclass/action/MethodCall.cpp b/libmexclass/cpp/source/libmexclass/action/MethodCall.cpp index e6853d1..ae196cf 100755 --- a/libmexclass/cpp/source/libmexclass/action/MethodCall.cpp +++ b/libmexclass/cpp/source/libmexclass/action/MethodCall.cpp @@ -11,8 +11,11 @@ std::optional MethodCall::execute() { // TODO: Implement ID retrieval logic from MethodCall properties. // Retrieve the appropriate polymorphic proxy::Proxy instance from the // proxy::ProxyManager using the given proxy::ID. - std::shared_ptr proxy = - libmexclass::proxy::ProxyManager::getProxy(proxy_id); + auto proxy = libmexclass::proxy::ProxyManager::getProxy(proxy_id); + + if (!proxy) { + return libmexclass::error::Error{"libmexclass:proxy:UnknownProxyID", "Unknown proxy ID " + std::to_string(proxy_id)}; + } // Invoke the appropriate method on the proxy::Proxy instance. // Note: To assign/return outputs from a method call, proxy::Proxy diff --git a/libmexclass/cpp/source/libmexclass/proxy/ProxyManager.cpp b/libmexclass/cpp/source/libmexclass/proxy/ProxyManager.cpp index 567de47..daca0e5 100755 --- a/libmexclass/cpp/source/libmexclass/proxy/ProxyManager.cpp +++ b/libmexclass/cpp/source/libmexclass/proxy/ProxyManager.cpp @@ -13,7 +13,12 @@ void ProxyManager::unmanageProxy(ID id) { } std::shared_ptr ProxyManager::getProxy(ID id) { - return ProxyManager::singleton.proxy_map[id]; + auto proxy_key_value = ProxyManager::singleton.proxy_map.find(id); + if (proxy_key_value != ProxyManager::singleton.proxy_map.end()) { + return proxy_key_value->second; + } else { + return nullptr; + } } ProxyManager ProxyManager::singleton{};