![]() |
Elementa v8.0.0
Minimalistic library for any C++ application (C++11 and up)
|
Serializers of diverse types to/from serial channels.
In Elementa, a serializer is a class able to serialize/deserialize another class, which is called the serializable class. This separation of concepts allows us to define several serialization formats for the same serializable class, and also to define serializations on primitive types that have none.
There are two ways of defining serializers: either derive from Serializer<> (creating a new serializer for some class) or derive the serializable class from Serializable (embedding the serializer directly into the serializable class).

Classes | |
| class | elementa::base::SzerError |
| Base class for all errors thrown by serializers. More... | |
| class | elementa::base::Serializer< Sble, ReturnDeserType, ReturnSerType > |
| Base abstract class / interface for any serializer. More... | |
| class | elementa::base::Serializable< DeserReturnType, SerReturnType > |
| Base class for any object that includes an implicit serializer. More... | |
| class | elementa::base::Szer_Char |
| Serializer of a single char. Not very useful, but sometimes it is needed. More... | |
| class | elementa::base::Szer_String |
| Serializer of std::string into fixed or non-fixed size. More... | |
| class | elementa::base::Szer_NatBin< NatType > |
| Serializer of natural numbers in fixed or non-fixed size. More... | |
| class | elementa::base::Szer_IntText |
| Serializer of integer numbers as text in fixed or non-fixed size. More... | |
| class | elementa::base::Szer_RealText |
| Serializer of real numbers as text in fixed or dynamic size. More... | |
| class | elementa::base::Szer_StringToken |
| Serializer of a std::string as a token (useful for lexers). More... | |
Typedefs | |
| template<class Sble , class DeserReturnType = void, class SerReturnType = void> | |
| using | elementa::base::ClassSzer = ClassSzer_< Sble, DeserReturnType, SerReturnType > |
| Objects of this class act as Serializer's for Serializable objects. More... | |
Functions | |
| template<class DeserReturnType , class SerReturnType > | |
| OutSerCh & | elementa::base::operator<< (OutSerCh &chout, const Serializable< DeserReturnType, SerReturnType > &o) |
| Operator for serializing Serializable objects directly on a channel. More... | |
| template<class DeserReturnType , class SerReturnType > | |
| InSerCh & | elementa::base::operator>> (InSerCh &chin, Serializable< DeserReturnType, SerReturnType > &o) |
| Operator for deserializing Serializable objects directly from a channel. More... | |
| void | elementa::base::Szer_NatBin< NatType >::ser (OutSerCh &chout, const NatType &obj) |
| Serialization of the object into CHOUT. More... | |
| void | elementa::base::Szer_NatBin< NatType >::deser (InSerCh &chin, NatType &obj) |
| Deserializes the number from CHIN. More... | |
| class elementa::base::SzerError |
Base class for all errors thrown by serializers.
Definition at line 82 of file serializers.h.


