The ZX Ecosystem v5.1.0;_GUI_v3.1.0
Loading...
Searching...
No Matches
CppAddons.h
Go to the documentation of this file.
1
2/* *************************************************************************/
21#ifndef CPPADDONS
22#define CPPADDONS
23
24#include <type_traits>
25#include <cstddef>
26#include <climits>
27#include <cstdint>
28#include <cmath>
29#include <cstring>
30#include <string>
31#include <sstream>
32#include <iomanip>
33#include <algorithm>
34#include <chrono>
35#include <thread>
36#include <bitset>
37#include <functional>
38#include <mutex>
39
40
41/* ****************************************************************************
42
43 MACROS
44
45******************************************************************************/
46
47/* -----------------------------------------------------------------------------
48
49 BINTOSTREAM(v,n)
50
51------------------------------------------------------------------------------*/
52
54#define BINTOSTREAM(v,n) (std::bitset<n>{v})
55
56
57/* -----------------------------------------------------------------------------
58
59 DEBUGPREFFIX
60
61------------------------------------------------------------------------------*/
62
64
65#define JUSTFILENAME (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : \
66 (strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : \
67 __FILE__) )
68
70
72#define DEBUGPREFFIX(txt) { \
73 std::ostringstream stringStream; \
74 stringStream << \
75 " [" << JUSTFILENAME << ":" << __FUNCTION__ \
76 << "@" << __LINE__ \
77 << "]";\
78 txt = stringStream.str(); \
79 }
80
81/* " " << std::chrono::system_clock::now().time_since_epoch() /\
82 std::chrono::microseconds(1) << \
83 " usecs " \*/
84
85
86/* -----------------------------------------------------------------------------
87
88 RUNTIMEEXCEP(txt), INTERNALERROR(txt)
89
90------------------------------------------------------------------------------*/
91
93#define RUNTIMEEXCEP(txt) { std::string pdebugpreffix;\
94 DEBUGPREFFIX(pdebugpreffix);\
95 throw(std::runtime_error(pdebugpreffix + ": " + \
96 txt)); }
97
99#define INTERNALERROR(txt) RUNTIMEEXCEP("Internal error - " + txt)
100
101
102/* ****************************************************************************
103
104 TYPES
105
106******************************************************************************/
107
108/* -----------------------------------------------------------------------------
109
110 FIntTraits
111
112------------------------------------------------------------------------------*/
113
121
122template<typename T>
124{
125};
126
128template<>
129class FIntTraits<uint8_t>
130{
131 public:
132
133 typedef uint8_t type;
134 static constexpr unsigned char sizeinbits = 8;
135 static constexpr type minval = 0;
136 static constexpr type maxval = UINT8_MAX;
137};
138
140template<>
141class FIntTraits<uint16_t>
142{
143 public:
144
145 typedef uint16_t type;
146 static constexpr unsigned char sizeinbits = 16;
147 static constexpr type minval = 0;
148 static constexpr type maxval = UINT16_MAX;
149};
150
152template<>
153class FIntTraits<uint32_t>
154{
155 public:
156
157 typedef uint32_t type;
158 static constexpr unsigned char sizeinbits = 32;
159 static constexpr type minval = 0;
160 static constexpr type maxval = UINT32_MAX;
161};
162
164template<>
165class FIntTraits<uint64_t>
166{
167 public:
168
169 typedef uint64_t type;
170 static constexpr unsigned char sizeinbits = 64;
171 static constexpr type minval = 0;
172 static constexpr type maxval = UINT64_MAX;
173};
174
176template<>
177class FIntTraits<int8_t>
178{
179 public:
180
181 typedef int8_t type;
182 static constexpr unsigned char sizeinbits = 8;
183 static constexpr type minval = INT8_MIN;
184 static constexpr type maxval = INT8_MAX;
185};
186
188template<>
189class FIntTraits<int16_t>
190{
191 public:
192
193 typedef int16_t type;
194 static constexpr unsigned char sizeinbits = 16;
195 static constexpr type minval = INT16_MIN;
196 static constexpr type maxval = INT16_MAX;
197};
198
200template<>
201class FIntTraits<int32_t>
202{
203 public:
204
205 typedef int32_t type;
206 static constexpr unsigned char sizeinbits = 32;
207 static constexpr type minval = INT32_MIN;
208 static constexpr type maxval = INT32_MAX;
209};
210
212template<>
213class FIntTraits<int64_t>
214{
215 public:
216
217 typedef int64_t type;
218 static constexpr unsigned char sizeinbits = 64;
219 static constexpr type minval = INT64_MIN;
220 static constexpr type maxval = INT64_MAX;
221};
222
225/* -----------------------------------------------------------------------------
226
227 NextIntType
228
229------------------------------------------------------------------------------*/
230
243
244template<uint8_t NUMOFBITS>
245class NextIntType // default case: no type fits the requirements, so undefined
246{
247};
248
250template<>
251class NextIntType<CHAR_BIT*1>
252{
253 public:
254
255 typedef int16_t type;
256};
257
259template<>
260class NextIntType<CHAR_BIT*2>
261{
262 public:
263
264 typedef int32_t type;
265};
266
268template<>
269class NextIntType<CHAR_BIT*3>
270{
271 public:
272
273 typedef int32_t type;
274};
275
277template<>
278class NextIntType<CHAR_BIT*4>
279{
280 public:
281
282 typedef int64_t type;
283};
284
286template<>
287class NextIntType<CHAR_BIT*5>
288{
289 public:
290
291 typedef int64_t type;
292};
293
295template<>
296class NextIntType<CHAR_BIT*6>
297{
298 public:
299
300 typedef int64_t type;
301};
302
304template<>
305class NextIntType<CHAR_BIT*7>
306{
307 public:
308
309 typedef int64_t type;
310};
311
314/* -----------------------------------------------------------------------------
315
316 LongestType
317
318----------------------------------------------------------------------------- */
319
337
338template<typename T0, typename T1, bool sizet0smallerthansizet1>
340{
341 public:
342
343 typedef T0 type;
344};
345
347template<typename T0, typename T1>
348class LongestType<T0,T1,true>
349{
350 public:
351
352 typedef T1 type;
353};
354
355
356/* -----------------------------------------------------------------------------
357
358 LongestUnsignedType
359
360----------------------------------------------------------------------------- */
361
363
367using LongestUnsignedType = unsigned long long;
368
372/* -----------------------------------------------------------------------------
373
374 NaturalRange
375
376------------------------------------------------------------------------------*/
377
379
383template <typename BaseIntegerType, // should be unsigned
384 BaseIntegerType MAXVAL> // maximum value, inclusive
386{
387 public:
388
389 typedef BaseIntegerType basetype;
390
391 NaturalRange(BaseIntegerType v):baseval_{v} { if (v > MAXVAL) outOfRange();}
393
402 operator BaseIntegerType(void) const noexcept { return(baseval_); }
405 BaseIntegerType numeric(void) const noexcept { return(baseval_); }
409 { if (baseval_ == MAXVAL) outOfRange();
410 ++baseval_;
411 return(*this); }
414 { if (baseval_ == MAXVAL) outOfRange();
415 ++baseval_;
416 return(*this); }
419 { if (baseval_ == 0) outOfRange();
420 --baseval_;
421 return(*this); }
424 { if (baseval_ == 0) outOfRange();
425 --baseval_;
426 return(*this); }
428 NaturalRange & operator+=(BaseIntegerType o)
429 { baseval_ += o;
430 if (baseval_ > MAXVAL) outOfRange();
431 return(*this); }
433 NaturalRange & operator-=(BaseIntegerType o)
434 { baseval_ -= o;
435 if (baseval_ < 0) outOfRange();
436 return(*this); }
437
440 protected:
442 static void outOfRange(void) { RUNTIMEEXCEP("Out of range"); }
445 BaseIntegerType baseval_;
446
447};
448
449
450/* -----------------------------------------------------------------------------
451
452 Enum class: IntervalClosedness
453 Template class: Interval
454
455------------------------------------------------------------------------------*/
456
458enum class IntervalCl: uint8_t {
459 CLOSED,
460 OPENED,
461 LEFT_CLOSED,
462 RIGHT_CLOSED
463 };
464
465
467
468template <typename ELEMTYPE>
470{
471 public:
472
473 Interval(void): closedl_{false},
474 closedr_{false},
475 allclosed_{false},
476 min_{ELEMTYPE{}}, // if numerical, gets 0
477 max_{ELEMTYPE{}} {}
479
481 Interval(ELEMTYPE mi, ELEMTYPE ma,
482 IntervalCl cl = IntervalCl::CLOSED):
483 closedl_{(cl == IntervalCl::CLOSED) ||
484 (cl == IntervalCl::LEFT_CLOSED)},
485 closedr_{(cl == IntervalCl::CLOSED) ||
486 (cl == IntervalCl::RIGHT_CLOSED)},
487 allclosed_{cl == IntervalCl::CLOSED},
488 min_{mi},max_{ma}
489 { if (ma < mi) RUNTIMEEXCEP("Invalid interval (max " +
490 std::to_string(ma) + " is < min " +
491 std::to_string(mi) + ")");
492 if ((closedl_ != closedr_) && (ma == mi))
493 RUNTIMEEXCEP("Invalid semi-closed interval"); }
494
495
496 ELEMTYPE minimum(void) const noexcept { return(min_); }
498
499 ELEMTYPE maximum(void) const noexcept { return(max_); }
501
502 ELEMTYPE width(void) const noexcept { return(max_ - min_); }
504
505 IntervalCl closedness(void) const noexcept
507 { return(closednessFromCloses(closedl_,closedr_)); }
508
509 bool closedMin(void) const noexcept { return(closedl_); }
511
512 bool closedMax(void) const noexcept { return(closedr_); }
514
515 bool empty(void) const noexcept { return((!allclosed_) && (min_ == max_)); }
517
520 Interval intersect(const Interval & oth) const noexcept
522 {
523 if ( ((oth.min_ > max_) || (oth.max_ < min_)) ||
524 ((oth.closedl_ != closedr_) && (oth.min_ == max_)) ||
525 ((oth.closedr_ != closedl_) && (oth.max_ == min_)) )
526 return(Interval{});
527 ELEMTYPE a,b;
528 bool cl,cr;
529 if ((oth.min_ > min_) || (oth.min_ == min_))
530 {
531 a = oth.min_;
532 cl = oth.closedl_;
533 b = std::min(oth.max_,max_);
534 cr = closedr_;
535 }
536 else
537 {
538 a = min_;
539 cl = closedl_;
540 b = std::min(oth.max_,max_);
541 cr = oth.closedr_;
542 }
543 return(Interval{a,b,closednessFromCloses(cl,cr)});
544 }
546 bool containPoint(ELEMTYPE e) const noexcept
548 { return( ((e > min_) && (e < max_)) ||
549 ((e == min_) && (closedl_)) ||
550 ((e == max_) && (closedr_)) ); }
552 std::string to_string(void) const
554 { return(std::string(1, closedl_ ? '[' : '(') +
555 std::to_string(min_) + ".." + std::to_string(max_) +
556 std::string(1, closedl_ ? ']' : ')')); }
557
558
559 private:
560
561 static IntervalCl closednessFromCloses(bool l, bool r) noexcept
562 { return(l == r ?
563 (l ? IntervalCl::CLOSED : IntervalCl::OPENED) :
564 (l ? IntervalCl::LEFT_CLOSED : IntervalCl::RIGHT_CLOSED)); }
565
566 const bool closedl_,closedr_,allclosed_;
567
568 ELEMTYPE min_,max_;
569};
570
571
572/* -----------------------------------------------------------------------------
573
574 IterableEnum
575
576------------------------------------------------------------------------------*/
577
579
598template <typename T>
600{
601 public:
602
603 /* ------------------------ Types, consts., etc. -------------------- */
604
607 {
608 public:
609
610 Iterator(T val = T::FIRST) noexcept: v_{static_cast<int>(val)}
611 {}
613
614 // leave all constructors, move and copies by default
615
616 T operator*(void) const noexcept { return(static_cast<T>(v_)); }
618
619 Iterator & operator++(void) noexcept
620 { if (v_ != static_cast<int>(T::LAST)+1) ++v_; return(*this); }
622
623 Iterator operator++(int) noexcept
624 { Iterator now = *this; operator++(); return(now); }
626
627 bool operator!=(const Iterator &o) const noexcept { return(v_ != o.v_);}
629
630 bool operator==(const Iterator &o) const noexcept { return(v_ == o.v_);}
632
633 private:
634
635 int v_; // cannot be T because it needs sometimes to be LAST+1
636 };
637
638 /* ------------------------ Class methods ------------------------ */
639
640 static IterableEnum & theIterable(void) noexcept
641 { static IterableEnum ie; return(ie); }
643
644 /* ------------------------ Constructors ------------------------ */
645
646 // constructors are private, but move/copies are not
647 IterableEnum(const IterableEnum &) = default;
649 IterableEnum & operator=(const IterableEnum &o) { return(*this); }
651 IterableEnum & operator=(IterableEnum &&o) { return(*this); }
653
654 /* ------------------------ Methods ------------------------ */
655
656 Iterator begin(void) const noexcept { return(Iterator()); }
658
659 Iterator end(void) const noexcept
660 { return(Iterator(static_cast<T>(static_cast<int>(T::LAST)+1))); }
662
663
664 private:
665
666 IterableEnum(void) = default; // private for forcing to use the singleton
667 virtual ~IterableEnum(void) = default;
668};
669
670
671/* -----------------------------------------------------------------------------
672
673 Template class: ThreadSafer
674
675------------------------------------------------------------------------------*/
676
678
686template <class Unsafe>
688{
689 public:
690
692 class Proxy
693 {
694 public:
695
696 Proxy(Unsafe * obj, std::mutex & mux):obj_{obj},mux_{mux} {mux_.lock();}
698
699 Proxy(Proxy && oth):obj_{oth.obj_},mux_{oth.mux_} {}
701
702 ~Proxy(void) { mux_.unlock(); }
704
705 Proxy(const Proxy &) = delete;
706 Proxy & operator=(const Proxy &) = delete;
707 Proxy & operator=(Proxy &&) = delete;
708
709 Unsafe * operator->(void) { return(obj_); }
711
712 private:
713
714 Unsafe * obj_;
715 std::mutex & mux_;
716 };
717
718
719 ThreadSafer(Unsafe & obj):obj_{&obj} {}
721
725 ThreadSafer(const ThreadSafer &) = delete;
726 ThreadSafer(ThreadSafer &&) = delete;
727 ThreadSafer & operator=(const ThreadSafer &) = delete;
728 ThreadSafer & operator=(ThreadSafer &&) = delete;
729
730 ~ThreadSafer(void) = default;
731
732 Proxy safe(void)
733 { return(Proxy{obj_,mux_}); }
735
739 private:
740
741 Unsafe * obj_;
742 std::mutex mux_;
743};
744
745
746
747/* ****************************************************************************
748
749 CONSTS
750
751******************************************************************************/
752
753/* -----------------------------------------------------------------------------
754
755 PI: the famous constant, not defined yet in C++11 ¬¬
756
757------------------------------------------------------------------------------*/
758
760
764static const double PI = std::acos(-1.0);
765
766
767/* ****************************************************************************
768
769 FUNCTIONS
770
771******************************************************************************/
772
773/* -----------------------------------------------------------------------------
774
775 string_hex_to_num()
776
777------------------------------------------------------------------------------*/
778
780
783template <typename NumType>
784NumType string_hex_to_num(const std::string & txt)
785{
786 static_assert(std::is_unsigned<NumType>::value,
787 "from_string_hex only works with integral unsigned types");
788
789 NumType v{0};
790 NumType mult{0};
791 NumType c;
792 for (int f = static_cast<int>(txt.size() - 1); f >= 0; --f)
793 {
794 if (std::isdigit(txt[f])) c = txt[f] - '0';
795 else if (std::isalpha(txt[f]))
796 {
797 if (std::isupper(txt[f])) c = txt[f] - 'A' + 10;
798 else c = txt[f] - 'a' + 10;
799 if (c > 15) break;
800 }
801 else break;
802 v += c << mult;
803 mult += 4;
804 }
805 return(v);
806}
807
808/* -----------------------------------------------------------------------------
809
810 num_to_string_hex()
811
812------------------------------------------------------------------------------*/
813
815
817template <typename NumType>
818std::string num_to_string_hex(NumType n, bool padding = true)
819{
820 static_assert(std::is_integral<NumType>::value,
821 "to_string_hex only works with integral types");
822
823 std::stringstream stream;
824 if (padding)
825 stream << std::hex <<
826 std::setw(sizeof(NumType)*2) << std::setfill('0') <<
827 std::uppercase <<
828 static_cast<LongestUnsignedType>(n); // cast needed for uint8_t...
829 else stream << std::hex << static_cast<LongestUnsignedType>(n);// not being char
830 return(stream.str());
831}
832
833/* -----------------------------------------------------------------------------
834
835 hex_dump()
836
837------------------------------------------------------------------------------*/
838
840
842template <typename PointedType>
843std::string hex_dump(const PointedType * data, size_t howmany, bool toupp = true,
844 bool sepelems = true)
845{
846 std::string res;
847 if ((data != nullptr) && (howmany > 0))
848 {
849 const auto * bytes = reinterpret_cast<const uint8_t *>(data);
850 for (size_t f = 0; f < howmany; ++f)
851 {
852 res += num_to_string_hex(*bytes);
853 if (sepelems && (f < howmany - 1)) res.push_back(' ');
854 ++bytes;
855 }
856 if (toupp)
857 std::transform(res.begin(),res.end(),res.begin(),
858 [](char c)->char
859 { return(std::toupper(c)); });
860 }
861 return(res);
862}
863
864/* -----------------------------------------------------------------------------
865
866 pointer_to_number(p)
867
868------------------------------------------------------------------------------*/
869
871inline LongestUnsignedType pointer_to_number(const void * p) noexcept
872 { return(reinterpret_cast<LongestUnsignedType>(p)); }
873
874
875/* -----------------------------------------------------------------------------
876
877 all_bits_set_up_to_n(n)
878
879------------------------------------------------------------------------------*/
880
882constexpr uint8_t all_bits_set_up_to_n(uint8_t n)
883{ // a constexpr can also be used with non-constexpr args, although in that
884 // case a normal inline function will be generated (instead of calculating
885 // the result in compiling time)
886
887 return( static_cast<uint8_t>( (static_cast<uint16_t>(1) << (n+1)) - 1) );
888}
889
890/* -----------------------------------------------------------------------------
891
892 reverse_bits(b)
893
894------------------------------------------------------------------------------*/
895
897template <typename T, unsigned char N = sizeof(T)*CHAR_BIT>
899{
900 T res = 0;
901 for (unsigned char f=0; f<N; f++)
902 {
903 res <<= 1;
904 if (v & 1) res |= 1;
905 v >>= 1;
906 }
907 return(res);
908}
909
910/* -----------------------------------------------------------------------------
911
912 count_bits_set(b)
913
914------------------------------------------------------------------------------*/
915
917template <typename T>
918unsigned char count_bits_set(T v)
919{
920 unsigned char n=0;
921 for (unsigned char f=0; f<sizeof(T)*CHAR_BIT; f++)
922 {
923 n += static_cast<unsigned char>((v & 1)!=0);
924 v >>= 1;
925 }
926 return(n);
927}
928
929/* -----------------------------------------------------------------------------
930
931 most_significant_bit_set(b)
932
933------------------------------------------------------------------------------*/
934
936template <typename T, unsigned char N = sizeof(T)*CHAR_BIT>
937unsigned char most_significant_bit_set(T b)
938{
939 T mask = static_cast<T>(1) << (N-1);
940 unsigned char res = N - 1;
941 for (unsigned char f = 0; f < N; ++f)
942 {
943 if ((b & mask) != 0) return(res);
944 --res;
945 mask >>= 1;
946 }
947 return(N);
948}
949
950/* -----------------------------------------------------------------------------
951
952 least_significant_bit_set(b)
953
954------------------------------------------------------------------------------*/
955
957template <typename T, unsigned char N = sizeof(T)*CHAR_BIT>
959{
960 T mask = static_cast<T>(1);
961 unsigned char res = 0;
962 for (unsigned char f = 0; f < N; ++f)
963 {
964 if ((b & mask) != 0) return(res);
965 ++res;
966 mask <<= 1;
967 }
968 return(N);
969}
970
971/* -----------------------------------------------------------------------------
972
973 more_bits_set(b1,b2)
974
975------------------------------------------------------------------------------*/
976
978template <typename T>
979bool more_bits_set(T b1, T b2)
980{
981 if ((b1 & b2) != b2) return(false); // if some bit in B2 is unset in B1
982 // here all bits of B2 are set in B1; thus, B1 & ~B2 yields new bits in B1
983 return( (b1 & (~b2)) != 0); // at least 1 bit in B1 is set but unset in B2
984}
985
986/* -----------------------------------------------------------------------------
987
988 substract_bits(b1,b2)
989
990------------------------------------------------------------------------------*/
991
993
998template <typename T>
999T substract_bits(T b1, T b2)
1000{
1001 return( b1 & (~b2) );
1002}
1003
1004/* -----------------------------------------------------------------------------
1005
1006 new_bits_set(b1,b2)
1007
1008------------------------------------------------------------------------------*/
1009
1011template <typename T>
1012bool new_bits_set(T b1, T b2)
1013{
1014 return( substract_bits(b1,b2) != 0 );
1015}
1016
1017/* -----------------------------------------------------------------------------
1018
1019 bits_of_constexprvalue(v)
1020
1021------------------------------------------------------------------------------*/
1022
1024template <unsigned char N>
1025constexpr unsigned char bits_of_constexprvalue()
1026 { return(N == 0 ? 0 : (1 + bits_of_constexprvalue<(N >> 1)>())); }
1027
1028
1029/* -----------------------------------------------------------------------------
1030
1031 bits_of_value(v)
1032
1033------------------------------------------------------------------------------*/
1034
1036template <typename T>
1037unsigned char bits_of_value(T v)
1038 { auto nbs = most_significant_bit_set<T>(v);
1039 return(nbs == sizeof(T)*CHAR_BIT ? 0 : nbs); }
1040
1041
1042/* -----------------------------------------------------------------------------
1043
1044 for_within()
1045
1046------------------------------------------------------------------------------*/
1047
1049
1060template<class TIter, class TBody>
1061void for_within(TIter i_first, TIter i_last, const TBody & body)
1062{
1063 if (i_first>i_last) return;
1064 TIter i=i_first;
1065 do
1066 {
1067 body(i);
1068 if (i==i_last) return; // in a for-loop this & the next would be swapped
1069 i++;
1070 } while(1);
1071}
1072
1073/* -----------------------------------------------------------------------------
1074
1075 Functions to move and copy memory blocks correctly in machines that
1076 have CHAR_BIT > 8.
1077
1078------------------------------------------------------------------------------*/
1079
1080#if CHAR_BIT==8
1082 #define fill_byte(dest,value,count) std::memset(dest,value,count)
1084 #define move_bytes(dest,src,count) std::memmove(dest,src,count)
1085#else
1086 #error Cannot compile in machines where char != 8 bits.
1087#endif
1088
1089/* -----------------------------------------------------------------------------
1090
1091 pad_string
1092
1093------------------------------------------------------------------------------*/
1094
1096
1097inline void pad_string(std::string & s, size_t len, bool leftorright = true,
1098 char padding = '\t')
1099{
1100 const size_t ss = s.size();
1101 if (ss < len)
1102 {
1103 const size_t howmany = len - ss;
1104 if (leftorright) s.insert(0,howmany,padding);
1105 else s.insert(ss,howmany,padding);
1106 }
1107}
1108
1109/* -----------------------------------------------------------------------------
1110
1111 time_text
1112
1113------------------------------------------------------------------------------*/
1114
1117{ return(std::chrono::duration_cast<std::chrono::microseconds>(
1118 std::chrono::steady_clock::now().time_since_epoch()
1119 ).count()); }
1120
1121#endif
1122 // CppAddOns
1124
Proxy(Unsafe *obj, std::mutex &mux)
Constructor.
Definition: CppAddons.h:696
bool operator!=(const Iterator &o) const noexcept
Iterator comparison.
Definition: CppAddons.h:627
ELEMTYPE width(void) const noexcept
Return the distance from the minimum to the maximum.
Definition: CppAddons.h:502
ThreadSafer(Unsafe &obj)
Constructor. Create the needed infrastructure for making obj safe.
Definition: CppAddons.h:719
Interval(ELEMTYPE mi, ELEMTYPE ma, IntervalCl cl=IntervalCl::CLOSED)
Constructor.
Definition: CppAddons.h:481
bool closedMax(void) const noexcept
Return the closedness of the maximum bound.
Definition: CppAddons.h:512
bool closedMin(void) const noexcept
Return the closedness of the minimum bound.
Definition: CppAddons.h:509
Unsafe * operator->(void)
Accessor.
Definition: CppAddons.h:709
Iterator begin(void) const noexcept
Return an iterator pointing to the first constant of the enum class T.
Definition: CppAddons.h:656
Proxy(Proxy &&oth)
Move constructor.
Definition: CppAddons.h:699
NaturalRange & operator--(void)
Implicit conversion to the base integer type.
Definition: CppAddons.h:417
BaseIntegerType numeric(void) const noexcept
Explicit conversion to the base integer type.
Definition: CppAddons.h:404
bool operator==(const Iterator &o) const noexcept
Iterator equality, based on the enum value of the iterator.
Definition: CppAddons.h:630
IntervalCl closedness(void) const noexcept
< Return the closedness of the interval.
Definition: CppAddons.h:505
BaseIntegerType basetype
Just syntactic sugar.
Definition: CppAddons.h:389
NaturalRange & operator++(void)
Implicit conversion to the base integer type.
Definition: CppAddons.h:407
Iterator(T val=T::FIRST) noexcept
Iterator from val.: point to it.
Definition: CppAddons.h:610
Iterator operator++(int) noexcept
Postfix: return, then increment.
Definition: CppAddons.h:623
Interval intersect(const Interval &oth) const noexcept
< Return the interval that is the intersection of both, or empty if none.
Definition: CppAddons.h:519
T operator*(void) const noexcept
De-referencing the iterator must give the value.
Definition: CppAddons.h:616
ELEMTYPE maximum(void) const noexcept
Return the maximum value.
Definition: CppAddons.h:499
Iterator & operator++(void) noexcept
Increment iterator (pre-fix: first increment, then return)
Definition: CppAddons.h:619
ELEMTYPE minimum(void) const noexcept
Return the minimum value.
Definition: CppAddons.h:496
IterableEnum(IterableEnum &&)=default
Moves allowed.
static IterableEnum & theIterable(void) noexcept
Return the singleton.
Definition: CppAddons.h:640
NaturalRange & operator+=(BaseIntegerType o)
Implicit conversion to the base integer type.
Definition: CppAddons.h:427
IterableEnum & operator=(IterableEnum &&o)
Assignment-moves allowed, but do nothing.
Definition: CppAddons.h:651
T0 type
Longest type of both.
Definition: CppAddons.h:343
Iterator end(void) const noexcept
Return an iterator pointing beyond the last constant of enum class T.
Definition: CppAddons.h:659
bool empty(void) const noexcept
Return true if the interval is empty.
Definition: CppAddons.h:515
bool containPoint(ELEMTYPE e) const noexcept
< Return true if E is inside the interval.
Definition: CppAddons.h:545
IterableEnum & operator=(const IterableEnum &o)
Assignment allowed, but do nothing.
Definition: CppAddons.h:649
static void outOfRange(void)
Raise a run time exception "out of range".
Definition: CppAddons.h:441
Interval(void)
Default constructor: empty interval.
Definition: CppAddons.h:473
~Proxy(void)
Destructor.
Definition: CppAddons.h:702
IterableEnum(const IterableEnum &)=default
Copies allowed.
Proxy safe(void)
Allows safe access to the object.
Definition: CppAddons.h:732
NaturalRange & operator-=(BaseIntegerType o)
Implicit conversion to the base integer type.
Definition: CppAddons.h:432
BaseIntegerType baseval_
The base type value of the number.
Definition: CppAddons.h:444
std::string to_string(void) const
< Return a string describing the interval.
Definition: CppAddons.h:551
NaturalRange(BaseIntegerType v)
Constructor from a value of the base integer type.
Definition: CppAddons.h:391
ThreadSafer(const ThreadSafer &)=delete
Collect diverse traits for the fixed integer type T.
Definition: CppAddons.h:124
An interval, typically numeric, either closed, opened or semi-closed.
Definition: CppAddons.h:470
A generic enum for adding iteration capabilities to C++11 enum class.
Definition: CppAddons.h:600
The iterator of IterableEnum.
Definition: CppAddons.h:607
Select the longest type in memory of two given types T0 and T1.
Definition: CppAddons.h:340
An unsigned integer type that is bound-protected.
Definition: CppAddons.h:386
Provide the first signed integer type that has more than NUMOFBITS in memory.
Definition: CppAddons.h:246
Makes an unsafe class thread-safe.
Definition: CppAddons.h:688
A proxy to access objects in a safe manner. Not used directly.
Definition: CppAddons.h:693
#define RUNTIMEEXCEP(txt)
Raise a runtime exception with the given std::string TXT + additional info.
Definition: CppAddons.h:93
unsigned char most_significant_bit_set(T b)
Return the index of the MSb set in B, or the number of bits in T if none.
Definition: CppAddons.h:937
std::string num_to_string_hex(NumType n, bool padding=true)
Convert an integral number to string in hexadecimal format, without preffix.
Definition: CppAddons.h:818
LongestUnsignedType utime_now(void)
Return the current time since epoch in microseconds.
Definition: CppAddons.h:1116
IntervalCl
Kinds of closedness for intervals.
Definition: CppAddons.h:458
bool new_bits_set(T b1, T b2)
Return TRUE if B1 has some bit set that B2 has not, indepently on other bits.
Definition: CppAddons.h:1012
unsigned char count_bits_set(T v)
Count the number of bits that are set in an integral value.
Definition: CppAddons.h:918
std::string hex_dump(const PointedType *data, size_t howmany, bool toupp=true, bool sepelems=true)
Return a string that represents all hex codes of the pointed bytes.
Definition: CppAddons.h:843
T reverse_bits(T v)
Reverse the bits in an integral value, up to bit number N-1.
Definition: CppAddons.h:898
LongestUnsignedType pointer_to_number(const void *p) noexcept
Return the numerical value (address) of the given pointer.
Definition: CppAddons.h:871
static const double PI
The PI constant.
Definition: CppAddons.h:764
unsigned char bits_of_value(T v)
The same as bits_of_constexprvalue(v) but for any variable value.
Definition: CppAddons.h:1037
constexpr unsigned char bits_of_constexprvalue()
Return the number of bits needed by a constexpr value N.
Definition: CppAddons.h:1025
void for_within(TIter i_first, TIter i_last, const TBody &body)
Iterate through consecutive values without using any value outside the range.
Definition: CppAddons.h:1061
NumType string_hex_to_num(const std::string &txt)
Return the unsigned int. value of a number expressed in hexadecimal form.
Definition: CppAddons.h:784
unsigned char least_significant_bit_set(T b)
Return the index of the LSb set in B, or the number of bits in T if none.
Definition: CppAddons.h:958
bool more_bits_set(T b1, T b2)
Return TRUE if B1 has the same bits set as B2 plus at least 1 more bit set.
Definition: CppAddons.h:979
void pad_string(std::string &s, size_t len, bool leftorright=true, char padding='\t')
Pad S up to LEN characters PADDING either to the left (TRUE) or right.
Definition: CppAddons.h:1097
constexpr uint8_t all_bits_set_up_to_n(uint8_t n)
Return a byte with its (N+1) least-significant bits set (N must be in [0,7])
Definition: CppAddons.h:882
T substract_bits(T b1, T b2)
Substract bits of B2 from B1.
Definition: CppAddons.h:999
uint8_t type
Specialized type itself.
Definition: CppAddons.h:133
int64_t type
Specialized type itself.
Definition: CppAddons.h:217
int8_t type
Specialized type itself.
Definition: CppAddons.h:181
int16_t type
Specialized type itself.
Definition: CppAddons.h:193
uint16_t type
Specialized type itself.
Definition: CppAddons.h:145
uint64_t type
Specialized type itself.
Definition: CppAddons.h:169
int32_t type
Specialized type itself.
Definition: CppAddons.h:205
uint32_t type
Specialized type itself.
Definition: CppAddons.h:157
T1 type
Longest type of both.
Definition: CppAddons.h:352
unsigned long long LongestUnsignedType
Longest unsigned integral type in the target machine.
Definition: CppAddons.h:367
int64_t type
Next type in memory length.
Definition: CppAddons.h:300
int32_t type
Next type in memory length.
Definition: CppAddons.h:273
int16_t type
Next type in memory length.
Definition: CppAddons.h:255
int64_t type
Next type in memory length.
Definition: CppAddons.h:282
int32_t type
Next type in memory length.
Definition: CppAddons.h:264
int64_t type
Next type in memory length.
Definition: CppAddons.h:309
int64_t type
Next type in memory length.
Definition: CppAddons.h:291