42#ifndef _DWMSTREAMIO_HH_
43#define _DWMSTREAMIO_HH_
56#include <unordered_map>
57#include <unordered_set>
88 static std::istream &
Read(std::istream & is,
char & c);
93 static std::ostream &
Write(std::ostream & os,
char c);
98 static std::istream &
Read(std::istream & is, uint8_t & c);
103 static std::ostream &
Write(std::ostream & os, uint8_t c);
108 static std::istream &
Read(std::istream & is,
bool & b);
113 static std::ostream &
Write(std::ostream & os,
bool b);
119 static std::istream &
Read(std::istream & is, int16_t & val);
125 static std::ostream &
Write(std::ostream & os, int16_t val);
131 static std::istream &
Read(std::istream & is, uint16_t & val);
137 static std::ostream &
Write(std::ostream & os, uint16_t val);
143 static std::istream &
Read(std::istream & is, int32_t & val);
149 static std::ostream &
Write(std::ostream & os, int32_t val);
155 static std::istream &
Read(std::istream & is, uint32_t & val);
161 static std::ostream &
Write(std::ostream & os, uint32_t val);
167 static std::istream &
Read(std::istream & is, int64_t & val);
173 static std::ostream &
Write(std::ostream & os,
const int64_t & val);
179 static std::istream &
Read(std::istream & is, uint64_t & val);
185 static std::ostream &
Write(std::ostream & os,
const uint64_t & val);
191 static std::istream &
Read(std::istream & is,
float & val);
197 static std::ostream &
Write(std::ostream & os,
float val);
203 static std::istream &
Read(std::istream & is,
double & val);
209 static std::ostream &
Write(std::ostream & os,
const double & val);
216 static std::istream &
Read(std::istream & is, std::string & s);
225 static std::ostream &
Write(std::ostream & os,
const std::string & s);
232 return(val.
Read(is));
240 {
return t.Read(is); }
247 return(val.
Write(os));
254 static std::ostream &
256 {
return t.Write(os); }
261 template <
typename _firstT,
typename _secondT>
262 static std::istream &
Read(std::istream & is,
263 std::pair<_firstT, _secondT> & p)
266 if (
Read(is, p.first))
275 template <
typename _firstT,
typename _secondT>
276 static std::ostream &
Write(std::ostream & os,
277 const std::pair<_firstT,_secondT> & p)
280 if (
Write(os, p.first)) {
290 template <
typename _keyT,
typename _valueT,
291 typename _Compare,
typename _Alloc>
292 static std::istream &
Read(std::istream & is,
293 std::map<_keyT, _valueT, _Compare, _Alloc> & m)
295 return(PairAssocContRead<std::map<_keyT, _valueT, _Compare, _Alloc> >(is, m));
301 template <
typename _keyT,
typename _valueT,
302 typename _Compare,
typename _Alloc>
303 static std::ostream &
Write(std::ostream & os,
304 const std::map<_keyT,_valueT, _Compare, _Alloc> & m)
306 return(ContainerWrite<std::map<_keyT,_valueT,_Compare,_Alloc> >(os, m));
313 template <
typename _keyT,
typename _valueT,
314 typename _Compare,
typename _Alloc>
315 static std::istream &
316 Read(std::istream & is, std::multimap<_keyT,_valueT,_Compare,_Alloc> & m)
318 return(PairAssocContRead<std::multimap<_keyT,_valueT,_Compare,_Alloc> >(is, m));
324 template <
typename _keyT,
typename _valueT,
325 typename _Compare,
typename _Alloc>
326 static std::ostream &
328 const std::multimap<_keyT,_valueT, _Compare, _Alloc> & m)
330 return(ContainerWrite<std::multimap<_keyT,_valueT,_Compare,_Alloc> >(os, m));
336 template <
typename _valueT,
size_t N>
337 static std::istream &
Read(std::istream & is,
338 std::array<_valueT, N> & a)
341 for (
size_t i = 0; i < N; ++i) {
342 if (!
Read(is, a[i])) {
353 template <
typename _valueT,
size_t N>
354 static std::ostream &
Write(std::ostream & os,
355 const std::array<_valueT, N> & a)
358 for (
size_t i = 0; i < N; ++i) {
359 if (!
Write(os, a[i])) {
370 template <
typename _valueT,
typename _Alloc>
371 static std::istream &
Read(std::istream & is,
372 std::vector<_valueT, _Alloc> & v)
374 return(ContainerRead<std::vector<_valueT, _Alloc> >(is, v));
380 template <
typename _valueT,
typename _Alloc>
381 static std::ostream &
Write(std::ostream & os,
382 const std::vector<_valueT, _Alloc> & v)
384 return(ContainerWrite<std::vector<_valueT, _Alloc> >(os, v));
390 template <
typename _valueT,
typename _Alloc>
391 static std::istream &
Read(std::istream & is,
392 std::deque<_valueT, _Alloc> & d)
394 return(ContainerRead<std::deque<_valueT, _Alloc> >(is, d));
400 template <
typename _valueT,
typename _Alloc>
401 static std::ostream &
Write(std::ostream & os,
402 const std::deque<_valueT, _Alloc> & d)
404 return(ContainerWrite<std::deque<_valueT, _Alloc> >(os, d));
410 template <
typename _valueT,
typename _Alloc>
411 static std::istream &
Read(std::istream & is,
412 std::list<_valueT, _Alloc> & l)
414 return(ContainerRead<std::list<_valueT, _Alloc> >(is, l));
420 template <
typename _valueT,
typename _Alloc>
421 static std::ostream &
Write(std::ostream & os,
422 const std::list<_valueT, _Alloc> & l)
424 return(ContainerWrite<std::list<_valueT, _Alloc> >(os, l));
430 template <
typename _valueT,
typename _Compare,
typename _Alloc>
431 static std::istream &
Read(std::istream & is,
432 std::set<_valueT, _Compare, _Alloc> & l)
434 return(ContainerRead<std::set<_valueT, _Compare, _Alloc> >(is, l));
440 template <
typename _valueT,
typename _Compare,
typename _Alloc>
441 static std::ostream &
Write(std::ostream & os,
442 const std::set<_valueT, _Compare, _Alloc> & l)
444 return(ContainerWrite<std::set<_valueT, _Compare, _Alloc> >(os, l));
450 template <
typename _valueT,
typename _Compare,
typename _Alloc>
451 static std::istream &
Read(std::istream & is,
452 std::multiset<_valueT, _Compare, _Alloc> & l)
454 return(ContainerRead<std::multiset<_valueT, _Compare, _Alloc> >(is, l));
460 template <
typename _valueT,
typename _Compare,
typename _Alloc>
461 static std::ostream &
463 const std::multiset<_valueT, _Compare, _Alloc> & l)
465 return(ContainerWrite<std::multiset<_valueT, _Compare, _Alloc> >(os, l));
471 template <
typename... Args>
472 static std::istream &
Read(std::istream & is,
473 std::tuple<Args...> & t)
475 std::apply([&is](
auto&&... args) {((
Read(is,args)) && ...);}, t);
482 template <
typename... Args>
483 static std::ostream &
Write(std::ostream & os,
484 const std::tuple<Args...> & t)
486 std::apply([&os](
auto&&... args) {((
Write(os,args)) && ...);}, t);
494 template <
typename _keyT,
typename _valueT,
495 typename _Hash,
typename _Pred,
typename _Alloc>
496 static std::istream &
498 std::unordered_map<_keyT, _valueT, _Hash, _Pred, _Alloc> & m)
500 return(PairAssocContRead<std::unordered_map<_keyT, _valueT, _Hash, _Pred, _Alloc> >(is, m));
507 template <
typename _keyT,
typename _valueT,
508 typename _Hash,
typename _Pred,
typename _Alloc>
509 static std::ostream &
511 const std::unordered_map<_keyT, _valueT, _Hash, _Pred, _Alloc> & m)
513 return(ContainerWrite<std::unordered_map<_keyT,_valueT,_Hash,_Pred,_Alloc> >(os, m));
520 template <
typename _keyT,
typename _valueT,
521 typename _Hash,
typename _Pred,
typename _Alloc>
522 static std::istream &
Read(std::istream & is,
523 std::unordered_multimap<_keyT, _valueT, _Hash, _Pred, _Alloc> & m)
525 return(PairAssocContRead<std::unordered_multimap<_keyT, _valueT, _Hash, _Pred, _Alloc> >(is, m));
532 template <
typename _keyT,
typename _valueT,
533 typename _Hash,
typename _Pred,
typename _Alloc>
534 static std::ostream &
536 const std::unordered_multimap<_keyT, _valueT, _Hash, _Pred, _Alloc> & m)
538 return(ContainerWrite<std::unordered_multimap<_keyT,_valueT,_Hash,_Pred,_Alloc> >(os, m));
545 template <
typename _valueT,
typename _Hash,
546 typename _Pred,
typename _Alloc>
547 static std::istream &
549 std::unordered_set<_valueT, _Hash, _Pred, _Alloc> & m)
551 return(ContainerRead<std::unordered_set<_valueT, _Hash, _Pred, _Alloc> >(is, m));
558 template <
typename _valueT,
typename _Hash,
559 typename _Pred,
typename _Alloc>
560 static std::ostream &
562 const std::unordered_set<_valueT, _Hash, _Pred, _Alloc> & m)
564 return(ContainerWrite<std::unordered_set<_valueT,_Hash,_Pred,_Alloc> >(os, m));
571 template <
typename _valueT,
typename _Hash,
572 typename _Pred,
typename _Alloc>
573 static std::istream &
575 std::unordered_multiset<_valueT, _Hash, _Pred, _Alloc> & m)
577 return(ContainerRead<std::unordered_multiset<_valueT, _Hash, _Pred, _Alloc> >(is, m));
584 template <
typename _valueT,
typename _Hash,
585 typename _Pred,
typename _Alloc>
586 static std::ostream &
588 const std::unordered_multiset<_valueT, _Hash, _Pred, _Alloc> & m)
590 return(ContainerWrite<std::unordered_multiset<_valueT,_Hash,_Pred,_Alloc> >(os, m));
596 template <
typename... Ts>
597 static std::istream &
Read(std::istream & is,
598 std::variant<Ts...> & v)
601 if (
Read(is, index)) {
602 if (index < std::variant_size_v<std::variant<Ts...>>) {
604 std::visit([&] (
auto && arg) {
Read(is, arg); }, v);
607 is.setstate(std::ios_base::failbit);
616 template <
typename... Ts>
617 static std::ostream &
Write(std::ostream & os,
618 const std::variant<Ts...> & v)
620 uint64_t index = v.index();
621 if (
Write(os, index)) {
622 std::visit([&os] (
const auto & arg) {
Write(os, arg); }, v);
633 static std::istream &
Read(std::istream & is, std::monostate & sm)
644 static std::ostream &
Write(std::ostream & os,
const std::monostate & sm)
653 template <
typename... Args>
654 static std::istream &
ReadV(std::istream & is, Args & ...args)
656 (
Read(is,args) &&...);
664 template <
typename... Args>
665 static std::ostream &
WriteV(std::ostream & os,
const Args & ...args)
667 (
Write(os,args) &&...);
675 template <
typename _inputIteratorT>
676 static std::ostream &
Write(std::ostream & os,
677 _inputIteratorT f, _inputIteratorT l)
680 for ( ; f != l; ++f) {
681 if (!
Write(os, *f)) {
693 template <
typename _containerT>
694 static std::istream & ContainerRead(std::istream & is,
701 if (
Read(is, numEntries)) {
702 for (uint64_t i = 0; i < numEntries; ++i) {
703 typename _containerT::value_type val;
706 c.insert(c.end(), std::move(val));
717 template <
typename _containerT>
718 static std::ostream & ContainerWrite(std::ostream & os,
719 const _containerT & c)
722 uint64_t numEntries = c.size();
723 if (
Write(os, numEntries)) {
739 template <
typename _containerT>
740 static std::istream &
741 PairAssocContRead(std::istream & is, _containerT & m)
747 if (
Read(is, numEntries)) {
748 for (uint64_t i = 0; i < numEntries; ++i) {
749 typename _containerT::key_type key;
751 typename _containerT::mapped_type val;
753 m.insert(
typename _containerT::value_type(std::move(key), std::move(val)));
A configure target for dealing with OS differences.
Dwm::HasStreamRead and Dwm::HasStreamWrite concepts. Dwm::StreamReadable, Dwm::StreamWritable and Dwm...
Dwm::VariantFromIndex function template.
std::variant< Ts... > VariantFromIndex(size_t index)
Returns a variant type V whose contents are set to a default constructed value of the type at index i...
Definition DwmVariantFromIndex.hh:57
This class contains a collection of static functions for reading and writing simple types,...
Definition DwmStreamIO.hh:83
static std::ostream & Write(std::ostream &os, float val)
Writes val to os, in IEEE format (see RFC 1832 and/or ANSI/IEEE Standard 754-1985).
static std::istream & ReadV(std::istream &is, Args &...args)
Reads multiple objects from an istream.
Definition DwmStreamIO.hh:654
static std::ostream & Write(std::ostream &os, const std::pair< _firstT, _secondT > &p)
Writes a pair<_firstT,_secondT> to an ostream. Returns the ostream.
Definition DwmStreamIO.hh:276
static std::ostream & Write(std::ostream &os, int16_t val)
Writes val to os, in network byte order (MSB first).
static std::ostream & Write(std::ostream &os, uint16_t val)
Writes val to os, in network byte order (MSB first).
static std::istream & Read(std::istream &is, std::set< _valueT, _Compare, _Alloc > &l)
Reads a set<_valueT> from an istream. Returns the istream.
Definition DwmStreamIO.hh:431
static std::ostream & Write(std::ostream &os, const std::deque< _valueT, _Alloc > &d)
Writes a deque<_valueT> to an ostream. Returns the ostream.
Definition DwmStreamIO.hh:401
static std::ostream & Write(std::ostream &os, uint8_t c)
Writes c to os. Returns os.
static std::ostream & WriteV(std::ostream &os, const Args &...args)
Writes multiple objects to an ostream.
Definition DwmStreamIO.hh:665
static std::istream & Read(std::istream &is, uint16_t &val)
Reads val from is, in network byte order (MSB first).
static std::ostream & Write(std::ostream &os, const std::multiset< _valueT, _Compare, _Alloc > &l)
Writes a multiset<_valueT> to an ostream. Returns the ostream.
Definition DwmStreamIO.hh:462
static std::ostream & Write(std::ostream &os, const HasStreamWrite auto &t)
Writes an object that meets the HasStreamWrite requirement to an ostream.
Definition DwmStreamIO.hh:255
static std::istream & Read(std::istream &is, std::unordered_set< _valueT, _Hash, _Pred, _Alloc > &m)
Reads an unordered_set<_valueT> from an istream.
Definition DwmStreamIO.hh:548
static std::istream & Read(std::istream &is, std::string &s)
Reads string s from is.
static std::istream & Read(std::istream &is, std::variant< Ts... > &v)
Reads a variant from an istream. Returns the istream.
Definition DwmStreamIO.hh:597
static std::ostream & Write(std::ostream &os, int32_t val)
Writes val to os, in network byte order (MSB first).
static std::istream & Read(std::istream &is, double &val)
Reads val from is, in IEEE format (see RFC 1832 and/or ANSI/IEEE Standard 754-1985).
static std::ostream & Write(std::ostream &os, const uint64_t &val)
Writes val to os, in network byte order (MSB first).
static std::istream & Read(std::istream &is, char &c)
Reads c from is. Returns is.
static std::istream & Read(std::istream &is, bool &b)
Reads b from is. Returns is.
static std::ostream & Write(std::ostream &os, const std::array< _valueT, N > &a)
Writes an array<_valueT,N> to an ostream. Returns the ostream.
Definition DwmStreamIO.hh:354
static std::istream & Read(std::istream &is, std::monostate &sm)
Just a dummy helper function for std::variant instances that hold a std::monostate.
Definition DwmStreamIO.hh:633
static std::ostream & Write(std::ostream &os, const std::unordered_multimap< _keyT, _valueT, _Hash, _Pred, _Alloc > &m)
Writes a unordered_multimap<_keyT,_valueT> to an ostream.
Definition DwmStreamIO.hh:535
static std::ostream & Write(std::ostream &os, const std::map< _keyT, _valueT, _Compare, _Alloc > &m)
Writes a map<_keyT,_valueT> to an ostream. Returns the ostream.
Definition DwmStreamIO.hh:303
static std::ostream & Write(std::ostream &os, const std::variant< Ts... > &v)
Writes a variant to an ostream. Returns the ostream.
Definition DwmStreamIO.hh:617
static std::istream & Read(std::istream &is, int32_t &val)
Reads val from is, in network byte order (MSB first).
static std::ostream & Write(std::ostream &os, const std::list< _valueT, _Alloc > &l)
Writes a list<_valueT> to an ostream. Returns the ostream.
Definition DwmStreamIO.hh:421
static std::istream & Read(std::istream &is, uint32_t &val)
Reads val from is, in network byte order (MSB first).
static std::istream & Read(std::istream &is, std::list< _valueT, _Alloc > &l)
Reads a list<_valueT> from an istream. Returns the istream.
Definition DwmStreamIO.hh:411
static std::istream & Read(std::istream &is, std::map< _keyT, _valueT, _Compare, _Alloc > &m)
Reads a map<_keyT,_valueT> from an istream. Returns the istream.
Definition DwmStreamIO.hh:292
static std::istream & Read(std::istream &is, std::tuple< Args... > &t)
Reads a tuple from an istream. Returns the istream.
Definition DwmStreamIO.hh:472
static std::istream & Read(std::istream &is, HasStreamRead auto &t)
Reads an object t that meets the HasStreamRead requirement from an istream.
Definition DwmStreamIO.hh:239
static std::ostream & Write(std::ostream &os, const std::unordered_set< _valueT, _Hash, _Pred, _Alloc > &m)
Writes a unordered_set<_valueT> to an ostream.
Definition DwmStreamIO.hh:561
static std::ostream & Write(std::ostream &os, const std::vector< _valueT, _Alloc > &v)
Writes a vector<_valueT> to an ostream. Returns the ostream.
Definition DwmStreamIO.hh:381
static std::istream & Read(std::istream &is, std::pair< _firstT, _secondT > &p)
Reads a pair<_firstT,_secondT> from an istream. Returns the istream.
Definition DwmStreamIO.hh:262
static std::istream & Read(std::istream &is, std::multimap< _keyT, _valueT, _Compare, _Alloc > &m)
Reads a multimap<_keyT,_valueT> from an istream.
Definition DwmStreamIO.hh:316
static std::ostream & Write(std::ostream &os, const std::monostate &sm)
Just a dummy helper function for std::variant instances that hold a std::monostate.
Definition DwmStreamIO.hh:644
static std::ostream & Write(std::ostream &os, const StreamWritable &val)
Wrapper function to write a StreamWritable object to an ostream.
Definition DwmStreamIO.hh:245
static std::ostream & Write(std::ostream &os, bool b)
Writes a bool b to an ostream os. Returns os.
static std::istream & Read(std::istream &is, std::unordered_map< _keyT, _valueT, _Hash, _Pred, _Alloc > &m)
Reads an unordered_map<_keyT,_valueT> from an istream.
Definition DwmStreamIO.hh:497
static std::istream & Read(std::istream &is, int16_t &val)
Reads val from is, in network byte order (MSB first).
static std::ostream & Write(std::ostream &os, const std::unordered_multiset< _valueT, _Hash, _Pred, _Alloc > &m)
Writes a unordered_multiset<_valueT> to an ostream.
Definition DwmStreamIO.hh:587
static std::ostream & Write(std::ostream &os, char c)
Writes c to os. Returns os.
static std::ostream & Write(std::ostream &os, const std::string &s)
Writes s to os.
static std::istream & Read(std::istream &is, std::deque< _valueT, _Alloc > &d)
Reads a deque<_valueT> from an istream. Returns the istream.
Definition DwmStreamIO.hh:391
static std::istream & Read(std::istream &is, uint8_t &c)
Reads c from is. Returns is.
static std::istream & Read(std::istream &is, std::unordered_multiset< _valueT, _Hash, _Pred, _Alloc > &m)
Reads an unordered_multiset<_valueT> from an istream.
Definition DwmStreamIO.hh:574
static std::ostream & Write(std::ostream &os, const std::multimap< _keyT, _valueT, _Compare, _Alloc > &m)
Writes a multimap<_keyT,_valueT> to an ostream. Returns the ostream.
Definition DwmStreamIO.hh:327
static std::istream & Read(std::istream &is, float &val)
Reads val from is, in IEEE format (see RFC 1832 and/or ANSI/IEEE Standard 754-1985).
static std::ostream & Write(std::ostream &os, const std::unordered_map< _keyT, _valueT, _Hash, _Pred, _Alloc > &m)
Writes a unordered_map<_keyT,_valueT> to an ostream.
Definition DwmStreamIO.hh:510
static std::istream & Read(std::istream &is, std::multiset< _valueT, _Compare, _Alloc > &l)
Reads a multiset<_valueT> from an istream. Returns the istream.
Definition DwmStreamIO.hh:451
static std::istream & Read(std::istream &is, int64_t &val)
Reads val from is, in network byte order (MSB first).
static std::ostream & Write(std::ostream &os, const int64_t &val)
Writes val to os, in network byte order (MSB first).
static std::istream & Read(std::istream &is, std::vector< _valueT, _Alloc > &v)
Reads a vector<_valueT> from an istream. Returns the istream.
Definition DwmStreamIO.hh:371
static std::ostream & Write(std::ostream &os, const std::set< _valueT, _Compare, _Alloc > &l)
Writes a set<_valueT> to an ostream. Returns the ostream.
Definition DwmStreamIO.hh:441
static std::istream & Read(std::istream &is, std::unordered_multimap< _keyT, _valueT, _Hash, _Pred, _Alloc > &m)
Reads an unordered_multimap<_keyT,_valueT> from an istream.
Definition DwmStreamIO.hh:522
static std::istream & Read(std::istream &is, std::array< _valueT, N > &a)
Reads an array<_valueT,N> from an istream. Returns the istream.
Definition DwmStreamIO.hh:337
static std::ostream & Write(std::ostream &os, uint32_t val)
Writes val to os, in network byte order (MSB first).
static std::ostream & Write(std::ostream &os, const double &val)
Writes val to os, in IEEE format (see RFC 1832 and/or ANSI/IEEE Standard 754-1985).
static std::istream & Read(std::istream &is, StreamReadable &val)
Wrapper function to read a StreamReadable object from an istream.
Definition DwmStreamIO.hh:230
static std::ostream & Write(std::ostream &os, const std::tuple< Args... > &t)
Writes a tuple to an ostream. Returns the ostream.
Definition DwmStreamIO.hh:483
static std::istream & Read(std::istream &is, uint64_t &val)
Reads val from is, in network byte order (MSB first).
This class defines an interface for classes that can read their contents from an istream.
Definition DwmStreamIOCapable.hh:74
virtual std::istream & Read(std::istream &is)=0
Read from an istream. Return the istream.
This class defines an interface for classes that can write their contents to an ostream.
Definition DwmStreamIOCapable.hh:92
virtual std::ostream & Write(std::ostream &os) const =0
Write to an ostream. Return the ostream.
T has a Read(std::istream &) method that returns the std::istream reference that was passed in as its...
Definition DwmStreamIOCapable.hh:56
T has a Write(std::ostream &) const method that returns the std::ostream reference that was passed in...
Definition DwmStreamIOCapable.hh:65