Public Types | |
Types, consts., etc. | |
| using | StdBase = std::runtime_error |
Public Member Functions | |
| SzerError (const std::string &explanation) | |
Launching methods | |
| virtual Exc & | asERR (const std::string &context) noexcept |
| Transform it to be thrown as ERR with the given context. More... | |
| virtual Exc & | asEXC (const std::string &place, const RTTextWithEnum::Combination &flags={RTTextWith::kAll_}) noexcept |
| Transform it to be thrown as EXC with the given place and flags. More... | |
Catching methods | |
| const char * | what (void) const noexcept override |
| Return the complete message. It will live as long as this exception. More... | |
| const char * | explanation (void) const noexcept |
| Return the explanation only. It will live as long as this exception. More... | |
| const char * | context (void) const noexcept |
| Return the place (EXC) or context (ERR) only. Will live as long as this. More... | |
|
inherited |
Definition at line 119 of file exceptions.h.
|
inline |
Definition at line 86 of file serializers.h.
|
virtualnoexceptinherited |
Transform it to be thrown as ERR with the given context.
Reimplemented in elementa::base::unimplemented, elementa::base::todo, elementa::base::notreach, elementa::base::invstate, elementa::base::internal_error, elementa::base::notfound, elementa::base::invarg, and elementa::base::outofrange.
|
virtualnoexceptinherited |
Transform it to be thrown as EXC with the given place and flags.
The flags are explained in debugging.h, runtime_src_place() function.
Referenced by elementa::base::Szer_Char::deser(), and elementa::base::Szer_NatBin< NatType >::deser().
|
inlineoverridenoexceptinherited |
Return the complete message. It will live as long as this exception.
The message will be one-line except if the explanation or place/context contain some CR.
Definition at line 161 of file exceptions.h.
|
inlinenoexceptinherited |
Return the explanation only. It will live as long as this exception.
Definition at line 164 of file exceptions.h.
|
inlinenoexceptinherited |
Return the place (EXC) or context (ERR) only. Will live as long as this.
Definition at line 167 of file exceptions.h.
| class elementa::base::Serializer |
Base abstract class / interface for any serializer.
New serializers must derive from this and be named "Szer_<sertype><mode>", where "sertype" is the type being serialized and "mode" the way the serializer works.
"ReturnSerType" and "ReturnDeserType" can be used for specifying return types for the serializer methods different from void (e.g., returning the total number of bytes deserialized in the deser() method).
The ser() and deser() methods throw an unimplemented error here for saving effort in writing derived serializers: they may provide just one serialization direction if they wish.
Usage:
A serializer S2 can be based on the code of another one S1 in two ways:
NOTE-1: Violation of the "unimplemented by default" rule: If S2 does not implement some of these methods but S1 does, the method will no longer be unimplemented (the one of S1 will be called). In that case you need to explicitly throw an unimplemented exception in the method of S2.
NOTE-2: Parallel hierarchies of serializers and serializables: If S1 is a base of serializers for serializable objects that derive from some object base A, and S1 is to be used polymorphically (i.e., we need to refer to szers derived from S1 as S1 * or S1 &), S1 must inherit from Serializer; also, derived serializers from S1 (intended for serializations of classes derived from A) must declare the ser() / deser() methods with parameters of type A, not of the classes derived from A, since otherwise those declarations would not be proper overridings (actually, the declared methods should have the "override" keyword to detect possible problems in this sense); finally, inside the methods, dynamic_cast<> has to be used for getting the particular class derived from A. On the other hand, if S1 is not to be used polymorphically, the inheritance from Serializer can be dropped from S1 and put instead in each derived serializer, which then can declare the methods with the particular classes derived from A, not requiring any dynamic_cast.
Definition at line 188 of file serializers.h.
Public Types | |
| using | SType = Sble |
| To access the Sble type parameter of this type. More... | |
| using | RDeserType = Sble |
| To access the ReturnDeserType type parameter of this type. More... | |
| using | RSerType = Sble |
| To access the ReturnSerType type parameter of this type. More... | |
Public Member Functions | |
| virtual ReturnSerType | ser (OutSerCh &chout, const Sble &obj) |
| Serializes an object, writing the resulting chars into CHOUT. More... | |
| virtual ReturnDeserType | deser (InSerCh &chin, Sble &obj) |
| Deserializes an object from a sequence of chars in CHIN. More... | |
| using elementa::base::Serializer< Sble, ReturnDeserType, ReturnSerType >::SType = Sble |
To access the Sble type parameter of this type.
Definition at line 193 of file serializers.h.
| using elementa::base::Serializer< Sble, ReturnDeserType, ReturnSerType >::RDeserType = Sble |
To access the ReturnDeserType type parameter of this type.
Definition at line 196 of file serializers.h.
| using elementa::base::Serializer< Sble, ReturnDeserType, ReturnSerType >::RSerType = Sble |
To access the ReturnSerType type parameter of this type.
Definition at line 199 of file serializers.h.
|
inlinevirtual |
Serializes an object, writing the resulting chars into CHOUT.
Must throw if any error during serialization, including channel errors (in that case, the channel can be left in the state it was when the error occurred).
Reimplemented in elementa::base::Szer_Char, elementa::base::Szer_IntText, elementa::base::Szer_RealText, elementa::base::Szer_NatBin< NatType >, elementa::base::Szer_StringToken, and elementa::base::Szer_String.
Definition at line 206 of file serializers.h.
References ELE_CODE_UNIMPLEMENTED.
|
inlinevirtual |
Deserializes an object from a sequence of chars in CHIN.
Must change the content of OBJ with the result. Throw if any error during serialization, including channel errors (in that case, the channel and the object are left in the state they were at the moment of the error).
Reimplemented in elementa::base::Szer_Char, elementa::base::Szer_IntText, elementa::base::Szer_RealText, elementa::base::Szer_NatBin< NatType >, elementa::base::Szer_StringToken, and elementa::base::Szer_String.
Definition at line 214 of file serializers.h.
References ELE_CODE_UNIMPLEMENTED.
| class elementa::base::Serializable |
Base class for any object that includes an implicit serializer.
Definition at line 250 of file serializers.h.
Public Member Functions | |
| virtual SerReturnType | serme (OutSerCh &chout) const |
| Method to serialize the object into CHOUT. More... | |
| virtual DeserReturnType | deserme (InSerCh &chin) |
| Method to deserialize the object from CHOUT. More... | |
|
inlinevirtual |
Method to serialize the object into CHOUT.
Note that the method must be const.
Definition at line 256 of file serializers.h.
References ELE_CODE_UNIMPLEMENTED.
Referenced by elementa::base::operator<<().
|
inlinevirtual |
Method to deserialize the object from CHOUT.
Definition at line 260 of file serializers.h.
References ELE_CODE_UNIMPLEMENTED.
Referenced by elementa::base::operator>>().
| class elementa::base::Szer_Char |
Serializer of a single char. Not very useful, but sometimes it is needed.
Definition at line 387 of file serializers.h.


