ADTF  3.18.2
result_type_impl.h
Go to the documentation of this file.
1 
15 #ifndef A_UTIL_UTIL_RESULT_DETAIL_RESULT_TYPE_IMPL_HEADER_INCLUDED
16 #define A_UTIL_UTIL_RESULT_DETAIL_RESULT_TYPE_IMPL_HEADER_INCLUDED
17 
18 #include <a_util/base/types.h> //a_util::maybe_unused
24 
25 #include <utility> //std::swap
26 
27 namespace a_util {
28 namespace result {
29 
30 inline Result::Result() : _result_handle()
31 {
32 }
33 
34 inline Result::Result(bool val)
35  : _result_handle(DescriptionType::makeResultDescription(static_cast<std::int32_t>(!val) * -1))
36 {
37 }
38 
39 inline Result::Result(const ResultInfo<void>&) : _result_handle()
40 {
41 }
42 
43 template <typename ErrorType>
45  : _result_handle(DescriptionType::makeResultDescription(error))
46 {
47  a_util::maybe_unused(error); // C4100
48 }
49 
50 template <typename ErrorType>
52  const char* error_description,
53  std::int32_t line,
54  const char* file,
55  const char* function)
56  : _result_handle()
57 {
58  a_util::maybe_unused(error); // C4100
59  *this = Result(error.getCode(),
60  error_description,
61  line,
62  file,
63  function,
65 }
66 
67 inline Result::Result(std::int32_t error_code)
68  : _result_handle(DescriptionType::makeResultDescription(error_code))
69 {
70 }
71 
72 inline Result::Result(std::int32_t error_code,
73  const char* error_description,
74  std::int32_t line,
75  const char* file,
76  const char* function)
77  : _result_handle()
78 {
79  *this = createFrom(error_code, error_description, line, file, function);
80 }
81 
82 inline Result::Result(const Result& other_result,
83  const char* error_description,
84  std::int32_t line,
85  const char* file,
86  const char* function)
87  : _result_handle()
88 {
89  *this = Result(other_result.getErrorCode(),
90  error_description,
91  line,
92  file,
93  function,
94  other_result.getErrorLabel());
95 }
96 
98 {
99  static_assert(sizeof(*this) == 8, "sizeof(*this) != 8");
100  static_assert(std::is_standard_layout<Result>::value, "Result must be standard layout type");
101 }
102 
103 inline Result::Result(const Result& other) : _result_handle(other._result_handle)
104 {
105 }
106 
108 {
109  swap(*this, other);
110  return *this;
111 }
112 
113 inline Result& Result::operator=(bool val)
114 {
115  return Result::operator=(Result(val));
116 }
117 
118 inline Result& Result::operator=(std::int32_t val)
119 {
120  return Result::operator=(Result(val));
121 }
122 
124 {
125  return Result::operator=(Result());
126 }
127 
128 inline Result::Result(Result&& other) : _result_handle()
129 {
130  swap(*this, other);
131  if (SUCCESS != other) {
132  other = Result();
133  }
134 }
135 
136 inline std::int32_t Result::getErrorCode() const
137 { // get error code from the user space if available
140  }
141  return _result_handle.getErrorCode();
142 }
143 
144 inline const char* Result::getErrorLabel() const
145 {
147  // we can get the stringified error code from the detailed description
149  }
150 
151  return *this ? "SUCCESS" : "(unknown)";
152 }
153 
154 inline const char* Result::getDescription() const
155 {
156  if (0 != _result_handle.getErrorCode()) {
157  // only the error code was set, no description available
158  return "";
159  }
160 
163  }
164 
165  return "No error occurred";
166 }
167 
168 inline std::int32_t Result::getLine() const
169 {
172  }
173 
174  return -1;
175 }
176 
177 inline const char* Result::getFile() const
178 {
181  }
182 
183  return "";
184 }
185 
186 inline const char* Result::getFunction() const
187 {
190  }
191 
192  return "";
193 }
194 
195 inline Result::operator bool() const noexcept
196 {
197  return getErrorCode() == 0;
198 }
199 
200 inline Result::Result(std::int32_t error_code,
201  const char* error_description,
202  std::int32_t line,
203  const char* file,
204  const char* function,
205  const char* label)
206  : _result_handle(DescriptionType::makeResultDescription<detail::ErrorDescriptionType>(
207  error_code, error_description, line, file, function, label))
208 {
209 }
210 
211 inline Result Result::createFrom(std::int32_t error_code,
212  const char* error_description,
213  std::int32_t line,
214  const char* file,
215  const char* function)
216 {
217  return Result(error_code,
218  error_description,
219  line,
220  file,
221  function,
222  0 == error_code ? "SUCCESS" : "(unknown)");
223 }
224 
225 inline bool operator==(const Result& lhs, const Result& rhs)
226 {
227  return lhs._result_handle == rhs._result_handle;
228 }
229 
230 inline bool operator!=(const Result& lhs, const Result& rhs)
231 {
232  return !(lhs == rhs);
233 }
234 
235 template <typename T>
236 inline bool operator==(const Result& result, const ResultInfo<T>&)
237 {
238  return result.getErrorCode() == ResultInfo<T>::getCode();
239 }
240 
241 template <typename T>
242 inline bool operator!=(const Result& result, const ResultInfo<T>& error_code)
243 {
244  return !(result == error_code);
245 }
246 
247 template <typename T>
248 inline bool operator==(const ResultInfo<T>& error_code, const Result& result)
249 {
250  return result == error_code;
251 }
252 
253 template <typename T>
254 inline bool operator!=(const ResultInfo<T>& error_code, const Result& result)
255 {
256  return result != error_code;
257 }
258 
259 inline void swap(Result& lhs, Result& rhs)
260 {
261  // enable ADL
262  using std::swap;
264 }
265 
266 template <>
267 inline bool isOk<Result>(const Result& result)
268 {
269  return 0 == result.getErrorCode();
270 }
271 
272 } // namespace result
273 } // namespace a_util
274 
275 namespace std {
276 template <>
277 inline void swap<a_util::result::Result>(a_util::result::Result& lhs, a_util::result::Result& rhs)
278 {
279  a_util::result::swap(lhs, rhs);
280 }
281 } // namespace std
282 
283 #endif // A_UTIL_UTIL_RESULT_DETAIL_RESULT_TYPE_IMPL_HEADER_INCLUDED
A common result class usable as return value throughout.
const char * getFunction() const
Get name of the function the error was reported in.
const char * getFile() const
Get name of the file the error was reported in.
Result & operator=(const ResultInfo< void > &)
Assign with a_util::result::SUCCESS.
std::int32_t getLine() const
Get line in source file where the error was reported.
Result()
Construct empty which basically means no error occurred.
const char * getDescription() const
Get user provided error description.
friend void swap(Result &lhs, Result &rhs)
Specialized friend swap function to enable ADL in std::swap later.
std::int32_t getErrorCode() const
Get error code.
const char * getErrorLabel() const
Get error label as string representation.
static Result createFrom(std::int32_t error_code, const char *error_description, std::int32_t line, const char *file, const char *function)
Create a detailed object of type Result on the heap from an error code.
DescriptionType _result_handle
The result handler wrapper.
virtual const char * getFunctionName() const =0
Interface method to get the name of the function the error was reported in.
virtual std::int32_t getLine() const =0
Interface method to get the line of the source file where the error was reported.
virtual const char * getErrorDescription() const =0
Interface method to get the error code as string representation.
virtual const char * getErrorCodeLabel() const =0
Interface method to get the error code label.
virtual std::int32_t getErrorCode() const =0
Interface method to get the error code.
virtual const char * getFileName() const =0
Interface method to get the name of the file the error was reported in.
std::int32_t getErrorCode() const
Get the error code if only the error code was set.
DescriptionIntf const * getDetailedDescription() const
Get the detailed description if any was allocated.
Declare the default error description class for Result return values.
void maybe_unused(T &&)
Mimics C++17 attribute maybe_unused to silence compiler warnings on potentially unused enitities.
ErrorDescription ErrorDescriptionType
Alias type needed for reference counting.
void swap(Result &lhs, Result &rhs)
bool operator==(const ResultInfo< ResultTypeLHS > &lhs, const ResultInfo< ResultTypeRHS > &rhs)
Compare two specialized result information objects for equality.
bool isOk< Result >(const Result &result)
Specialization of a_util::result::isOk<typename ResultType>() for correct lookup.
bool operator!=(const ResultInfo< ResultTypeLHS > &lhs, const ResultInfo< ResultTypeRHS > &rhs)
Compare two specialized result information objects for inequality.
Serves as the root component, with common functionality documented in core functionality.
Definition: base.h:24
Private API for ResultDescription type and functions.
Private implementation for ResultDescription type and functions.
Common include for ResultInfo functionality.
Public API for Result type and functions.
Implements basic success result information for usage with a_util::result::Result.
Basic result information template, used by a_util::result::Result.
static std::int32_t getCode()
Get the result code distributed with value of type T.
Public types and functions defining a_util core functionality.