3#include "elementa/license.inc"
4#include "elementa/checks.inc"
6#ifndef ELEMENTA_BASE_SERIAL_CHANNELS_SERIALIZERS_H
7#define ELEMENTA_BASE_SERIAL_CHANNELS_SERIALIZERS_H
14#include "elementa/base/serial_channels.h"
186 class ReturnDeserType = void,
187 class ReturnSerType =
void>
248template <
class DeserReturnType = void,
249 class SerReturnType =
void>
275template <
class DeserReturnType,
288template <
class DeserReturnType,
305template <
class Sble,
class DeserReturnType,
class SerReturnType>
306class ClassSzer_ final:
public Serializer<Sble,DeserReturnType,SerReturnType>
310 SerReturnType ser(
OutSerCh & chout,
const Sble & obj)
311 {
return(obj.serme(chout)); }
313 DeserReturnType deser(
InSerCh & chin, Sble & obj)
314 {
return(obj.deserme(chin)); }
319template <
class Sble,
class SerReturnType>
320class ClassSzer_<Sble,void,SerReturnType> final:
321 public Serializer<Sble,void,SerReturnType>
325 SerReturnType
ser(
OutSerCh & chout,
const Sble & obj)
326 {
return(obj.serme(chout)); }
329 { obj.deserme(chin); }
333template <
class Sble,
class DeserReturnType>
334class ClassSzer_<Sble,DeserReturnType,void> final:
335 public Serializer<Sble,DeserReturnType,void>
340 { obj.serme(chout); }
343 {
return(obj.deserme(chin)); }
348class ClassSzer_<Sble,void,void> final:
public Serializer<Sble,void,void>
353 { obj.serme(chout); }
356 { obj.deserme(chin); }
368template <
class Sble,
class DeserReturnType =
void,
class SerReturnType =
void>
369using ClassSzer = ClassSzer_<Sble,DeserReturnType,SerReturnType>;
491template <
typename NatType>
515 static_assert(std::is_integral<NatType>::value &&
516 !std::is_signed<NatType>::value,
517 "Szer_NatBin instantiated with a non-integral or signed type");
655 const std::string & unilinecomm =
"// \n",
656 const std::string & seps =
"\n\t \f\r\v\x00");
668 std::string separators_;
669 std::string multilinebeg_;
670 std::string multilineend_;
671 std::string unilinebeg_;
672 std::string unilineend_;
674 static void split_marks(
const std::string & ms,
675 std::string & beg, std::string & end);
677 Type kind_of_begin(
char c,
int prio = 0);
694template <
typename NatType>
702 chout.put(rem & 0xff);
707 throw(
SzerError{
"Serializing too long natural"}.
714 chout.put(rem & 0xff);
721template <
typename NatType>
733 obj |=
static_cast<NatType
>(
static_cast<uint8_t
>(c)) << (8*f);
736 else throw(
SzerError{
"Deserializing natural in non-fixed size mode"}.
#define ELE_CODE_PLACE
Produces a std::string with the place of source where the macro is placed.
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.
const char * explanation(void) const noexcept
Return the explanation only. It will live as long as this exception.
Base class for all errors / exceptions in Elementa. Just derive from it.
#define ELE_CLASS_EXCOVERRIDE(C)
Shortening macro that must be used inside classes derived from Exc.
#define ELE_CODE_UNIMPLEMENTED
To throw an unimplemented exception.
long long LongestInt
Longest signed integral type in the target machine.
long double LongestReal
Longest real type.
Error to throw when the channel is exhausted but it should not be.
std::istream InSerCh
"Base class" that represents any input serial channel in Elementa.
bool serch_isexhausted(InSerCh &serch, const std::string &place="", const RTTextWithEnum::Combination &flags={RTTextWith::kAll_})
Return TRUE if it is eof or exhausted; then throw if the channel is bad/fail.
std::streamsize SerChSize
Type of the size of any channel.
std::ostream OutSerCh
"Base class" that represents any output serial channel in Elementa.
void serch_isgood(RootSerCh &serchconst, const std::string &place="", const RTTextWithEnum::Combination &flags={RTTextWith::kAll_})
Throw an exception if the channel is not good; do nothing otherwise.
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.
void ser(OutSerCh &chout, const LongestInt &obj)
Serialization of the object into CHOUT.
int deser(InSerCh &chin, std::string &obj)
Deserializes the next token from CHIN.
Sble RDeserType
To access the ReturnDeserType type parameter of this type.
void deser(InSerCh &chin, LongestReal &obj)
Deserializes the object from CHIN.
Szer_NatBin(SerChSize size=sizeof(NatType))
Constructor for fixed size (SIZE > 0) or dynamic size (SIZE == 0).
void ser(OutSerCh &chout, const char &c)
Serializes an object, writing the resulting chars into CHOUT.
Szer_String(SerChSize size=0, char c=' ')
Constructor both for fixed (SIZE>0) and dynamic sized (SIZE==0) strings.
void deser(InSerCh &chin, char &c)
Deserializes an object from a sequence of chars in CHIN.
virtual DeserReturnType deserme(InSerCh &chin)
Method to deserialize the object from CHOUT.
void ser(OutSerCh &chout, const std::string &s)
Serialization of a string into CHOUT.
SerChSize sersize(void) const
Return the size for which the serializer was created.
Sble RSerType
To access the ReturnSerType type parameter of this type.
virtual ReturnSerType ser(OutSerCh &chout, const Sble &obj)
Serializes an object, writing the resulting chars into CHOUT.
void deser(InSerCh &chin, std::string &s)
Deserializes the string from CHIN.
Szer_IntText(SerChSize size=sizeof(LongestInt))
Constructor for fixed size (SIZE > 0) or dynamic size (SIZE == 0).
Sble SType
To access the Sble type parameter of this type.
Szer_RealText(SerChSize size=sizeof(LongestReal))
Constructor.
SerChSize sersize(void) const
Return the size for which the serializer was created.
SerChSize sersize(void) const
Return the size for which the serializer was created.
void deser(InSerCh &chin, LongestInt &obj)
Deserializes the object from CHIN.
virtual ReturnDeserType deser(InSerCh &chin, Sble &obj)
Deserializes an object from a sequence of chars in CHIN.
void ser(OutSerCh &chout, const LongestReal &obj)
Serialization of the object into CHOUT.
virtual SerReturnType serme(OutSerCh &chout) const
Method to serialize the object into CHOUT.
SerChSize sersize(void) const
Return the size for which the serializer was created.
void ser(OutSerCh &chout, const std::string &obj)
Serialization of the object into CHOUT.
Base class for any object that includes an implicit serializer.
Base abstract class / interface for any serializer.
Serializer of a single char. Not very useful, but sometimes it is needed.
Serializer of integer numbers as text in fixed or non-fixed size.
Serializer of natural numbers in fixed or non-fixed size.
Serializer of real numbers as text in fixed or dynamic size.
Serializer of std::string into fixed or non-fixed size.
Serializer of a std::string as a token (useful for lexers).
Base class for all errors thrown by serializers.
InSerCh & operator>>(InSerCh &chin, Serializable< DeserReturnType, SerReturnType > &o)
Operator for deserializing Serializable objects directly from a channel.
OutSerCh & operator<<(OutSerCh &chout, const Serializable< DeserReturnType, SerReturnType > &o)
Operator for serializing Serializable objects directly on a channel.
void ser(OutSerCh &chout, const NatType &obj)
Serialization of the object into CHOUT.
ClassSzer_< Sble, DeserReturnType, SerReturnType > ClassSzer
Objects of this class act as Serializer's for Serializable objects.
void deser(InSerCh &chin, NatType &obj)
Deserializes the number from CHIN.