Public Types | |
| using | SType = char |
| To access the Sble type parameter of this type. More... | |
| using | RDeserType = char |
| To access the ReturnDeserType type parameter of this type. More... | |
| using | RSerType = char |
| To access the ReturnSerType type parameter of this type. More... | |
Public Member Functions | |
| void | ser (OutSerCh &chout, const char &c) |
| Serializes an object, writing the resulting chars into CHOUT. More... | |
| void | deser (InSerCh &chin, char &c) |
| Deserializes an object from a sequence of chars in CHIN. More... | |
|
inherited |
To access the Sble type parameter of this type.
Definition at line 193 of file serializers.h.
|
inherited |
To access the ReturnDeserType type parameter of this type.
Definition at line 196 of file serializers.h.
|
inherited |
To access the ReturnSerType type parameter of this type.
Definition at line 199 of file serializers.h.
|
inlinevirtual |
Serializes an object, writing the resulting chars into CHOUT.
Must throw if any error during serialization, including channel errors (in that case, the channel can be left in the state it was when the error occurred).
Reimplemented from elementa::base::Serializer< char >.
Definition at line 391 of file serializers.h.
References ELE_CODE_PLACE, and elementa::base::serch_isgood().
|
inlinevirtual |
Deserializes an object from a sequence of chars in CHIN.
Must change the content of OBJ with the result. Throw if any error during serialization, including channel errors (in that case, the channel and the object are left in the state they were at the moment of the error).
Reimplemented from elementa::base::Serializer< char >.
Definition at line 394 of file serializers.h.
References elementa::base::Exc::asEXC(), ELE_CODE_PLACE, and elementa::base::serch_isexhausted().
| class elementa::base::Szer_String |
Serializer of std::string into fixed or non-fixed size.
For fixed size, the string is serialized occupying that size. For non-fixed size, the string is serialized as long as it is into the channel.
Definition at line 426 of file serializers.h.


