tcalc 0.2.0
 
Loading...
Searching...
No Matches
error.hpp File Reference

tcalc error definition. More...

#include <cerrno>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <exception>
#include <string>
#include <unordered_map>
#include <expected>
#include "tcalc/common.hpp"

Go to the source code of this file.

Classes

class  tcalc::error::Error
 Error class. More...
 

Macros

#define _TCALC_EXPECTED_NS   std
 
#define ret_err(expr)
 
#define unwrap_err(expr)
 
#define log_err(expr)
 
#define log_err_exit(expr)
 

Typedefs

template<typename T >
using tcalc::error::Result = _TCALC_EXPECTED_NS::expected< T, Error >
 Result type.
 

Enumerations

enum class  tcalc::error::Code : uint8_t {
  SYNTAX_ERROR , UNDEFINED_VAR , UNDEFINED_FUNC , MISMATCHED_ARGS ,
  ZERO_DIVISION , RECURSION_LIMIT , FILE_NOT_FOUND
}
 Error code. More...
 

Functions

TCALC_INLINE auto tcalc::error::err (Code code, const std::string &message) noexcept
 Create an error.
 
TCALC_INLINE auto tcalc::error::raw_err (Code code) noexcept
 Create a raw error with errno.
 
TCALC_INLINE auto tcalc::error::err (Code code) noexcept
 Create an error with errno.
 
_TCALC_EXPECTED_NS::unexpected< Errortcalc::error::err (Code code, const char *fmt,...) noexcept
 Create an error with format.
 
template<typename T , typename... Args>
TCALC_INLINE auto tcalc::error::ok (Args &&... args) noexcept
 Create a result with value.
 
template<>
TCALC_INLINE auto tcalc::error::ok< void > () noexcept
 Create a result with void value.
 

Variables

const std::unordered_map< Code, std::string > tcalc::error::CODE_NAMES
 

Detailed Description

tcalc error definition.

Author
Dessera (desse.nosp@m.ra@q.nosp@m.q.com)
Version
0.2.0
Date
2025-06-15

Macro Definition Documentation

◆ log_err

#define log_err (   expr)
Value:
{ \
auto _ret = (expr); \
if (!_ret.has_value()) { \
_ret.error().log(); \
} \
}

◆ log_err_exit

#define log_err_exit (   expr)
Value:
{ \
auto _ret = (expr); \
if (!_ret.has_value()) { \
_ret.error().log(); \
std::exit(EXIT_FAILURE); \
} \
}

◆ ret_err

#define ret_err (   expr)
Value:
if (auto res = (expr); !res.has_value()) { \
return _TCALC_EXPECTED_NS::unexpected(res.error()); \
}

◆ unwrap_err

#define unwrap_err (   expr)
Value:
({ \
auto _ret = (expr); \
if (!_ret.has_value()) { \
return _TCALC_EXPECTED_NS::unexpected(_ret.error()); \
} \
_ret.value(); \
})

Typedef Documentation

◆ Result

template<typename T >
using tcalc::error::Result = typedef _TCALC_EXPECTED_NS::expected<T, Error>

Result type.

Template Parameters
TReturn type.

Enumeration Type Documentation

◆ Code

enum class tcalc::error::Code : uint8_t
strong

Error code.

Enumerator
SYNTAX_ERROR 

Syntax error in tokenizer and parser.

UNDEFINED_VAR 

Undefined variable.

UNDEFINED_FUNC 

Undefined function.

MISMATCHED_ARGS 

Mismatched arguments.

ZERO_DIVISION 

Division by zero.

RECURSION_LIMIT 

Recursion limit exceeded.

FILE_NOT_FOUND 

File not found.

Function Documentation

◆ err() [1/3]

TCALC_INLINE auto tcalc::error::err ( Code  code)
noexcept

Create an error with errno.

Parameters
codeError code.
Returns
Result<T> Error.

◆ err() [2/3]

_TCALC_EXPECTED_NS::unexpected< Error > tcalc::error::err ( Code  code,
const char fmt,
  ... 
)
noexcept

Create an error with format.

Parameters
codeError code.
fmtFormat string.
argsArguments.
Returns
Result<T> Error.
Note
To prevent using <format> or <print> headers, this function uses std::vsnprintf.

◆ err() [3/3]

TCALC_INLINE auto tcalc::error::err ( Code  code,
const std::string &  message 
)
noexcept

Create an error.

Parameters
codeError code.
messageError message.
Returns
Result<T> Error.

◆ ok()

template<typename T , typename... Args>
TCALC_INLINE auto tcalc::error::ok ( Args &&...  args)
noexcept

Create a result with value.

Template Parameters
TReturn type.
ArgsArguments to construct the value.
Parameters
argsArguments to construct the value.
Returns
Result<T> Result.

◆ ok< void >()

template<>
TCALC_INLINE auto tcalc::error::ok< void > ( )
noexcept

Create a result with void value.

Returns
Result<void> Result.

◆ raw_err()

TCALC_INLINE auto tcalc::error::raw_err ( Code  code)
noexcept

Create a raw error with errno.

Parameters
codeError code.
Returns
Error Raw error.

Variable Documentation

◆ CODE_NAMES

const std::unordered_map<Code, std::string> tcalc::error::CODE_NAMES
inline
Initial value:
= {
{ Code::SYNTAX_ERROR, "SYNAX_ERROR" },
{ Code::UNDEFINED_VAR, "UNDEFINED_VAR" },
{ Code::UNDEFINED_FUNC, "UNDEFINED_FUNC" },
{ Code::MISMATCHED_ARGS, "MISMATCHED_ARGS" },
{ Code::ZERO_DIVISION, "ZERO_DIVISION" },
{ Code::RECURSION_LIMIT, "RECURSION_LIMIT" },
{ Code::FILE_NOT_FOUND, "FILE_NOT_FOUND" },
}

Error code names.