3#include "elementa/license.inc"
4#include "elementa/checks.inc"
6#ifndef ELEMENTA_BASE_SERIALCHANNELS_BASICS_H
7#define ELEMENTA_BASE_SERIALCHANNELS_BASICS_H
379 static constexpr bool value =
380 ( std::is_base_of<InSerCh,T>::value &&
381 std::is_same<typename T::char_type,char>::value
392 static constexpr bool value =
393 ( ( std::is_base_of<InSerCh,T>::value ||
394 std::is_base_of<InOutSerCh,T>::value ) &&
395 std::is_same<typename T::char_type,char>::value
406 static constexpr bool value =
407 ( std::is_base_of<OutSerCh,T>::value &&
408 std::is_same<typename T::char_type,char>::value
419 static constexpr bool value =
420 ( ( std::is_base_of<OutSerCh,T>::value ||
421 std::is_base_of<InOutSerCh,T>::value ) &&
422 std::is_same<typename T::char_type,char>::value
433 static constexpr bool value =
434 ( std::is_base_of<InOutSerCh,T>::value &&
435 std::is_same<typename T::char_type,char>::value
470 const std::string & place =
"",
471 const RTTextWithEnum::Combination & flags
472 = {RTTextWith::kAll_});
477 const std::string & place =
"",
478 const RTTextWithEnum::Combination & flags
479 = {RTTextWith::kAll_});
485 const std::string & place =
"",
486 const RTTextWithEnum::Combination & flags
487 = {RTTextWith::kAll_});
491 const std::string & place =
"",
492 const RTTextWithEnum::Combination & flags
493 = {RTTextWith::kAll_});
497 const std::string & place =
"",
498 const RTTextWithEnum::Combination & flags
499 = {RTTextWith::kAll_});
542 Exc{std::string{
"Serial channel error: "} +
571 "Channel provided less data than expected",
585 "Too many data in channel",
677template <
class SCOrg,
class SCDest>
682 "Cannot instantiate general serch_transfer with non-channel");
686 ELE_CODE_TRACE({},
"general case of serch_transfer (chin state={" <<
688 "}, chout state={" <<
690 "}, count = " << count);
692 if ( (chin.good()) && (chout.good()) )
714 finish = (count == 0 ? false : (res >= count));
737template <
class SCOrg,
class SCDest>
744 ELE_CODE_TRACE({},
"rvalue version of serch_transfer - redirecting");
776 using Ptr = std::shared_ptr<SerChLoc>;
793 { copyFrom(oth);
return(*
this); }
829 virtual void inc(
char c =
' ') { ++lpos_; }
834 {
if ((t==
nullptr)||(n==0))
return;
849 {
return(lpos_ == o.lpos_); }
853 {
return(!
operator==(o)); }
857 virtual std::string
to_string(
bool compact =
false) const noexcept;
873 {
if (
this != &oth) lpos_ = oth.lpos_; }
876 {
if (
this != &oth) copyFrom(oth); }
911 { copyFrom(oth);
return(*
this); }
919 { moveFrom(oth);
return(*
this); }
933 void inc(
char c)
override;
935 void dec(
char c)
override;
938 {
return((eol_ == o.eol_)&&(row_ == o.row_)&&(col_ == o.col_)); }
940 {
return(!
operator==(o)); }
942 {
return(
operator==(
dynamic_cast<const SerChLocText &
>(o))); }
944 {
return(!
operator==(o)); }
945 std::string
to_string(
bool compact =
false) const noexcept override;
958 {
if (
this != &oth) copyFrom(oth); }
960 std::string rowinfotostring(
void)
const;
Any class derived from this base class must be clonable.
#define ELE_CODE_TRACE_OFF
Place this inside local scope (e.g., routine) to deactivate traces there.
#define ELE_CODE_UNTRACE(flags,...)
Macro to use instead of an existing ELE_CODE_TRACE to deactivate that trace.
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.
LongestUnsigned to_number(const void *p)
Convert a pointer address to a number.
bool operator!=(const SerChLoc &o) const override
Unequality.
bool operator==(const SerChLoc &o) const override
Equality.
std::string to_string(bool compact=false) const noexcept override
Convert the location to a string.
void inc(char c) override
Increment the location after reading/writing char C.
virtual void dec(const char *t, SerChSize n)
Decrement the location after un-reading/un-writing a block T of N chars.
SerChLocText & operator=(const SerChLocText &oth)
Copy assignment: the stored value is cleared.
virtual void inc(char c=' ')
Increment the location after reading/writing char C.
SerChLocText & operator=(SerChLocText &&oth)
Move assignment: the stored value of OTH is moved to this.
virtual ~SerChLocText(void)
Destructor.
SerChLocText(SerChLocText &&oth)
Move constructor: the stored value of OTH is moved to this.
SerChLoc & operator=(SerChLoc &&oth)
Move assignment: the stored value of OTH is moved to this.
SerChSize column(void) const noexcept
Get current column.
virtual SerChLoc * clone(void) const
Clone the location.
void dec(char c) override
Decrement the location after un-reading/un-writing char C.
std::shared_ptr< SerChLoc > Ptr
Safe pointer for polymorphic behaviours.
SerChSize linear(void) const noexcept
Get the current location.
void inc(const char *t, SerChSize n) override
Increment the location after reading/writing a block T of N chars.
SerChLoc(void) noexcept
Default constructor: establish 0 as the current location in the channel.
virtual void inc(const char *t, SerChSize n)
Increment the location after reading/writing a block T of N chars.
SerChLoc & operator=(const SerChLoc &oth)
Copy assignment: the stored value is cleared.
SerChSize row(void) const noexcept
Get current row.
SerChLocText(const SerChLocText &oth)
Copy constructor: the stored value is cleared.
void reset(void) noexcept override
Reset the location to the beginning.
virtual void dec(char c)
Decrement the location after un-reading/un-writing char C.
virtual ~SerChLoc(void)
Destructor.
virtual std::string to_string(bool compact=false) const noexcept
Convert the location to a string.
virtual bool operator!=(const SerChLoc &o) const
Unequality.
virtual bool operator==(const SerChLoc &o) const
Equality.
virtual void reset(void) noexcept
Reset the location to the beginning.
SerChLoc(SerChLoc &&oth)
Move constructor: the stored value of OTH is moved to this.
SerChLocText(char endofline='\n') noexcept
Default constructor: location pointing to the 1st character of the text.
SerChLoc(const SerChLoc &oth)
Copy constructor: the stored value is cleared.
void dec(const char *t, SerChSize n) override
Decrement the location after un-reading/un-writing a block T of N chars.
SerChLocText * clone(void) const
Clone the location.
Errors related to container-based channels.
Error to throw when the channel is exhausted but it should not be.
Error to throw when the channel provides less data than it should on read()
Errors related to locations on channels.
Errors thrown by serch_isgood and other functions.
Error to throw when the channel does not admit too many data.
Errors thrown by channels that must be opened.
Base class for exceptions associated to some serial channel operation.
Location in a channel, at least in linear form, either at reading or writing.
A location into a text channel consisting of a row and a column.
With this template you can check whether T is an input or inout channel.
With this template you can check whether T is an in + out serial channel.
With this template you can check whether T is an input serial channel.
With this template you can check whether T is an output or inout channel.
With this template you can check whether T is an output serial channel.
With this template you can check whether T is any kind of serial channel.
std::istream InSerCh
"Base class" that represents any input serial channel in Elementa.
std::streambuf::int_type SerChDatum
Type that can hold either a char or eof.
std::ios RootSerCh
Class that is at the root of any serial channel in Elementa.
std::iostream InOutSerCh
"Base class" that represents any input + output 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.
bool serch_iseofbutok(RootSerCh &serchconst, const std::string &place="", const RTTextWithEnum::Combination &flags={RTTextWith::kAll_})
Return TRUE if the channel is eof() (maybe bad/fail); throw if !eof nor good.
SerChSize serch_transfer(SCOrg &chin, SCDest &chout, SerChSize count=0)
Write COUNT items read from channel CHIN into channel CHOUT.
std::ios_base::iostate serch_copy_st(RootSerCh &chorg, RootSerCh &chdest)
Copy the state of CHORG into DEST, returning that state.
constexpr SerChDatum kSerChEOF
Const that holds the eof value.
void serch_hasreadN(InSerCh &chin, char *dest, SerChSize n, const std::string &place="", const RTTextWithEnum::Combination &flags={RTTextWith::kAll_})
Make sure that N chars are read from CHIN; throws if any error.
std::char_traits< char > SerChCharTraits
To have access to the methods of char_traits corresponding to these channs.
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.
std::string serch_st_to_string(std::ios_base::iostate st) noexcept
Converts an io state into a string.
void serch_haswrittenN(OutSerCh &chout, const char *org, SerChSize n, const std::string &place="", const RTTextWithEnum::Combination &flags={RTTextWith::kAll_})
Make sure that N chars are written to CHOUT; throws if any error.
std::string concatWithMiddle(const std::string &s1, const std::string &s2, const std::string &m=". ")
Concatenate two strings putting a middle one only if the second is not empty.