Public Types | |
| using | SType = std::string |
| To access the Sble type parameter of this type. More... | |
| using | RDeserType = std::string |
| To access the ReturnDeserType type parameter of this type. More... | |
| using | RSerType = std::string |
| To access the ReturnSerType type parameter of this type. More... | |
Public Member Functions | |
| Szer_String (SerChSize size=0, char c=' ') | |
| Constructor both for fixed (SIZE>0) and dynamic sized (SIZE==0) strings. More... | |
| void | ser (OutSerCh &chout, const std::string &s) |
| Serialization of a string into CHOUT. More... | |
| void | deser (InSerCh &chin, std::string &s) |
| Deserializes the string from CHIN. More... | |
| SerChSize | sersize (void) const |
| Return the size for which the serializer was created. More... | |
|
inherited |
To access the Sble type parameter of this type.
Definition at line 193 of file serializers.h.
|
inherited |
To access the ReturnDeserType type parameter of this type.
Definition at line 196 of file serializers.h.
|
inherited |
To access the ReturnSerType type parameter of this type.
Definition at line 199 of file serializers.h.
|
inline |
Constructor both for fixed (SIZE>0) and dynamic sized (SIZE==0) strings.
See the meaning of SIZE and C in ser() and deser() methods.
Definition at line 432 of file serializers.h.
|
virtual |
Serialization of a string into CHOUT.
If the serializer works with fixed size, the serializations will occupy that size always (filling with C if shorter). If the serializer works with dynamic size, the serializations will occupy the size of the serialized string exactly. Only if that string is empty, an endmarker character equal to C is serialized.
Reimplemented from elementa::base::Serializer< std::string >.
Referenced by elementa::adts::graphs::Szer_GraphML< Vertex, Edge >::ser().
|
virtual |
Deserializes the string from CHIN.
If the deserializer works with fixed size, the object receiving the result will be .resize()-d to that size and then filled with exactly SIZE chars read from CHIN. If the deserializer works with dynamic size, the object receiving the result will be .clear()-ed and then each deserialized char will be .push_back() to it, until the C char is detected (it will not be included in the result).
Reimplemented from elementa::base::Serializer< std::string >.
|
inline |
Return the size for which the serializer was created.
Definition at line 455 of file serializers.h.
| class elementa::base::Szer_NatBin |
Serializer of natural numbers in fixed or non-fixed size.
This serializer encodes positive numbers or 0 in binary form, as if they were in memory.
For fixed size, the number is serialized occupying the given size, filling with bytes = 0 if needed. For non-fixed size, the number is serialized with the needed length. In that case, the deserializer is not implemented (deserialization can only be done in fixed-size mode).
Definition at line 492 of file serializers.h.


Public Types | |
| using | SType = NatType |
| To access the Sble type parameter of this type. More... | |
| using | RDeserType = NatType |
| To access the ReturnDeserType type parameter of this type. More... | |
| using | RSerType = NatType |
| To access the ReturnSerType type parameter of this type. More... | |
Public Member Functions | |
| Szer_NatBin (SerChSize size=sizeof(NatType)) | |
| Constructor for fixed size (SIZE > 0) or dynamic size (SIZE == 0). More... | |
| void | ser (OutSerCh &chout, const NatType &obj) |
| Serialization of the object into CHOUT. More... | |
| void | deser (InSerCh &chin, NatType &obj) |
| Deserializes the number from CHIN. More... | |
| SerChSize | sersize (void) const |
| Return the size for which the serializer was created. More... | |
|
inherited |
To access the Sble type parameter of this type.
Definition at line 193 of file serializers.h.
|
inherited |
To access the ReturnDeserType type parameter of this type.
Definition at line 196 of file serializers.h.
|
inherited |
To access the ReturnSerType type parameter of this type.
Definition at line 199 of file serializers.h.
|
inline |
Constructor for fixed size (SIZE > 0) or dynamic size (SIZE == 0).
Definition at line 497 of file serializers.h.
|
inline |
Return the size for which the serializer was created.
Definition at line 507 of file serializers.h.
| class elementa::base::Szer_IntText |
Serializer of integer numbers as text in fixed or non-fixed size.
This serializer encodes integers in text form.
For fixed size, the number is serialized occupying the entire size, filling with characters == ' ' if needed. For dynamic size, the number is serialized as long as it is needed. In that case, the deserializer is not implemented (deserialization can only be done in fixed-size mode).
Definition at line 540 of file serializers.h.


