ie_parameter.hpp
1 // Copyright (C) 2018 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4 
5 /**
6  * @brief A header file for the CNNNetworkIterator class
7  * @file ie_cnn_network_iterator.hpp
8  */
9 #pragma once
10 
11 #include <details/ie_exception.hpp>
12 #include <algorithm>
13 #include <iterator>
14 #include <vector>
15 #include <cctype>
16 #include <string>
17 #include <map>
18 
19 namespace InferenceEngine {
20 
21 /**
22  * @brief This class represents an object to work with different parameters
23  */
24 class Parameter {
25 public:
26  /**
27  * @brief Default constructor
28  */
29  Parameter() = default;
30 
31  /**
32  * @brief The constructor creates a Parameter object with string value
33  * @param value string value
34  */
35  Parameter(const std::string& value): initialized(true), value(value) {} // NOLINT
36 
37  /**
38  * @brief The constructor creates a Parameter object with template value
39  * @param value template value
40  */
41  template <class T>
42  Parameter(const T& value): initialized(true), value(std::to_string(value)) {} // NOLINT
43 
44  /**
45  * @brief The constructor creates a Parameter object with a vector of template values
46  * @param values vector of template values
47  */
48  template <class T>
49  Parameter(const std::vector<T>& values): initialized(true) { // NOLINT
50  for (const auto& val : values) {
51  if (!value.empty())
52  value += ",";
53  value += std::to_string(val);
54  }
55  }
56 
57  /**
58  * @brief The cast to string object
59  * Throws exception if parameter was not found.
60  * @return string value
61  */
62  operator std::string() const { // NOLINT
63  return asString();
64  }
65 
66  /**
67  * @brief Returns a string value for the given parameter or returns the default one
68  * @param def Default value of the parameter if not found
69  * @return A string value
70  */
71  std::string asString(std::string def) const {
72  if (!initialized) {
73  return def;
74  }
75  return value;
76  }
77 
78  /**
79  * @brief Returns a string value for the given parameter.
80  * Throws exception if parameter was not found.
81  * @return A string value
82  */
83  std::string asString() const {
84  if (!initialized) {
85  THROW_IE_EXCEPTION << "Parameter was not initialized!";
86  }
87  return value;
88  }
89 
90  /**
91  * @brief Gets float value for the given parameter
92  * @param def - default value of the parameter if not found
93  * @return float value
94  */
95  float asFloat(float def) const {
96  std::string val = asString(std::to_string(def));
97  try {
98  return std::stof(val);
99  } catch (...) {
100  THROW_IE_EXCEPTION << "Value " << val << " cannot be casted to float.";
101  }
102  }
103 
104  /**
105  * @brief Returns a float value for the given layer parameter
106  * @return A float value for the specified parameter
107  */
108  float asFloat() const {
109  std::string val = asString();
110  try {
111  return std::stof(val);
112  } catch (...) {
113  THROW_IE_EXCEPTION << "Value " << val << " cannot be casted to float.";
114  }
115  }
116 
117  /**
118  * @brief Returns a vector of float values for the given parameter or returns the default value
119  * @param def Default value of the parameter if not found
120  * @return vector of float values
121  */
122  std::vector<float> asFloats(std::vector<float> def) const {
123  std::string vals = asString("");
124  std::vector<float> result;
125  std::istringstream stream(vals);
126  std::string str;
127  if (vals.empty())
128  return def;
129  while (getline(stream, str, ',')) {
130  try {
131  result.push_back(std::stof(str));
132  } catch (...) {
133  THROW_IE_EXCEPTION << "Value " << vals << " cannot be casted to floats.";
134  }
135  }
136  return result;
137  }
138 
139  /**
140  * @brief Returns a vector of float values for the given parameter
141  * @return vector of float values
142  */
143  std::vector<float> asFloats() const {
144  std::string vals = asString();
145  std::vector<float> result;
146  std::istringstream stream(vals);
147  std::string str;
148  while (getline(stream, str, ',')) {
149  try {
150  result.push_back(std::stof(str));
151  } catch (...) {
152  THROW_IE_EXCEPTION << "Value " << vals << " cannot be casted to floats.";
153  }
154  }
155  return result;
156  }
157 
158  /**
159  * @brief Returns an integer value for the given parameter or returns the default value
160  * @param def Default value of the parameter if not found
161  * @return An int value for the specified parameter
162  */
163  int asInt(int def) const {
164  std::string val = asString(std::to_string(def));
165  try {
166  return std::stoi(val);
167  } catch (...) {
168  THROW_IE_EXCEPTION << "Value " << val << " cannot be casted to int.";
169  }
170  }
171 
172  /**
173  * @brief Returns an integer value for the given parameter
174  * @return An int value for the specified parameter
175  */
176  int asInt() const {
177  std::string val = asString();
178  try {
179  return std::stoi(val);
180  } catch (...) {
181  THROW_IE_EXCEPTION << "Value " << val << " cannot be casted to int.";
182  }
183  }
184 
185 
186  /**
187  * @brief Returns a vector of int values for the given parameter or returns the default value
188  * @param def Default value of the parameter if not found
189  * @return vector of int values
190  */
191  std::vector<int> asInts(std::vector<int> def) const {
192  std::string vals = asString("");
193  std::vector<int> result;
194  std::istringstream stream(vals);
195  std::string str;
196  if (vals.empty())
197  return def;
198  while (getline(stream, str, ',')) {
199  try {
200  result.push_back(std::stoi(str));
201  } catch (...) {
202  THROW_IE_EXCEPTION << "Value " << vals << " cannot be casted to ints.";
203  }
204  }
205  return result;
206  }
207 
208  /**
209  * @brief Returns a vector of int values for the given parameter
210  * @return vector of int values
211  */
212  std::vector<int> asInts() const {
213  std::string vals = asString();
214  std::vector<int> result;
215  std::istringstream stream(vals);
216  std::string str;
217  while (getline(stream, str, ',')) {
218  try {
219  result.push_back(std::stoi(str));
220  } catch (...) {
221  THROW_IE_EXCEPTION << "Value " << vals << " cannot be casted to ints.";
222  }
223  }
224  return result;
225  }
226  /**
227  * @brief Returns an unsigned integer value for the given parameter or returns the default value
228  * @param def Default value of the parameter if not found
229  * @return An unsigned integer value for the specified parameter
230  */
231  unsigned int asUInt(unsigned int def) const {
232  std::string val = asString(std::to_string(def));
233  std::string message = "Value " + val + " cannot be casted to unsigned int.";
234  try {
235  int value = std::stoi(val);
236  if (value < 0) {
237  THROW_IE_EXCEPTION << message;
238  }
239  return static_cast<unsigned int>(value);
240  } catch (...) {
241  THROW_IE_EXCEPTION << message;
242  }
243  }
244 
245  /**
246  * @brief Returns an unsigned integer value for the given parameter
247  * @return An unsigned integer value for the specified parameter
248  */
249  unsigned int asUInt() const {
250  std::string val = asString();
251  std::string message = "Value " + val + " cannot be casted to unsigned int.";
252  try {
253  int value = std::stoi(val);
254  if (value < 0) {
255  THROW_IE_EXCEPTION << message;
256  }
257  return static_cast<unsigned int>(value);
258  } catch (...) {
259  THROW_IE_EXCEPTION << message;
260  }
261  }
262 
263 
264  /**
265  * @brief Returns a vector of unsigned int values for the given parameter or returns the default value
266  * @param def Default value of the parameter if not found
267  * @return vector of unsigned int values
268  */
269  std::vector<unsigned int> asUInts(std::vector<unsigned int> def) const {
270  std::string vals = asString("");
271  std::vector<unsigned int> result;
272  std::istringstream stream(vals);
273  std::string str;
274  std::string message = "Value " + vals + " cannot be casted to unsigned ints.";
275  if (vals.empty())
276  return def;
277  while (getline(stream, str, ',')) {
278  try {
279  int value = std::stoi(str);
280  if (value < 0) {
281  THROW_IE_EXCEPTION << message;
282  }
283  result.push_back(static_cast<unsigned int>(value));
284  } catch (...) {
285  THROW_IE_EXCEPTION << message;
286  }
287  }
288  return result;
289  }
290 
291  /**
292  * @brief Returns a vector of unsigned int values for the given parameter
293  * @return vector of unsigned int values
294  */
295  std::vector<unsigned int> asUInts() const {
296  std::string vals = asString();
297  std::vector<unsigned int> result;
298  std::istringstream stream(vals);
299  std::string str;
300  std::string message = "Value " + vals + " cannot be casted to unsigned ints.";
301  while (getline(stream, str, ',')) {
302  try {
303  int value = std::stoi(str);
304  if (value < 0) {
305  THROW_IE_EXCEPTION << message;
306  }
307  result.push_back(static_cast<unsigned int>(value));
308  } catch (...) {
309  THROW_IE_EXCEPTION << message;
310  }
311  }
312  return result;
313  }
314 
315  /**
316  * @brief Returns an boolean value for the given parameter.
317  * The valid values are (true, false, 1, 0).
318  * @param def Default value of the parameter if not found
319  * @return An bool value for the specified parameter
320  */
321  bool asBool(bool def) const {
322  std::string val = asString(std::to_string(def));
323  std::string loweredCaseValue;
324  std::transform(val.begin(), val.end(), std::back_inserter(loweredCaseValue), [](char value) {
325  return std::tolower(value);
326  });
327 
328  bool result = false;
329 
330  if (!(std::istringstream(loweredCaseValue) >> std::boolalpha >> result)) {
331  // attempting parse using non alpha bool
332  return static_cast<bool>(asInt(def));
333  }
334 
335  return result;
336  }
337 
338  /**
339  * @brief Returns an boolean value for the given parameter.
340  * The valid values are (true, false, 1, 0).
341  * @return An bool value for the specified parameter
342  */
343  bool asBool() const {
344  std::string val = asString();
345  std::string loweredCaseValue;
346  std::transform(val.begin(), val.end(), std::back_inserter(loweredCaseValue), [](char value) {
347  return std::tolower(value);
348  });
349 
350  bool result = false;
351 
352  if (!(std::istringstream(loweredCaseValue) >> std::boolalpha >> result)) {
353  // attempting parse using non alpha bool
354  return static_cast<bool>(asInt());
355  }
356 
357  return result;
358  }
359 
360 private:
361  bool initialized;
362  std::string value;
363 };
364 
365 } // namespace InferenceEngine
#define THROW_IE_EXCEPTION
A macro used to throw the exception with a notable description.
Definition: ie_exception.hpp:22
Parameter(const std::vector< T > &values)
The constructor creates a Parameter object with a vector of template values.
Definition: ie_parameter.hpp:49
int asInt(int def) const
Returns an integer value for the given parameter or returns the default value.
Definition: ie_parameter.hpp:163
Definition: ie_argmax_layer.hpp:11
std::vector< int > asInts() const
Returns a vector of int values for the given parameter.
Definition: ie_parameter.hpp:212
float asFloat(float def) const
Gets float value for the given parameter.
Definition: ie_parameter.hpp:95
std::vector< unsigned int > asUInts(std::vector< unsigned int > def) const
Returns a vector of unsigned int values for the given parameter or returns the default value...
Definition: ie_parameter.hpp:269
unsigned int asUInt(unsigned int def) const
Returns an unsigned integer value for the given parameter or returns the default value.
Definition: ie_parameter.hpp:231
std::string asString(std::string def) const
Returns a string value for the given parameter or returns the default one.
Definition: ie_parameter.hpp:71
Parameter()=default
Default constructor.
bool asBool() const
Returns an boolean value for the given parameter. The valid values are (true, false, 1, 0).
Definition: ie_parameter.hpp:343
std::vector< float > asFloats() const
Returns a vector of float values for the given parameter.
Definition: ie_parameter.hpp:143
float asFloat() const
Returns a float value for the given layer parameter.
Definition: ie_parameter.hpp:108
int asInt() const
Returns an integer value for the given parameter.
Definition: ie_parameter.hpp:176
Parameter(const std::string &value)
The constructor creates a Parameter object with string value.
Definition: ie_parameter.hpp:35
std::string asString() const
Returns a string value for the given parameter. Throws exception if parameter was not found...
Definition: ie_parameter.hpp:83
This class represents an object to work with different parameters.
Definition: ie_parameter.hpp:24
unsigned int asUInt() const
Returns an unsigned integer value for the given parameter.
Definition: ie_parameter.hpp:249
Parameter(const T &value)
The constructor creates a Parameter object with template value.
Definition: ie_parameter.hpp:42
std::vector< unsigned int > asUInts() const
Returns a vector of unsigned int values for the given parameter.
Definition: ie_parameter.hpp:295
bool asBool(bool def) const
Returns an boolean value for the given parameter. The valid values are (true, false, 1, 0).
Definition: ie_parameter.hpp:321
std::vector< float > asFloats(std::vector< float > def) const
Returns a vector of float values for the given parameter or returns the default value.
Definition: ie_parameter.hpp:122
A header file for the main Inference Engine exception.
std::vector< int > asInts(std::vector< int > def) const
Returns a vector of int values for the given parameter or returns the default value.
Definition: ie_parameter.hpp:191