Common System 0.2.0
Common interfaces and patterns for system integration
Loading...
Searching...
No Matches
utilities.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
21#pragma once
22
23#include "core.h"
24#include "compat.h"
25
26#include <stdexcept>
27#include <system_error>
28#include <utility>
29#include <variant>
30
31namespace kcenon::common {
32
33// ============================================================================
34// Factory Functions
35// ============================================================================
36
37// Factory functions for creating Results
38// See docs/BEST_PRACTICES.md for detailed usage patterns
39
53template<typename T>
54inline Result<T> ok(T value) {
55 return Result<T>(std::move(value));
56}
57
71inline VoidResult ok() {
72 return VoidResult(std::monostate{});
73}
74
90template<typename T>
91inline Result<T> make_error(int code, const std::string& message,
92 const std::string& module = "") {
93 return Result<T>(error_info{code, message, module});
94}
95
99template<typename T>
100inline Result<T> make_error(int code, const std::string& message,
101 const std::string& module,
102 const std::string& details) {
103 return Result<T>(error_info{code, message, module, details});
104}
105
109template<typename T>
111 return Result<T>(err);
112}
113
114// ============================================================================
115// Exception Conversion
116// ============================================================================
117
128class exception_mapper {
129public:
135 static error_info map_unknown_exception(const std::string& module = "") {
136 return error_info{error_codes::INTERNAL_ERROR, "Unknown exception caught", module,
137 "Non-standard exception (not derived from std::exception)"};
138 }
139
146 static error_info map_generic_exception(const std::exception& e, const std::string& module = "") {
147 return error_info{error_codes::INTERNAL_ERROR, e.what(), module, "std::exception"};
148 }
149};
150
172template<typename T, typename F>
173Result<T> try_catch(F&& func, const std::string& module = "") {
174 try {
175 return ok<T>(func());
176 }
177 // Memory allocation failure
178 catch (const std::bad_alloc& e) {
179 return Result<T>(error_info{error_codes::OUT_OF_MEMORY, e.what(), module, "std::bad_alloc"});
180 }
181 // Invalid argument (check before logic_error)
182 catch (const std::invalid_argument& e) {
183 return Result<T>(error_info{error_codes::INVALID_ARGUMENT, e.what(), module, "std::invalid_argument"});
184 }
185 // Out of range (check before logic_error)
186 catch (const std::out_of_range& e) {
187 return Result<T>(error_info{error_codes::INVALID_ARGUMENT, e.what(), module, "std::out_of_range"});
188 }
189 // System errors (check before runtime_error as it inherits from it)
190 catch (const std::system_error& e) {
191 return Result<T>(error_info{e.code().value(), e.what(), module,
192 std::string("std::system_error: ") + e.code().category().name()});
193 }
194 // Logic errors
195 catch (const std::logic_error& e) {
196 return Result<T>(error_info{error_codes::INTERNAL_ERROR, e.what(), module, "std::logic_error"});
197 }
198 // Runtime errors
199 catch (const std::runtime_error& e) {
200 return Result<T>(error_info{error_codes::INTERNAL_ERROR, e.what(), module, "std::runtime_error"});
201 }
202 // Generic std::exception
203 catch (const std::exception& e) {
205 }
206 // Unknown exception
207 catch (...) {
209 }
210}
211
223template<typename F>
224VoidResult try_catch_void(F&& func, const std::string& module = "") {
225 try {
226 func();
227 return ok();
228 }
229 // Memory allocation failure
230 catch (const std::bad_alloc& e) {
231 return VoidResult(error_info{error_codes::OUT_OF_MEMORY, e.what(), module, "std::bad_alloc"});
232 }
233 // Invalid argument (check before logic_error)
234 catch (const std::invalid_argument& e) {
235 return VoidResult(error_info{error_codes::INVALID_ARGUMENT, e.what(), module, "std::invalid_argument"});
236 }
237 // Out of range (check before logic_error)
238 catch (const std::out_of_range& e) {
239 return VoidResult(error_info{error_codes::INVALID_ARGUMENT, e.what(), module, "std::out_of_range"});
240 }
241 // System errors (check before runtime_error as it inherits from it)
242 catch (const std::system_error& e) {
243 return VoidResult(error_info{e.code().value(), e.what(), module,
244 std::string("std::system_error: ") + e.code().category().name()});
245 }
246 // Logic errors
247 catch (const std::logic_error& e) {
248 return VoidResult(error_info{error_codes::INTERNAL_ERROR, e.what(), module, "std::logic_error"});
249 }
250 // Runtime errors
251 catch (const std::runtime_error& e) {
252 return VoidResult(error_info{error_codes::INTERNAL_ERROR, e.what(), module, "std::runtime_error"});
253 }
254 // Generic std::exception
255 catch (const std::exception& e) {
257 }
258 // Unknown exception
259 catch (...) {
261 }
262}
263
264} // namespace kcenon::common
265
266// ============================================================================
267// Convenience Macros (from result_macros.h)
268// ============================================================================
269
270// Convenience macros for Result<T> pattern usage
271
279#define COMMON_RETURN_IF_ERROR(expr) \
280 do { \
281 auto _result_temp = (expr); \
282 if (_result_temp.is_err()) { \
283 return _result_temp.error(); \
284 } \
285 } while(false)
286
294#define COMMON_ASSIGN_OR_RETURN(decl, expr) \
295 auto _result_##decl = (expr); \
296 if (_result_##decl.is_err()) { \
297 return _result_##decl.error(); \
298 } \
299 decl = std::move(_result_##decl).value()
300
307#define COMMON_RETURN_ERROR_IF(condition, code, message, module) \
308 do { \
309 if (condition) { \
310 return kcenon::common::error_info{code, message, module}; \
311 } \
312 } while(false)
313
320#define COMMON_RETURN_ERROR_IF_WITH_DETAILS(condition, code, message, module, details) \
321 do { \
322 if (condition) { \
323 return kcenon::common::error_info{code, message, module, details}; \
324 } \
325 } while(false)
Result type for error handling with member function support.
Definition core.cppm:165
static error_info map_unknown_exception(const std::string &module="")
Map unknown exception (catch-all)
Definition utilities.h:135
static error_info map_generic_exception(const std::exception &e, const std::string &module="")
Map generic exception.
Definition utilities.h:146
Backward compatibility layer for error codes.
constexpr int OUT_OF_MEMORY
Definition compat.h:38
constexpr int INVALID_ARGUMENT
Definition compat.h:31
constexpr int INTERNAL_ERROR
Definition compat.h:42
Core interfaces.
Definition adapter.h:21
Result< T > make_error(int code, const std::string &message, const std::string &module="")
Create an error result with code and message.
Definition utilities.h:91
Result< std::monostate > VoidResult
Specialized Result for void operations.
Definition core.h:70
Result< T > try_catch(F &&func, const std::string &module="")
Convert exception to Result with automatic error code mapping.
Definition utilities.h:173
VoidResult err(const error_info &error)
Factory function to create error VoidResult.
Definition core.cppm:432
VoidResult ok()
Create a successful void result.
Definition utilities.h:71
VoidResult try_catch_void(F &&func, const std::string &module="")
Convert exception to VoidResult with automatic error code mapping.
Definition utilities.h:224
Consolidated core types for Result<T> pattern.
Standard error information used by Result<T>.
Definition core.cppm:106