47 #include <sys/types.h>
59#include <unordered_map>
60#include <unordered_set>
91 static int Read(gzFile gzf,
char & c);
97 static int Write(gzFile gzf,
char c);
103 static int Read(gzFile gzf, uint8_t & c);
109 static int Write(gzFile gzf, uint8_t c);
115 static int Read(gzFile gzf,
bool & b);
121 static int Write(gzFile gzf,
bool b);
127 static int Read(gzFile gzf, int16_t & val);
134 static int Write(gzFile gzf, int16_t val);
140 static int Read(gzFile gzf, uint16_t & val);
147 static int Write(gzFile gzf, uint16_t val);
153 static int Read(gzFile gzf, int32_t & val);
160 static int Write(gzFile gzf, int32_t val);
166 static int Read(gzFile gzf, uint32_t & val);
173 static int Write(gzFile gzf, uint32_t val);
179 static int Read(gzFile gzf, int64_t & val);
186 static int Write(gzFile gzf,
const int64_t & val);
192 static int Read(gzFile gzf, uint64_t & val);
199 static int Write(gzFile gzf,
const uint64_t & val);
206 static int Read(gzFile gzf,
float & val);
213 static int Write(gzFile gzf,
float val);
220 static int Read(gzFile gzf,
double & val);
227 static int Write(gzFile gzf,
const double & val);
235 static int Read(gzFile gzf, std::string & s);
244 static int Write(gzFile gzf,
const std::string & s);
250 {
return(val.
Read(gzf)); }
257 {
return val.Read(gzf); }
263 {
return(val.
Write(gzf)); }
270 {
return val.Write(gzf); }
276 template <
typename _firstT,
typename _secondT>
277 static int Read(gzFile gzf, std::pair<_firstT, _secondT> & p)
281 int bytesRead =
Read(gzf, p.first);
284 bytesRead =
Read(gzf, p.second);
298 template <
typename _firstT,
typename _secondT>
299 static int Write(gzFile gzf,
const std::pair<_firstT,_secondT> & p)
303 int bytesWritten =
Write(gzf, p.first);
304 if (bytesWritten > 0) {
306 bytesWritten =
Write(gzf, p.second);
307 if (bytesWritten > 0) {
322 template <
typename _keyT,
typename _valueT,
323 typename _Compare,
typename _Alloc>
324 static int Read(gzFile gzf, std::map<_keyT, _valueT, _Compare, _Alloc> & m)
326 return(PairAssocContRead<std::map<_keyT, _valueT, _Compare, _Alloc> >(gzf, m));
333 template<
typename _keyT,
typename _valueT,
334 typename _Compare,
typename _Alloc>
336 Write(gzFile gzf,
const std::map<_keyT,_valueT,_Compare,_Alloc> & m)
338 return(ContainerWrite<std::map<_keyT,_valueT,_Compare,_Alloc> >(gzf, m));
345 template <
typename _keyT,
typename _valueT,
346 typename _Compare,
typename _Alloc>
348 Read(gzFile gzf, std::multimap<_keyT, _valueT, _Compare, _Alloc> & m)
350 return(PairAssocContRead<std::multimap<_keyT,_valueT,_Compare,_Alloc> >(gzf, m));
357 template <
typename _keyT,
typename _valueT,
358 typename _Compare,
typename _Alloc>
360 Write(gzFile gzf,
const std::multimap<_keyT,_valueT, _Compare, _Alloc> & m)
362 return(ContainerWrite<std::multimap<_keyT,_valueT,_Compare,_Alloc> >(gzf, m));
369 template <
typename _valueT,
size_t N>
370 static int Read(gzFile gzf, std::array<_valueT, N> & a)
373 for (
size_t i = 0; i < N; ++i) {
374 int bytesRead =
Read(gzf, a[i]);
390 template <
typename _valueT,
size_t N>
391 static int Write(gzFile gzf,
const std::array<_valueT, N> & a)
394 for (
size_t i = 0; i < N; ++i) {
395 int bytesWritten =
Write(gzf, a[i]);
396 if (bytesWritten > 0) {
411 template <
typename _valueT,
typename _Alloc>
412 static int Read(gzFile gzf, std::vector<_valueT, _Alloc> & v)
414 return(ContainerRead<std::vector<_valueT, _Alloc> >(gzf, v));
421 template <
typename _valueT,
typename _Alloc>
422 static int Write(gzFile gzf,
const std::vector<_valueT, _Alloc> & v)
424 return(ContainerWrite<std::vector<_valueT, _Alloc> >(gzf, v));
431 template <
typename _valueT,
typename _Alloc>
432 static int Read(gzFile gzf, std::deque<_valueT, _Alloc> & d)
434 return(ContainerRead<std::deque<_valueT, _Alloc> >(gzf, d));
441 template <
typename _valueT,
typename _Alloc>
442 static int Write(gzFile gzf,
const std::deque<_valueT, _Alloc> & d)
444 return(ContainerWrite<std::deque<_valueT, _Alloc> >(gzf, d));
451 template <
typename _valueT,
typename _Alloc>
452 static int Read(gzFile gzf, std::list<_valueT, _Alloc> & l)
454 return(ContainerRead<std::list<_valueT, _Alloc> >(gzf, l));
461 template <
typename _valueT,
typename _Alloc>
462 static int Write(gzFile gzf,
const std::list<_valueT, _Alloc> & l)
464 return(ContainerWrite<std::list<_valueT, _Alloc> >(gzf, l));
471 template <
typename _valueT,
typename _Compare,
typename _Alloc>
472 static int Read(gzFile gzf, std::set<_valueT, _Compare, _Alloc> & l)
474 return(ContainerRead<std::set<_valueT, _Compare, _Alloc> >(gzf, l));
481 template <
typename _valueT,
typename _Compare,
typename _Alloc>
482 static int Write(gzFile gzf,
const std::set<_valueT, _Compare, _Alloc> & l)
484 return(ContainerWrite<std::set<_valueT, _Compare, _Alloc> >(gzf, l));
491 template <
typename _valueT,
typename _Compare,
typename _Alloc>
492 static int Read(gzFile gzf, std::multiset<_valueT, _Compare, _Alloc> & l)
494 return(ContainerRead<std::multiset<_valueT, _Compare, _Alloc> >(gzf, l));
501 template <
typename _valueT,
typename _Compare,
typename _Alloc>
503 Write(gzFile gzf,
const std::multiset<_valueT, _Compare, _Alloc> & l)
505 return(ContainerWrite<std::multiset<_valueT, _Compare, _Alloc> >(gzf, l));
513 static int Read(gzFile gzf, std::monostate & sm)
523 static int Write(gzFile gzf,
const std::monostate & sm)
532 template <
typename... Ts>
533 static int Read(gzFile gzf, std::variant<Ts...> & v)
537 int bytesRead =
Read(gzf, index);
540 if (index < std::variant_size_v<std::variant<Ts...>>) {
542 std::visit([&] (
auto && arg) { rc =
Read(gzf, arg); }, v);
543 if (bytesRead >= 0) {
558 template <
typename... Ts>
559 static int Write(gzFile gzf,
const std::variant<Ts...> & v)
562 uint64_t index = v.index();
563 int bytesWritten =
Write(gzf, index);
564 if (bytesWritten > 0) {
566 std::visit([&] (
const auto & arg)
567 { bytesWritten =
Write(gzf, arg); }, v);
568 if (bytesWritten >= 0) {
582 template <
typename... Args>
583 static int Read(gzFile gzf, std::tuple<Args...> & t)
585 return(std::apply([&gzf](
auto&&...args)
588 auto read_tuple_mem = [&gzf,&rc](
auto&& x) {
589 int bytesRead =
Read(gzf, x);
590 if (bytesRead > 0) { rc += bytesRead;
return true; }
591 else {
return false; }
593 if ((read_tuple_mem(args) && ...)) {
606 template <
typename... Args>
607 static int Write(gzFile gzf,
const std::tuple<Args...> & t)
609 return(std::apply([&gzf](
auto&&...args)
612 auto read_tuple_mem = [&gzf,&rc](
auto&& x) {
613 int bytesWritten =
Write(gzf, x);
614 if (bytesWritten > 0) { rc += bytesWritten;
return true; }
615 else {
return false; }
617 if ((read_tuple_mem(args) && ...)) {
630 template<
typename _keyT,
typename _valueT,
typename _Hash,
631 typename _Pred,
typename _Alloc>
633 Read(gzFile gzf, std::unordered_map<_keyT,_valueT,_Hash,_Pred,_Alloc> & hm)
635 return(PairAssocContRead<std::unordered_map<_keyT,_valueT,_Hash,_Pred,_Alloc> >(gzf, hm));
642 template<
typename _keyT,
typename _valueT,
typename _Hash,
643 typename _Pred,
typename _Alloc>
646 const std::unordered_map<_keyT,_valueT,_Hash,_Pred,_Alloc> & hm)
648 return(ContainerWrite<std::unordered_map<_keyT,_valueT,_Hash,_Pred,_Alloc> >(gzf, hm));
655 template<
typename _valueT,
typename _Hash,
656 typename _Pred,
typename _Alloc>
658 Read(gzFile gzf, std::unordered_set<_valueT,_Hash,_Pred,_Alloc> & hm)
660 return(ContainerRead<std::unordered_set<_valueT,_Hash,_Pred,_Alloc> >(gzf, hm));
667 template<
typename _valueT,
typename _Hash,
668 typename _Pred,
typename _Alloc>
671 const std::unordered_set<_valueT,_Hash,_Pred,_Alloc> & hm)
673 return(ContainerWrite<std::unordered_set<_valueT,_Hash,_Pred,_Alloc> >(gzf, hm));
680 template<
typename _keyT,
typename _valueT,
typename _Hash,
681 typename _Pred,
typename _Alloc>
683 Read(gzFile gzf, std::unordered_multimap<_keyT,_valueT,_Hash,_Pred,_Alloc> & hm)
685 return(PairAssocContRead<std::unordered_multimap<_keyT,_valueT,_Hash,_Pred,_Alloc> >(gzf, hm));
692 template<
typename _keyT,
typename _valueT,
typename _Hash,
693 typename _Pred,
typename _Alloc>
696 const std::unordered_multimap<_keyT,_valueT,_Hash,_Pred,_Alloc> & hm)
698 return(ContainerWrite<std::unordered_multimap<_keyT,_valueT,_Hash,_Pred,_Alloc> >(gzf, hm));
705 template<
typename _valueT,
typename _Hash,
706 typename _Pred,
typename _Alloc>
708 Read(gzFile gzf, std::unordered_multiset<_valueT,_Hash,_Pred,_Alloc> & hm)
710 return(ContainerRead<std::unordered_multiset<_valueT,_Hash,_Pred,_Alloc> >(gzf, hm));
717 template<
typename _valueT,
typename _Hash,
718 typename _Pred,
typename _Alloc>
721 const std::unordered_multiset<_valueT,_Hash,_Pred,_Alloc> & hm)
723 return(ContainerWrite<std::unordered_multiset<_valueT,_Hash,_Pred,_Alloc> >(gzf, hm));
730 template <
typename ...Args>
731 static int ReadV(gzFile gzf, Args & ...args)
734 auto readOne = [&] (
auto & arg) {
736 int bytesRead =
Read(gzf, arg);
746 (readOne(args) && ...);
754 template <
typename ...Args>
755 static int WriteV(gzFile gzf,
const Args & ...args)
758 auto writeOne = [&] (
auto & arg) {
760 int bytesWritten =
Write(gzf, arg);
761 if (bytesWritten > 0) {
770 (writeOne(args) && ...);
778 template <
typename _inputIteratorT>
779 static int Write(gzFile gzf, _inputIteratorT f, _inputIteratorT l)
783 for ( ; f != l; ++f) {
784 int bytesWritten =
Write(gzf, *f);
785 if (bytesWritten > 0) {
802 template <
typename _containerT>
803 static int ContainerRead(gzFile gzf, _containerT & c)
810 int bytesRead =
Read(gzf, numEntries);
811 if (bytesRead ==
sizeof(numEntries)) {
813 for (uint64_t i = 0; i < numEntries; ++i) {
814 typename _containerT::value_type val;
815 bytesRead =
Read(gzf, val);
818 c.insert(c.end(), std::move(val));
835 template <
typename _containerT>
836 static int ContainerWrite(gzFile gzf,
const _containerT & c)
840 uint64_t numEntries = c.size();
841 uint64_t bytesWritten =
Write(gzf, numEntries);
842 if (bytesWritten ==
sizeof(numEntries)) {
849 if (bytesWritten > 0) {
866 template <
typename _containerT>
867 static int PairAssocContRead(gzFile gzf, _containerT & m)
874 int bytesRead =
Read(gzf, numEntries);
875 if (bytesRead ==
sizeof(numEntries)) {
877 for (uint64_t i = 0; i < numEntries; ++i) {
878 typename _containerT::key_type key;
879 bytesRead =
Read(gzf, key);
882 typename _containerT::mapped_type val;
883 bytesRead =
Read(gzf, val);
886 m.insert(
typename _containerT::value_type(std::move(key),
Dwm::HasGZRead and Dwm::HasGZWrite concepts. Dwm::GZReadable, Dwm::GZWritable and Dwm::GZIOCapable pu...
A configure target for dealing with OS differences.
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 in network ...
Definition DwmGZIO.hh:85
static int Read(gzFile gzf, std::string &s)
Reads s from gzf.
static int Write(gzFile gzf, const double &val)
Writes val tp gzf, in IEEE format (see RFC 1832 and/or ANSI/IEEE Standard 754-1985).
static int Read(gzFile gzf, std::tuple< Args... > &t)
Reads a tuple from a gzFile.
Definition DwmGZIO.hh:583
static int Write(gzFile gzf, const std::unordered_multimap< _keyT, _valueT, _Hash, _Pred, _Alloc > &hm)
Writes an unordered_multimap to a gzFile.
Definition DwmGZIO.hh:695
static int Write(gzFile gzf, uint16_t val)
Writes val to gzf, in network byte order (MSB first).
static int ReadV(gzFile gzf, Args &...args)
Reads args from gzf.
Definition DwmGZIO.hh:731
static int Read(gzFile gzf, GZReadable &val)
Wrapper function to read a GZReadable object from a gzFile.
Definition DwmGZIO.hh:249
static int Read(gzFile gzf, std::multimap< _keyT, _valueT, _Compare, _Alloc > &m)
Reads a multimap<_keyT,_valueT> from a gzFile.
Definition DwmGZIO.hh:348
static int Write(gzFile gzf, char c)
Writes c to gzf.
static int Read(gzFile gzf, int16_t &val)
Reads val from gzf, in network byte order (MSB first).
static int Write(gzFile gzf, const int64_t &val)
Writes val to gzf, in network byte order (MSB first).
static int Read(gzFile gzf, double &val)
Reads val from gzf, in IEEE format (see RFC 1832 and/or ANSI/IEEE Standard 754-1985).
static int Read(gzFile gzf, bool &b)
Reads b from gzf.
static int Read(gzFile gzf, int32_t &val)
Reads val from gzf, in network byte order (MSB first).
static int Read(gzFile gzf, std::deque< _valueT, _Alloc > &d)
Reads a deque<_valueT> from a gzFile.
Definition DwmGZIO.hh:432
static int Read(gzFile gzf, std::vector< _valueT, _Alloc > &v)
Reads a vector<_valueT> from a gzFile.
Definition DwmGZIO.hh:412
static int Read(gzFile gzf, uint32_t &val)
Reads val from gzf, in network byte order (MSB first).
static int Read(gzFile gzf, HasGZRead auto &val)
Reads val from gzf, where val meets the requirements of the HasGZRead concept.
Definition DwmGZIO.hh:256
static int Read(gzFile gzf, float &val)
Reads val from gzf, in IEEE format (see RFC 1832 and/or ANSI/IEEE Standard 754-1985).
static int Write(gzFile gzf, const std::array< _valueT, N > &a)
Writes an array<_valueT,N> to a gzFile.
Definition DwmGZIO.hh:391
static int Read(gzFile gzf, std::multiset< _valueT, _Compare, _Alloc > &l)
Reads a multiset<_valueT> from a gzFile.
Definition DwmGZIO.hh:492
static int Read(gzFile gzf, std::set< _valueT, _Compare, _Alloc > &l)
Reads a set<_valueT> from a gzFile.
Definition DwmGZIO.hh:472
static int Write(gzFile gzf, bool b)
Writes b to gzf.
static int Read(gzFile gzf, std::monostate &sm)
Just a dummy helper function for std::variant instances that hold a std::monostate.
Definition DwmGZIO.hh:513
static int Read(gzFile gzf, std::variant< Ts... > &v)
Reads a variant from a gzFile.
Definition DwmGZIO.hh:533
static int Write(gzFile gzf, const uint64_t &val)
Writes val to gzf, in network byte order (MSB first).
static int Write(gzFile gzf, const GZWritable &val)
Wrapper function to write a GZWritable object to a gzFile.
Definition DwmGZIO.hh:262
static int Write(gzFile gzf, const std::vector< _valueT, _Alloc > &v)
Writes a vector<_valueT> to a gzFile.
Definition DwmGZIO.hh:422
static int Read(gzFile gzf, std::unordered_set< _valueT, _Hash, _Pred, _Alloc > &hm)
Reads an unordered_set from a gzFile.
Definition DwmGZIO.hh:658
static int Write(gzFile gzf, uint8_t c)
Writes c to gzf.
static int Write(gzFile gzf, const std::list< _valueT, _Alloc > &l)
Writes a list<_valueT> to a gzFile.
Definition DwmGZIO.hh:462
static int Write(gzFile gzf, const std::unordered_map< _keyT, _valueT, _Hash, _Pred, _Alloc > &hm)
Writes an unordered_map to a gzFile.
Definition DwmGZIO.hh:645
static int Write(gzFile gzf, const std::map< _keyT, _valueT, _Compare, _Alloc > &m)
Writes a map<_keyT,_valueT> to a gzFile.
Definition DwmGZIO.hh:336
static int Read(gzFile gzf, std::map< _keyT, _valueT, _Compare, _Alloc > &m)
Reads a map<_keyT,_valueT> from a gzFile.
Definition DwmGZIO.hh:324
static int Read(gzFile gzf, std::unordered_multimap< _keyT, _valueT, _Hash, _Pred, _Alloc > &hm)
Reads an unordered_multimap from a gzFile.
Definition DwmGZIO.hh:683
static int Read(gzFile gzf, std::list< _valueT, _Alloc > &l)
Reads a list<_valueT> from a gzFile.
Definition DwmGZIO.hh:452
static int Read(gzFile gzf, std::array< _valueT, N > &a)
Reads an array<_valueT,N> from a gzFile.
Definition DwmGZIO.hh:370
static int Read(gzFile gzf, uint8_t &c)
Reads c from gzf.
static int Write(gzFile gzf, const std::monostate &sm)
Just a dummy helper function for std::variant instances that hold a std::monostate.
Definition DwmGZIO.hh:523
static int Write(gzFile gzf, const std::variant< Ts... > &v)
Writes a variant to a gzFile.
Definition DwmGZIO.hh:559
static int Read(gzFile gzf, int64_t &val)
Reads val from gzf, in network byte order (MSB first).
static int Write(gzFile gzf, const std::multimap< _keyT, _valueT, _Compare, _Alloc > &m)
Writes a multimap<_keyT,_valueT> to a gzFile.
Definition DwmGZIO.hh:360
static int Write(gzFile gzf, const HasGZWrite auto &val)
Writes val to gzf, where val meets the requirements of the HasGZWrite concept.
Definition DwmGZIO.hh:269
static int Write(gzFile gzf, const std::tuple< Args... > &t)
Writes a tuple to a gzFile.
Definition DwmGZIO.hh:607
static int Write(gzFile gzf, int16_t val)
Writes val to gzf, in network byte order (MSB first).
static int Write(gzFile gzf, const std::unordered_set< _valueT, _Hash, _Pred, _Alloc > &hm)
Writes an unordered_set to a gzFile.
Definition DwmGZIO.hh:670
static int Write(gzFile gzf, const std::string &s)
Writes s to gzf.
static int Read(gzFile gzf, uint16_t &val)
Reads val from gzf, in network byte order (MSB first).
static int Write(gzFile gzf, const std::unordered_multiset< _valueT, _Hash, _Pred, _Alloc > &hm)
Writes an unordered_multiset to a gzFile.
Definition DwmGZIO.hh:720
static int Write(gzFile gzf, const std::multiset< _valueT, _Compare, _Alloc > &l)
Writes a multiset<_valueT> to a gzFile.
Definition DwmGZIO.hh:503
static int Write(gzFile gzf, const std::pair< _firstT, _secondT > &p)
Writes a pair<_firstT,_secondT> to a gzFile.
Definition DwmGZIO.hh:299
static int Write(gzFile gzf, const std::set< _valueT, _Compare, _Alloc > &l)
Writes a set<_valueT> to a gzFile.
Definition DwmGZIO.hh:482
static int Read(gzFile gzf, uint64_t &val)
Reads val from gzf, in network byte order (MSB first).
static int WriteV(gzFile gzf, const Args &...args)
Writes args to gzf.
Definition DwmGZIO.hh:755
static int Write(gzFile gzf, uint32_t val)
Writes val to gzf, in network byte order (MSB first).
static int Read(gzFile gzf, std::unordered_multiset< _valueT, _Hash, _Pred, _Alloc > &hm)
Reads an unordered_multiset from a gzFile.
Definition DwmGZIO.hh:708
static int Read(gzFile gzf, char &c)
Reads from gzf.
static int Read(gzFile gzf, std::unordered_map< _keyT, _valueT, _Hash, _Pred, _Alloc > &hm)
Reads an unordered_map from a gzFile.
Definition DwmGZIO.hh:633
static int Write(gzFile gzf, const std::deque< _valueT, _Alloc > &d)
Writes a deque<_valueT> to a gzFile.
Definition DwmGZIO.hh:442
static int Write(gzFile gzf, int32_t val)
Writes val to gzf, in network byte order (MSB first).
static int Write(gzFile gzf, float val)
Writes val tp gzf, in IEEE format (see RFC 1832 and/or ANSI/IEEE Standard 754-1985).
static int Read(gzFile gzf, std::pair< _firstT, _secondT > &p)
Reads a pair<_firstT,_secondT> from a gzFile.
Definition DwmGZIO.hh:277
This class is a pure virtual class, defining an interface for classes that can read their contents fr...
Definition DwmGZIOCapable.hh:78
virtual int Read(gzFile gzf)=0
Read from a gzFile.
This class is a pure virtual class, defining an interface for classes that can write their contents t...
Definition DwmGZIOCapable.hh:97
virtual int Write(gzFile gzf) const =0
Write to a gzFile.
T has a Read(gzFile) member that returns int (return value of gzread() on success,...
Definition DwmGZIOCapable.hh:60
T has a Write(gzFile) const member that returns int (return value of gzwrite() on success,...
Definition DwmGZIOCapable.hh:69