ie_exception.hpp
Go to the documentation of this file.
1 // Copyright (C) 2018 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4 
5 /**
6  * @brief A header file for the main Inference Engine exception
7  * \file ie_exception.hpp
8  */
9 #pragma once
10 
11 #include <memory>
12 #include <string>
13 #include <sstream>
14 #include <vector>
15 #include <functional>
16 #include <utility>
17 
18 /**
19  * @def THROW_IE_EXCEPTION
20  * @brief A macro used to throw the exception with a notable description
21  */
22 #define THROW_IE_EXCEPTION\
23  throw InferenceEngine::details::InferenceEngineException(__FILE__, __LINE__)\
24 
25 /**
26  * @def IE_ASSERT
27  * @brief Uses assert() function if NDEBUG is not defined, InferenceEngine exception otherwise
28  */
29 #ifdef NDEBUG
30  #define IE_ASSERT(EXPRESSION)\
31  if (!(EXPRESSION)) throw InferenceEngine::details::InferenceEngineException(__FILE__, __LINE__) << "AssertionFailed: " << #EXPRESSION // NOLINT
32 #else
33 #include <cassert>
34 #define IE_ASSERT(EXPRESSION)\
35  assert((EXPRESSION)); std::stringstream()
36 #endif // NDEBUG
37 
38 namespace InferenceEngine {
39 enum StatusCode: int;
40 namespace details {
41 
42 /**
43  * @brief The InferenceEngineException class implements the main Inference Engine exception
44  */
45 class InferenceEngineException : public std::exception {
46  mutable std::string errorDesc;
47  StatusCode status_code = static_cast<StatusCode>(0);
48  std::string _file;
49  int _line;
50  std::shared_ptr<std::stringstream> exception_stream;
51  bool save_to_status_code = false;
52 
53 public:
54  /**
55  * @brief A C++ std::exception API member
56  * @return An exception description with a file name and file line
57  */
58  const char *what() const noexcept override {
59  if (errorDesc.empty() && exception_stream) {
60  errorDesc = exception_stream->str();
61 #ifndef NDEBUG
62  errorDesc += "\n" + _file + ":" + std::to_string(_line);
63 #endif
64  }
65  return errorDesc.c_str();
66  }
67 
68  /**
69  * @brief A constructor. Creates an InferenceEngineException object from a specific file and line
70  * @param filename File where exception has been thrown
71  * @param line Line of the exception emitter
72  */
73  InferenceEngineException(const std::string &filename, const int line)
74  : _file(filename), _line(line) {
75  }
76 
77  /**
78  * @brief noexcept required for copy ctor
79  * @details The C++ Standard, [except.throw], paragraph 3 [ISO/IEC 14882-2014]
80  */
81  InferenceEngineException(const InferenceEngineException & that) noexcept {
82  errorDesc = that.errorDesc;
83  status_code = that.status_code;
84  _file = that._file;
85  _line = that._line;
86  exception_stream = that.exception_stream;
87  }
88 
89  /**
90  * @brief A stream output operator to be used within exception
91  * @param arg Object for serialization in the exception message
92  */
93  template<class T>
94  InferenceEngineException& operator<<(const T &arg) {
95  if (save_to_status_code) {
96  auto can_convert = status_code_assign(arg);
97  save_to_status_code = false;
98  if (can_convert.second) {
99  this->status_code = can_convert.first;
100  return *this;
101  }
102  }
103  if (!exception_stream) {
104  exception_stream.reset(new std::stringstream());
105  }
106  (*exception_stream) << arg;
107  return *this;
108  }
109 
110  /**
111  * @brief Manipulator to indicate that next item has to be converted to StatusCode to save
112  * @param iex InferenceEngineException object
113  */
114  friend InferenceEngineException& as_status(InferenceEngineException& iex) {
115  iex.save_to_status_code = true;
116  return iex;
117  }
118 
119  /**
120  * @brief A stream output operator to catch InferenceEngineException manipulators
121  * @param manip InferenceEngineException manipulator to call
122  */
123  InferenceEngineException& operator<<(InferenceEngineException& (*manip)(InferenceEngineException &)) {
124  return manip(*this);
125  }
126 
127  /** @brief Check if it has StatusCode value */
128  bool hasStatus() const {
129  return this->status_code == 0 ? false : true;
130  }
131 
132  /** @brief Get StatusCode value */
133  StatusCode getStatus() const {
134  return this->status_code;
135  }
136 
137 private:
138  std::pair<StatusCode, bool> status_code_assign(const StatusCode& status) {
139  return {status, true};
140  }
141 
142  template <typename T>
143  std::pair<StatusCode, bool> status_code_assign(const T &) {
144  return {static_cast<StatusCode>(0), false};
145  }
146 };
147 
148 InferenceEngineException& as_status(InferenceEngineException& iex);
149 
150 static_assert(std::is_nothrow_copy_constructible<InferenceEngineException>::value,
151  "InferenceEngineException must be nothrow copy constructible");
152 } // namespace details
153 } // namespace InferenceEngine
Definition: ie_argmax_layer.hpp:11