Compute Library
 19.08
Error.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016-2018 ARM Limited.
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 #ifndef __ARM_COMPUTE_ERROR_H__
25 #define __ARM_COMPUTE_ERROR_H__
26 
27 #include <stdarg.h>
28 #include <string>
29 
30 namespace arm_compute
31 {
38 template <typename... T>
39 inline void ignore_unused(T &&...)
40 {
41 }
42 
44 enum class ErrorCode
45 {
46  OK,
49 };
50 
52 class Status
53 {
54 public:
57  : _code(ErrorCode::OK), _error_description(" ")
58  {
59  }
65  explicit Status(ErrorCode error_status, std::string error_description = " ")
66  : _code(error_status), _error_description(error_description)
67  {
68  }
70  Status(const Status &) = default;
72  Status(Status &&) = default;
74  Status &operator=(const Status &) = default;
76  Status &operator=(Status &&) = default;
81  explicit operator bool() const noexcept
82  {
83  return _code == ErrorCode::OK;
84  }
90  {
91  return _code;
92  }
97  std::string error_description() const
98  {
99  return _error_description;
100  }
102  void throw_if_error() const
103  {
104  if(!bool(*this))
105  {
106  internal_throw_on_error();
107  }
108  }
109 
110 private:
112  [[noreturn]] void internal_throw_on_error() const;
113 
114 private:
115  ErrorCode _code;
116  std::string _error_description;
117 };
118 
130 Status create_error_va_list(ErrorCode error_code, const char *function, const char *file, const int line, const char *msg, va_list args);
142 Status create_error(ErrorCode error_code, const char *function, const char *file, const int line, const char *msg, ...);
151 [[noreturn]] void error(const char *function, const char *file, const int line, const char *msg, ...);
152 }
160 #define ARM_COMPUTE_UNUSED(...) ::arm_compute::ignore_unused(__VA_ARGS__) // NOLINT
161 
167 #define ARM_COMPUTE_CREATE_ERROR(error_code, ...) ::arm_compute::create_error(error_code, __func__, __FILE__, __LINE__, __VA_ARGS__) // NOLINT
168 
177 #define ARM_COMPUTE_CREATE_ERROR_LOC(error_code, func, file, line, ...) ::arm_compute::create_error(error_code, func, file, line, __VA_ARGS__) // NOLINT
178 
183 #define ARM_COMPUTE_RETURN_ERROR_MSG(...) \
184  do \
185  { \
186  return ARM_COMPUTE_CREATE_ERROR(arm_compute::ErrorCode::RUNTIME_ERROR, __VA_ARGS__); \
187  } while(false)
188 
193 #define ARM_COMPUTE_RETURN_ON_ERROR(status) \
194  do \
195  { \
196  if(!bool(status)) \
197  { \
198  return status; \
199  } \
200  } while(false)
201 
206 #define ARM_COMPUTE_THROW_ON_ERROR(error) \
207  error.throw_if_error();
208 
214 #define ARM_COMPUTE_RETURN_ERROR_ON_MSG(cond, ...) \
215  do \
216  { \
217  if(cond) \
218  { \
219  return ARM_COMPUTE_CREATE_ERROR(arm_compute::ErrorCode::RUNTIME_ERROR, __VA_ARGS__); \
220  } \
221  } while(false)
222 
231 #define ARM_COMPUTE_RETURN_ERROR_ON_LOC_MSG(cond, func, file, line, ...) \
232  do \
233  { \
234  if(cond) \
235  { \
236  return ARM_COMPUTE_CREATE_ERROR_LOC(arm_compute::ErrorCode::RUNTIME_ERROR, func, file, line, __VA_ARGS__); \
237  } \
238  } while(false)
239 
244 #define ARM_COMPUTE_RETURN_ERROR_ON(cond) \
245  ARM_COMPUTE_RETURN_ERROR_ON_MSG(cond, #cond)
246 
254 #define ARM_COMPUTE_RETURN_ERROR_ON_LOC(cond, func, file, line) \
255  ARM_COMPUTE_RETURN_ERROR_ON_LOC_MSG(cond, func, file, line, #cond)
256 
261 #define ARM_COMPUTE_ERROR(...) ::arm_compute::error(__func__, __FILE__, __LINE__, __VA_ARGS__) // NOLINT
262 
270 #define ARM_COMPUTE_ERROR_LOC(func, file, line, ...) ::arm_compute::error(func, file, line, __VA_ARGS__) // NOLINT
271 
277 #define ARM_COMPUTE_EXIT_ON_MSG(cond, ...) \
278  do \
279  { \
280  if(cond) \
281  { \
282  ARM_COMPUTE_ERROR(__VA_ARGS__); \
283  } \
284  } while(false)
285 
286 #ifdef ARM_COMPUTE_ASSERTS_ENABLED
287 
291 #define ARM_COMPUTE_ERROR_THROW_ON(status) \
292  status.throw_if_error()
293 
299 #define ARM_COMPUTE_ERROR_ON_MSG(cond, ...) \
300  ARM_COMPUTE_EXIT_ON_MSG(cond, __VA_ARGS__)
301 
310 #define ARM_COMPUTE_ERROR_ON_LOC_MSG(cond, func, file, line, ...) \
311  do \
312  { \
313  if(cond) \
314  { \
315  ARM_COMPUTE_ERROR_LOC(func, file, line, __VA_ARGS__); \
316  } \
317  } while(false)
318 
325 #define ARM_COMPUTE_CONST_ON_ERROR(cond, val, msg) (cond) ? throw std::logic_error(msg) : val;
326 #else /* ARM_COMPUTE_ASSERTS_ENABLED */
327 #define ARM_COMPUTE_ERROR_THROW_ON(status)
328 #define ARM_COMPUTE_ERROR_ON_MSG(cond, ...)
329 #define ARM_COMPUTE_ERROR_ON_LOC_MSG(cond, func, file, line, ...)
330 #define ARM_COMPUTE_CONST_ON_ERROR(cond, val, msg) val
331 #endif /* ARM_COMPUTE_ASSERTS_ENABLED */
332 
337 #define ARM_COMPUTE_ERROR_ON(cond) \
338  ARM_COMPUTE_ERROR_ON_MSG(cond, #cond)
339 
347 #define ARM_COMPUTE_ERROR_ON_LOC(cond, func, file, line) \
348  ARM_COMPUTE_ERROR_ON_LOC_MSG(cond, func, file, line, #cond)
349 
350 #ifndef ARM_COMPUTE_EXCEPTIONS_DISABLED
351 #define ARM_COMPUTE_THROW(ex) throw(ex)
352 #else /* ARM_COMPUTE_EXCEPTIONS_DISABLED */
353 #define ARM_COMPUTE_THROW(ex) (ex), std::abort()
354 #endif /* ARM_COMPUTE_EXCEPTIONS_DISABLED */
355 
356 #endif /* __ARM_COMPUTE_ERROR_H__ */
Status(ErrorCode error_status, std::string error_description=" ")
Default Constructor.
Definition: Error.h:65
Status class.
Definition: Error.h:52
Copyright (c) 2017-2018 ARM Limited.
ErrorCode
Available error codes.
Definition: Error.h:44
Status & operator=(const Status &)=default
Allow instances of this class to be copy assigned.
void ignore_unused(T &&...)
Ignores unused arguments.
Definition: Error.h:39
void error(const char *function, const char *file, const int line, const char *msg,...)
Print an error message then throw an std::runtime_error.
Definition: Error.cpp:52
void throw_if_error() const
Throws a runtime exception in case it contains a valid error status.
Definition: Error.h:102
Status create_error(ErrorCode error_code, const char *function, const char *file, const int line, const char *msg,...)
Creates an error containing the error message.
Definition: Error.cpp:43
Status create_error_va_list(ErrorCode error_code, const char *function, const char *file, const int line, const char *msg, va_list args)
Creates an error containing the error message from variable argument list.
Definition: Error.cpp:34
Status()
Default Constructor.
Definition: Error.h:56
std::string error_description() const
Gets error description if any.
Definition: Error.h:97
ErrorCode error_code() const
Gets error code.
Definition: Error.h:89