Elementa v8.0.0
Minimalistic library for any C++ application (C++11 and up)
Loading...
Searching...
No Matches
validatable.h
Go to the documentation of this file.
1
3#include "elementa/license.inc"
4#include "elementa/checks.inc"
5
6#ifndef ELEMENTA_PATTERNS_VALIDATABLE_H
7#define ELEMENTA_PATTERNS_VALIDATABLE_H
8
9#include <atomic>
11
12
13namespace elementa
14{
15
16namespace patterns
17{
18
74/* ***************************************************************************
75
76 Base class: Validatable
77 Macro declarations: ELE_CLASS_VALIDATABLE
78
79*******************************************************************************/
80
82#define ELE_CLASS_VALIDATABLE virtual public elementa::patterns::Validatable
83
85
100{
101 public:
102
104
106 bool valid(void) const noexcept { return(valid_); }
107
109
110 virtual void checkValid(const std::string & place = "") const
111 { if (!valid_)
112 ELE_CODE_INVSTATE(place.empty() ?
113 "Unspecified operation requested on "
114 "an invalid object" :
115 place); }
116
117 protected: // Avoids to create objects directly from Undefinable.
118
119 Validatable(bool initvalid = false) noexcept: valid_{initvalid} {}
120
122
126 Validatable(const Validatable & oth) { copyFrom(oth); }
127
129
136 { if (this != &oth) copyFrom(oth); return(*this); }
137
138 // Move constructor and assignment are not deleted: they will fall back to
139 // the normal copy ones.
140
141 virtual ~Validatable(void) = default;
142
144 void setValid(bool defval = true) noexcept { valid_ = defval; }
145
146 void setInvalid(void) noexcept { valid_ = false; }
147
148 private:
149
150 alignas(16) std::atomic<bool> valid_;
157 void copyFrom(const Validatable & oth)
158 { valid_ = (oth.valid_ ? true : false); }
159
160 void moveFrom(Validatable && oth)
161 { copyFrom(oth); oth.valid_ = false; }
162};
163
164 // Validatable
166
167
168} // end patterns namespace
169
170} // end elementa namespace
171
172
173#endif
174
175
#define ELE_CODE_INVSTATE(expl)
To throw an invalid-state exception with an explanation.
Definition: exceptions.h:306
Validatable(const Validatable &oth)
Derived classes must call this base ctor if they have non-default ones.
Definition: validatable.h:126
Validatable & operator=(const Validatable &oth)
Derived classes must call this base optor if they have non-default ones.
Definition: validatable.h:135
bool valid(void) const noexcept
Public method to be used by users of the derived class.
Definition: validatable.h:106
virtual void checkValid(const std::string &place="") const
Call this method if an exception is needed when in invalid state.
Definition: validatable.h:110
void setValid(bool defval=true) noexcept
Derived classes can rewrite their own (hiding this, calling this).
Definition: validatable.h:144
Any class derived from this base class gets a "valid"/"invalid" state.
Definition: validatable.h:100