ie_parameter.hpp
Go to the documentation of this file.
1 // Copyright (C) 2018-2020 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4 
5 /**
6  * @brief A header file for the Parameter class
7  * @file ie_parameter.hpp
8  */
9 #pragma once
10 
11 #include <algorithm>
12 #include <cctype>
13 #include <details/ie_exception.hpp>
14 #include <iterator>
15 #include <map>
16 #include <memory>
17 #include <string>
18 #include <tuple>
19 #include <typeinfo>
20 #include <utility>
21 #include <vector>
22 
23 #include "ie_blob.h"
24 
25 namespace ngraph {
26 
27 class Variant;
28 
29 } // namespace ngraph
30 
31 namespace InferenceEngine {
32 
33 /**
34  * @brief This class represents an object to work with different parameters
35  *
36  */
37 class INFERENCE_ENGINE_API_CLASS(Parameter) {
38 public:
39  /**
40  * @brief Default constructor
41  */
42  Parameter() = default;
43 
44  /**
45  * @brief Move constructor
46  *
47  * @param parameter Parameter object
48  */
49  Parameter(Parameter&& parameter) noexcept {
50  std::swap(ptr, parameter.ptr);
51  }
52 
53  /**
54  * @brief Creates parameter from variant.
55  * This method creates empty parameter if variant doesn't contain Parameter
56  *
57  * @param var ngraph variant
58  */
59  Parameter(const std::shared_ptr<ngraph::Variant>& var);
60 
61  /**
62  * @brief Creates parameter from variant.
63  * This method creates empty parameter if variant doesn't contain Parameter
64  *
65  * @param var ngraph variant
66  */
67  Parameter(std::shared_ptr<ngraph::Variant>& var);
68 
69  /**
70  * @brief Copy constructor
71  *
72  * @param parameter Parameter object
73  */
74  Parameter(const Parameter& parameter) {
75  *this = parameter;
76  }
77 
78  /**
79  * @brief Constructor creates parameter with object
80  *
81  * @tparam T Parameter type
82  * @tparam U Identity type-transformation
83  * @param parameter object
84  */
85  template <class T,
86  typename = typename std::enable_if<!std::is_same<typename std::decay<T>::type, Parameter>::value>::type>
87  Parameter(T&& parameter) { // NOLINT
88  static_assert(!std::is_same<typename std::decay<T>::type, Parameter>::value, "To prevent recursion");
89  ptr = new RealData<typename std::decay<T>::type>(std::forward<T>(parameter));
90  }
91 
92  /**
93  * @brief Constructor creates string parameter from char *
94  *
95  * @param str char array
96  */
97  Parameter(const char* str): Parameter(std::string(str)) {} // NOLINT
98 
99  /**
100  * @brief Destructor
101  */
102  virtual ~Parameter();
103 
104  /**
105  * Copy operator for Parameter
106  * @param parameter Parameter object
107  * @return Parameter
108  */
109  Parameter& operator=(const Parameter& parameter) {
110  if (this == &parameter) {
111  return *this;
112  }
113  clear();
114  if (!parameter.empty()) ptr = parameter.ptr->copy();
115  return *this;
116  }
117 
118  /**
119  * Remove a value from parameter
120  */
121  void clear() {
122  delete ptr;
123  ptr = nullptr;
124  }
125 
126  /**
127  * Checks that parameter contains a value
128  * @return false if parameter contains a value else false
129  */
130  bool empty() const noexcept {
131  return nullptr == ptr;
132  }
133 
134  /**
135  * Checks the type of value
136  * @tparam T Type of value
137  * @return true if type of value is correct
138  */
139  template <class T>
140  bool is() const {
141  return empty() ? false : ptr->is(typeid(T));
142  }
143 
144  /**
145  * Dynamic cast to specified type
146  * @tparam T type
147  * @return casted object
148  */
149  template <typename T>
150  T&& as() && {
151  return std::move(dyn_cast<T>(ptr));
152  }
153 
154  /**
155  * Dynamic cast to specified type
156  * @tparam T type
157  * @return casted object
158  */
159  template <class T>
160  T& as() & {
161  return dyn_cast<T>(ptr);
162  }
163  /**
164  * Dynamic cast to specified type
165  * @tparam T type
166  * @return casted object
167  */
168  template <class T>
169  const T& as() const& {
170  return dyn_cast<T>(ptr);
171  }
172 
173  /**
174  * Dynamic cast to specified type
175  * @tparam T type
176  * @return casted object
177  */
178  template <class T>
179  operator T &&() && {
180  return std::move(dyn_cast<typename std::remove_cv<T>::type>(ptr));
181  }
182 
183  /**
184  * Dynamic cast to specified type
185  * @tparam T type
186  * @return casted object
187  */
188  template <class T>
189  operator T&() & {
190  return dyn_cast<typename std::remove_cv<T>::type>(ptr);
191  }
192 
193  /**
194  * Dynamic cast to specified type
195  * @tparam T type
196  * @return casted object
197  */
198  template <class T>
199  operator const T&() const& {
200  return dyn_cast<typename std::remove_cv<T>::type>(ptr);
201  }
202 
203  /**
204  * @brief Converts parameter to shared pointer on ngraph::Variant
205  *
206  * @return shared pointer on ngraph::Variant
207  */
208  std::shared_ptr<ngraph::Variant> asVariant() const;
209 
210  /**
211  * @brief Casts to shared pointer on ngraph::Variant
212  *
213  * @return shared pointer on ngraph::Variant
214  */
215  operator std::shared_ptr<ngraph::Variant>() const {
216  return asVariant();
217  }
218 
219  /**
220  * Dynamic cast to specified type
221  * @tparam T type
222  * @return casted object
223  */
224  template <class T>
225  operator T&() const& {
226  return dyn_cast<typename std::remove_cv<T>::type>(ptr);
227  }
228 
229  /**
230  * @brief The comparison operator for the Parameter
231  *
232  * @param rhs object to compare
233  * @return true if objects are equal
234  */
235  bool operator==(const Parameter& rhs) const {
236  return *ptr == *(rhs.ptr);
237  }
238  /**
239  * @brief The comparison operator for the Parameter
240  *
241  * @param rhs object to compare
242  * @return true if objects aren't equal
243  */
244  bool operator!=(const Parameter& rhs) const {
245  return !(*this == rhs);
246  }
247 
248 private:
249  template <class T, class EqualTo>
250  struct CheckOperatorEqual {
251  template <class U, class V>
252  static auto test(U*) -> decltype(std::declval<U>() == std::declval<V>()) {
253  return false;
254  }
255 
256  template <typename, typename>
257  static auto test(...) -> std::false_type {
258  return {};
259  }
260 
261  using type = typename std::is_same<bool, decltype(test<T, EqualTo>(nullptr))>::type;
262  };
263 
264  template <class T, class EqualTo = T>
265  struct HasOperatorEqual : CheckOperatorEqual<T, EqualTo>::type {};
266 
267  struct Any {
268 #if defined(__clang__) && !defined(__SYCL_COMPILER_VERSION)
269  virtual ~Any();
270 #else
271  virtual ~Any() = default;
272 #endif // __clang__ && !__SYCL_COMPILER_VERSION
273  virtual bool is(const std::type_info&) const = 0;
274  virtual Any* copy() const = 0;
275  virtual bool operator==(const Any& rhs) const = 0;
276  };
277 
278  template <class T>
279  struct RealData : Any, std::tuple<T> {
280  using std::tuple<T>::tuple;
281 
282  bool is(const std::type_info& id) const override {
283  return id == typeid(T);
284  }
285  Any* copy() const override {
286  return new RealData {get()};
287  }
288 
289  T& get() & {
290  return std::get<0>(*static_cast<std::tuple<T>*>(this));
291  }
292 
293  const T& get() const& {
294  return std::get<0>(*static_cast<const std::tuple<T>*>(this));
295  }
296 
297  template <class U>
298  typename std::enable_if<!HasOperatorEqual<U>::value, bool>::type
299  equal(const Any& left, const Any& rhs) const {
300  THROW_IE_EXCEPTION << "Parameter doesn't contain equal operator";
301  }
302 
303  template <class U>
304  typename std::enable_if<HasOperatorEqual<U>::value, bool>::type
305  equal(const Any& left, const Any& rhs) const {
306  return dyn_cast<U>(&left) == dyn_cast<U>(&rhs);
307  }
308 
309  bool operator==(const Any& rhs) const override {
310  return rhs.is(typeid(T)) && equal<T>(*this, rhs);
311  }
312  };
313 
314  template <typename T>
315  static T& dyn_cast(Any* obj) {
316  if (obj == nullptr) THROW_IE_EXCEPTION << "Parameter is empty!";
317  return dynamic_cast<RealData<T>&>(*obj).get();
318  }
319 
320  template <typename T>
321  static const T& dyn_cast(const Any* obj) {
322  if (obj == nullptr) THROW_IE_EXCEPTION << "Parameter is empty!";
323  return dynamic_cast<const RealData<T>&>(*obj).get();
324  }
325 
326  Any* ptr = nullptr;
327 };
328 
329 #if defined(__clang__) && !defined(__SYCL_COMPILER_VERSION)
330 extern template struct INFERENCE_ENGINE_API_CLASS(InferenceEngine::Parameter::RealData<InferenceEngine::Blob::Ptr>);
331 extern template struct INFERENCE_ENGINE_API_CLASS(InferenceEngine::Parameter::RealData<int>);
332 extern template struct INFERENCE_ENGINE_API_CLASS(InferenceEngine::Parameter::RealData<bool>);
333 extern template struct INFERENCE_ENGINE_API_CLASS(InferenceEngine::Parameter::RealData<float>);
334 extern template struct INFERENCE_ENGINE_API_CLASS(InferenceEngine::Parameter::RealData<uint32_t>);
335 extern template struct INFERENCE_ENGINE_API_CLASS(InferenceEngine::Parameter::RealData<std::string>);
336 extern template struct INFERENCE_ENGINE_API_CLASS(InferenceEngine::Parameter::RealData<unsigned long>);
337 extern template struct INFERENCE_ENGINE_API_CLASS(InferenceEngine::Parameter::RealData<std::vector<int>>);
338 extern template struct INFERENCE_ENGINE_API_CLASS(InferenceEngine::Parameter::RealData<std::vector<std::string>>);
339 extern template struct INFERENCE_ENGINE_API_CLASS(InferenceEngine::Parameter::RealData<std::vector<unsigned long>>);
340 extern template struct INFERENCE_ENGINE_API_CLASS(
341  InferenceEngine::Parameter::RealData<std::tuple<unsigned int, unsigned int>>);
342 extern template struct INFERENCE_ENGINE_API_CLASS(
343  InferenceEngine::Parameter::RealData<std::tuple<unsigned int, unsigned int, unsigned int>>);
344 #endif // __clang__ && !__SYCL_COMPILER_VERSION
345 
346 } // namespace InferenceEngine
This class represents an object to work with different parameters.
Definition: ie_parameter.hpp:37
virtual ~Parameter()
Destructor.
Parameter()=default
Default constructor.
Parameter(const Parameter &parameter)
Copy constructor.
Definition: ie_parameter.hpp:74
T & as() &
Definition: ie_parameter.hpp:160
Parameter & operator=(const Parameter &parameter)
Definition: ie_parameter.hpp:109
bool is() const
Definition: ie_parameter.hpp:140
void clear()
Definition: ie_parameter.hpp:121
std::shared_ptr< ngraph::Variant > asVariant() const
Converts parameter to shared pointer on ngraph::Variant.
const T & as() const &
Definition: ie_parameter.hpp:169
Parameter(std::shared_ptr< ngraph::Variant > &var)
Creates parameter from variant. This method creates empty parameter if variant doesn't contain Parame...
Parameter(T &&parameter)
Constructor creates parameter with object.
Definition: ie_parameter.hpp:87
Parameter(Parameter &&parameter) noexcept
Move constructor.
Definition: ie_parameter.hpp:49
bool operator!=(const Parameter &rhs) const
The comparison operator for the Parameter.
Definition: ie_parameter.hpp:244
Parameter(const std::shared_ptr< ngraph::Variant > &var)
Creates parameter from variant. This method creates empty parameter if variant doesn't contain Parame...
bool operator==(const Parameter &rhs) const
The comparison operator for the Parameter.
Definition: ie_parameter.hpp:235
bool empty() const noexcept
Definition: ie_parameter.hpp:130
T && as() &&
Definition: ie_parameter.hpp:150
Parameter(const char *str)
Constructor creates string parameter from char *.
Definition: ie_parameter.hpp:97
A header file for Blob and generic TBlob<>
A header file for the main Inference Engine exception.
#define THROW_IE_EXCEPTION
A macro used to throw general exception with a description.
Definition: ie_exception.hpp:25
Inference Engine C++ API.
Definition: cldnn_config.hpp:15
Parameter parameter(TensorShape shape, NumericType dtype=np.float32, Optional[str] name=None)
Node equal(NodeInput left_node, NodeInput right_node, str auto_broadcast="NUMPY", Optional[str] name=None)