57#if __has_include("defs.h")
63#if (defined(_MSVC_LANG) && _MSVC_LANG < 201703L) \
64 || (!defined(_MSVC_LANG) && (__cplusplus < 201703L))
65#error "errorcode requires c++17"
93 ~erc () noexcept (false);
94 erc& operator= (const
erc& rhs);
95 erc& operator= (
erc&& rhs);
96 operator
int () const;
107 bool operator== (const
erc& other) const;
108 bool operator!= (const
erc& other) const;
119 void message (const std::
string& m);
127 mutable
int active : 1;
129 const errfac& facility_;
159 virtual std::string
message (
const erc& e)
const;
162 const std::string&
name ()
const;
170 virtual void raise (
const erc& e)
const;
171 virtual void log (
const erc& e)
const;
177 static errfac* default_facility;
180#if (defined(_MSVC_LANG) && _MSVC_LANG >= 202002L) \
181 || (!defined(_MSVC_LANG) && (__cplusplus >= 202002L))
201template <checkable T>
224 checked (T&& obj_, erc&& err)
225 : erc (std::move (err))
226 , obj (std::move (obj_))
229 checked (T&& obj_,
const erc& err)
231 , obj (std::move (obj_))
246 ~checked () noexcept (false) = default;
253 erc::operator= (rhs);
264 *(
erc*)
this = std::move (rhs);
265 obj = std::move (rhs.obj);
273 erc::operator= (rhs);
380 return std::string (
"error ") + std::to_string (e.value);
385 return *default_facility;
394 default_facility = facility ? facility : &
deffac;
407 if (e.priority_ >= log_level)
409 if (e.priority_ >= throw_level)
421 fprintf (stderr,
"%s - %s\n",
name ().c_str (), e.
message ().c_str ());
441 , facility_{errfac::Default ()}
447 , priority_{(unsigned short)l}
448 , facility_{errfac::Default ()}
455 , priority_{(unsigned short)l}
474 , priority_{other.priority_}
475 , active{other.active}
476 , facility_{other.facility_}
486 , priority_{other.priority_}
487 , active{other.active}
488 , facility_{other.facility_}
516 int rhs_active = rhs.active;
518 if (active && priority_)
519 facility_.raise (*
this);
521 priority_ = rhs.priority_;
522 const_cast<errfac&
> (facility_) = rhs.facility_;
540 bool rhs_active = rhs.active;
542 if (active && value && priority_)
543 facility_.raise (*
this);
545 priority_ = rhs.priority_;
546 const_cast<errfac&
>(facility_) = rhs.facility_;
579 const_cast<erc&
> (other).active =
false;
580 if ((!priority_ || !value) && (!other.priority_ || !other.value))
582 if (&facility_ == &other.facility_ && priority_ == other.priority_ && value == other.value)
605 if (value && active && priority_)
606 facility_.raise (*
this);
628 return msg.empty () ? facility_.message (*
this) : msg;
642inline erc::operator int ()
const
Provides a mechanism similar to expected for creating objects associated with error codes.
Definition errorcode.h:203
checked(const checked< T > &other)
Copy constructor.
Definition errorcode.h:235
const T * operator->() const
Definition errorcode.h:308
T * operator->()
Definition errorcode.h:299
checked(const T &obj_, const erc &err)
Constructor using a T and an error code. Both are copy-constructed.
Definition errorcode.h:213
checked(checked< T > &&other)
Move constructor.
Definition errorcode.h:241
checked()
Definition errorcode.h:207
objects returned as a function result or thrown directly.
Definition errorcode.h:73
void reactivate() const
Similar to re-throwing an exception.
Definition errorcode.h:649
bool operator!=(const erc &other) const
Inequality comparison operator.
Definition errorcode.h:596
int code() const
Return numerical value.
Definition errorcode.h:615
std::string message() const
Get logging message.
Definition errorcode.h:626
bool operator==(const erc &other) const
Equality comparison operator.
Definition errorcode.h:576
level priority() const
Return priority value.
Definition errorcode.h:553
~erc() noexcept(false)
Destructor. Call raise() function to see if the error should get logged or thrown.
Definition errorcode.h:496
void deactivate() const
Marks error code as inactive.
Definition errorcode.h:659
static erc success
The SUCCESS indicator.
Definition errorcode.h:121
void raise() const
Definition errorcode.h:603
erc()
Default ctor for erc objects creates an inactive error.
Definition errorcode.h:437
bool is_active() const
Return activity flag.
Definition errorcode.h:558
const errfac & facility() const
Return reference to facility.
Definition errorcode.h:563
erc & operator=(const erc &rhs)
Principal assignment operator.
Definition errorcode.h:512
level
Error levels (borrowed from BSD Unix)
Definition errorcode.h:77
@ emerg
always logged, thrown
Definition errorcode.h:85
@ none
always not logged, not thrown
Definition errorcode.h:78
@ notice
default not logged, not thrown
Definition errorcode.h:80
@ info
default not logged, not thrown
Definition errorcode.h:79
@ error
default logged, thrown
Definition errorcode.h:82
@ alert
default logged, thrown
Definition errorcode.h:84
@ critical
default logged, thrown
Definition errorcode.h:83
@ warning
default logged, not thrown
Definition errorcode.h:81
An error facility routes a group of errors handled in a similar manner.
Definition errorcode.h:141
virtual void raise(const erc &e) const
Check if error must be logged or thrown.
Definition errorcode.h:405
const std::string & name() const
get name
Definition errorcode.h:372
erc::level log_priority() const
get log priority
Definition errorcode.h:367
virtual void log(const erc &e) const
Definition errorcode.h:419
erc::level throw_priority() const
get throw priority
Definition errorcode.h:357
void log_priority(erc::level pri)
set log priority
Definition errorcode.h:362
static void Default(errfac *f)
set default facility
Definition errorcode.h:392
errfac(const std::string &name="Error")
Set defaults for log and throw levels.
Definition errorcode.h:346
static errfac & Default()
get default facility
Definition errorcode.h:383
void throw_priority(erc::level pri)
set throw priority
Definition errorcode.h:352
virtual std::string message(const erc &e) const
return message to be logged
Definition errorcode.h:378
Definition errorcode.h:183
errfac deffac
The default facility.
Definition errorcode.h:327
timeval operator*(const timeval &op1, int op2)
Multiplication by an integer.
Definition tvops.h:188