forked from openvinotoolkit/openvino
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathso_ptr.hpp
More file actions
120 lines (101 loc) · 3.25 KB
/
so_ptr.hpp
File metadata and controls
120 lines (101 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright (C) 2018-2026 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
/**
* @brief This is a wrapper class for handling plugin instantiation and releasing resources
* @file openvino/runtime/so_ptr.hpp
*/
#pragma once
#include <cassert>
#include <functional>
#include <memory>
#include <string>
#include <type_traits>
#include "openvino/runtime/common.hpp"
namespace ov {
/**
* @brief This class instantiate object using shared library
* @tparam T An type of object SoPtr can hold
*/
template <class T>
struct SoPtr {
/**
* @brief Default constructor
*/
SoPtr() = default;
SoPtr(const SoPtr&) = default;
SoPtr(SoPtr&&) noexcept = default;
SoPtr& operator=(const SoPtr&) = default;
SoPtr& operator=(SoPtr&&) noexcept = default;
/**
* @brief Destructor preserves unloading order of implementation object and reference to library
*/
~SoPtr() {
_ptr = {};
}
/**
* @brief Constructs an object with existing shared object reference and loaded pointer
* @param ptr pointer to the loaded object
* @param so Existing reference to library
*/
SoPtr(const std::shared_ptr<T>& ptr, const std::shared_ptr<void>& so) : _ptr{ptr}, _so{so} {}
/**
* @brief Constructs an object with existing shared object reference
* @param ptr pointer to the loaded object
*/
SoPtr(const std::shared_ptr<T>& ptr) : _ptr{ptr}, _so{nullptr} {}
/**
* @brief Constructs an object with existing shared object reference
* @param ptr pointer to the loaded object
*/
template <class U, typename std::enable_if<std::is_base_of<T, U>::value, bool>::type = true>
SoPtr(const std::shared_ptr<U>& ptr) : _ptr{std::dynamic_pointer_cast<T>(ptr)},
_so{nullptr} {}
/**
* @brief The copy-like constructor, can create So Pointer that dereferenced into child type if T is derived of U
* @param that copied SoPtr object
*/
template <typename U>
SoPtr(const SoPtr<U>& that) : _ptr{std::dynamic_pointer_cast<T>(that._ptr)},
_so{that._so} {
OPENVINO_ASSERT(_ptr != nullptr);
}
/**
* @brief Standard pointer operator
* @return underlined interface with disabled Release method
*/
T* operator->() const noexcept {
return _ptr.get();
}
/**
* @brief Dereference stored pointer to T object.
* @return Reference to T object.
*/
T& operator*() const noexcept {
return *_ptr;
}
explicit operator bool() const noexcept {
return _ptr != nullptr;
}
friend bool operator==(std::nullptr_t, const SoPtr& ptr) noexcept {
return !ptr;
}
friend bool operator==(const SoPtr& ptr, std::nullptr_t) noexcept {
return !ptr;
}
friend bool operator!=(std::nullptr_t, const SoPtr& ptr) noexcept {
return static_cast<bool>(ptr);
}
friend bool operator!=(const SoPtr& ptr, std::nullptr_t) noexcept {
return static_cast<bool>(ptr);
}
/**
* @brief Gets a smart pointer to the custom object
*/
std::shared_ptr<T> _ptr;
/**
* @brief The shared object or dynamic loaded library
*/
std::shared_ptr<void> _so;
};
} // namespace ov