libDwm-0.9.45
DwmBZ2IO.hh
Go to the documentation of this file.
1//===========================================================================
2// @(#) $DwmPath$
3//===========================================================================
4// Copyright (c) Daniel W. McRobb 2004-2006, 2016, 2020, 2024
5// All rights reserved.
6//
7// Redistribution and use in source and binary forms, with or without
8// modification, are permitted provided that the following conditions
9// are met:
10//
11// 1. Redistributions of source code must retain the above copyright
12// notice, this list of conditions and the following disclaimer.
13// 2. Redistributions in binary form must reproduce the above copyright
14// notice, this list of conditions and the following disclaimer in the
15// documentation and/or other materials provided with the distribution.
16// 3. The names of the authors and copyright holders may not be used to
17// endorse or promote products derived from this software without
18// specific prior written permission.
19//
20// IN NO EVENT SHALL DANIEL W. MCROBB BE LIABLE TO ANY PARTY FOR
21// DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
22// INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE,
23// EVEN IF DANIEL W. MCROBB HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
24// DAMAGE.
25//
26// THE SOFTWARE PROVIDED HEREIN IS ON AN "AS IS" BASIS, AND
27// DANIEL W. MCROBB HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT,
28// UPDATES, ENHANCEMENTS, OR MODIFICATIONS. DANIEL W. MCROBB MAKES NO
29// REPRESENTATIONS AND EXTENDS NO WARRANTIES OF ANY KIND, EITHER
30// IMPLIED OR EXPRESS, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
31// WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR
32// PURPOSE, OR THAT THE USE OF THIS SOFTWARE WILL NOT INFRINGE ANY PATENT,
33// TRADEMARK OR OTHER RIGHTS.
34//===========================================================================
35
36//---------------------------------------------------------------------------
40//---------------------------------------------------------------------------
41
42#ifndef _DWMBZ2IO_HH_
43#define _DWMBZ2IO_HH_
44
45#include <array>
46#include <cassert>
47#include <cstdint>
48#include <cstdio>
49#include <deque>
50#include <iostream>
51#include <list>
52#include <map>
53#include <set>
54#include <string>
55#include <tuple>
56#include <unordered_map>
57#include <unordered_set>
58#include <variant>
59#include <vector>
60
61#include "DwmPortability.hh"
62#include "DwmBZ2IOCapable.hh"
64
65namespace Dwm {
66
67 //--------------------------------------------------------------------------
80 //--------------------------------------------------------------------------
81 class BZ2IO
82 {
83 public:
84 //------------------------------------------------------------------------
87 //------------------------------------------------------------------------
88 static int BZRead(BZFILE *bzf, char & c);
89
90 //------------------------------------------------------------------------
93 //------------------------------------------------------------------------
94 static int BZWrite(BZFILE *bzf, char c);
95
96 //------------------------------------------------------------------------
99 //------------------------------------------------------------------------
100 static int BZRead(BZFILE *bzf, uint8_t & c);
101
102 //------------------------------------------------------------------------
105 //------------------------------------------------------------------------
106 static int BZWrite(BZFILE *bzf, uint8_t c);
107
108 //------------------------------------------------------------------------
111 //------------------------------------------------------------------------
112 static int BZRead(BZFILE *bzf, bool & b);
113
114 //------------------------------------------------------------------------
117 //------------------------------------------------------------------------
118 static int BZWrite(BZFILE *bzf, bool b);
119
120 //------------------------------------------------------------------------
123 //------------------------------------------------------------------------
124 static int BZRead(BZFILE *bzf, int16_t & val);
125
126 //------------------------------------------------------------------------
130 //------------------------------------------------------------------------
131 static int BZWrite(BZFILE *bzf, int16_t val);
132
133 //------------------------------------------------------------------------
136 //------------------------------------------------------------------------
137 static int BZRead(BZFILE *bzf, uint16_t & val);
138
139 //------------------------------------------------------------------------
143 //------------------------------------------------------------------------
144 static int BZWrite(BZFILE *bzf, uint16_t val);
145
146 //------------------------------------------------------------------------
149 //------------------------------------------------------------------------
150 static int BZRead(BZFILE *bzf, int32_t & val);
151
152 //------------------------------------------------------------------------
156 //------------------------------------------------------------------------
157 static int BZWrite(BZFILE *bzf, int32_t val);
158
159 //------------------------------------------------------------------------
162 //------------------------------------------------------------------------
163 static int BZRead(BZFILE *bzf, uint32_t & val);
164
165 //------------------------------------------------------------------------
169 //------------------------------------------------------------------------
170 static int BZWrite(BZFILE *bzf, uint32_t val);
171
172 //------------------------------------------------------------------------
175 //------------------------------------------------------------------------
176 static int BZRead(BZFILE *bzf, int64_t & val);
177
178 //------------------------------------------------------------------------
182 //------------------------------------------------------------------------
183 static int BZWrite(BZFILE *bzf, const int64_t & val);
184
185 //------------------------------------------------------------------------
188 //------------------------------------------------------------------------
189 static int BZRead(BZFILE *bzf, uint64_t & val);
190
191 //------------------------------------------------------------------------
195 //------------------------------------------------------------------------
196 static int BZWrite(BZFILE *bzf, const uint64_t & val);
197
198 //------------------------------------------------------------------------
202 //------------------------------------------------------------------------
203 static int BZRead(BZFILE *bzf, float & val);
204
205 //------------------------------------------------------------------------
209 //------------------------------------------------------------------------
210 static int BZWrite(BZFILE *bzf, float val);
211
212 //------------------------------------------------------------------------
216 //------------------------------------------------------------------------
217 static int BZRead(BZFILE *bzf, double & val);
218
219 //------------------------------------------------------------------------
223 //------------------------------------------------------------------------
224 static int BZWrite(BZFILE *bzf, const double & val);
225
226 //------------------------------------------------------------------------
231 //------------------------------------------------------------------------
232 static int BZRead(BZFILE *bzf, std::string & s);
233
234 //------------------------------------------------------------------------
240 //------------------------------------------------------------------------
241 static int BZWrite(BZFILE *bzf, const std::string & s);
242
243 //------------------------------------------------------------------------
245 //------------------------------------------------------------------------
246 static int BZRead(BZFILE *bzf, BZ2Readable & val)
247 { return(val.BZRead(bzf)); }
248
249 //------------------------------------------------------------------------
252 //------------------------------------------------------------------------
253 static int BZRead(BZFILE *bzf, HasBZRead auto & val)
254 { return val.BZRead(bzf); }
255
256 //------------------------------------------------------------------------
258 //------------------------------------------------------------------------
259 static int BZWrite(BZFILE *bzf, const BZ2Writable & val)
260 { return(val.BZWrite(bzf)); }
261
262 //------------------------------------------------------------------------
265 //------------------------------------------------------------------------
266 static int BZWrite(BZFILE *bzf, const HasBZWrite auto & val)
267 { return val.BZWrite(bzf); }
268
269 //------------------------------------------------------------------------
272 //------------------------------------------------------------------------
273 template <typename _firstT, typename _secondT>
274 static int BZRead(BZFILE *bzf, std::pair<_firstT, _secondT> & p)
275 {
276 int rc = -1;
277 if (bzf) {
278 int bytesRead = BZRead(bzf, p.first);
279 if (bytesRead > 0) {
280 rc = bytesRead;
281 bytesRead = BZRead(bzf, p.second);
282 if (bytesRead > 0)
283 rc += bytesRead;
284 else
285 rc = -1;
286 }
287 }
288 return(rc);
289 }
290
291 //------------------------------------------------------------------------
294 //------------------------------------------------------------------------
295 template <typename _firstT, typename _secondT>
296 static int BZWrite(BZFILE *bzf, const std::pair<_firstT,_secondT> & p)
297 {
298 int rc = -1;
299 if (bzf) {
300 int bytesWritten = BZWrite(bzf, p.first);
301 if (bytesWritten > 0) {
302 rc = bytesWritten;
303 bytesWritten = BZWrite(bzf, p.second);
304 if (bytesWritten > 0) {
305 rc += bytesWritten;
306 }
307 else {
308 rc = -1;
309 }
310 }
311 }
312 return(rc);
313 }
314
315 //------------------------------------------------------------------------
318 //------------------------------------------------------------------------
319 template <typename _keyT, typename _valueT,
320 typename _Compare, typename _Alloc>
321 static int BZRead(BZFILE *bzf, std::map<_keyT, _valueT, _Compare, _Alloc> & m)
322 {
323 return(PairAssocContBZRead<std::map<_keyT, _valueT, _Compare, _Alloc> >(bzf, m));
324 }
325
326 //------------------------------------------------------------------------
329 //------------------------------------------------------------------------
330 template<typename _keyT, typename _valueT,
331 typename _Compare, typename _Alloc>
332 static int
333 BZWrite(BZFILE *bzf, const std::map<_keyT,_valueT,_Compare,_Alloc> & m)
334 {
335 return(ContainerBZWrite<std::map<_keyT,_valueT,_Compare,_Alloc> >(bzf, m));
336 }
337
338 //------------------------------------------------------------------------
341 //------------------------------------------------------------------------
342 template <typename _keyT, typename _valueT,
343 typename _Compare, typename _Alloc>
344 static int
345 BZRead(BZFILE *bzf, std::multimap<_keyT, _valueT, _Compare, _Alloc> & m)
346 {
347 return(PairAssocContBZRead<std::multimap<_keyT,_valueT,_Compare,_Alloc> >(bzf, m));
348 }
349
350 //------------------------------------------------------------------------
353 //------------------------------------------------------------------------
354 template <typename _keyT, typename _valueT,
355 typename _Compare, typename _Alloc>
356 static int
357 BZWrite(BZFILE *bzf, const std::multimap<_keyT,_valueT, _Compare, _Alloc> & m)
358 {
359 return(ContainerBZWrite<std::multimap<_keyT,_valueT,_Compare,_Alloc> >(bzf, m));
360 }
361
362 //------------------------------------------------------------------------
365 //------------------------------------------------------------------------
366 template <typename _valueT, size_t N>
367 static int BZRead(BZFILE *bzf, std::array<_valueT, N> & a)
368 {
369 int rc = 0;
370 for (size_t i = 0; i < N; ++i) {
371 int bytesRead = BZRead(bzf, a[i]);
372 if (bytesRead > 0) {
373 rc += bytesRead;
374 }
375 else {
376 rc = -1;
377 break;
378 }
379 }
380 return rc;
381 }
382
383 //------------------------------------------------------------------------
386 //------------------------------------------------------------------------
387 template <typename _valueT, size_t N>
388 static int BZWrite(BZFILE *bzf, const std::array<_valueT, N> & a)
389 {
390 int rc = 0;
391 for (size_t i = 0; i < N; ++i) {
392 int bytesWritten = BZWrite(bzf, a[i]);
393 if (bytesWritten > 0) {
394 rc += bytesWritten;
395 }
396 else {
397 rc = -1;
398 break;
399 }
400 }
401 return rc;
402 }
403
404 //------------------------------------------------------------------------
407 //------------------------------------------------------------------------
408 template <typename _valueT, typename _Alloc>
409 static int BZRead(BZFILE *bzf, std::vector<_valueT, _Alloc> & v)
410 {
411 return(ContainerBZRead<std::vector<_valueT, _Alloc> >(bzf, v));
412 }
413
414 //------------------------------------------------------------------------
417 //------------------------------------------------------------------------
418 template <typename _valueT, typename _Alloc>
419 static int BZWrite(BZFILE *bzf, const std::vector<_valueT, _Alloc> & v)
420 {
421 return(ContainerBZWrite<std::vector<_valueT, _Alloc> >(bzf, v));
422 }
423
424 //------------------------------------------------------------------------
427 //------------------------------------------------------------------------
428 template <typename _valueT, typename _Alloc>
429 static int BZRead(BZFILE *bzf, std::deque<_valueT, _Alloc> & d)
430 {
431 return(ContainerBZRead<std::deque<_valueT, _Alloc> >(bzf, d));
432 }
433
434 //------------------------------------------------------------------------
437 //------------------------------------------------------------------------
438 template <typename _valueT, typename _Alloc>
439 static int BZWrite(BZFILE *bzf, const std::deque<_valueT, _Alloc> & d)
440 {
441 return(ContainerBZWrite<std::deque<_valueT, _Alloc> >(bzf, d));
442 }
443
444 //------------------------------------------------------------------------
447 //------------------------------------------------------------------------
448 template <typename _valueT, typename _Alloc>
449 static int BZRead(BZFILE *bzf, std::list<_valueT, _Alloc> & l)
450 {
451 return(ContainerBZRead<std::list<_valueT, _Alloc> >(bzf, l));
452 }
453
454 //------------------------------------------------------------------------
457 //------------------------------------------------------------------------
458 template <typename _valueT, typename _Alloc>
459 static int BZWrite(BZFILE *bzf, const std::list<_valueT, _Alloc> & l)
460 {
461 return(ContainerBZWrite<std::list<_valueT, _Alloc> >(bzf, l));
462 }
463
464 //------------------------------------------------------------------------
467 //------------------------------------------------------------------------
468 template <typename _valueT, typename _Compare, typename _Alloc>
469 static int BZRead(BZFILE *bzf, std::set<_valueT, _Compare, _Alloc> & l)
470 {
471 return(ContainerBZRead<std::set<_valueT, _Compare, _Alloc> >(bzf, l));
472 }
473
474 //------------------------------------------------------------------------
477 //------------------------------------------------------------------------
478 template <typename _valueT, typename _Compare, typename _Alloc>
479 static int BZWrite(BZFILE *bzf, const std::set<_valueT, _Compare, _Alloc> & l)
480 {
481 return(ContainerBZWrite<std::set<_valueT, _Compare, _Alloc> >(bzf, l));
482 }
483
484 //------------------------------------------------------------------------
487 //------------------------------------------------------------------------
488 template <typename _valueT, typename _Compare, typename _Alloc>
489 static int BZRead(BZFILE *bzf, std::multiset<_valueT, _Compare, _Alloc> & l)
490 {
491 return(ContainerBZRead<std::multiset<_valueT, _Compare, _Alloc> >(bzf, l));
492 }
493
494 //------------------------------------------------------------------------
497 //------------------------------------------------------------------------
498 template <typename _valueT, typename _Compare, typename _Alloc>
499 static int
500 BZWrite(BZFILE *bzf, const std::multiset<_valueT, _Compare, _Alloc> & l)
501 {
502 return(ContainerBZWrite<std::multiset<_valueT, _Compare, _Alloc> >(bzf, l));
503 }
504
505 //------------------------------------------------------------------------
507 //------------------------------------------------------------------------
508 template <typename... Args>
509 static int BZWrite(BZFILE *bzf, const std::tuple<Args...> & t)
510 {
511 return(std::apply([&bzf](auto&&...args)
512 {
513 int rc = 0;
514 auto read_tuple_mem = [&bzf,&rc](auto&& x) {
515 int bytesWritten = BZWrite(bzf, x);
516 if (bytesWritten > 0) { rc += bytesWritten; return true; }
517 else { return false; }
518 };
519 if ((read_tuple_mem(args) && ...)) {
520 return rc;
521 }
522 else {
523 return -1;
524 }
525 }, t));
526 }
527
528 //------------------------------------------------------------------------
530 //------------------------------------------------------------------------
531 template <typename... Args>
532 static int BZRead(BZFILE *bzf, std::tuple<Args...> & t)
533 {
534 return(std::apply([&bzf](auto&&...args)
535 {
536 int rc = 0;
537 auto read_tuple_mem = [&bzf,&rc](auto&& x) {
538 int bytesRead = BZRead(bzf, x);
539 if (bytesRead > 0) { rc += bytesRead; return true; }
540 else { return false; }
541 };
542 if ((read_tuple_mem(args) && ...)) {
543 return rc;
544 }
545 else {
546 return -1;
547 }
548 }, t));
549 }
550
551 //------------------------------------------------------------------------
554 //------------------------------------------------------------------------
555 template<typename _keyT, typename _valueT, typename _Hash,
556 typename _Pred, typename _Alloc>
557 static int
558 BZRead(BZFILE *bzf,
559 std::unordered_map<_keyT,_valueT,_Hash,_Pred,_Alloc> & hm)
560 {
561 return(PairAssocContBZRead<std::unordered_map<_keyT,_valueT,_Hash,_Pred,_Alloc> >(bzf, hm));
562 }
563
564 //------------------------------------------------------------------------
567 //------------------------------------------------------------------------
568 template<typename _keyT, typename _valueT, typename _Hash,
569 typename _Pred, typename _Alloc>
570 static int
571 BZWrite(BZFILE *bzf,
572 const std::unordered_map<_keyT,_valueT,_Hash,_Pred,_Alloc> & hm)
573 {
574 return(ContainerBZWrite<std::unordered_map<_keyT,_valueT,_Hash,_Pred,_Alloc> >(bzf, hm));
575 }
576
577 //------------------------------------------------------------------------
580 //------------------------------------------------------------------------
581 template<typename _valueT, typename _Hash,
582 typename _Pred, typename _Alloc>
583 static int
584 BZRead(BZFILE *bzf,
585 std::unordered_set<_valueT,_Hash,_Pred,_Alloc> & hm)
586 {
587 return(ContainerBZRead<std::unordered_set<_valueT,_Hash,_Pred,_Alloc> >(bzf, hm));
588 }
589
590 //------------------------------------------------------------------------
593 //------------------------------------------------------------------------
594 template<typename _valueT, typename _Hash,
595 typename _Pred, typename _Alloc>
596 static int
597 BZWrite(BZFILE *bzf,
598 const std::unordered_set<_valueT,_Hash,_Pred,_Alloc> & hm)
599 {
600 return(ContainerBZWrite<std::unordered_set<_valueT,_Hash,_Pred,_Alloc> >(bzf, hm));
601 }
602
603 //------------------------------------------------------------------------
606 //------------------------------------------------------------------------
607 template<typename _keyT, typename _valueT, typename _Hash,
608 typename _Pred, typename _Alloc>
609 static int
610 BZRead(BZFILE *bzf,
611 std::unordered_multimap<_keyT,_valueT,_Hash,_Pred,_Alloc> & hm)
612 {
613 return(PairAssocContBZRead<std::unordered_multimap<_keyT,_valueT,_Hash,_Pred,_Alloc> >(bzf, hm));
614 }
615
616 //------------------------------------------------------------------------
619 //------------------------------------------------------------------------
620 template<typename _keyT, typename _valueT, typename _Hash,
621 typename _Pred, typename _Alloc>
622 static int
623 BZWrite(BZFILE *bzf,
624 const std::unordered_multimap<_keyT,_valueT,_Hash,_Pred,_Alloc> & hm)
625 {
626 return(ContainerBZWrite<std::unordered_multimap<_keyT,_valueT,_Hash,_Pred,_Alloc> >(bzf, hm));
627 }
628
629 //------------------------------------------------------------------------
632 //------------------------------------------------------------------------
633 template<typename _valueT, typename _Hash,
634 typename _Pred, typename _Alloc>
635 static int
636 BZRead(BZFILE *bzf,
637 std::unordered_multiset<_valueT,_Hash,_Pred,_Alloc> & hm)
638 {
639 return(ContainerBZRead<std::unordered_multiset<_valueT,_Hash,_Pred,_Alloc> >(bzf, hm));
640 }
641
642 //------------------------------------------------------------------------
645 //------------------------------------------------------------------------
646 template<typename _valueT, typename _Hash,
647 typename _Pred, typename _Alloc>
648 static int
649 BZWrite(BZFILE *bzf,
650 const std::unordered_multiset<_valueT,_Hash,_Pred,_Alloc> & hm)
651 {
652 return(ContainerBZWrite<std::unordered_multiset<_valueT,_Hash,_Pred,_Alloc> >(bzf, hm));
653 }
654
655 //------------------------------------------------------------------------
659 //------------------------------------------------------------------------
660 static int BZRead(BZFILE *bzf, std::monostate & sm)
661 {
662 return 0;
663 }
664
665 //------------------------------------------------------------------------
669 //------------------------------------------------------------------------
670 static int BZWrite(BZFILE *bzf, const std::monostate & sm)
671 {
672 return 0;
673 }
674
675 //------------------------------------------------------------------------
678 //------------------------------------------------------------------------
679 template <typename... Ts>
680 static int BZRead(BZFILE *bzf, std::variant<Ts...> & v)
681 {
682 int rc = -1;
683 uint64_t index = 0;
684 int bytesRead = BZRead(bzf, index);
685 if (bytesRead > 0) {
686 rc = bytesRead;
687 if (index < std::variant_size_v<std::variant<Ts...>>) {
688 v = VariantFromIndex<Ts...>(index);
689 std::visit([&] (auto && arg) { rc = BZRead(bzf, arg); }, v);
690 if (bytesRead >= 0) {
691 rc += bytesRead;
692 }
693 else {
694 rc = -1;
695 }
696 }
697 }
698 return rc;
699 }
700
701 //------------------------------------------------------------------------
703 //------------------------------------------------------------------------
704 template <typename... Ts>
705 static int BZWrite(BZFILE *bzf, const std::variant<Ts...> & v)
706 {
707 int rc = -1;
708 uint64_t index = v.index();
709 int bytesWritten = BZWrite(bzf, index);
710 if (bytesWritten > 0) {
711 rc = bytesWritten;
712 std::visit([&] (const auto & arg)
713 { bytesWritten = BZWrite(bzf, arg); }, v);
714 if (bytesWritten >= 0) {
715 rc += bytesWritten;
716 }
717 else {
718 rc = -1;
719 }
720 }
721 return rc;
722 }
723
724 //------------------------------------------------------------------------
727 //------------------------------------------------------------------------
728 template <typename ...Args>
729 static int BZReadV(BZFILE *bzf, Args & ...args)
730 {
731 int rv = 0;
732 // We need this lambda in order to have a means of short circuiting
733 // in our fold expression; we convert a failure to a boolean.
734 auto readOne = [&] (auto & arg) {
735 bool rc = true;
736 int bytesRead = BZRead(bzf, arg);
737 if (bytesRead > 0) {
738 rv += bytesRead;
739 }
740 else {
741 rv = -1;
742 rc = false;
743 }
744 return rc;
745 };
746 (readOne(args) && ...);
747 return rv;
748 }
749
750 //------------------------------------------------------------------------
753 //------------------------------------------------------------------------
754 template <typename ...Args>
755 static int BZWriteV(BZFILE *bzf, const Args & ...args)
756 {
757 ssize_t rv = 0;
758 // We need this lambda in order to have a means of short circuiting
759 // in our fold expression; we convert a failure to a boolean.
760 auto writeOne = [&] (auto & arg) {
761 bool rc = true;
762 int bytesWritten = BZWrite(bzf, arg);
763 if (bytesWritten > 0) {
764 rv += bytesWritten;
765 }
766 else {
767 rv = -1;
768 rc = false;
769 }
770 return rc;
771 };
772 (writeOne(args) && ...);
773 return rv;
774 }
775
776 private:
777 //------------------------------------------------------------------------
779 //------------------------------------------------------------------------
780 template <typename _inputIteratorT>
781 static int BZWrite(BZFILE *bzf, _inputIteratorT f, _inputIteratorT l)
782 {
783 int rc = 0;
784 if (bzf) {
785 for ( ; f != l; ++f) {
786 int bytesWritten = BZWrite(bzf, *f);
787 if (bytesWritten > 0) {
788 rc += bytesWritten;
789 }
790 else {
791 rc = -1;
792 break;
793 }
794 }
795 }
796 return(rc);
797 }
798
799 //------------------------------------------------------------------------
803 //------------------------------------------------------------------------
804 template <typename _containerT>
805 static int ContainerBZRead(BZFILE *bzf, _containerT & c)
806 {
807 if (! c.empty())
808 c.clear();
809 int rc = -1;
810 if (bzf) {
811 uint64_t numEntries;
812 int bytesRead = BZRead(bzf, numEntries);
813 if (bytesRead == sizeof(numEntries)) {
814 rc = bytesRead;
815 for (uint64_t i = 0; i < numEntries; ++i) {
816 typename _containerT::value_type val;
817 bytesRead = BZRead(bzf, val);
818 if (bytesRead > 0) {
819 rc += bytesRead;
820 c.insert(c.end(), std::move(val));
821 }
822 else {
823 rc = -1;
824 break;
825 }
826 }
827 }
828 }
829 return(rc);
830 }
831
832 //------------------------------------------------------------------------
836 //------------------------------------------------------------------------
837 template <typename _containerT>
838 static int ContainerBZWrite(BZFILE *bzf, const _containerT & c)
839 {
840 int rc = -1;
841 if (bzf) {
842 uint64_t numEntries = c.size();
843 uint64_t bytesWritten = BZWrite(bzf, numEntries);
844 if (bytesWritten == sizeof(numEntries)) {
845 rc = bytesWritten;
846 if (numEntries) {
847 bytesWritten =
849 c.begin(),
850 c.end());
851 if (bytesWritten > 0)
852 rc += bytesWritten;
853 else
854 rc = -1;
855 }
856 }
857 }
858 return(rc);
859 }
860
861 //------------------------------------------------------------------------
865 //------------------------------------------------------------------------
866 template <typename _containerT>
867 static int PairAssocContBZRead(BZFILE *bzf, _containerT & m)
868 {
869 int rc = -1;
870 if (! m.empty())
871 m.clear();
872 if (bzf) {
873 uint64_t numEntries;
874 int bytesRead = BZRead(bzf, numEntries);
875 if (bytesRead == sizeof(numEntries)) {
876 rc = bytesRead;
877 for (uint64_t i = 0; i < numEntries; ++i) {
878 typename _containerT::key_type key;
879 bytesRead = BZRead(bzf, key);
880 if (bytesRead > 0) {
881 rc += bytesRead;
882 typename _containerT::mapped_type val;
883 bytesRead = BZRead(bzf, val);
884 if (bytesRead > 0) {
885 rc += bytesRead;
886 m.insert(typename _containerT::value_type(std::move(key),
887 std::move(val)));
888 }
889 else {
890 rc = -1;
891 break;
892 }
893 }
894 else {
895 rc = -1;
896 break;
897 }
898 }
899 }
900 }
901 return(rc);
902 }
903
904 };
905
906} // namespace Dwm
907
908#endif // _DWMBZ2IO_HH_
909
910//---------------------------- emacs settings -----------------------------
911// Local Variables:
912// mode: C++
913// tab-width: 2
914// indent-tabs-mode: nil
915// c-basic-offset: 2
916// End:
917//-------------------------------------------------------------------------
Dwm::HasBZRead and Dwm::HasBZWrite concepts. Dwm::BZ2Readable, Dwm::BZ2Writable and Dwm::BZ2IOCapable...
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 DwmBZ2IO.hh:82
static int BZWrite(BZFILE *bzf, const std::unordered_set< _valueT, _Hash, _Pred, _Alloc > &hm)
Writes an unordered_set to a BZFILE pointer.
Definition DwmBZ2IO.hh:597
static int BZWrite(BZFILE *bzf, const std::unordered_map< _keyT, _valueT, _Hash, _Pred, _Alloc > &hm)
Writes an unordered_map to a BZFILE pointer.
Definition DwmBZ2IO.hh:571
static int BZRead(BZFILE *bzf, int64_t &val)
Reads val from bzf, in network byte order (MSB first).
static int BZRead(BZFILE *bzf, std::unordered_multiset< _valueT, _Hash, _Pred, _Alloc > &hm)
Reads an unordered_multiset from a BZFILE pointer.
Definition DwmBZ2IO.hh:636
static int BZWrite(BZFILE *bzf, const BZ2Writable &val)
Wrapper function to write a BZ2Writable object to a BZFILE pointer.
Definition DwmBZ2IO.hh:259
static int BZWrite(BZFILE *bzf, int32_t val)
Writes val to bzf, in network byte order (MSB first).
static int BZRead(BZFILE *bzf, bool &b)
Reads b from bzf.
static int BZRead(BZFILE *bzf, HasBZRead auto &val)
Reads val from bzf, where val meets the requirements of the HasBZRead concept.
Definition DwmBZ2IO.hh:253
static int BZWrite(BZFILE *bzf, const HasBZWrite auto &val)
Writes val to bzf, where val meets the requirements of the HasBZWrite concept.
Definition DwmBZ2IO.hh:266
static int BZRead(BZFILE *bzf, uint64_t &val)
Reads val from bzf, in network byte order (MSB first).
static int BZRead(BZFILE *bzf, int32_t &val)
Reads val from bzf, in network byte order (MSB first).
static int BZRead(BZFILE *bzf, std::unordered_map< _keyT, _valueT, _Hash, _Pred, _Alloc > &hm)
Reads an unordered_map from a BZFILE pointer.
Definition DwmBZ2IO.hh:558
static int BZRead(BZFILE *bzf, int16_t &val)
Reads val from bzf, in network byte order (MSB first).
static int BZWrite(BZFILE *bzf, const std::set< _valueT, _Compare, _Alloc > &l)
Writes a set<_valueT> to a BZFILE pointer.
Definition DwmBZ2IO.hh:479
static int BZWrite(BZFILE *bzf, const std::deque< _valueT, _Alloc > &d)
Writes a deque<_valueT> to a BZFILE pointer.
Definition DwmBZ2IO.hh:439
static int BZWrite(BZFILE *bzf, const std::multiset< _valueT, _Compare, _Alloc > &l)
Writes a multiset<_valueT> to a BZFILE pointer.
Definition DwmBZ2IO.hh:500
static int BZWrite(BZFILE *bzf, const std::pair< _firstT, _secondT > &p)
Writes a pair<_firstT,_secondT> to a BZFILE pointer.
Definition DwmBZ2IO.hh:296
static int BZWrite(BZFILE *bzf, float val)
Writes val tp bzf, in IEEE format (see RFC 1832 and/or ANSI/IEEE Standard 754-1985).
static int BZRead(BZFILE *bzf, std::string &s)
Reads s from bzf.
static int BZWrite(BZFILE *bzf, const std::vector< _valueT, _Alloc > &v)
Writes a vector<_valueT> to a BZFILE pointer.
Definition DwmBZ2IO.hh:419
static int BZRead(BZFILE *bzf, double &val)
Reads val from bzf, in IEEE format (see RFC 1832 and/or ANSI/IEEE Standard 754-1985).
static int BZRead(BZFILE *bzf, std::pair< _firstT, _secondT > &p)
Reads a pair<_firstT,_secondT> from a BZFILE pointer.
Definition DwmBZ2IO.hh:274
static int BZWrite(BZFILE *bzf, uint16_t val)
Writes val to bzf, in network byte order (MSB first).
static int BZRead(BZFILE *bzf, char &c)
Reads from bzf.
static int BZRead(BZFILE *bzf, std::multiset< _valueT, _Compare, _Alloc > &l)
Reads a multiset<_valueT> from a BZFILE pointer.
Definition DwmBZ2IO.hh:489
static int BZWrite(BZFILE *bzf, uint32_t val)
Writes val to bzf, in network byte order (MSB first).
static int BZWrite(BZFILE *bzf, const std::multimap< _keyT, _valueT, _Compare, _Alloc > &m)
Writes a multimap<_keyT,_valueT> to a BZFILE pointer.
Definition DwmBZ2IO.hh:357
static int BZWrite(BZFILE *bzf, const double &val)
Writes val tp bzf, in IEEE format (see RFC 1832 and/or ANSI/IEEE Standard 754-1985).
static int BZWrite(BZFILE *bzf, char c)
Writes c to bzf.
static int BZWrite(BZFILE *bzf, int16_t val)
Writes val to bzf, in network byte order (MSB first).
static int BZWrite(BZFILE *bzf, const uint64_t &val)
Writes val to bzf, in network byte order (MSB first).
static int BZRead(BZFILE *bzf, std::vector< _valueT, _Alloc > &v)
Reads a vector<_valueT> from a BZFILE pointer.
Definition DwmBZ2IO.hh:409
static int BZWrite(BZFILE *bzf, const std::list< _valueT, _Alloc > &l)
Writes a list<_valueT> to a BZFILE pointer.
Definition DwmBZ2IO.hh:459
static int BZWriteV(BZFILE *bzf, const Args &...args)
Writes args to bzf.
Definition DwmBZ2IO.hh:755
static int BZRead(BZFILE *bzf, std::deque< _valueT, _Alloc > &d)
Reads a deque<_valueT> from a BZFILE pointer.
Definition DwmBZ2IO.hh:429
static int BZRead(BZFILE *bzf, float &val)
Reads val from bzf, in IEEE format (see RFC 1832 and/or ANSI/IEEE Standard 754-1985).
static int BZRead(BZFILE *bzf, std::array< _valueT, N > &a)
Reads an array<_valueT,N> from a BZFILE pointer.
Definition DwmBZ2IO.hh:367
static int BZRead(BZFILE *bzf, uint16_t &val)
Reads val from bzf, in network byte order (MSB first).
static int BZWrite(BZFILE *bzf, const std::unordered_multimap< _keyT, _valueT, _Hash, _Pred, _Alloc > &hm)
Writes an unordered_multimap to a BZFILE pointer.
Definition DwmBZ2IO.hh:623
static int BZRead(BZFILE *bzf, uint8_t &c)
Reads c from bzf.
static int BZRead(BZFILE *bzf, std::unordered_multimap< _keyT, _valueT, _Hash, _Pred, _Alloc > &hm)
Reads an unordered_multimap from a BZFILE pointer.
Definition DwmBZ2IO.hh:610
static int BZWrite(BZFILE *bzf, const std::monostate &sm)
Just a dummy helper function for std::variant instances that hold a std::monostate.
Definition DwmBZ2IO.hh:670
static int BZWrite(BZFILE *bzf, uint8_t c)
Writes c to bzf.
static int BZWrite(BZFILE *bzf, const std::string &s)
Writes s to bzf.
static int BZRead(BZFILE *bzf, std::list< _valueT, _Alloc > &l)
Reads a list<_valueT> from a BZFILE pointer.
Definition DwmBZ2IO.hh:449
static int BZReadV(BZFILE *bzf, Args &...args)
Reads args from bzf.
Definition DwmBZ2IO.hh:729
static int BZRead(BZFILE *bzf, std::monostate &sm)
Just a dummy helper function for std::variant instances that hold a std::monostate.
Definition DwmBZ2IO.hh:660
static int BZRead(BZFILE *bzf, std::map< _keyT, _valueT, _Compare, _Alloc > &m)
Reads a map<_keyT,_valueT> from a BZFILE pointer.
Definition DwmBZ2IO.hh:321
static int BZRead(BZFILE *bzf, std::unordered_set< _valueT, _Hash, _Pred, _Alloc > &hm)
Reads an unordered_set from a BZFILE pointer.
Definition DwmBZ2IO.hh:584
static int BZWrite(BZFILE *bzf, const int64_t &val)
Writes val to bzf, in network byte order (MSB first).
static int BZRead(BZFILE *bzf, std::set< _valueT, _Compare, _Alloc > &l)
Reads a set<_valueT> from a BZFILE pointer.
Definition DwmBZ2IO.hh:469
static int BZWrite(BZFILE *bzf, bool b)
Writes b to bzf.
static int BZRead(BZFILE *bzf, uint32_t &val)
Reads val from bzf, in network byte order (MSB first).
static int BZRead(BZFILE *bzf, std::multimap< _keyT, _valueT, _Compare, _Alloc > &m)
Reads a multimap<_keyT,_valueT> from a BZFILE pointer.
Definition DwmBZ2IO.hh:345
static int BZWrite(BZFILE *bzf, const std::array< _valueT, N > &a)
Writes an array<_valueT,N> to a BZFILE pointer.
Definition DwmBZ2IO.hh:388
static int BZRead(BZFILE *bzf, BZ2Readable &val)
Wrapper function to read a BZ2Readable object from a BZFILE pointer.
Definition DwmBZ2IO.hh:246
static int BZWrite(BZFILE *bzf, const std::map< _keyT, _valueT, _Compare, _Alloc > &m)
Writes a map<_keyT,_valueT> to a BZFILE pointer.
Definition DwmBZ2IO.hh:333
static int BZWrite(BZFILE *bzf, const std::unordered_multiset< _valueT, _Hash, _Pred, _Alloc > &hm)
Writes an unordered_multiset to a BZFILE pointer.
Definition DwmBZ2IO.hh:649
static int BZRead(BZFILE *bzf, std::variant< Ts... > &v)
Reads a variant from a BZFILE.
Definition DwmBZ2IO.hh:680
This class is a pure virtual class, defining an interface for classes that can read their contents fr...
Definition DwmBZ2IOCapable.hh:79
virtual int BZRead(BZFILE *bzf)=0
Read from a BZFILE pointer.
This class is a pure virtual class, defining an interface for classes that can write their contents t...
Definition DwmBZ2IOCapable.hh:95
virtual int BZWrite(BZFILE *bzf) const =0
Write to a BZFILE pointer.
T has a BZRead(BZFILE *) member that returns int (return value of BZ2_bzread() on success,...
Definition DwmBZ2IOCapable.hh:61
T has a BZWrite(BZFILE *) const member that returns int (return value of BZ2_bzwrite() on success,...
Definition DwmBZ2IOCapable.hh:70