3#include "elementa/license.inc"
4#include "elementa/checks.inc"
6#ifndef ELEMENTA_BASE_SERIALCHANNELS_FILTERS_H
7#define ELEMENTA_BASE_SERIALCHANNELS_FILTERS_H
134 sbuf_{*
this,inch_,filter_},
140 filter_{oth.filter_},
141 sbuf_{*this,inch_,filter_},
149 F &
filter(
void)
noexcept {
return(filter_); }
154 class StreambufSerChIFilter final:
public std::streambuf
158 StreambufSerChIFilter(
InSerCh & wrappingch,
160 F &
filter): wrappingchin_{wrappingch},
166 ~StreambufSerChIFilter(
void) =
default;
168 StreambufSerChIFilter(
const StreambufSerChIFilter &) =
delete;
169 StreambufSerChIFilter & operator=(
const StreambufSerChIFilter &)=
delete;
170 StreambufSerChIFilter(StreambufSerChIFilter &&) =
delete;
171 StreambufSerChIFilter & operator=(StreambufSerChIFilter &&) =
delete;
190 StreambufSerChIFilter sbuf_;
192 static_assert(std::is_copy_constructible<F>::value,
193 "The filter template argument in SerChIFilter must be"
194 " copy-constructible");
222 filter_{oth.filter_},
223 sbuf_{*this,chout_,filter_},
231 F &
filter(
void)
noexcept {
return(filter_); }
236 class StreambufSerChOFilter final:
public std::streambuf
240 StreambufSerChOFilter(
OutSerCh & wrappingch,
242 F &
filter): wrappingchout_{wrappingch},
249 ~StreambufSerChOFilter(
void) =
default;
251 StreambufSerChOFilter(
const StreambufSerChOFilter &) =
delete;
252 StreambufSerChOFilter & operator=(
const StreambufSerChOFilter &)=
delete;
253 StreambufSerChOFilter(StreambufSerChOFilter &&) =
delete;
254 StreambufSerChOFilter & operator=(StreambufSerChOFilter &&) =
delete;
268 StreambufSerChOFilter sbuf_;
270 static_assert(std::is_copy_constructible<F>::value,
271 "The filter template argument in SerChOFilter must be"
272 " copy-constructible");
281template <
class FI,
class FO>
290 const FI & filterin = FI{},
291 const FO & filterout = FO{}): chinout_{chinout},
293 filterout_{filterout},
294 sbuf_{*
this,chinout_,
295 filterin_,filterout_},
301 filterin_{oth.filterin_},
302 filterout_{oth.filterout_},
303 sbuf_{*this,chinout_,
304 filterin_,filterout_},
312 FI &
filterIn(
void)
noexcept {
return(filterin_); }
320 class StreambufSerChIOFilter final:
public std::streambuf
324 StreambufSerChIOFilter(
InOutSerCh & wrappingch,
327 FO & filterout): wrappingchinout_{wrappingch},
330 filterout_{filterout}
334 ~StreambufSerChIOFilter(
void) =
default;
336 StreambufSerChIOFilter(
const StreambufSerChIOFilter &) =
delete;
337 StreambufSerChIOFilter & operator=(
const StreambufSerChIOFilter &)
339 StreambufSerChIOFilter(StreambufSerChIOFilter &&) =
delete;
340 StreambufSerChIOFilter & operator=(StreambufSerChIOFilter &&) =
delete;
365 StreambufSerChIOFilter sbuf_;
367 static_assert(std::is_copy_constructible<FI>::value,
368 "The filter input template argument in SerChIOFilter must be"
369 " copy-constructible");
370 static_assert(std::is_copy_constructible<FO>::value,
371 "The filter output template argument in SerChIOFilter must be"
372 " copy-constructible");
393template <
class FI,
class FO>
395 const FI & f1,
const FO & f2)
452 {
return(
dynamic_cast<L &
>(*fch.
filter().ploc)); }
457 {
return(
dynamic_cast<L &
>(*fch.
filter().ploc)); }
567 ELE_CODE_TRACE({},
"State of wrapping: " <<
569 ", state of wrapped: " <<
574 else res = filter_(chin_,SerChBufOp::kUFlow,chin_.get());
577 ELE_CODE_TRACE({},
".... State of wrapping: " <<
579 ", state of wrapped: " <<
590 ELE_CODE_TRACE({},
"State of wrapping: " <<
592 ", state of wrapped: " <<
597 else res = filter_(chin_,SerChBufOp::kUnderflow,chin_.peek());
600 ELE_CODE_TRACE({},
".... State of wrapping: " <<
602 ", state of wrapped: " <<
613 ELE_CODE_TRACE({},
"State of wrapping: " <<
615 ", state of wrapped: " <<
622 if (c ==
kSerChEOF) res = chin_.unget().peek();
629 else res = filter_(chin_,SerChBufOp::kPBackFail,res);
633 ELE_CODE_TRACE({},
".... State of wrapping: " <<
635 ", state of wrapped: " <<
649 chout_.put(filter_(chout_,SerChBufOp::kOverflow,c));
#define ELE_CODE_TRACE_OFF
Place this inside local scope (e.g., routine) to deactivate traces there.
#define ELE_CLASS_ENUM(NameEnumClass,...)
Define an extended enum type.
std::shared_ptr< SerChLoc > Ptr
Safe pointer for polymorphic behaviours.
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.
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.
std::ostream OutSerCh
"Base class" that represents any output serial channel in Elementa.
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.
std::string serch_st_to_string(std::ios_base::iostate st) noexcept
Converts an io state into a string.
SerChOFilter(SerChOFilter &&oth)
Move constructor from another with same filters. Share same channel.
static L & get_loc(SerChIFilter< SerChFilt_Loc > &fch)
Get the downcast location object of class L from the filtered channel.
F & filter(void) noexcept
Getter for the internal filter function.
OutSerCh & channel(void) noexcept
Getter for the referenced channel.
OutSerCh & channel(void) noexcept
Getter for the referenced channel.
SerChIFilter(SerChIFilter &&oth)
Move constructor from another with same filters. Share same channel.
SerChFilt_Loc(const SerChLoc &loc=SerChLocText{})
Default constructor. By default, SerChLocText is used for locations.
SerChOFilter(OutSerCh &chout, const F &filter=F{})
Constructor.
static SerChOFilter< SerChFilt_Loc > make_filtch(OutSerCh &osch)
Return a SerChOFilter that uses a new location object of class L.
FO & filterOut(void) noexcept
Getter for the internal output filter function.
SerChDatum operator()(RootSerCh &, SerChBufOp op, SerChDatum c)
Each time this method is called, the predicate function is called.
FI & filterIn(void) noexcept
Getter for the internal input filter function.
InSerCh & channel(void) noexcept
Getter for the referenced channel.
static L & get_loc(SerChOFilter< SerChFilt_Loc > &fch)
Get the downcast location object of class L from the filtered channel.
SerChFilt_Divert(OutSerCh &div)
Constructor.
SerChFilt_Loc(const SerChLoc::Ptr &pl)
Constructor from an already existing location object.
SerChLoc::Ptr ploc
Location object, updated by the () operator.
SerChIFilter(InSerCh &inch, const F &filter=F{})
Constructor.
F & filter(void) noexcept
Getter for the internal filter function.
SerChIOFilter(SerChIOFilter &&oth)
Move constructor from another with same filters. Share same channel.
SerChIOFilter(InOutSerCh &chinout, const FI &filterin=FI{}, const FO &filterout=FO{})
Constructor
std::function< InSerCh &(void) > SelPred
Prototype of the predicate used for selecting a channel from the list.
SerChFilt_Demux(SelPred pred)
Constructor: it admits the selection predicate.
static SerChIFilter< SerChFilt_Loc > make_filtch(InSerCh &isch)
Return a SerChIFilter that uses a new location object of class L.
A filter that serves data from multiple input channels.
A filter that sends a copy of the readings of an InSerCh to another channel.
A filter to keep a location on a channel.
A filter for InSerCh. It filters their inputs after getting or putbacking'em.
A double filter for input/output channels.
A filter for output channels. It processes their outputs before writing them.
SerChIOFilter< FI, FO > make_schfiltIO(InOutSerCh &sch, const FI &f1, const FO &f2)
Creator of a SerChIOFilter that deduces automatically the type of the filter.
SerChDatum serch_filtered_overflow(OutSerCh &wrappingchout_, OutSerCh &chout_, F &filter_, SerChDatum c)
Intended to implement overflow() in output/inout chnnels filtered by FILTER_.
SerChIFilter< F > make_schfiltI(InSerCh &sch, const F &f)
Creator of a SerChIFilter that deduces automatically the type of the filter.
std::function< SerChDatum(RootSerCh &, SerChBufOp, SerChDatum) > serchfilt_null
Change nothing in the flow of the channel.
SerChDatum serch_filtered_uflow(InSerCh &wrappingchin_, InSerCh &chin_, F &filter_)
Intended to implement uflow() in input or inout channels filtered by FILTER_.
SerChDatum serch_filtered_pbackfail(InSerCh &wrappingchin_, InSerCh &chin_, F &filter_, SerChDatum c)
Intended to implement pbackfail() in input/inout chnnels filtered by FILTER_.
std::function< SerChDatum(RootSerCh &, SerChBufOp, SerChDatum) > serchfilt_tolower
Change to lowercase everything flowing through the channel.
SerChDatum serch_filtered_underflow(InSerCh &wrappingchin_, InSerCh &chin_, F &filter_)
Intended to implement underflow() in input/inout chnnels filtered by FILTER_.
SerChOFilter< F > make_schfiltO(OutSerCh &sch, const F &f)
Creator of a SerChOFilter that deduces automatically the type of the filter.
std::function< SerChDatum(RootSerCh &, SerChBufOp, SerChDatum) > serchfilt_toupper
Change to uppercase everything flowing through the channel.