TEC
A lightweight C++ library enabling safe, efficient execution in multithreaded and concurrent systems.
Loading...
Searching...
No Matches
tec_status.hpp
Go to the documentation of this file.
1// Time-stamp: <Last changed 2026-02-20 16:03:55 by magnolia>
2/*----------------------------------------------------------------------
3------------------------------------------------------------------------
4Copyright (c) 2020-2026 The Emacs Cat (https://github.com/olddeuteronomy/tec).
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17------------------------------------------------------------------------
18----------------------------------------------------------------------*/
26#pragma once
27
28#include <iostream>
29#include <optional>
30#include <ostream>
31#include <sstream>
32#include <string>
33
34#include "tec/tec_def.hpp" // IWYU pragma: keep
35
36
37namespace tec {
38
44struct Error {
50 enum class Kind : int {
51 Ok,
52 Err,
53 IOErr,
55 NetErr,
56 RpcErr,
58 Invalid,
59 System,
62 };
63
71 template <typename TCode = int>
72 struct Code {
73 constexpr static const TCode Unspecified{-1};
74 };
75};
76
84inline constexpr const char* kind_as_string(Error::Kind k) {
85 switch (k) {
86 case Error::Kind::Ok: return "Success";
87 case Error::Kind::Err: return "Generic";
88 case Error::Kind::IOErr: return "IO";
89 case Error::Kind::RuntimeErr: return "Runtime";
90 case Error::Kind::NetErr: return "Network";
91 case Error::Kind::RpcErr: return "Rpc";
92 case Error::Kind::TimeoutErr: return "Timeout";
93 case Error::Kind::Invalid: return "Invalid";
94 case Error::Kind::System: return "System";
95 case Error::Kind::NotImplemented: return "NotImplemented";
96 case Error::Kind::Unsupported: return "Unsupported";
97 default: return "Unspecified";
98 }
99}
100
109template <typename TCode, typename TDesc>
110struct TStatus {
112 std::optional<TCode> code;
113 std::optional<TDesc> desc;
114
120 constexpr bool ok() const { return kind == Error::Kind::Ok; }
121
127 constexpr operator bool() const { return ok(); }
128
137 friend std::ostream& operator<<(std::ostream& out, const TStatus& status) {
138 out << "[" << kind_as_string(status.kind) << "]";
139 if (!status.ok()) {
140 out << " Code=" << status.code.value_or(Error::Code<TCode>::Unspecified)
141 << " Desc=\"" << (status.desc.has_value() ? status.desc.value() : "") << "\"";
142 }
143 return out;
144 }
145
151 std::string as_string() {
152 std::ostringstream buf;
153 buf << *this;
154 return buf.str();
155 }
156
163 : kind{Error::Kind::Ok}
164 {}
165
173 : kind{_kind}
174 , code{Error::Code<TCode>::Unspecified}
175 {}
176
185 TStatus(const TDesc& _desc, Error::Kind _kind = Error::Kind::Err)
186 : kind{_kind}
187 , code{Error::Code<TCode>::Unspecified}
188 , desc{_desc}
189 {}
190
199 TStatus(const TCode& _code, Error::Kind _kind = Error::Kind::Err)
200 : kind{_kind}
201 , code{_code}
202 {}
203
212 TStatus(const TCode& _code, const TDesc& _desc, Error::Kind _kind = Error::Kind::Err)
213 : kind{_kind}
214 , code{_code}
215 , desc{_desc}
216 {}
217}; // struct TStatus
218
224
225} // namespace tec
Defines error codes with a default unspecified value.
Definition tec_status.hpp:72
static constexpr const TCode Unspecified
Default unspecified error code.
Definition tec_status.hpp:73
Defines error types and codes for error handling in the tec library.
Definition tec_status.hpp:44
Kind
Enumerates possible error categories.
Definition tec_status.hpp:50
@ Invalid
Invalid data or state.
@ NetErr
Network-related error.
@ TimeoutErr
Timeout during an operation.
@ NotImplemented
Not implemented.
@ System
System-level error.
@ Ok
Indicates successful execution.
@ RuntimeErr
Runtime error during execution.
@ Unsupported
The feature is unsupported.
@ Err
Generic error.
@ RpcErr
Remote procedure call error.
@ IOErr
Input/output operation failure.
Represents the status of an execution with error details.
Definition tec_status.hpp:110
std::optional< TDesc > desc
Optional error description.
Definition tec_status.hpp:113
Error::Kind kind
The error category.
Definition tec_status.hpp:111
TStatus()
Constructs a successful status.
Definition tec_status.hpp:162
TStatus(const TDesc &_desc, Error::Kind _kind=Error::Kind::Err)
Constructs an error status with a description.
Definition tec_status.hpp:185
TStatus(const TCode &_code, Error::Kind _kind=Error::Kind::Err)
Constructs an error status with a code.
Definition tec_status.hpp:199
constexpr bool ok() const
Checks if the status indicates success.
Definition tec_status.hpp:120
std::string as_string()
Converts the status to a string representation.
Definition tec_status.hpp:151
TStatus(Error::Kind _kind)
Constructs an error status with an unspecified code.
Definition tec_status.hpp:172
friend std::ostream & operator<<(std::ostream &out, const TStatus &status)
Outputs the status to an output stream.
Definition tec_status.hpp:137
TStatus(const TCode &_code, const TDesc &_desc, Error::Kind _kind=Error::Kind::Err)
Constructs an error status with code and description.
Definition tec_status.hpp:212
std::optional< TCode > code
Optional error code.
Definition tec_status.hpp:112
Common definitions and utilities for the tec namespace.
constexpr const char * kind_as_string(Error::Kind k)
Converts an Error::Kind value to its string representation.
Definition tec_status.hpp:84