Line data Source code
1 : // SPDX-License-Identifier: Apache-2.0
2 : /**
3 : * Copyright (C) 2020 Jijoong Moon <jijoong.moon@samsung.com>
4 : *
5 : * @file nntrainer_error.h
6 : * @date 03 April 2020
7 : * @brief NNTrainer Error Codes
8 : * @see https://github.com/nnstreamer/nntrainer
9 : * @author Jijoong Moon <jijoong.moon@samsung.com>
10 : * @bug No known bugs except for NYI items
11 : */
12 :
13 : #ifndef __NNTRAINER_ERROR_H__
14 : #define __NNTRAINER_ERROR_H__
15 :
16 : #if defined(__TIZEN__)
17 : #include <tizen_error.h>
18 : #define ML_ERROR_BAD_ADDRESS TIZEN_ERROR_BAD_ADDRESS
19 : #define ML_ERROR_RESULT_OUT_OF_RANGE TIZEN_ERROR_RESULT_OUT_OF_RANGE
20 : #else
21 : #include <cerrno>
22 : #define ML_ERROR_BAD_ADDRESS (-EFAULT)
23 : #define ML_ERROR_RESULT_OUT_OF_RANGE (-ERANGE)
24 : #endif
25 :
26 : #include <cstring>
27 : #include <functional>
28 : #include <iostream>
29 : #include <sstream>
30 : #include <stdexcept>
31 :
32 : #define NNTR_THROW_IF(pred, err) \
33 : if ((pred)) \
34 : nntrainer::exception::ErrorNotification<err> {}
35 :
36 : #define NNTR_THROW_IF_CLEANUP(pred, err, cleanup_func) \
37 : if ((pred)) \
38 : nntrainer::exception::ErrorNotification<err> { cleanup_func }
39 :
40 : #ifdef _WIN32
41 : #define SAFE_STRERROR(errnum, buffer, size) strerror_s(buffer, size, errnum)
42 : #else
43 : #define SAFE_STRERROR(errnum, buffer, size) strerror_r(errnum, buffer, size)
44 : #endif
45 :
46 : #if ML_API_COMMON
47 : #include <ml-api-common.h>
48 : #else
49 : /**
50 : @ref:
51 : https://gitlab.freedesktop.org/dude/gst-plugins-base/commit/89095e7f91cfbfe625ec2522da49053f1f98baf8
52 : */
53 : #if !defined(ESTRPIPE)
54 : #define ESTRPIPE EPIPE
55 : #endif /* !defined(ESTRPIPE) */
56 :
57 : #define _ERROR_UNKNOWN (-1073741824LL)
58 : #define TIZEN_ERROR_TIMED_OUT (TIZEN_ERROR_UNKNOWN + 1)
59 : #define TIZEN_ERROR_NOT_SUPPORTED (TIZEN_ERROR_UNKNOWN + 2)
60 : #define TIZEN_ERROR_PERMISSION_DENIED (-EACCES)
61 : #define TIZEN_ERROR_OUT_OF_MEMORY (-ENOMEM)
62 : typedef enum {
63 : ML_ERROR_NONE = 0, /**< Success! */
64 : ML_ERROR_INVALID_PARAMETER = -EINVAL, /**< Invalid parameter */
65 : ML_ERROR_TRY_AGAIN =
66 : -EAGAIN, /**< The pipeline is not ready, yet (not negotiated, yet) */
67 : ML_ERROR_UNKNOWN = _ERROR_UNKNOWN, /**< Unknown error */
68 : ML_ERROR_TIMED_OUT = (_ERROR_UNKNOWN + 1), /**< Time out */
69 : ML_ERROR_NOT_SUPPORTED =
70 : (_ERROR_UNKNOWN + 2), /**< The feature is not supported */
71 : ML_ERROR_PERMISSION_DENIED = -EACCES, /**< Permission denied */
72 : ML_ERROR_OUT_OF_MEMORY = -ENOMEM, /**< Out of memory (Since 6.0) */
73 : } ml_error_e;
74 : #endif
75 :
76 : namespace nntrainer {
77 :
78 : /// @note underscore_case is used for ::exception to keep in accordance with
79 : /// std::exception
80 : namespace exception {
81 :
82 : /**
83 : * @brief Error Notification class, error is thrown when the class is destroyed.
84 : * DO NOT use this outside as this contains throwing destructor.
85 : *
86 : * @tparam Err Error type that except cstring as an argument.
87 : */
88 : template <typename Err,
89 : typename std::enable_if_t<std::is_base_of<std::exception, Err>::value,
90 : Err> * = nullptr>
91 : class ErrorNotification {
92 : public:
93 : /**
94 : * @brief Construct a new Error Notification object
95 : *
96 : */
97 464 : explicit ErrorNotification() : cleanup_func() {}
98 :
99 : /**
100 : * @brief Construct a new Error Notification object
101 : *
102 : * @param cleanup_func_ clean up function
103 : */
104 1 : explicit ErrorNotification(std::function<void()> cleanup_func_) :
105 1 : cleanup_func(cleanup_func_) {}
106 :
107 : #if defined(_MSC_VER)
108 : #pragma warning(push)
109 : #pragma warning(disable : 4722) // MSVC: destructor never returns
110 : #endif
111 :
112 : /**
113 : * @brief Destroy the Error Notification object, Error is thrown when
114 : * destroying this
115 : *
116 : */
117 465 : ~ErrorNotification() noexcept(false) {
118 465 : if (cleanup_func) {
119 : try {
120 : cleanup_func();
121 0 : } catch (const std::exception &e) {
122 0 : std::cerr << "Exception during cleanup: " << e.what() << '\n';
123 : }
124 : }
125 1395 : throw Err(ss.str().c_str());
126 465 : }
127 :
128 : #if defined(_MSC_VER)
129 : #pragma warning(pop)
130 : #endif
131 :
132 : /**
133 : * @brief Error notification stream wrapper
134 : *
135 : * @tparam T anything that will be delegated to the move
136 : * @param out out stream
137 : * @param e anything to pass to the stream
138 : * @return ErrorNotification<Err>&& self
139 : */
140 : template <typename T>
141 : friend ErrorNotification<Err> &&operator<<(ErrorNotification<Err> &&out,
142 : T &&e) {
143 559 : out.ss << std::forward<T>(e);
144 465 : return std::move(out);
145 : }
146 :
147 : private:
148 : std::stringstream ss;
149 : std::function<void()> cleanup_func;
150 : };
151 :
152 : /**
153 : * @brief derived class of invalid argument to represent specific functionality
154 : * not supported
155 : * @note this could be either intended or not yet implemented
156 : */
157 : struct not_supported : public std::invalid_argument {
158 151 : using invalid_argument::invalid_argument;
159 : };
160 :
161 : /**
162 : * @brief derived class of invalid argument to represent permission is denied
163 : */
164 : struct permission_denied : public std::invalid_argument {
165 0 : using invalid_argument::invalid_argument;
166 : };
167 :
168 : } // namespace exception
169 :
170 : } // namespace nntrainer
171 :
172 : #endif /* __NNTRAINER_ERROR_H__ */
|