From 8e1fed68f414fab8436c5b8fc5c04e226627adcf Mon Sep 17 00:00:00 2001 From: Ilya Lavrenov Date: Tue, 18 May 2021 15:05:42 +0300 Subject: [PATCH] Reverted back SOPointer split --- .../include/details/ie_so_pointer.hpp | 269 ++++++------------ .../cpp/ie_executable_network.cpp | 2 +- .../inference_engine/cpp/ie_infer_request.cpp | 5 +- .../src/inference_engine/ie_core.cpp | 18 +- 4 files changed, 93 insertions(+), 201 deletions(-) diff --git a/inference-engine/include/details/ie_so_pointer.hpp b/inference-engine/include/details/ie_so_pointer.hpp index f95710822df036..62abb7bb10e497 100644 --- a/inference-engine/include/details/ie_so_pointer.hpp +++ b/inference-engine/include/details/ie_so_pointer.hpp @@ -24,13 +24,7 @@ namespace details { * parameter */ template -class SOCreatorTrait { -public: - /** - * @brief A dummy name for fabric - */ - static constexpr auto name = ""; -}; +class SOCreatorTrait {}; /** * @brief Enables only `char` or `wchar_t` template specializations @@ -39,49 +33,23 @@ class SOCreatorTrait { template using enableIfSupportedChar = typename std::enable_if<(std::is_same::value || std::is_same::value)>::type; -/// @cond -IE_SUPPRESS_DEPRECATED_START -template -struct HasRelease { - template static char test(decltype(&C::Release)); - template static long test(...); - constexpr static const bool value = sizeof(test(nullptr)) == sizeof(char); -}; -IE_SUPPRESS_DEPRECATED_END -/// @endcond - -/** - * @brief This class instantiate object loaded from the shared library - * @tparam T An type of object SOPointer can hold - */ -template ::value> -class SOPointer; - -#define CATCH_IE_EXCEPTION(ExceptionType) catch (const InferenceEngine::ExceptionType& e) {throw e;} -#define CATCH_IE_EXCEPTIONS \ - CATCH_IE_EXCEPTION(GeneralError) \ - CATCH_IE_EXCEPTION(NotImplemented) \ - CATCH_IE_EXCEPTION(NetworkNotLoaded) \ - CATCH_IE_EXCEPTION(ParameterMismatch) \ - CATCH_IE_EXCEPTION(NotFound) \ - CATCH_IE_EXCEPTION(OutOfBounds) \ - CATCH_IE_EXCEPTION(Unexpected) \ - CATCH_IE_EXCEPTION(RequestBusy) \ - CATCH_IE_EXCEPTION(ResultNotReady) \ - CATCH_IE_EXCEPTION(NotAllocated) \ - CATCH_IE_EXCEPTION(InferNotStarted) \ - CATCH_IE_EXCEPTION(NetworkNotRead) \ - CATCH_IE_EXCEPTION(InferCancelled) - /** - * @brief This class instantiate object loaded from the shared library and use Release method to destroy the object + * @brief This class instantiate object using shared library * @tparam T An type of object SOPointer can hold */ template -class SOPointer { - template +class SOPointer { + template friend class SOPointer; + IE_SUPPRESS_DEPRECATED_START + struct HasRelease { + template static char test(decltype(&C::Release)); + template static long test(...); + constexpr static const bool value = sizeof(test(nullptr)) == sizeof(char); + }; + IE_SUPPRESS_DEPRECATED_END + public: /** * @brief Default constructor @@ -89,86 +57,51 @@ class SOPointer { SOPointer() = default; /** - * @brief Constructs an object using name to load library - * @param name Existing pointer to a library loader + * @brief The main constructor + * @param name Name of a shared library file */ - template - SOPointer(const std::basic_string& name) : - SOPointer(details::SharedObjectLoader(name.c_str())) { + template > + SOPointer(const std::basic_string & name) + : _so(name.c_str()) { + Load(std::integral_constant{}); } /** + * @brief Constructs an object with existing reference * @brief Constructs an object with existing loader - * @param so_loader Existing pointer to a library loader + * @param soLoader Existing pointer to a library loader */ - SOPointer(const SharedObjectLoader& so_loader) - : _so{so_loader} { - try { - void* create = nullptr; - try { - create = _so.get_symbol((SOCreatorTrait::name + std::string("Shared")).c_str()); - } catch (const NotFound&) {} - if (create == nullptr) { - create = _so.get_symbol(SOCreatorTrait::name); - using CreateF = StatusCode(T*&, ResponseDesc*); - T* object = nullptr; - ResponseDesc desc; - StatusCode sts = reinterpret_cast(create)(object, &desc); - if (sts != OK) { - IE_EXCEPTION_SWITCH(sts, ExceptionType, - InferenceEngine::details::ThrowNow{} <<= std::stringstream{} << IE_LOCATION << desc.msg) - } - IE_SUPPRESS_DEPRECATED_START - _ptr = std::shared_ptr{object, [] (T* ptr){ptr->Release();}}; - IE_SUPPRESS_DEPRECATED_END - } else { - using CreateF = void(std::shared_ptr&); - reinterpret_cast(create)(_ptr); - } - } CATCH_IE_EXCEPTIONS catch (const std::exception& ex) { - IE_THROW() << ex.what(); - } catch(...) { - IE_THROW(Unexpected); - } - } + SOPointer(const SharedObjectLoader& so, const std::shared_ptr& ptr) : _so{so}, _ptr{ptr} {} /** * @brief Constructs an object with existing loader * @param so Existing pointer to a library loader - * @param ptr A shared pointer to the object */ - SOPointer(const SharedObjectLoader& so, const std::shared_ptr& ptr) : _so{so}, _ptr{ptr} {} + explicit SOPointer(const SharedObjectLoader& so) + : _so(so) { + Load(std::integral_constant{}); + } /** * @brief The copy-like constructor, can create So Pointer that dereferenced into child type if T is derived of U * @param that copied SOPointer object */ - template - SOPointer(const SOPointer& that) + template + SOPointer(const SOPointer& that) : _so(that._so), - _ptr(std::dynamic_pointer_cast(that._ptr)) {} + _ptr(std::dynamic_pointer_cast(that._ptr)) { + IE_ASSERT(_ptr != nullptr); + } /** * @brief Standard pointer operator + * @return underlined interface with disabled Release method */ T* operator->() const noexcept { return _ptr.get(); } - /** - * @return raw pointer - */ - T* get() noexcept { - return _ptr.get(); - } - - /** - * @return raw pointer - */ - const T* get() const noexcept { - return _ptr.get(); - } - explicit operator bool() const noexcept { return _ptr != nullptr; } @@ -195,36 +128,59 @@ class SOPointer { } protected: - SharedObjectLoader _so; - std::shared_ptr _ptr; -}; - -/** - * @brief This class instantiate object loaded from the shared library - * @tparam T An type of object SOPointer can hold - */ -template -class SOPointer { - template - friend class SOPointer; +#define CATCH_IE_EXCEPTION(ExceptionType) catch (const InferenceEngine::ExceptionType& e) {throw e;} +#define CATCH_IE_EXCEPTIONS \ + CATCH_IE_EXCEPTION(GeneralError) \ + CATCH_IE_EXCEPTION(NotImplemented) \ + CATCH_IE_EXCEPTION(NetworkNotLoaded) \ + CATCH_IE_EXCEPTION(ParameterMismatch) \ + CATCH_IE_EXCEPTION(NotFound) \ + CATCH_IE_EXCEPTION(OutOfBounds) \ + CATCH_IE_EXCEPTION(Unexpected) \ + CATCH_IE_EXCEPTION(RequestBusy) \ + CATCH_IE_EXCEPTION(ResultNotReady) \ + CATCH_IE_EXCEPTION(NotAllocated) \ + CATCH_IE_EXCEPTION(InferNotStarted) \ + CATCH_IE_EXCEPTION(NetworkNotRead) \ + CATCH_IE_EXCEPTION(InferCancelled) -public: /** - * @brief Default constructor + * @brief Implements load of object from library if Release method is presented */ - SOPointer() = default; + void Load(std::true_type) { + try { + void* create = nullptr; + try { + create = _so.get_symbol((SOCreatorTrait::name + std::string("Shared")).c_str()); + } catch (const NotFound&) {} + if (create == nullptr) { + create = _so.get_symbol(SOCreatorTrait::name); + using CreateF = StatusCode(T*&, ResponseDesc*); + T* object = nullptr; + ResponseDesc desc; + StatusCode sts = reinterpret_cast(create)(object, &desc); + if (sts != OK) { + IE_EXCEPTION_SWITCH(sts, ExceptionType, + InferenceEngine::details::ThrowNow{} <<= std::stringstream{} << IE_LOCATION << desc.msg) + } + IE_SUPPRESS_DEPRECATED_START + _ptr = std::shared_ptr(object, [] (T* ptr){ptr->Release();}); + IE_SUPPRESS_DEPRECATED_END + } else { + using CreateF = void(std::shared_ptr&); + reinterpret_cast(create)(_ptr); + } + } CATCH_IE_EXCEPTIONS catch (const std::exception& ex) { + IE_THROW() << ex.what(); + } catch(...) { + IE_THROW(Unexpected); + } + } /** - * @brief Constructs an object using name to load library - * @param name Existing pointer to a library loader + * @brief Implements load of object from library */ - template > - SOPointer(const std::basic_string& name) : - SOPointer(details::SharedObjectLoader(name.c_str())) { - } - - SOPointer(const SharedObjectLoader& so_loader) - : _so{so_loader} { + void Load(std::false_type) { try { using CreateF = void(std::shared_ptr&); reinterpret_cast(_so.get_symbol(SOCreatorTrait::name))(_ptr); @@ -234,75 +190,18 @@ class SOPointer { IE_THROW(Unexpected); } } + #undef CATCH_IE_EXCEPTION + #undef CATCH_IE_EXCEPTIONS /** - * @brief Constructs an object with existing loader - * @param soLoader Existing pointer to a library loader + * @brief The DLL */ - SOPointer(const SharedObjectLoader& so, const std::shared_ptr& ptr) : _so{so}, _ptr{ptr} {} - - /** - * @brief The copy-like constructor, can create So Pointer that dereferenced into child type if T is derived of U - * @param that copied SOPointer object - */ - template - SOPointer(const SOPointer& that) - : _so(that._so), - _ptr(std::dynamic_pointer_cast(that._ptr)) {} - - /** - * @brief Standard pointer operator - */ - T* operator->() const noexcept { - return _ptr.get(); - } - - /** - * @return raw pointer - */ - T* get() noexcept { - return _ptr.get(); - } + SharedObjectLoader _so; /** - * @return raw pointer + * @brief Gets a smart pointer to the custom object */ - const T* get() const noexcept { - return _ptr.get(); - } - - explicit operator bool() const noexcept { - return _ptr != nullptr; - } - - friend bool operator==(std::nullptr_t, const SOPointer& ptr) noexcept { - return !ptr; - } - friend bool operator==(const SOPointer& ptr, std::nullptr_t) noexcept { - return !ptr; - } - friend bool operator!=(std::nullptr_t, const SOPointer& ptr) noexcept { - return static_cast(ptr); - } - friend bool operator!=(const SOPointer& ptr, std::nullptr_t) noexcept { - return static_cast(ptr); - } - - operator const SharedObjectLoader&() const noexcept { - return _so; - } - - operator std::shared_ptr& () noexcept { - return _ptr; - } - -protected: - SharedObjectLoader _so; std::shared_ptr _ptr; }; - -#undef CATCH_IE_EXCEPTION -#undef CATCH_IE_EXCEPTIONS - } // namespace details } // namespace InferenceEngine diff --git a/inference-engine/src/inference_engine/cpp/ie_executable_network.cpp b/inference-engine/src/inference_engine/cpp/ie_executable_network.cpp index f096c56fb0bad9..c792af606a7fa3 100644 --- a/inference-engine/src/inference_engine/cpp/ie_executable_network.cpp +++ b/inference-engine/src/inference_engine/cpp/ie_executable_network.cpp @@ -54,7 +54,7 @@ std::vector ExecutableNetwork::QueryState() { std::vector controller; EXEC_NET_CALL_STATEMENT( for (auto&& state : _impl->QueryState()) { - controller.emplace_back(VariableState { _so, static_cast>(state) }); + controller.emplace_back(VariableState{ _so, state }); }); return controller; } diff --git a/inference-engine/src/inference_engine/cpp/ie_infer_request.cpp b/inference-engine/src/inference_engine/cpp/ie_infer_request.cpp index af2cad1c816abe..7d9e57bba1e9f7 100644 --- a/inference-engine/src/inference_engine/cpp/ie_infer_request.cpp +++ b/inference-engine/src/inference_engine/cpp/ie_infer_request.cpp @@ -192,9 +192,8 @@ IE_SUPPRESS_DEPRECATED_END std::vector InferRequest::QueryState() { std::vector controller; INFER_REQ_CALL_STATEMENT( - for (auto&& _state : _impl->QueryState()) { - VariableState state{_so, static_cast>(_state)}; - controller.emplace_back(state); + for (auto&& state : _impl->QueryState()) { + controller.emplace_back(VariableState{_so, state}); } ) return controller; diff --git a/inference-engine/src/inference_engine/ie_core.cpp b/inference-engine/src/inference_engine/ie_core.cpp index 6273bf3f30380f..39052af8badd66 100644 --- a/inference-engine/src/inference_engine/ie_core.cpp +++ b/inference-engine/src/inference_engine/ie_core.cpp @@ -903,22 +903,19 @@ CNNNetwork Core::ReadNetwork(const std::string& model, const Blob::CPtr& weights ExecutableNetwork Core::LoadNetwork(const CNNNetwork& network, const std::string& deviceName, const std::map& config) { auto exec = _impl->LoadNetwork(network, deviceName, config); - return { static_cast(exec), - static_cast>(exec) }; + return { exec, exec }; } ExecutableNetwork Core::LoadNetwork(const CNNNetwork& network, RemoteContext::Ptr context, const std::map& config) { auto exec = _impl->LoadNetwork(network, context, config); - return { static_cast(exec), - static_cast>(exec) }; + return { exec, exec }; } ExecutableNetwork Core::LoadNetwork(const std::string& modelPath, const std::string& deviceName, const std::map& config) { auto exec = _impl->LoadNetwork(modelPath, deviceName, config); - return { static_cast(exec), - static_cast>(exec) }; + return { exec, exec }; } RemoteContext::Ptr Core::CreateContext(const std::string& deviceName, const ParamMap& params) { @@ -989,15 +986,13 @@ ExecutableNetwork Core::ImportNetwork(const std::string& modelFileName, const st auto parsed = parseDeviceNameIntoConfig(deviceName, config); auto exec = _impl->GetCPPPluginByName(parsed._deviceName).ImportNetwork(modelFileName, parsed._config); - return { static_cast(exec), - static_cast>(exec) }; + return { exec, exec }; } ExecutableNetwork Core::ImportNetwork(std::istream& networkModel, const std::string& deviceName, const std::map& config) { auto exec = _impl->ImportNetwork(networkModel, deviceName, config); - return { static_cast(exec), - static_cast>(exec) }; + return { exec, exec }; } ExecutableNetwork Core::ImportNetwork(std::istream& networkModel, @@ -1015,8 +1010,7 @@ ExecutableNetwork Core::ImportNetwork(std::istream& networkModel, auto parsed = parseDeviceNameIntoConfig(deviceName, config); auto exec = _impl->GetCPPPluginByName(deviceName).ImportNetwork(networkModel, context, parsed._config); - return { static_cast(exec), - static_cast>(exec) }; + return { exec, exec }; } QueryNetworkResult Core::QueryNetwork(const CNNNetwork& network, const std::string& deviceName,