Public Types | |
| using | SType = LongestInt |
| To access the Sble type parameter of this type. More... | |
| using | RDeserType = LongestInt |
| To access the ReturnDeserType type parameter of this type. More... | |
| using | RSerType = LongestInt |
| To access the ReturnSerType type parameter of this type. More... | |
Public Member Functions | |
| Szer_IntText (SerChSize size=sizeof(LongestInt)) | |
| Constructor for fixed size (SIZE > 0) or dynamic size (SIZE == 0). More... | |
| void | ser (OutSerCh &chout, const LongestInt &obj) |
| Serialization of the object into CHOUT. More... | |
| void | deser (InSerCh &chin, LongestInt &obj) |
| Deserializes the object from CHIN. More... | |
| SerChSize | sersize (void) const |
| Return the size for which the serializer was created. More... | |
|
inherited |
To access the Sble type parameter of this type.
Definition at line 193 of file serializers.h.
|
inherited |
To access the ReturnDeserType type parameter of this type.
Definition at line 196 of file serializers.h.
|
inherited |
To access the ReturnSerType type parameter of this type.
Definition at line 199 of file serializers.h.
|
inline |
Constructor for fixed size (SIZE > 0) or dynamic size (SIZE == 0).
Definition at line 545 of file serializers.h.
|
virtual |
Serialization of the object into CHOUT.
Reimplemented from elementa::base::Serializer< LongestInt >.
Referenced by elementa::adts::graphs::Szer_GraphCompactText< Vertex, Edge >::ser().
|
virtual |
Deserializes the object from CHIN.
Reimplemented from elementa::base::Serializer< LongestInt >.
|
inline |
Return the size for which the serializer was created.
Definition at line 555 of file serializers.h.
| class elementa::base::Szer_RealText |
Serializer of real numbers as text in fixed or dynamic size.
This serializer encodes reals in text form.
For fixed size, the number is serialized occupying the entire size, filling with characters == ' ' if needed. For dynamic size, the number is serialized as long as it is needed. In that case, the deserializer is not implemented (deserialization can only be done in fixed-size mode).
Definition at line 582 of file serializers.h.


Public Types | |
| using | SType = LongestReal |
| To access the Sble type parameter of this type. More... | |
| using | RDeserType = LongestReal |
| To access the ReturnDeserType type parameter of this type. More... | |
| using | RSerType = LongestReal |
| To access the ReturnSerType type parameter of this type. More... | |
Public Member Functions | |
| Szer_RealText (SerChSize size=sizeof(LongestReal)) | |
| Constructor. More... | |
| void | ser (OutSerCh &chout, const LongestReal &obj) |
| Serialization of the object into CHOUT. More... | |
| void | deser (InSerCh &chin, LongestReal &obj) |
| Deserializes the object from CHIN. More... | |
| SerChSize | sersize (void) const |
| Return the size for which the serializer was created. More... | |
|
inherited |
To access the Sble type parameter of this type.
Definition at line 193 of file serializers.h.
|
inherited |
To access the ReturnDeserType type parameter of this type.
Definition at line 196 of file serializers.h.
|
inherited |
To access the ReturnSerType type parameter of this type.
Definition at line 199 of file serializers.h.
|
inline |
Constructor.
Definition at line 587 of file serializers.h.
|
virtual |
Serialization of the object into CHOUT.
Reimplemented from elementa::base::Serializer< LongestReal >.
|
virtual |
Deserializes the object from CHIN.
Reimplemented from elementa::base::Serializer< LongestReal >.
|
inline |
Return the size for which the serializer was created.
Definition at line 597 of file serializers.h.
| class elementa::base::Szer_StringToken |
Serializer of a std::string as a token (useful for lexers).
In de-serialization, this class reads the first character from the channel and then read more characters as long as they are of the same type: either separators or non-separators; when a different type of character is found, it pushes it back and ends, changing the object (a string) to the token read.
The only exception to this behavior is when the de-serializer finds the first mark of a multiline or uniline comment (if provided by the user): in that case it reads the entire comment (including enclosed marks) and return that. Comments are not dealt with if their enclosing marks are not provided.
The deserializer returns the type of the token read.
In serialization, it just calls a Szer_String with dynamic size (the one of the string) or does nothing if the string is empty. It does not check whether the string consists only of separators or non-separators or comment marks.
In both cases, this serializer is case-sensitive.
Definition at line 636 of file serializers.h.


