Skip to content

Commit

Permalink
Reverted back SOPointer split
Browse files Browse the repository at this point in the history
  • Loading branch information
ilya-lavrenov committed May 18, 2021
1 parent 13fbf85 commit 8e1fed6
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 201 deletions.
269 changes: 84 additions & 185 deletions inference-engine/include/details/ie_so_pointer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,7 @@ namespace details {
* parameter
*/
template <class T>
class SOCreatorTrait {
public:
/**
* @brief A dummy name for fabric
*/
static constexpr auto name = "";
};
class SOCreatorTrait {};

/**
* @brief Enables only `char` or `wchar_t` template specializations
Expand All @@ -39,136 +33,75 @@ class SOCreatorTrait {
template <typename C>
using enableIfSupportedChar = typename std::enable_if<(std::is_same<C, char>::value || std::is_same<C, wchar_t>::value)>::type;

/// @cond
IE_SUPPRESS_DEPRECATED_START
template <class T>
struct HasRelease {
template <typename C> static char test(decltype(&C::Release));
template <typename C> static long test(...);
constexpr static const bool value = sizeof(test<T>(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 <class T, bool = HasRelease<T>::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 T>
class SOPointer<T, true> {
template <class U, bool>
class SOPointer {
template <class U>
friend class SOPointer;

IE_SUPPRESS_DEPRECATED_START
struct HasRelease {
template <typename C> static char test(decltype(&C::Release));
template <typename C> static long test(...);
constexpr static const bool value = sizeof(test<T>(nullptr)) == sizeof(char);
};
IE_SUPPRESS_DEPRECATED_END

public:
/**
* @brief Default constructor
*/
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 <typename C>
SOPointer(const std::basic_string<C>& name) :
SOPointer(details::SharedObjectLoader(name.c_str())) {
template <typename C,
typename = enableIfSupportedChar<C>>
SOPointer(const std::basic_string<C> & name)
: _so(name.c_str()) {
Load(std::integral_constant<bool, HasRelease::value>{});
}

/**
* @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<T>::name + std::string("Shared")).c_str());
} catch (const NotFound&) {}
if (create == nullptr) {
create = _so.get_symbol(SOCreatorTrait<T>::name);
using CreateF = StatusCode(T*&, ResponseDesc*);
T* object = nullptr;
ResponseDesc desc;
StatusCode sts = reinterpret_cast<CreateF*>(create)(object, &desc);
if (sts != OK) {
IE_EXCEPTION_SWITCH(sts, ExceptionType,
InferenceEngine::details::ThrowNow<ExceptionType>{} <<= std::stringstream{} << IE_LOCATION << desc.msg)
}
IE_SUPPRESS_DEPRECATED_START
_ptr = std::shared_ptr<T>{object, [] (T* ptr){ptr->Release();}};
IE_SUPPRESS_DEPRECATED_END
} else {
using CreateF = void(std::shared_ptr<T>&);
reinterpret_cast<CreateF*>(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<T>& 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<T>& ptr) : _so{so}, _ptr{ptr} {}
explicit SOPointer(const SharedObjectLoader& so)
: _so(so) {
Load(std::integral_constant<bool, HasRelease::value>{});
}

/**
* @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 <class U>
SOPointer(const SOPointer<U, true>& that)
template <typename U>
SOPointer(const SOPointer<U>& that)
: _so(that._so),
_ptr(std::dynamic_pointer_cast<T>(that._ptr)) {}
_ptr(std::dynamic_pointer_cast<T>(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;
}
Expand All @@ -195,36 +128,59 @@ class SOPointer<T, true> {
}

protected:
SharedObjectLoader _so;
std::shared_ptr<T> _ptr;
};

/**
* @brief This class instantiate object loaded from the shared library
* @tparam T An type of object SOPointer can hold
*/
template <class T>
class SOPointer<T, false> {
template <class U, bool>
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<T>::name + std::string("Shared")).c_str());
} catch (const NotFound&) {}
if (create == nullptr) {
create = _so.get_symbol(SOCreatorTrait<T>::name);
using CreateF = StatusCode(T*&, ResponseDesc*);
T* object = nullptr;
ResponseDesc desc;
StatusCode sts = reinterpret_cast<CreateF*>(create)(object, &desc);
if (sts != OK) {
IE_EXCEPTION_SWITCH(sts, ExceptionType,
InferenceEngine::details::ThrowNow<ExceptionType>{} <<= std::stringstream{} << IE_LOCATION << desc.msg)
}
IE_SUPPRESS_DEPRECATED_START
_ptr = std::shared_ptr<T>(object, [] (T* ptr){ptr->Release();});
IE_SUPPRESS_DEPRECATED_END
} else {
using CreateF = void(std::shared_ptr<T>&);
reinterpret_cast<CreateF*>(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 <typename C, typename = enableIfSupportedChar<C>>
SOPointer(const std::basic_string<C>& 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<T>&);
reinterpret_cast<CreateF*>(_so.get_symbol(SOCreatorTrait<T>::name))(_ptr);
Expand All @@ -234,75 +190,18 @@ class SOPointer<T, false> {
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<T>& 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 <class U>
SOPointer(const SOPointer<U, false>& that)
: _so(that._so),
_ptr(std::dynamic_pointer_cast<T>(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<bool>(ptr);
}
friend bool operator!=(const SOPointer& ptr, std::nullptr_t) noexcept {
return static_cast<bool>(ptr);
}

operator const SharedObjectLoader&() const noexcept {
return _so;
}

operator std::shared_ptr<T>& () noexcept {
return _ptr;
}

protected:
SharedObjectLoader _so;
std::shared_ptr<T> _ptr;
};

#undef CATCH_IE_EXCEPTION
#undef CATCH_IE_EXCEPTIONS

} // namespace details
} // namespace InferenceEngine
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ std::vector<VariableState> ExecutableNetwork::QueryState() {
std::vector<VariableState> controller;
EXEC_NET_CALL_STATEMENT(
for (auto&& state : _impl->QueryState()) {
controller.emplace_back(VariableState { _so, static_cast<std::shared_ptr<IVariableStateInternal>>(state) });
controller.emplace_back(VariableState{ _so, state });
});
return controller;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,8 @@ IE_SUPPRESS_DEPRECATED_END
std::vector<VariableState> InferRequest::QueryState() {
std::vector<VariableState> controller;
INFER_REQ_CALL_STATEMENT(
for (auto&& _state : _impl->QueryState()) {
VariableState state{_so, static_cast<std::shared_ptr<IVariableStateInternal>>(_state)};
controller.emplace_back(state);
for (auto&& state : _impl->QueryState()) {
controller.emplace_back(VariableState{_so, state});
}
)
return controller;
Expand Down
Loading

0 comments on commit 8e1fed6

Please sign in to comment.