libDwm-0.9.45
DwmDescriptorIO.hh
Go to the documentation of this file.
1//===========================================================================
2// @(#) $DwmPath$
3//===========================================================================
4// Copyright (c) Daniel W. McRobb 2004-2007, 2016, 2017, 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 _DWMDESCRIPTORIO_HH_
43#define _DWMDESCRIPTORIO_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"
64
65namespace Dwm {
66
67 //--------------------------------------------------------------------------
82 //--------------------------------------------------------------------------
84 {
85 public:
86 //------------------------------------------------------------------------
89 //------------------------------------------------------------------------
90 static ssize_t Read(int fd, char & c);
91
92 //------------------------------------------------------------------------
95 //------------------------------------------------------------------------
96 static ssize_t Write(int fd, char c);
97
98 //------------------------------------------------------------------------
101 //------------------------------------------------------------------------
102 static ssize_t Read(int fd, uint8_t & c);
103
104 //------------------------------------------------------------------------
107 //------------------------------------------------------------------------
108 static ssize_t Write(int fd, uint8_t c);
109
110 //------------------------------------------------------------------------
113 //------------------------------------------------------------------------
114 static ssize_t Read(int fd, bool & b);
115
116 //------------------------------------------------------------------------
119 //------------------------------------------------------------------------
120 static ssize_t Write(int fd, bool b);
121
122 //------------------------------------------------------------------------
125 //------------------------------------------------------------------------
126 static ssize_t Read(int fd, int16_t & val);
127
128 //------------------------------------------------------------------------
132 //------------------------------------------------------------------------
133 static ssize_t Write(int fd, int16_t val);
134
135 //------------------------------------------------------------------------
138 //------------------------------------------------------------------------
139 static ssize_t Read(int fd, uint16_t & val);
140
141 //------------------------------------------------------------------------
145 //------------------------------------------------------------------------
146 static ssize_t Write(int fd, uint16_t val);
147
148 //------------------------------------------------------------------------
151 //------------------------------------------------------------------------
152 static ssize_t Read(int fd, int32_t & val);
153
154 //------------------------------------------------------------------------
158 //------------------------------------------------------------------------
159 static ssize_t Write(int fd, int32_t val);
160
161 //------------------------------------------------------------------------
164 //------------------------------------------------------------------------
165 static ssize_t Read(int fd, uint32_t & val);
166
167 //------------------------------------------------------------------------
171 //------------------------------------------------------------------------
172 static ssize_t Write(int fd, uint32_t val);
173
174 //------------------------------------------------------------------------
177 //------------------------------------------------------------------------
178 static ssize_t Read(int fd, int64_t & val);
179
180 //------------------------------------------------------------------------
184 //------------------------------------------------------------------------
185 static ssize_t Write(int fd, const int64_t & val);
186
187 //------------------------------------------------------------------------
190 //------------------------------------------------------------------------
191 static ssize_t Read(int fd, uint64_t & val);
192
193 //------------------------------------------------------------------------
197 //------------------------------------------------------------------------
198 static ssize_t Write(int fd, const uint64_t & val);
199
200 //------------------------------------------------------------------------
204 //------------------------------------------------------------------------
205 static ssize_t Read(int fd, float & val);
206
207 //------------------------------------------------------------------------
211 //------------------------------------------------------------------------
212 static ssize_t Write(int fd, float val);
213
214 //------------------------------------------------------------------------
218 //------------------------------------------------------------------------
219 static ssize_t Read(int fd, double & val);
220
221 //------------------------------------------------------------------------
225 //------------------------------------------------------------------------
226 static ssize_t Write(int fd, const double & val);
227
228 //------------------------------------------------------------------------
233 //------------------------------------------------------------------------
234 static ssize_t Read(int fd, std::string & s);
235
236 //------------------------------------------------------------------------
242 //------------------------------------------------------------------------
243 static ssize_t Write(int fd, const std::string & s);
244
245 //------------------------------------------------------------------------
248 //------------------------------------------------------------------------
249 static ssize_t Read(int fd, DescriptorReadable & val)
250 { return(val.Read(fd)); }
251
252 //------------------------------------------------------------------------
255 //------------------------------------------------------------------------
256 static ssize_t Read(int fd, HasDescriptorRead auto & val)
257 { return val.Read(fd); }
258
259 //------------------------------------------------------------------------
262 //------------------------------------------------------------------------
263 static ssize_t Write(int fd, const DescriptorWritable & val)
264 { return(val.Write(fd)); }
265
266 //------------------------------------------------------------------------
269 //------------------------------------------------------------------------
270 static ssize_t Write(int fd, const HasDescriptorWrite auto & val)
271 { return val.Write(fd); }
272
273 //------------------------------------------------------------------------
276 //------------------------------------------------------------------------
277 template <typename _firstT, typename _secondT>
278 static ssize_t Read(int fd, std::pair<_firstT, _secondT> & p)
279 {
280 ssize_t rc = -1;
281 if (fd >= 0) {
282 ssize_t bytesRead = Read(fd, p.first);
283 if (bytesRead > 0) {
284 rc = bytesRead;
285 bytesRead = Read(fd, p.second);
286 if (bytesRead > 0)
287 rc += bytesRead;
288 else
289 rc = -1;
290 }
291 }
292 return(rc);
293 }
294
295 //------------------------------------------------------------------------
298 //------------------------------------------------------------------------
299 template <typename _firstT, typename _secondT>
300 static ssize_t Write(int fd, const std::pair<_firstT,_secondT> & p)
301 {
302 ssize_t rc = -1;
303 if (fd >= 0) {
304 ssize_t bytesWritten = Write(fd, p.first);
305 if (bytesWritten > 0) {
306 rc = bytesWritten;
307 bytesWritten = Write(fd, p.second);
308 if (bytesWritten > 0) {
309 rc += bytesWritten;
310 }
311 else {
312 rc = -1;
313 }
314 }
315 }
316 return(rc);
317 }
318
319 //------------------------------------------------------------------------
322 //------------------------------------------------------------------------
323 template <typename _keyT, typename _valueT,
324 typename _Compare, typename _Alloc>
325 static ssize_t Read(int fd, std::map<_keyT, _valueT, _Compare, _Alloc> & m)
326 {
327 return(PairAssocContRead<std::map<_keyT, _valueT, _Compare, _Alloc> >(fd, m));
328 }
329
330 //------------------------------------------------------------------------
333 //------------------------------------------------------------------------
334 template<typename _keyT, typename _valueT,
335 typename _Compare, typename _Alloc>
336 static ssize_t
337 Write(int fd, const std::map<_keyT,_valueT,_Compare,_Alloc> & m)
338 {
339 return(ContainerWrite<std::map<_keyT,_valueT,_Compare,_Alloc> >(fd, m));
340 }
341
342 //------------------------------------------------------------------------
345 //------------------------------------------------------------------------
346 template <typename _keyT, typename _valueT,
347 typename _Compare, typename _Alloc>
348 static ssize_t
349 Read(int fd, std::multimap<_keyT, _valueT, _Compare, _Alloc> & m)
350 {
351 return(PairAssocContRead<std::multimap<_keyT,_valueT,_Compare,_Alloc> >(fd, m));
352 }
353
354 //------------------------------------------------------------------------
357 //------------------------------------------------------------------------
358 template <typename _keyT, typename _valueT,
359 typename _Compare, typename _Alloc>
360 static ssize_t
361 Write(int fd, const std::multimap<_keyT,_valueT, _Compare, _Alloc> & m)
362 {
363 return(ContainerWrite<std::multimap<_keyT,_valueT,_Compare,_Alloc> >(fd, m));
364 }
365
366 //------------------------------------------------------------------------
369 //------------------------------------------------------------------------
370 template <typename _valueT, std::size_t N>
371 static ssize_t Read(int fd, std::array<_valueT, N> & a)
372 {
373 ssize_t rc = 0;
374 for (std::size_t i = 0; i < N; ++i) {
375 ssize_t bytesRead = Read(fd, a[i]);
376 if (bytesRead > 0) {
377 rc += bytesRead;
378 }
379 else {
380 rc = -1;
381 break;
382 }
383 }
384 return rc;
385 }
386
387 //------------------------------------------------------------------------
390 //------------------------------------------------------------------------
391 template <typename _valueT, std::size_t N>
392 static ssize_t Write(int fd, const std::array<_valueT, N> & a)
393 {
394 ssize_t rc = 0;
395 for (std::size_t i = 0; i < N; ++i) {
396 ssize_t bytesWritten = Write(fd, a[i]);
397 if (bytesWritten > 0) {
398 rc += bytesWritten;
399 }
400 else {
401 rc = -1;
402 break;
403 }
404 }
405 return rc;
406 }
407
408 //------------------------------------------------------------------------
411 //------------------------------------------------------------------------
412 template <typename _valueT, typename _Alloc>
413 static ssize_t Read(int fd, std::vector<_valueT, _Alloc> & v)
414 {
415 return(ContainerRead<std::vector<_valueT, _Alloc> >(fd, v));
416 }
417
418 //------------------------------------------------------------------------
421 //------------------------------------------------------------------------
422 template <typename _valueT, typename _Alloc>
423 static ssize_t Write(int fd, const std::vector<_valueT, _Alloc> & v)
424 {
425 return(ContainerWrite<std::vector<_valueT, _Alloc> >(fd, v));
426 }
427
428 //------------------------------------------------------------------------
431 //------------------------------------------------------------------------
432 template <typename _valueT, typename _Alloc>
433 static ssize_t Read(int fd, std::deque<_valueT, _Alloc> & d)
434 {
435 return(ContainerRead<std::deque<_valueT, _Alloc> >(fd, d));
436 }
437
438 //------------------------------------------------------------------------
441 //------------------------------------------------------------------------
442 template <typename _valueT, typename _Alloc>
443 static ssize_t Write(int fd, const std::deque<_valueT, _Alloc> & d)
444 {
445 return(ContainerWrite<std::deque<_valueT, _Alloc> >(fd, d));
446 }
447
448 //------------------------------------------------------------------------
451 //------------------------------------------------------------------------
452 template <typename _valueT, typename _Alloc>
453 static ssize_t Read(int fd, std::list<_valueT, _Alloc> & l)
454 {
455 return(ContainerRead<std::list<_valueT, _Alloc> >(fd, l));
456 }
457
458 //------------------------------------------------------------------------
461 //------------------------------------------------------------------------
462 template <typename _valueT, typename _Alloc>
463 static ssize_t Write(int fd, const std::list<_valueT, _Alloc> & l)
464 {
465 return(ContainerWrite<std::list<_valueT, _Alloc> >(fd, l));
466 }
467
468 //------------------------------------------------------------------------
471 //------------------------------------------------------------------------
472 template <typename _valueT, typename _Compare, typename _Alloc>
473 static ssize_t Read(int fd, std::set<_valueT, _Compare, _Alloc> & l)
474 {
475 return(ContainerRead<std::set<_valueT, _Compare, _Alloc> >(fd, l));
476 }
477
478 //------------------------------------------------------------------------
481 //------------------------------------------------------------------------
482 template <typename _valueT, typename _Compare, typename _Alloc>
483 static ssize_t Write(int fd, const std::set<_valueT, _Compare, _Alloc> & l)
484 {
485 return(ContainerWrite<std::set<_valueT, _Compare, _Alloc> >(fd, l));
486 }
487
488 //------------------------------------------------------------------------
491 //------------------------------------------------------------------------
492 template <typename _valueT, typename _Compare, typename _Alloc>
493 static ssize_t Read(int fd, std::multiset<_valueT, _Compare, _Alloc> & l)
494 {
495 return(ContainerRead<std::multiset<_valueT, _Compare, _Alloc> >(fd, l));
496 }
497
498 //------------------------------------------------------------------------
501 //------------------------------------------------------------------------
502 template <typename _valueT, typename _Compare, typename _Alloc>
503 static ssize_t
504 Write(int fd, const std::multiset<_valueT, _Compare, _Alloc> & l)
505 {
506 return(ContainerWrite<std::multiset<_valueT, _Compare, _Alloc> >(fd, l));
507 }
508
509 //------------------------------------------------------------------------
512 //------------------------------------------------------------------------
513 template <typename... Args>
514 static ssize_t Read(int fd, std::tuple<Args...> & t)
515 {
516 return(std::apply([&fd](auto&&...args)
517 {
518 ssize_t rc = 0;
519 auto read_tuple_mem = [&fd,&rc](auto&& x) {
520 int bytesRead = Read(fd, x);
521 if (bytesRead > 0) { rc += bytesRead; return true; }
522 else { return false; }
523 };
524 if ((read_tuple_mem(args) && ...)) {
525 return rc;
526 }
527 else {
528 return (ssize_t)(-1);
529 }
530 }, t));
531 }
532
533 //------------------------------------------------------------------------
536 //------------------------------------------------------------------------
537 template <typename... Args>
538 static ssize_t Write(int fd, const std::tuple<Args...> & t)
539 {
540 return(std::apply([&fd](auto&&...args)
541 {
542 ssize_t rc = 0;
543 auto read_tuple_mem = [&fd,&rc](auto&& x) {
544 int bytesWritten = Write(fd, x);
545 if (bytesWritten > 0) { rc += bytesWritten; return true; }
546 else { return false; }
547 };
548 if ((read_tuple_mem(args) && ...)) {
549 return rc;
550 }
551 else {
552 return (ssize_t)(-1);
553 }
554 }, t));
555 }
556
557 //------------------------------------------------------------------------
560 //------------------------------------------------------------------------
561 template <typename _keyT, typename _valueT,
562 typename _Hash, typename _Pred, typename _Alloc>
563 static ssize_t
564 Read(int fd, std::unordered_map<_keyT, _valueT, _Hash, _Pred, _Alloc> & m)
565 {
566 return(PairAssocContRead<std::unordered_map<_keyT, _valueT, _Hash, _Pred, _Alloc> >(fd, m));
567 }
568
569 //------------------------------------------------------------------------
572 //------------------------------------------------------------------------
573 template<typename _keyT, typename _valueT,
574 typename _Hash, typename _Pred, typename _Alloc>
575 static ssize_t
576 Write(int fd,
577 const std::unordered_map<_keyT,_valueT,_Hash,_Pred,_Alloc> & m)
578 {
579 return(ContainerWrite<std::unordered_map<_keyT,_valueT,_Hash,_Pred,_Alloc> >(fd, m));
580 }
581
582 //------------------------------------------------------------------------
585 //------------------------------------------------------------------------
586 template <typename _keyT, typename _valueT,
587 typename _Hash, typename _Pred, typename _Alloc>
588 static ssize_t
589 Read(int fd, std::unordered_multimap<_keyT, _valueT, _Hash, _Pred, _Alloc> & m)
590 {
591 return(PairAssocContRead<std::unordered_multimap<_keyT, _valueT, _Hash, _Pred, _Alloc> >(fd, m));
592 }
593
594 //------------------------------------------------------------------------
597 //------------------------------------------------------------------------
598 template<typename _keyT, typename _valueT,
599 typename _Hash, typename _Pred, typename _Alloc>
600 static ssize_t
601 Write(int fd, const std::unordered_multimap<_keyT,_valueT,_Hash,_Pred,_Alloc> & m)
602 {
603 return(ContainerWrite<std::unordered_multimap<_keyT,_valueT,_Hash,_Pred,_Alloc> >(fd, m));
604 }
605
606 //------------------------------------------------------------------------
609 //------------------------------------------------------------------------
610 template <typename _valueT, typename _Hash,
611 typename _Pred, typename _Alloc>
612 static ssize_t
613 Read(int fd, std::unordered_set<_valueT, _Hash, _Pred, _Alloc> & m)
614 {
615 return(ContainerRead<std::unordered_set<_valueT, _Hash, _Pred, _Alloc> >(fd, m));
616 }
617
618 //------------------------------------------------------------------------
621 //------------------------------------------------------------------------
622 template<typename _valueT, typename _Hash,
623 typename _Pred, typename _Alloc>
624 static ssize_t
625 Write(int fd, const std::unordered_set<_valueT,_Hash,_Pred,_Alloc> & m)
626 {
627 return(ContainerWrite<std::unordered_set<_valueT,_Hash,_Pred,_Alloc> >(fd, m));
628 }
629
630 //------------------------------------------------------------------------
633 //------------------------------------------------------------------------
634 template <typename _valueT, typename _Hash,
635 typename _Pred, typename _Alloc>
636 static ssize_t
637 Read(int fd, std::unordered_multiset<_valueT, _Hash, _Pred, _Alloc> & m)
638 {
639 return(ContainerRead<std::unordered_multiset<_valueT, _Hash, _Pred, _Alloc> >(fd, m));
640 }
641
642 //------------------------------------------------------------------------
645 //------------------------------------------------------------------------
646 template<typename _valueT, typename _Hash,
647 typename _Pred, typename _Alloc>
648 static ssize_t
649 Write(int fd, const std::unordered_multiset<_valueT,_Hash,_Pred,_Alloc> & m)
650 {
651 return(ContainerWrite<std::unordered_multiset<_valueT,_Hash,_Pred,_Alloc> >(fd, m));
652 }
653
654 //------------------------------------------------------------------------
658 //------------------------------------------------------------------------
659 static ssize_t Read(int fd, std::monostate & sm)
660 {
661 return 0;
662 }
663
664 //------------------------------------------------------------------------
668 //------------------------------------------------------------------------
669 static ssize_t Write(int fd, const std::monostate & sm)
670 {
671 return 0;
672 }
673
674 //------------------------------------------------------------------------
677 //------------------------------------------------------------------------
678 template <typename... Ts>
679 static ssize_t Read(int fd, std::variant<Ts...> & v)
680 {
681 ssize_t rc = -1;
682 uint64_t index = 0;
683 ssize_t bytesRead = Read(fd, index);
684 if (bytesRead > 0) {
685 rc = bytesRead;
686 if (index < std::variant_size_v<std::variant<Ts...>>) {
687 v = VariantFromIndex<Ts...>(index);
688 std::visit([&] (auto && arg) { bytesRead = Read(fd, arg); }, v);
689 if (bytesRead >= 0) {
690 rc += bytesRead;
691 }
692 else {
693 rc = -1;
694 }
695 }
696 else {
697 rc = -1;
698 }
699 }
700 return rc;
701 }
702
703 //------------------------------------------------------------------------
706 //------------------------------------------------------------------------
707 template <typename... Ts>
708 static ssize_t Write(int fd, const std::variant<Ts...> & v)
709 {
710 ssize_t rc = -1;
711 uint64_t index = v.index();
712 ssize_t bytesWritten = Write(fd, index);
713 if (bytesWritten > 0) {
714 rc = bytesWritten;
715 std::visit([&fd,&bytesWritten] (const auto & arg)
716 { bytesWritten = Write(fd, arg); }, v);
717 if (bytesWritten >= 0) {
718 rc += bytesWritten;
719 }
720 else {
721 rc = -1;
722 }
723 }
724 return rc;
725 }
726
727 //------------------------------------------------------------------------
730 //------------------------------------------------------------------------
731 template <typename ...Args>
732 static ssize_t ReadV(int fd, Args & ...args)
733 {
734 ssize_t rv = 0;
735 auto readOne = [&] (auto & arg) {
736 bool rc = true;
737 int bytesRead = Read(fd,arg);
738 if (bytesRead > 0) {
739 rv += bytesRead;
740 }
741 else {
742 rv = -1;
743 rc = false;
744 }
745 return rc;
746 };
747 (readOne(args) && ...);
748 return rv;
749 }
750
751 //------------------------------------------------------------------------
754 //------------------------------------------------------------------------
755 template <typename ...Args>
756 static ssize_t WriteV(int fd, const Args & ...args)
757 {
758 ssize_t rv = 0;
759 auto writeOne = [&] (auto & arg) {
760 bool rc = true;
761 int bytesWritten = Write(fd, arg);
762 if (bytesWritten > 0) {
763 rv += bytesWritten;
764 }
765 else {
766 rv = -1;
767 rc = false;
768 }
769 return rc;
770 };
771 (writeOne(args) && ...);
772 return rv;
773 }
774
775 //------------------------------------------------------------------------
777 //------------------------------------------------------------------------
778 static ssize_t Read(int fd, void *buf, size_t buflen);
779
780 //------------------------------------------------------------------------
782 //------------------------------------------------------------------------
783 static ssize_t Write(int fd, const void *buf, size_t buflen);
784
785 private:
786 //------------------------------------------------------------------------
788 //------------------------------------------------------------------------
789 template <typename _inputIteratorT>
790 static ssize_t Write(int fd, _inputIteratorT f, _inputIteratorT l)
791 {
792 ssize_t rc = 0;
793 if (fd >= 0) {
794 for ( ; f != l; ++f) {
795 ssize_t bytesWritten = Write(fd, *f);
796 if (bytesWritten > 0) {
797 rc += bytesWritten;
798 }
799 else {
800 rc = -1;
801 break;
802 }
803 }
804 }
805 return(rc);
806 }
807
808 //------------------------------------------------------------------------
812 //------------------------------------------------------------------------
813 template <typename _containerT>
814 static ssize_t ContainerRead(int fd, _containerT & c)
815 {
816 if (! c.empty())
817 c.clear();
818 ssize_t rc = -1;
819 if (fd >= 0) {
820 uint64_t numEntries;
821 ssize_t bytesRead = Read(fd, numEntries);
822 if (bytesRead == sizeof(numEntries)) {
823 rc = bytesRead;
824 for (uint64_t i = 0; i < numEntries; ++i) {
825 typename _containerT::value_type val;
826 bytesRead = Read(fd, val);
827 if (bytesRead > 0) {
828 rc += bytesRead;
829 c.insert(c.end(), std::move(val));
830 }
831 else {
832 rc = -1;
833 break;
834 }
835 }
836 }
837 }
838 return(rc);
839 }
840
841 //------------------------------------------------------------------------
845 //------------------------------------------------------------------------
846 template <typename _containerT>
847 static ssize_t ContainerWrite(int fd, const _containerT & c)
848 {
849 ssize_t rc = -1;
850 if (fd >= 0) {
851 uint64_t numEntries = c.size();
852 uint64_t bytesWritten = Write(fd, numEntries);
853 if (bytesWritten == sizeof(numEntries)) {
854 rc = bytesWritten;
855 if (numEntries) {
856 bytesWritten =
858 c.begin(),
859 c.end());
860 if (bytesWritten > 0)
861 rc += bytesWritten;
862 else
863 rc = -1;
864 }
865 }
866 }
867 return(rc);
868 }
869
870 //------------------------------------------------------------------------
874 //------------------------------------------------------------------------
875 template <typename _containerT>
876 static ssize_t PairAssocContRead(int fd, _containerT & m)
877 {
878 ssize_t rc = -1;
879 if (! m.empty())
880 m.clear();
881 if (fd >= 0) {
882 uint64_t numEntries;
883 ssize_t bytesRead = Read(fd, numEntries);
884 if (bytesRead == sizeof(numEntries)) {
885 rc = bytesRead;
886 for (uint64_t i = 0; i < numEntries; ++i) {
887 typename _containerT::key_type key;
888 bytesRead = Read(fd, key);
889 if (bytesRead > 0) {
890 rc += bytesRead;
891 typename _containerT::mapped_type val;
892 bytesRead = Read(fd, val);
893 if (bytesRead > 0) {
894 rc += bytesRead;
895 m.insert(typename _containerT::value_type(std::move(key),
896 std::move(val)));
897 }
898 else {
899 rc = -1;
900 break;
901 }
902 }
903 else {
904 rc = -1;
905 break;
906 }
907 }
908 }
909 }
910 return(rc);
911 }
912
913 };
914
915
916} // namespace Dwm
917
918#endif // _DWMDESCRIPTORIO_HH_
919
920//---------------------------- emacs settings -----------------------------
921// Local Variables:
922// mode: C++
923// tab-width: 2
924// indent-tabs-mode: nil
925// c-basic-offset: 2
926// End:
927//-------------------------------------------------------------------------
Dwm::HasDescriptorRead and Dwm::HasDescriptorWrite concepts. Dwm::DescriptorReadable,...
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,...
Definition DwmDescriptorIO.hh:84
static ssize_t Write(int fd, const std::monostate &sm)
Just a dummy helper function for std::variant instances that hold a std::monostate.
Definition DwmDescriptorIO.hh:669
static ssize_t Write(int fd, const std::tuple< Args... > &t)
Writes a tuple to a file descriptor.
Definition DwmDescriptorIO.hh:538
static ssize_t Write(int fd, const double &val)
Writes val to fd, in IEEE format (see RFC 1832 and/or ANSI/IEEE Standard 754-1985).
static ssize_t Write(int fd, uint8_t c)
Writes c to fd.
static ssize_t Read(int fd, std::multiset< _valueT, _Compare, _Alloc > &l)
Reads a multiset<_valueT> from a file descriptor.
Definition DwmDescriptorIO.hh:493
static ssize_t Read(int fd, std::map< _keyT, _valueT, _Compare, _Alloc > &m)
Reads a map<_keyT,_valueT> from a file descriptor.
Definition DwmDescriptorIO.hh:325
static ssize_t Read(int fd, std::deque< _valueT, _Alloc > &d)
Reads a deque<_valueT> from a file descriptor.
Definition DwmDescriptorIO.hh:433
static ssize_t Read(int fd, uint64_t &val)
Reads val from fd, in network byte order (MSB first).
static ssize_t Read(int fd, std::unordered_multiset< _valueT, _Hash, _Pred, _Alloc > &m)
Reads an unordered_multiset<_valueT> from a file descriptor.
Definition DwmDescriptorIO.hh:637
static ssize_t Read(int fd, int16_t &val)
Reads val from fd, in network byte order (MSB first).
static ssize_t Read(int fd, bool &b)
Reads b from fd.
static ssize_t Read(int fd, uint8_t &c)
Reads c from fd.
static ssize_t Write(int fd, uint16_t val)
Writes val to fd, in network byte order (MSB first).
static ssize_t Read(int fd, std::monostate &sm)
Just a dummy helper function for std::variant instances that hold a std::monostate.
Definition DwmDescriptorIO.hh:659
static ssize_t Read(int fd, std::pair< _firstT, _secondT > &p)
Reads a pair<_firstT,_secondT> from a file descriptor.
Definition DwmDescriptorIO.hh:278
static ssize_t Write(int fd, const std::set< _valueT, _Compare, _Alloc > &l)
Writes a set<_valueT> to a file descriptor.
Definition DwmDescriptorIO.hh:483
static ssize_t Write(int fd, const int64_t &val)
Writes val to fd, in network byte order (MSB first).
static ssize_t Read(int fd, int32_t &val)
Reads val from fd, in network byte order (MSB first).
static ssize_t Write(int fd, const std::multimap< _keyT, _valueT, _Compare, _Alloc > &m)
Writes a multimap<_keyT,_valueT> to a file descriptor.
Definition DwmDescriptorIO.hh:361
static ssize_t Read(int fd, std::set< _valueT, _Compare, _Alloc > &l)
Reads a set<_valueT> from a file descriptor.
Definition DwmDescriptorIO.hh:473
static ssize_t Read(int fd, HasDescriptorRead auto &val)
Reads val from fd, where val meets the requirements of the HasDescriptorRead concept.
Definition DwmDescriptorIO.hh:256
static ssize_t Write(int fd, const std::unordered_set< _valueT, _Hash, _Pred, _Alloc > &m)
Writes an unordered_set<_valueT> to a file descriptor.
Definition DwmDescriptorIO.hh:625
static ssize_t Write(int fd, char c)
Writes c to fd.
static ssize_t Write(int fd, const DescriptorWritable &val)
Wrapper function to write a DescriptorWritable object to a descriptor.
Definition DwmDescriptorIO.hh:263
static ssize_t Write(int fd, const std::unordered_multimap< _keyT, _valueT, _Hash, _Pred, _Alloc > &m)
Writes an unordered_multimap<_keyT,_valueT> to a file descriptor.
Definition DwmDescriptorIO.hh:601
static ssize_t Write(int fd, bool b)
Writes b to fd.
static ssize_t Write(int fd, const uint64_t &val)
Writes val to fd, in network byte order (MSB first).
static ssize_t Read(int fd, char &c)
Reads c from fd.
static ssize_t Read(int fd, uint32_t &val)
Reads val from fd, in network byte order (MSB first).
static ssize_t Write(int fd, const std::array< _valueT, N > &a)
Writes an array<_valueT,N> to a file descriptor.
Definition DwmDescriptorIO.hh:392
static ssize_t Write(int fd, const HasDescriptorWrite auto &val)
Writes val to fd, where val meets the requirements of the HasDescriptorWrite concept.
Definition DwmDescriptorIO.hh:270
static ssize_t Write(int fd, const std::string &s)
Writes s to fd.
static ssize_t Read(int fd, std::array< _valueT, N > &a)
Read an array<_valueT,N> from a file descriptor.
Definition DwmDescriptorIO.hh:371
static ssize_t Write(int fd, const std::unordered_multiset< _valueT, _Hash, _Pred, _Alloc > &m)
Writes an unordered_multiset<_valueT> to a file descriptor.
Definition DwmDescriptorIO.hh:649
static ssize_t WriteV(int fd, const Args &...args)
Writes args to fd.
Definition DwmDescriptorIO.hh:756
static ssize_t Read(int fd, std::unordered_map< _keyT, _valueT, _Hash, _Pred, _Alloc > &m)
Reads an unordered_map<_keyT,_valueT> from a file descriptor.
Definition DwmDescriptorIO.hh:564
static ssize_t Write(int fd, int16_t val)
Writes val to fd, in network byte order (MSB first).
static ssize_t Read(int fd, DescriptorReadable &val)
Wrapper function to read a DescriptorReadable object from a descriptor.
Definition DwmDescriptorIO.hh:249
static ssize_t Write(int fd, const std::deque< _valueT, _Alloc > &d)
Writes a deque<_valueT> to a file descriptor.
Definition DwmDescriptorIO.hh:443
static ssize_t Write(int fd, float val)
Writes val to fd, in IEEE format (see RFC 1832 and/or ANSI/IEEE Standard 754-1985).
static ssize_t Write(int fd, const std::vector< _valueT, _Alloc > &v)
Writes a vector<_valueT> to a file descriptor.
Definition DwmDescriptorIO.hh:423
static ssize_t Write(int fd, uint32_t val)
Writes val to fd, in network byte order (MSB first).
static ssize_t ReadV(int fd, Args &...args)
Reads args from fd.
Definition DwmDescriptorIO.hh:732
static ssize_t Read(int fd, int64_t &val)
Reads val from fd, in network byte order (MSB first).
static ssize_t Write(int fd, const std::variant< Ts... > &v)
Writes a variant to a descriptor.
Definition DwmDescriptorIO.hh:708
static ssize_t Read(int fd, std::unordered_set< _valueT, _Hash, _Pred, _Alloc > &m)
Reads an unordered_set<_valueT> from a file descriptor.
Definition DwmDescriptorIO.hh:613
static ssize_t Read(int fd, std::list< _valueT, _Alloc > &l)
Reads a list<_valueT> from a file descriptor.
Definition DwmDescriptorIO.hh:453
static ssize_t Write(int fd, const std::unordered_map< _keyT, _valueT, _Hash, _Pred, _Alloc > &m)
Writes an unordered_map<_keyT,_valueT> to a file descriptor.
Definition DwmDescriptorIO.hh:576
static ssize_t Write(int fd, const std::multiset< _valueT, _Compare, _Alloc > &l)
Writes a multiset<_valueT> to a file descriptor.
Definition DwmDescriptorIO.hh:504
static ssize_t Write(int fd, const std::map< _keyT, _valueT, _Compare, _Alloc > &m)
Writes a map<_keyT,_valueT> to a file descriptor.
Definition DwmDescriptorIO.hh:337
static ssize_t Read(int fd, std::variant< Ts... > &v)
Reads a variant from a descriptor.
Definition DwmDescriptorIO.hh:679
static ssize_t Read(int fd, std::vector< _valueT, _Alloc > &v)
Reads a vector<_valueT> from a file descriptor.
Definition DwmDescriptorIO.hh:413
static ssize_t Read(int fd, std::multimap< _keyT, _valueT, _Compare, _Alloc > &m)
Reads a multimap<_keyT,_valueT> from a file descriptor.
Definition DwmDescriptorIO.hh:349
static ssize_t Read(int fd, float &val)
Reads val from fd, in IEEE format (see RFC 1832 and/or ANSI/IEEE Standard 754-1985).
static ssize_t Read(int fd, std::unordered_multimap< _keyT, _valueT, _Hash, _Pred, _Alloc > &m)
Reads an unordered_multimap<_keyT,_valueT> from a file descriptor.
Definition DwmDescriptorIO.hh:589
static ssize_t Read(int fd, double &val)
Reads val from fd, in IEEE format (see RFC 1832 and/or ANSI/IEEE Standard 754-1985).
static ssize_t Write(int fd, const std::pair< _firstT, _secondT > &p)
Writes a pair<_firstT,_secondT> to a file descriptor.
Definition DwmDescriptorIO.hh:300
static ssize_t Read(int fd, uint16_t &val)
Reads val from fd, in network byte order (MSB first).
static ssize_t Write(int fd, const std::list< _valueT, _Alloc > &l)
Writes a list<_valueT> to a file descriptor.
Definition DwmDescriptorIO.hh:463
static ssize_t Read(int fd, std::string &s)
Reads s from fd.
static ssize_t Write(int fd, int32_t val)
Writes val to fd, in network byte order (MSB first).
static ssize_t Read(int fd, std::tuple< Args... > &t)
Reads a tuple from a file descriptor.
Definition DwmDescriptorIO.hh:514
This class defines an interface for classes that can read their contents from a file descriptor.
Definition DwmDescriptorIOCapable.hh:78
virtual ssize_t Read(int fd)=0
Read from a file descriptor.
This class defines an interface for classes that can write their contents to a file descriptor.
Definition DwmDescriptorIOCapable.hh:97
virtual ssize_t Write(int fd) const =0
Write to a file descriptor.
T has a Read(int) member that returns ssize_t (number of bytes read on success, -1 on failure).
Definition DwmDescriptorIOCapable.hh:60
T has a Write(int fd) const member that returns ssize_t (number of bytes read on success,...
Definition DwmDescriptorIOCapable.hh:69