libDwm-0.9.45
DwmIOUtils.hh
Go to the documentation of this file.
1//===========================================================================
2// @(#) $DwmPath$
3//===========================================================================
4// Copyright (c) Daniel W. McRobb 2004, 2006, 2007, 2016, 2020
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 PURPOSE,
32// OR THAT THE USE OF THIS SOFTWARE WILL NOT INFRINGE ANY PATENT,
33// TRADEMARK OR OTHER RIGHTS.
34//===========================================================================
35
36//---------------------------------------------------------------------------
40//---------------------------------------------------------------------------
41
42#ifndef _DWMIOUTILS_HH_
43#define _DWMIOUTILS_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"
63
64namespace Dwm {
65
66 //--------------------------------------------------------------------------
77 //--------------------------------------------------------------------------
78 class IOUtils
79 {
80 public:
81 //------------------------------------------------------------------------
84 //------------------------------------------------------------------------
85 static uint64_t StreamedLength(char c)
86 {
87 return(sizeof(c));
88 }
89
90 //------------------------------------------------------------------------
93 //------------------------------------------------------------------------
94 static uint64_t StreamedLength(uint8_t c)
95 {
96 return(sizeof(c));
97 }
98
99 //------------------------------------------------------------------------
102 //------------------------------------------------------------------------
103 static uint64_t StreamedLength(bool b)
104 {
105 return(1);
106 }
107
108 //------------------------------------------------------------------------
111 //------------------------------------------------------------------------
112 static uint64_t StreamedLength(int16_t val)
113 {
114 return(sizeof(val));
115 }
116
117 //------------------------------------------------------------------------
120 //------------------------------------------------------------------------
121 static uint64_t StreamedLength(uint16_t val)
122 {
123 return(sizeof(val));
124 }
125
126 //------------------------------------------------------------------------
129 //------------------------------------------------------------------------
130 static uint64_t StreamedLength(int32_t val)
131 {
132 return(sizeof(val));
133 }
134
135 //------------------------------------------------------------------------
138 //------------------------------------------------------------------------
139 static uint64_t StreamedLength(uint32_t val)
140 {
141 return(sizeof(val));
142 }
143
144 //------------------------------------------------------------------------
147 //------------------------------------------------------------------------
148 static uint64_t StreamedLength(int64_t val)
149 {
150 return(sizeof(val));
151 }
152
153 //------------------------------------------------------------------------
156 //------------------------------------------------------------------------
157 static uint64_t StreamedLength(uint64_t val)
158 {
159 return(sizeof(val));
160 }
161
162 //------------------------------------------------------------------------
165 //------------------------------------------------------------------------
166 static uint64_t StreamedLength(float val)
167 {
168 return(4);
169 }
170
171 //------------------------------------------------------------------------
174 //------------------------------------------------------------------------
175 static uint64_t StreamedLength(double val)
176 {
177 return(8);
178 }
179
180 //------------------------------------------------------------------------
183 //------------------------------------------------------------------------
184 static uint64_t StreamedLength(const std::string & s)
185 {
186 return(sizeof(uint64_t) + s.size());
187 }
188
189 //------------------------------------------------------------------------
192 //------------------------------------------------------------------------
193 template <typename _firstT, typename _secondT>
194 static uint64_t StreamedLength(const std::pair<_firstT, _secondT> & p)
195 {
196 return(StreamedLength(p.first) + StreamedLength(p.second));
197 }
198
199 //------------------------------------------------------------------------
202 //------------------------------------------------------------------------
203 template <typename _keyT, typename _valueT,
204 typename _Compare, typename _Alloc>
205 static uint64_t
206 StreamedLength(const std::map<_keyT, _valueT, _Compare, _Alloc> & m)
207 {
208 return(ContainerStreamedLength<std::map<_keyT, _valueT, _Compare, _Alloc> >(m));
209 }
210
211 //------------------------------------------------------------------------
214 //------------------------------------------------------------------------
215 template <typename _keyT, typename _valueT,
216 typename _Compare, typename _Alloc>
217 static uint64_t
218 StreamedLength(const std::multimap<_keyT,_valueT,_Compare,_Alloc> & m)
219 {
220 return(ContainerStreamedLength<std::multimap<_keyT,_valueT,_Compare,_Alloc> >(m));
221 }
222
223 //------------------------------------------------------------------------
226 //------------------------------------------------------------------------
227 template <typename _valueT, size_t N>
228 static uint64_t StreamedLength(const std::array<_valueT, N> & a)
229 {
230 uint64_t rc = 0;
231 for (size_t i = 0; i < N; ++i) {
232 rc += StreamedLength(a[i]);
233 }
234 return rc;
235 }
236
237 //------------------------------------------------------------------------
240 //------------------------------------------------------------------------
241 template <typename _valueT, typename _Alloc>
242 static uint64_t StreamedLength(const std::vector<_valueT, _Alloc> & v)
243 {
244 return(ContainerStreamedLength<std::vector<_valueT, _Alloc> >(v));
245 }
246
247 //------------------------------------------------------------------------
250 //------------------------------------------------------------------------
251 template <typename _valueT, typename _Alloc>
252 static uint64_t StreamedLength(const std::deque<_valueT, _Alloc> & d)
253 {
254 return(ContainerStreamedLength<std::deque<_valueT, _Alloc> >(d));
255 }
256
257 //------------------------------------------------------------------------
260 //------------------------------------------------------------------------
261 template <typename _valueT, typename _Alloc>
262 static uint64_t StreamedLength(const std::list<_valueT, _Alloc> & l)
263 {
264 return(ContainerStreamedLength<std::list<_valueT, _Alloc> >(l));
265 }
266
267 //------------------------------------------------------------------------
270 //------------------------------------------------------------------------
271 template <typename _valueT, typename _Compare, typename _Alloc>
272 static uint64_t
273 StreamedLength(const std::set<_valueT, _Compare, _Alloc> & l)
274 {
275 return(ContainerStreamedLength<std::set<_valueT, _Compare, _Alloc> >(l));
276 }
277
278 //------------------------------------------------------------------------
281 //------------------------------------------------------------------------
282 template <typename _valueT, typename _Compare, typename _Alloc>
283 static uint64_t
284 StreamedLength(const std::multiset<_valueT, _Compare, _Alloc> & l)
285 {
286 return(ContainerStreamedLength<std::multiset<_valueT, _Compare, _Alloc> >(l));
287 }
288
289 //------------------------------------------------------------------------
291 //------------------------------------------------------------------------
292 template <typename T, typename... Args>
293 static uint64_t VarStreamedLength(const T & t, Args... args)
294 {
295 uint64_t rc = StreamedLength(t);
296 if (sizeof...(Args) > 0)
297 rc += VarStreamedLength(args...);
298 return(rc);
299 }
300
301 //------------------------------------------------------------------------
304 //------------------------------------------------------------------------
305 template <typename T, typename... Args>
306 static uint64_t StreamedLength(const std::tuple<T, Args...> & t)
307 {
308 return(TupleStreamedLength<std::tuple<T, Args...> >(t));
309 }
310
311 //------------------------------------------------------------------------
313 //------------------------------------------------------------------------
314 template <typename T>
315 static uint64_t TupleStreamedLength(const T & t)
316 {
317 return(TupleIOHelper<T,std::tuple_size<T>::value-1>::StreamedLength(t));
318 }
319
320 //------------------------------------------------------------------------
322 //------------------------------------------------------------------------
323 static uint64_t StreamedLength(const std::monostate & sm)
324 {
325 return 0;
326 }
327
328 //------------------------------------------------------------------------
330 //------------------------------------------------------------------------
331 template <typename... Ts>
332 static uint64_t StreamedLength(const std::variant<Ts...> & v)
333 {
334 uint64_t rc = sizeof(uint64_t);
335 std::visit([&] (const auto & arg)
336 { rc += StreamedLength(arg); }, v);
337 return rc;
338 }
339
340 //------------------------------------------------------------------------
342 //------------------------------------------------------------------------
343 template <typename _keyT, typename _valueT,
344 typename _Hash, typename _Pred, typename _Alloc>
345 static uint64_t
346 StreamedLength(const std::unordered_map<_keyT, _valueT, _Hash, _Pred, _Alloc> & m)
347 {
348 using myType = std::unordered_map<_keyT, _valueT, _Hash, _Pred, _Alloc>;
349
350 return(ContainerStreamedLength<myType>(m));
351 }
352
353 //------------------------------------------------------------------------
355 //------------------------------------------------------------------------
356 template <typename _keyT, typename _valueT,
357 typename _Hash, typename _Pred, typename _Alloc>
358 static uint64_t
359 StreamedLength(const std::unordered_multimap<_keyT, _valueT, _Hash, _Pred, _Alloc> & m)
360 {
361 using myType = std::unordered_multimap<_keyT,_valueT,_Hash,_Pred,_Alloc>;
362 return(ContainerStreamedLength<myType>(m));
363 }
364
365 //------------------------------------------------------------------------
367 //------------------------------------------------------------------------
368 template <typename _valueT, typename _Hash,
369 typename _Pred, typename _Alloc>
370 static uint64_t
371 StreamedLength(const std::unordered_set<_valueT, _Hash, _Pred, _Alloc> & m)
372 {
373 using myType = std::unordered_set<_valueT, _Hash, _Pred, _Alloc>;
374 return(ContainerStreamedLength<myType>(m));
375 }
376
377 //------------------------------------------------------------------------
379 //------------------------------------------------------------------------
380 template <typename _valueT, typename _Hash,
381 typename _Pred, typename _Alloc>
382 static uint64_t
383 StreamedLength(const std::unordered_multiset<_valueT, _Hash, _Pred, _Alloc> & m)
384 {
385 using myType = std::unordered_multiset<_valueT, _Hash, _Pred, _Alloc>;
386 return(ContainerStreamedLength<myType>(m));
387 }
388
389 //------------------------------------------------------------------------
391 //------------------------------------------------------------------------
392 static uint64_t StreamedLength(const StreamedLengthCapable & sl)
393 { return sl.StreamedLength(); }
394
395 //------------------------------------------------------------------------
397 //------------------------------------------------------------------------
398 static uint64_t StreamedLength(const HasStreamedLength auto & sl)
399 { return sl.StreamedLength(); }
400
401 //------------------------------------------------------------------------
403 //------------------------------------------------------------------------
404 template <typename... Args>
405 static uint64_t StreamedLengthV(const Args & ...args)
406 {
407 return (StreamedLength(args) +...);
408 }
409
410 private:
411 //------------------------------------------------------------------------
413 //------------------------------------------------------------------------
414 template <typename _inputIteratorT>
415 static uint64_t StreamedLength(_inputIteratorT f, _inputIteratorT l)
416 {
417 uint64_t rc = 0;
418 for ( ; f != l; ++f)
419 rc += StreamedLength(*f);
420 return(rc);
421 }
422
423 //------------------------------------------------------------------------
425 //------------------------------------------------------------------------
426 template <typename _containerT>
427 static uint64_t ContainerStreamedLength(const _containerT & c)
428 {
429 uint64_t rc = sizeof(uint64_t); // for size()
430
432 c.end());
433 return(rc);
434 }
435
436 //------------------------------------------------------------------------
439 //------------------------------------------------------------------------
440 template <typename T, size_t elt>
441 class TupleIOHelper;
442
443 //------------------------------------------------------------------------
445 //------------------------------------------------------------------------
446 template <typename T>
447 class TupleIOHelper<T, 0>
448 {
449 public:
450 //----------------------------------------------------------------------
453 //----------------------------------------------------------------------
454 static uint64_t StreamedLength(const T & t)
455 {
456 return(IOUtils::StreamedLength(std::get<0>(t)));
457 }
458 };
459
460 //------------------------------------------------------------------------
462 //------------------------------------------------------------------------
463 template <typename T, size_t elt>
464 class TupleIOHelper
465 {
466 public:
467 //----------------------------------------------------------------------
470 //----------------------------------------------------------------------
471 static uint64_t StreamedLength(const T & t)
472 {
473 uint64_t rc = TupleIOHelper<T,elt-1>::StreamedLength(t);
474 rc += IOUtils::StreamedLength(std::get<elt>(t));
475 return(rc);
476 }
477 };
478
479 };
480
481} // namespace Dwm
482
483#endif // _DWMIOUTILS_HH_
484
485
486//---------------------------- emacs settings -----------------------------
487// Local Variables:
488// mode: C++
489// tab-width: 2
490// indent-tabs-mode: nil
491// c-basic-offset: 2
492// End:
493//-------------------------------------------------------------------------
A configure target for dealing with OS differences.
Dwm::StreamedLengthCapable pure virtual class declaration.
This class contains a collection of static functions for calculating the streamed length of simple ty...
Definition DwmIOUtils.hh:79
static uint64_t StreamedLength(const std::string &s)
Returns the number of bytes that should be written if we call Write() for a string.
Definition DwmIOUtils.hh:184
static uint64_t StreamedLength(const std::list< _valueT, _Alloc > &l)
Returns the number of bytes that should be written if we call Write() for a list<_valueT>
Definition DwmIOUtils.hh:262
static uint64_t TupleStreamedLength(const T &t)
T must be a tuple.
Definition DwmIOUtils.hh:315
static uint64_t StreamedLength(const std::set< _valueT, _Compare, _Alloc > &l)
Returns the number of bytes that should be written if we call Write() for a set<_valueT>
Definition DwmIOUtils.hh:273
static uint64_t StreamedLength(const std::map< _keyT, _valueT, _Compare, _Alloc > &m)
Returns the number of bytes that should be written if we call Write() for a map<_keyT,...
Definition DwmIOUtils.hh:206
static uint64_t StreamedLength(bool b)
Returns the number of bytes that would be written if we called Write() member for a bool.
Definition DwmIOUtils.hh:103
static uint64_t StreamedLength(float val)
Returns the number of bytes that should be written if we call Write() for a float.
Definition DwmIOUtils.hh:166
static uint64_t StreamedLength(int16_t val)
Returns the number of bytes that would be written if we called Write() for an int16_t.
Definition DwmIOUtils.hh:112
static uint64_t StreamedLength(int64_t val)
Returns the number of bytes that would be written if we called Write() for an int64_t.
Definition DwmIOUtils.hh:148
static uint64_t StreamedLength(uint16_t val)
Returns the number of bytes that would be written if we called Write() for a uint16_t.
Definition DwmIOUtils.hh:121
static uint64_t StreamedLength(const std::array< _valueT, N > &a)
Returns the number of bytes that should be written if we call Write() for an array<_valueT,...
Definition DwmIOUtils.hh:228
static uint64_t StreamedLength(uint32_t val)
Returns the number of bytes that would be written if we called Write() for a uint32_t.
Definition DwmIOUtils.hh:139
static uint64_t StreamedLength(const std::multimap< _keyT, _valueT, _Compare, _Alloc > &m)
Returns the number of bytes that should be written if we call Write() for a multimap<_keyT,...
Definition DwmIOUtils.hh:218
static uint64_t StreamedLength(char c)
Returns the number of bytes that would be written if we called Write() for a char.
Definition DwmIOUtils.hh:85
static uint64_t StreamedLength(const std::vector< _valueT, _Alloc > &v)
Returns the number of bytes that should be written if we call Write() for a vector<_valueT>
Definition DwmIOUtils.hh:242
static uint64_t StreamedLength(uint64_t val)
Returns the number of bytes that would be written if we called Write() for a uint64_t.
Definition DwmIOUtils.hh:157
static uint64_t StreamedLength(double val)
Returns the number of bytes that should be written if we call Write() for a double.
Definition DwmIOUtils.hh:175
static uint64_t StreamedLength(const std::multiset< _valueT, _Compare, _Alloc > &l)
Returns the number of bytes that should be written if we call Write() for a multiset<_valueT>
Definition DwmIOUtils.hh:284
static uint64_t StreamedLength(const std::deque< _valueT, _Alloc > &d)
Returns the number of bytes that should be written if we call Write() for a deque<_valueT>
Definition DwmIOUtils.hh:252
static uint64_t StreamedLength(uint8_t c)
Returns the number of bytes that would be written if we called Write() for a uint8_t.
Definition DwmIOUtils.hh:94
static uint64_t StreamedLength(const std::pair< _firstT, _secondT > &p)
Returns the number of bytes that should be written if we call Write() for a pair<_firstT,...
Definition DwmIOUtils.hh:194
static uint64_t StreamedLength(const std::tuple< T, Args... > &t)
Returns the number of bytes that should be written if we call Write() for a tuple.
Definition DwmIOUtils.hh:306
static uint64_t StreamedLength(int32_t val)
Returns the number of bytes that would be written if we called Write() for an int32_t.
Definition DwmIOUtils.hh:130