3#include "elementa/license.inc"
4#include "elementa/checks.inc"
6#ifndef ELEMENTA_BASE_SERIALCHANNELS_CHANNELS_H
7#define ELEMENTA_BASE_SERIALCHANNELS_CHANNELS_H
90 class NullOutStreamBuf:
public std::streambuf
92 using BaseStreamBuf = std::streambuf;
104 NullOutStreamBuf sbuf_;
145 const char *
buffer(
void)
const {
return(sbuf_.buffer()); }
149 class NullOutStreamBuf:
public std::streambuf
151 using BaseStreamBuf = std::streambuf;
156 ~NullOutStreamBuf(
void);
158 SerChSize size(
void)
const {
return(size_); }
159 void reset(
void) { count_ = 0; }
160 SerChSize count(
void)
const {
return(count_); }
161 const char * buffer(
void)
const {
return(buffer_); }
171 NullOutStreamBuf sbuf_;
207 Cont &
container(
void)
noexcept {
return(sbuf_.container()); }
212 class AppContOutStreamBuf:
public std::streambuf
214 using BaseStreamBuf = std::streambuf;
218 AppContOutStreamBuf(Cont & cont):cont_{cont} {}
219 ~AppContOutStreamBuf(
void) =
default;
221 Cont & container(
void)
noexcept {
return(cont_); }
235 AppContOutStreamBuf sbuf_;
261 class ConstInStreamBuf:
public std::streambuf
263 using BaseStreamBuf = std::streambuf;
267 ConstInStreamBuf(
char c):c_{c}
279 ConstInStreamBuf sbuf_;
315 class ProcInStreamBuf:
public std::streambuf
317 using BaseStreamBuf = std::streambuf;
336 ProcInStreamBuf sbuf_;
359class InOutStreamBuf_MemoryPriv;
367 void reset(
const char * begin,
const char * end);
375 std::streamsize xsgetn(
char *s, std::streamsize n)
override;
389 void reset(
char * begin,
char * end);
397 std::streamsize xsgetn(
char *s, std::streamsize n)
override;
398 std::streamsize xsputn(
const char* s, std::streamsize n)
override;
430 template <
typename T>
439 std::string::size_type offbegin = 0,
440 std::string::size_type offend = 0);
449 template <
typename T>
451 typename std::vector<T>::size_type offbegin = 0,
452 typename std::vector<T>::size_type offend = 0);
461 template <
typename T, std::
size_t N>
463 typename std::array<T,N>::size_type offbegin = 0,
464 typename std::array<T,N>::size_type offend = 0);
479 {
if (good() && chout.good())
return(sbuf_.transfer(chout.sbuf_,count));
501 ELE_CODE_TRACE({},
"both containers case of serch_transfer (chin state={"<<
503 "}, chout state={" <<
505 "}, count = " << count);
533 template <
typename T>
540 std::string::size_type offbegin = 0,
541 std::string::size_type offend = 0);
549 template <
typename T>
551 typename std::vector<T>::size_type offbegin = 0,
552 typename std::vector<T>::size_type offend = 0);
560 template <
typename T, std::
size_t N>
562 typename std::array<T,N>::size_type offbegin = 0,
563 typename std::array<T,N>::size_type offend = 0);
577 {
if (good() && chout.good())
return(sbuf_.transfer(chout.sbuf_,count));
599 ELE_CODE_TRACE({},
"both containers case of serch_transfer (chin state={"<<
601 "}, chout state={" <<
603 "}, count = " << count);
623 begin_{
reinterpret_cast<char *
>(begin)},
624 end_{
reinterpret_cast<char *
>(begin) + n},
629 static_assert(std::is_integral<T>::value,
630 "The size type must be integral in the "
631 "SerChIO_Memory constructor");
636 typename std::vector<T>::size_type offbegin,
637 typename std::vector<T>::size_type offend):
641 if ( (v.size() > 0) && (offbegin < v.size()) &&
642 (offend < v.size()) && (offbegin + offend < v.size()) )
644 begin_ =
reinterpret_cast<char *
>(v.data() + offbegin);
645 end_ =
reinterpret_cast<char *
>(v.data() + (v.size() - offend));
651template <
typename T, std::
size_t N>
653 typename std::array<T,N>::size_type offbegin,
654 typename std::array<T,N>::size_type offend):
658 if ( (a.size() > 0) && (offbegin < a.size()) &&
659 (offend < a.size()) && (offbegin + offend < a.size()) )
661 begin_ =
reinterpret_cast<char *
>(a.data() + offbegin);
662 end_ =
reinterpret_cast<char *
>(a.data() + (a.size() - offend));
677 begin_{
reinterpret_cast<const char *
>(begin)},
678 end_{
reinterpret_cast<const char *
>(begin) + n},
683 static_assert(std::is_integral<T>::value,
684 "The size type must be integral in the "
685 "SerChI_Memory constructor");
690 typename std::vector<T>::size_type offbegin,
691 typename std::vector<T>::size_type offend):
695 if ( (v.size() > 0) && (offbegin < v.size()) &&
696 (offend < v.size()) && (offbegin + offend < v.size()) )
698 begin_ =
reinterpret_cast<const char *
>(v.data() + offbegin);
699 end_ =
reinterpret_cast<const char *
>(v.data() +
700 (v.size() - offend));
706template <
typename T, std::
size_t N>
708 typename std::array<T,N>::size_type offbegin,
709 typename std::array<T,N>::size_type offend):
713 if ( (a.size() > 0) && (offbegin < a.size()) &&
714 (offend < a.size()) && (offbegin + offend < a.size()) )
716 begin_ =
reinterpret_cast<const char *
>(a.data() + offbegin);
717 end_ =
reinterpret_cast<const char *
>(a.data() +
718 (a.size() - offend));
#define ELE_CODE_TRACE_OFF
Place this inside local scope (e.g., routine) to deactivate traces there.
#define ELE_CODE_INVARG(expl)
To throw an invalid-argument exception with an explanation.
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::iostream InOutSerCh
"Base class" that represents any input + output serial channel in Elementa.
std::streamsize SerChSize
Type of the size of any channel.
std::ostream OutSerCh
"Base class" that represents any output serial channel in Elementa.
constexpr SerChDatum kSerChEOF
Const that holds the eof value.
std::string serch_st_to_string(std::ios_base::iostate st) noexcept
Converts an io state into a string.
SerChSize transfer(SerChIO_Memory &chout, SerChSize count=0)
Specialized version of serch_transfer when both channels are of memory.
SerChO_NullCoBuf(SerChSize buffer=0)
Create a serial channel able to count and with a buffer of BUFFER chars.
const char * buffer(void) const
Return nullptr if the buffer has size 0, or the buffer otherwise.
SerChSize size(void) const
Return the buffer size defined in the constructor.
SerChIO_Memory(std::string &str, std::string::size_type offbegin=0, std::string::size_type offend=0)
Constructor for a region within a string.
Cont & container(void) noexcept
Return a reference to the container.
SerChI_Memory(const std::string &str, std::string::size_type offbegin=0, std::string::size_type offend=0)
Constructor for a region within a string.
void reset(void)
Reset the count of the channel and, if any, clear the buffer.
SerChI_Procedural(Procedure &p)
Constructor. P must outlive this channel: only a reference is taken.
SerChO_AppCont(Cont &cont)
Create a serial channel to append chars at the end of CONT.
void reset(void)
Reset the channel to its initial position and state.
SerChSize transfer(SerChIO_Memory &chout, SerChSize count=0)
Specialized version of serch_transfer when both channels are of memory.
void reset(void)
Reset the channel to its initial position and state.
std::function< SerChDatum(void) > Procedure
A procedure that generates the characters.
SerChSize count(void) const
Return the current number of chars already serialized.
Private class that needs to be here for the following channels to work.
Private class that needs to be here for the following channels to work.
Input serial channel that provides a pre-defined char value endlessly.
Input + output ser. chan. associated to a pre-allocated region of memory.
Input serial channel that provides values based on some procedure.
Input + output ser. chan. associated to a pre-allocated region of memory.
Output ser. chan. that appends to a container without pre-allocated storage.
Null output serial channel to discard anything written to it.
Null output serial channel that can count what is written and also buffer it.
SerChSize serch_transfer< SerChIO_Memory, SerChIO_Memory >(SerChIO_Memory &chin, SerChIO_Memory &chout, SerChSize count)
Full specialization of serch_transfer for both channels being Memory.
SerChIO_Memory(void *begin, T n) noexcept
Template constructor.
SerChSize serch_transfer< SerChI_Memory, SerChIO_Memory >(SerChI_Memory &chin, SerChIO_Memory &chout, SerChSize count)
Full specialization of serch_transfer for both channels being Memory.
SerChI_Memory(const void *begin, T n) noexcept
Template constructor.
SerChI_Constant serchI_dummy
A singleton, dummy input channel that always produces space characters.