Public Types | |
| using | SType = std::string |
| To access the Sble type parameter of this type. More... | |
| using | RDeserType = std::string |
| To access the ReturnDeserType type parameter of this type. More... | |
| using | RSerType = std::string |
| To access the ReturnSerType type parameter of this type. More... | |
Public Member Functions | |
| ELE_CLASS_ENUM (Type, kSeparators, kNonSeparators, kMultiComment, kUniComment, kNone) | |
| Types of tokens that can be de-serialized. | |
| Szer_StringToken (const std::string &multilinecomm="/* */", const std::string &unilinecomm="// \n", const std::string &seps="\n\t \f\r\v\x00") | |
| Constructor associating the szer to some separators. More... | |
| void | ser (OutSerCh &chout, const std::string &obj) |
| Serialization of the object into CHOUT. More... | |
| int | deser (InSerCh &chin, std::string &obj) |
| Deserializes the next token from CHIN. More... | |
|
inherited |
To access the Sble type parameter of this type.
Definition at line 193 of file serializers.h.
|
inherited |
To access the ReturnDeserType type parameter of this type.
Definition at line 196 of file serializers.h.
|
inherited |
To access the ReturnSerType type parameter of this type.
Definition at line 199 of file serializers.h.
| elementa::base::Szer_StringToken::Szer_StringToken | ( | const std::string & | multilinecomm = "/* */", |
| const std::string & | unilinecomm = "// \n", |
||
| const std::string & | seps = "\n\t \f\r\v\x00" |
||
| ) |
Constructor associating the szer to some separators.
The enclosing marks of a multiline comment are in MULTILINECOMM, separated by a single space -that cannot be part of any enclosing mark-. The same occurs with UNILINECOMM for uniline comments. If any of these parameters is missing or empty, that type of comment is not dealt with.
|
virtual |
Serialization of the object into CHOUT.
Reimplemented from elementa::base::Serializer< std::string, int >.
|
virtual |
Deserializes the next token from CHIN.
Return its type (see enum above) cast into int. Clear OBJ before deserializing.
Reimplemented from elementa::base::Serializer< std::string, int >.
| using elementa::base::ClassSzer = typedef ClassSzer_<Sble,DeserReturnType,SerReturnType> |
#include <elementa/base/serial_channels/serializers.h>
Objects of this class act as Serializer's for Serializable objects.
An object of this class calls the implicit serialization of a class SBLE that is derived from Serializable through an interface compatible with the one of Serializer.
Notice that, if the serializable class makes some initialization of the serialization process in its constructor, the initialized data that is used here is the one of the OBJ argument.
Definition at line 369 of file serializers.h.
| OutSerCh & elementa::base::operator<< | ( | OutSerCh & | chout, |
| const Serializable< DeserReturnType, SerReturnType > & | o | ||
| ) |
#include <elementa/base/serial_channels/serializers.h>
Operator for serializing Serializable objects directly on a channel.
If the serializable class return anything in serme(), that value is discarded.
Definition at line 277 of file serializers.h.
References elementa::base::Serializable< DeserReturnType, SerReturnType >::serme().
| InSerCh & elementa::base::operator>> | ( | InSerCh & | chin, |
| Serializable< DeserReturnType, SerReturnType > & | o | ||
| ) |
#include <elementa/base/serial_channels/serializers.h>
Operator for deserializing Serializable objects directly from a channel.
If the serializable class return anything in deserme(), that value is discarded.
Definition at line 290 of file serializers.h.
References elementa::base::Serializable< DeserReturnType, SerReturnType >::deserme().
|
virtual |
#include <elementa/base/serial_channels/serializers.h>
Serialization of the object into CHOUT.
Reimplemented from elementa::base::Serializer< NatType >.
Definition at line 695 of file serializers.h.
References ELE_CODE_PLACE, and elementa::base::serch_isgood().
|
virtual |
#include <elementa/base/serial_channels/serializers.h>
Deserializes the number from CHIN.
Reimplemented from elementa::base::Serializer< NatType >.
Definition at line 722 of file serializers.h.
References elementa::base::Exc::asEXC(), ELE_CODE_PLACE, and elementa::base::serch_isexhausted().