Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
result_types.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2024, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
11#pragma once
12
22
23#if KCENON_WITH_COMMON_SYSTEM
24 #include <kcenon/common/patterns/result.h>
25 #include <kcenon/common/error/error_codes.h>
26#endif
27
28#include <variant>
29#include <string>
30
31namespace kcenon::network {
32
33#if KCENON_WITH_COMMON_SYSTEM
34 // ============================================================
35 // Use common::Result<T> directly for new code
36 // ============================================================
37 //
38 // network_system uses common_system's Result<T> as the primary
39 // error handling mechanism. New code should use kcenon::common::Result<T>
40 // directly for ecosystem-wide consistency.
41 //
42 // The type aliases (network::Result<T>, network::VoidResult,
43 // network::error_info) are provided for internal use and backward
44 // compatibility but are deprecated for external use.
45 //
46 // Migration path for external code:
47 // - kcenon::network::Result<T> -> kcenon::common::Result<T>
48 // - kcenon::network::VoidResult -> kcenon::common::VoidResult
49 // - kcenon::network::error_info -> kcenon::common::error_info
50 //
51 // See: https://github.com/kcenon/network_system/issues/494
52 // ============================================================
53
54 // Type aliases for internal use (deprecated for external use)
55 // These map directly to common_system types
56 template<typename T>
57 using Result = ::kcenon::common::Result<T>;
58 using VoidResult = ::kcenon::common::VoidResult;
59 using error_info = ::kcenon::common::error_info;
60
61namespace internal {
62 // Internal type aliases - same as namespace-level types
63 // Provided for explicit internal-only usage pattern
64 template<typename T>
65 using Result = ::kcenon::common::Result<T>;
66 using VoidResult = ::kcenon::common::VoidResult;
67 using error_info = ::kcenon::common::error_info;
68} // namespace internal
69
70 // Error code namespace (includes common_errors and network_system)
71 namespace error_codes = ::kcenon::common::error::codes;
72
73 // Extended error codes not yet in common_system
74 namespace error_codes_ext {
75 namespace network_system {
76 constexpr int circuit_open = -604;
77 }
78 }
79
80 // Helper functions for creating Results
81 template<typename T>
82 inline Result<T> ok(T&& value) {
83 return Result<T>(std::forward<T>(value));
84 }
85
86 inline VoidResult ok() {
87 return VoidResult(std::monostate{});
88 }
89
90 template<typename T>
91 inline Result<T> error(int code, const std::string& message,
92 const std::string& source = "network_system",
93 const std::string& details = "") {
94 return Result<T>(error_info(code, message, source, details));
95 }
96
97 inline VoidResult error_void(int code, const std::string& message,
98 const std::string& source = "network_system",
99 const std::string& details = "") {
100 return VoidResult(error_info(code, message, source, details));
101 }
102
103 // Helper to extract details string from error_info (handles optional<string>)
104 inline std::string get_error_details(const error_info& err) {
105 return err.details.value_or("");
106 }
107
108 // Helper to extract source/module string from error_info
109 inline const std::string& get_error_source(const error_info& err) {
110 return err.module;
111 }
112
113#else
114 // Fallback: Simple Result<T> implementation when common_system is not available
115 // This maintains API compatibility but with minimal error information
116
118 int code;
119 std::string message;
120 std::string source;
121 std::string details;
122
123 simple_error(int c, std::string msg, std::string src = "", std::string det = "")
124 : code(c), message(std::move(msg)), source(std::move(src)), details(std::move(det)) {}
125 };
126
127 template<typename T>
128 class Result {
129 public:
130 Result(const T& val) : data_(val) {}
131 Result(T&& val) : data_(std::forward<T>(val)) {}
133
134 // Static factory methods for API compatibility with common::Result<T>
135 template<typename U = T>
136 static Result<T> ok(U&& value) {
137 return Result<T>(std::forward<U>(value));
138 }
139
141 return Result<T>(error);
142 }
143
144 static Result<T> err(int code, const std::string& message, const std::string& source = "") {
145 return Result<T>(simple_error(code, message, source));
146 }
147
148 bool is_ok() const { return std::holds_alternative<T>(data_); }
149 bool is_err() const { return !is_ok(); }
150
151 const T& value() const { return std::get<T>(data_); }
152 T& value() { return std::get<T>(data_); }
153
154 const simple_error& error() const { return std::get<simple_error>(data_); }
155
156 operator bool() const { return is_ok(); }
157
158 private:
159 std::variant<T, simple_error> data_;
160 };
161
164
165namespace internal {
166 // Internal type aliases for network_system implementation
167 // These provide a consistent interface regardless of common_system availability
168 template<typename T>
172} // namespace internal
173
174 // Minimal error codes (fallback)
175 namespace error_codes {
176 namespace network_system {
177 constexpr int connection_failed = -600;
178 constexpr int connection_refused = -601;
179 constexpr int connection_timeout = -602;
180 constexpr int connection_closed = -603;
181 constexpr int circuit_open = -604;
182 constexpr int send_failed = -640;
183 constexpr int receive_failed = -641;
184 constexpr int server_not_started = -660;
185 constexpr int server_already_running = -661;
186 constexpr int bind_failed = -662;
187 }
188 namespace common_errors {
189 constexpr int success = 0;
190 constexpr int invalid_argument = -1;
191 constexpr int not_found = -2;
192 constexpr int permission_denied = -3;
193 constexpr int timeout = -4;
194 constexpr int cancelled = -5;
195 constexpr int not_initialized = -6;
196 constexpr int already_exists = -7;
197 constexpr int out_of_memory = -8;
198 constexpr int io_error = -9;
199 constexpr int network_error = -10;
200 constexpr int internal_error = -99;
201 }
202 // Legacy compatibility
203 namespace common {
205 constexpr int not_initialized = common_errors::not_initialized;
206 constexpr int already_exists = common_errors::already_exists;
207 constexpr int internal_error = -99;
208 }
209 }
210
211 // Extended error codes (for API compatibility with KCENON_WITH_COMMON_SYSTEM path)
212 namespace error_codes_ext {
213 namespace network_system {
214 constexpr int circuit_open = -604;
215 }
216 }
217
218 // Helper functions
219 template<typename T>
220 inline Result<T> ok(T&& value) {
221 return Result<T>(std::forward<T>(value));
222 }
223
224 inline VoidResult ok() {
225 return VoidResult(std::monostate{});
226 }
227
228 template<typename T>
229 inline Result<T> error(int code, const std::string& message,
230 const std::string& source = "network_system",
231 const std::string& details = "") {
232 return Result<T>(simple_error(code, message, source, details));
233 }
234
235 inline VoidResult error_void(int code, const std::string& message,
236 const std::string& source = "network_system",
237 const std::string& details = "") {
238 return VoidResult(simple_error(code, message, source, details));
239 }
240
241 // Helper to extract details string from simple_error (string type)
242 inline std::string get_error_details(const simple_error& err) {
243 return err.details;
244 }
245
246 // Helper to extract source string from simple_error
247 inline const std::string& get_error_source(const simple_error& err) {
248 return err.source;
249 }
250
251#endif
252
253} // namespace kcenon::network
static Result< T > err(int code, const std::string &message, const std::string &source="")
static Result< T > ok(U &&value)
std::variant< T, simple_error > data_
const T & value() const
Result(const simple_error &err)
static Result< T > err(const simple_error &error)
const simple_error & error() const
Feature flags for network_system.
::kcenon::network::VoidResult VoidResult
::kcenon::network::Result< T > Result
::kcenon::network::error_info error_info
Main namespace for all Network System components.
const std::string & get_error_source(const simple_error &err)
std::string get_error_details(const simple_error &err)
Result< std::monostate > VoidResult
simple_error error_info
VoidResult error_void(int code, const std::string &message, const std::string &source="network_system", const std::string &details="")
VoidResult ok()
simple_error(int c, std::string msg, std::string src="", std::string det="")