54#define BINTOSTREAM(v,n) (std::bitset<n>{v})
65#define JUSTFILENAME (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : \
66 (strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : \
72#define DEBUGPREFFIX(txt) { \
73 std::ostringstream stringStream; \
75 " [" << JUSTFILENAME << ":" << __FUNCTION__ \
78 txt = stringStream.str(); \
93#define RUNTIMEEXCEP(txt) { std::string pdebugpreffix;\
94 DEBUGPREFFIX(pdebugpreffix);\
95 throw(std::runtime_error(pdebugpreffix + ": " + \
99#define INTERNALERROR(txt) RUNTIMEEXCEP("Internal error - " + txt)
134 static constexpr unsigned char sizeinbits = 8;
135 static constexpr type minval = 0;
136 static constexpr type maxval = UINT8_MAX;
146 static constexpr unsigned char sizeinbits = 16;
147 static constexpr type minval = 0;
148 static constexpr type maxval = UINT16_MAX;
158 static constexpr unsigned char sizeinbits = 32;
159 static constexpr type minval = 0;
160 static constexpr type maxval = UINT32_MAX;
170 static constexpr unsigned char sizeinbits = 64;
171 static constexpr type minval = 0;
172 static constexpr type maxval = UINT64_MAX;
182 static constexpr unsigned char sizeinbits = 8;
183 static constexpr type minval = INT8_MIN;
184 static constexpr type maxval = INT8_MAX;
194 static constexpr unsigned char sizeinbits = 16;
195 static constexpr type minval = INT16_MIN;
196 static constexpr type maxval = INT16_MAX;
206 static constexpr unsigned char sizeinbits = 32;
207 static constexpr type minval = INT32_MIN;
208 static constexpr type maxval = INT32_MAX;
218 static constexpr unsigned char sizeinbits = 64;
219 static constexpr type minval = INT64_MIN;
220 static constexpr type maxval = INT64_MAX;
244template<u
int8_t NUMOFBITS>
338template<
typename T0,
typename T1,
bool sizet0smallerthansizet1>
347template<
typename T0,
typename T1>
383template <
typename BaseIntegerType,
384 BaseIntegerType MAXVAL>
402 operator BaseIntegerType(
void)
const noexcept {
return(
baseval_); }
468template <
typename ELEMTYPE>
490 std::to_string(ma) +
" is < min " +
491 std::to_string(mi) +
")");
492 if ((closedl_ != closedr_) && (ma == mi))
496 ELEMTYPE
minimum(
void)
const noexcept {
return(min_); }
499 ELEMTYPE
maximum(
void)
const noexcept {
return(max_); }
502 ELEMTYPE
width(
void)
const noexcept {
return(max_ - min_); }
507 {
return(closednessFromCloses(closedl_,closedr_)); }
509 bool closedMin(
void)
const noexcept {
return(closedl_); }
512 bool closedMax(
void)
const noexcept {
return(closedr_); }
515 bool empty(
void)
const noexcept {
return((!allclosed_) && (min_ == max_)); }
523 if ( ((oth.min_ > max_) || (oth.max_ < min_)) ||
524 ((oth.closedl_ != closedr_) && (oth.min_ == max_)) ||
525 ((oth.closedr_ != closedl_) && (oth.max_ == min_)) )
529 if ((oth.min_ > min_) || (oth.min_ == min_))
533 b = std::min(oth.max_,max_);
540 b = std::min(oth.max_,max_);
543 return(
Interval{a,b,closednessFromCloses(cl,cr)});
548 {
return( ((e > min_) && (e < max_)) ||
549 ((e == min_) && (closedl_)) ||
550 ((e == max_) && (closedr_)) ); }
554 {
return(std::string(1, closedl_ ?
'[' :
'(') +
555 std::to_string(min_) +
".." + std::to_string(max_) +
556 std::string(1, closedl_ ?
']' :
')')); }
561 static IntervalCl closednessFromCloses(
bool l,
bool r)
noexcept
563 (l ? IntervalCl::CLOSED : IntervalCl::OPENED) :
564 (l ? IntervalCl::LEFT_CLOSED : IntervalCl::RIGHT_CLOSED)); }
566 const bool closedl_,closedr_,allclosed_;
610 Iterator(T val = T::FIRST)
noexcept: v_{
static_cast<int>(val)}
616 T
operator*(
void)
const noexcept {
return(
static_cast<T
>(v_)); }
620 {
if (v_ !=
static_cast<int>(T::LAST)+1) ++v_;
return(*
this); }
660 {
return(
Iterator(
static_cast<T
>(
static_cast<int>(T::LAST)+1))); }
686template <
class Unsafe>
696 Proxy(Unsafe * obj, std::mutex & mux):obj_{obj},mux_{mux} {mux_.lock();}
733 {
return(
Proxy{obj_,mux_}); }
764static const double PI = std::acos(-1.0);
783template <
typename NumType>
786 static_assert(std::is_unsigned<NumType>::value,
787 "from_string_hex only works with integral unsigned types");
792 for (
int f =
static_cast<int>(txt.size() - 1); f >= 0; --f)
794 if (std::isdigit(txt[f])) c = txt[f] -
'0';
795 else if (std::isalpha(txt[f]))
797 if (std::isupper(txt[f])) c = txt[f] -
'A' + 10;
798 else c = txt[f] -
'a' + 10;
817template <
typename NumType>
820 static_assert(std::is_integral<NumType>::value,
821 "to_string_hex only works with integral types");
823 std::stringstream stream;
825 stream << std::hex <<
826 std::setw(
sizeof(NumType)*2) << std::setfill(
'0') <<
829 else stream << std::hex << static_cast<LongestUnsignedType>(n);
830 return(stream.str());
842template <
typename Po
intedType>
843std::string
hex_dump(
const PointedType * data,
size_t howmany,
bool toupp =
true,
844 bool sepelems =
true)
847 if ((data !=
nullptr) && (howmany > 0))
849 const auto * bytes =
reinterpret_cast<const uint8_t *
>(data);
850 for (
size_t f = 0; f < howmany; ++f)
853 if (sepelems && (f < howmany - 1)) res.push_back(
' ');
857 std::transform(res.begin(),res.end(),res.begin(),
859 { return(std::toupper(c)); });
887 return(
static_cast<uint8_t
>( (
static_cast<uint16_t
>(1) << (n+1)) - 1) );
897template <
typename T,
unsigned char N = sizeof(T)*CHAR_BIT>
901 for (
unsigned char f=0; f<N; f++)
921 for (
unsigned char f=0; f<
sizeof(T)*CHAR_BIT; f++)
923 n +=
static_cast<unsigned char>((v & 1)!=0);
936template <
typename T,
unsigned char N = sizeof(T)*CHAR_BIT>
939 T mask =
static_cast<T
>(1) << (N-1);
940 unsigned char res = N - 1;
941 for (
unsigned char f = 0; f < N; ++f)
943 if ((b & mask) != 0)
return(res);
957template <
typename T,
unsigned char N = sizeof(T)*CHAR_BIT>
960 T mask =
static_cast<T
>(1);
961 unsigned char res = 0;
962 for (
unsigned char f = 0; f < N; ++f)
964 if ((b & mask) != 0)
return(res);
981 if ((b1 & b2) != b2)
return(
false);
983 return( (b1 & (~b2)) != 0);
1001 return( b1 & (~b2) );
1011template <
typename T>
1024template <
unsigned char N>
1036template <
typename T>
1038 {
auto nbs = most_significant_bit_set<T>(v);
1039 return(nbs ==
sizeof(T)*CHAR_BIT ? 0 : nbs); }
1060template<
class TIter,
class TBody>
1063 if (i_first>i_last)
return;
1068 if (i==i_last)
return;
1082 #define fill_byte(dest,value,count) std::memset(dest,value,count)
1084 #define move_bytes(dest,src,count) std::memmove(dest,src,count)
1086 #error Cannot compile in machines where char != 8 bits.
1097inline void pad_string(std::string & s,
size_t len,
bool leftorright =
true,
1098 char padding =
'\t')
1100 const size_t ss = s.size();
1103 const size_t howmany = len - ss;
1104 if (leftorright) s.insert(0,howmany,padding);
1105 else s.insert(ss,howmany,padding);
1117{
return(std::chrono::duration_cast<std::chrono::microseconds>(
1118 std::chrono::steady_clock::now().time_since_epoch()
Proxy(Unsafe *obj, std::mutex &mux)
Constructor.
bool operator!=(const Iterator &o) const noexcept
Iterator comparison.
ELEMTYPE width(void) const noexcept
Return the distance from the minimum to the maximum.
ThreadSafer(Unsafe &obj)
Constructor. Create the needed infrastructure for making obj safe.
Interval(ELEMTYPE mi, ELEMTYPE ma, IntervalCl cl=IntervalCl::CLOSED)
Constructor.
bool closedMax(void) const noexcept
Return the closedness of the maximum bound.
bool closedMin(void) const noexcept
Return the closedness of the minimum bound.
Unsafe * operator->(void)
Accessor.
Iterator begin(void) const noexcept
Return an iterator pointing to the first constant of the enum class T.
Proxy(Proxy &&oth)
Move constructor.
NaturalRange & operator--(void)
Implicit conversion to the base integer type.
BaseIntegerType numeric(void) const noexcept
Explicit conversion to the base integer type.
bool operator==(const Iterator &o) const noexcept
Iterator equality, based on the enum value of the iterator.
IntervalCl closedness(void) const noexcept
< Return the closedness of the interval.
BaseIntegerType basetype
Just syntactic sugar.
NaturalRange & operator++(void)
Implicit conversion to the base integer type.
Iterator(T val=T::FIRST) noexcept
Iterator from val.: point to it.
Iterator operator++(int) noexcept
Postfix: return, then increment.
Interval intersect(const Interval &oth) const noexcept
< Return the interval that is the intersection of both, or empty if none.
T operator*(void) const noexcept
De-referencing the iterator must give the value.
ELEMTYPE maximum(void) const noexcept
Return the maximum value.
Iterator & operator++(void) noexcept
Increment iterator (pre-fix: first increment, then return)
ELEMTYPE minimum(void) const noexcept
Return the minimum value.
IterableEnum(IterableEnum &&)=default
Moves allowed.
static IterableEnum & theIterable(void) noexcept
Return the singleton.
NaturalRange & operator+=(BaseIntegerType o)
Implicit conversion to the base integer type.
IterableEnum & operator=(IterableEnum &&o)
Assignment-moves allowed, but do nothing.
T0 type
Longest type of both.
Iterator end(void) const noexcept
Return an iterator pointing beyond the last constant of enum class T.
bool empty(void) const noexcept
Return true if the interval is empty.
bool containPoint(ELEMTYPE e) const noexcept
< Return true if E is inside the interval.
IterableEnum & operator=(const IterableEnum &o)
Assignment allowed, but do nothing.
static void outOfRange(void)
Raise a run time exception "out of range".
Interval(void)
Default constructor: empty interval.
IterableEnum(const IterableEnum &)=default
Copies allowed.
Proxy safe(void)
Allows safe access to the object.
NaturalRange & operator-=(BaseIntegerType o)
Implicit conversion to the base integer type.
BaseIntegerType baseval_
The base type value of the number.
std::string to_string(void) const
< Return a string describing the interval.
NaturalRange(BaseIntegerType v)
Constructor from a value of the base integer type.
ThreadSafer(const ThreadSafer &)=delete
Collect diverse traits for the fixed integer type T.
An interval, typically numeric, either closed, opened or semi-closed.
A generic enum for adding iteration capabilities to C++11 enum class.
The iterator of IterableEnum.
Select the longest type in memory of two given types T0 and T1.
An unsigned integer type that is bound-protected.
Provide the first signed integer type that has more than NUMOFBITS in memory.
Makes an unsafe class thread-safe.
A proxy to access objects in a safe manner. Not used directly.
#define RUNTIMEEXCEP(txt)
Raise a runtime exception with the given std::string TXT + additional info.
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.
std::string num_to_string_hex(NumType n, bool padding=true)
Convert an integral number to string in hexadecimal format, without preffix.
LongestUnsignedType utime_now(void)
Return the current time since epoch in microseconds.
IntervalCl
Kinds of closedness for intervals.
bool new_bits_set(T b1, T b2)
Return TRUE if B1 has some bit set that B2 has not, indepently on other bits.
unsigned char count_bits_set(T v)
Count the number of bits that are set in an integral value.
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.
T reverse_bits(T v)
Reverse the bits in an integral value, up to bit number N-1.
LongestUnsignedType pointer_to_number(const void *p) noexcept
Return the numerical value (address) of the given pointer.
static const double PI
The PI constant.
unsigned char bits_of_value(T v)
The same as bits_of_constexprvalue(v) but for any variable value.
constexpr unsigned char bits_of_constexprvalue()
Return the number of bits needed by a constexpr value N.
void for_within(TIter i_first, TIter i_last, const TBody &body)
Iterate through consecutive values without using any value outside the range.
NumType string_hex_to_num(const std::string &txt)
Return the unsigned int. value of a number expressed in hexadecimal form.
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.
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.
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.
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])
T substract_bits(T b1, T b2)
Substract bits of B2 from B1.
uint8_t type
Specialized type itself.
int64_t type
Specialized type itself.
int8_t type
Specialized type itself.
int16_t type
Specialized type itself.
uint16_t type
Specialized type itself.
uint64_t type
Specialized type itself.
int32_t type
Specialized type itself.
uint32_t type
Specialized type itself.
T1 type
Longest type of both.
unsigned long long LongestUnsignedType
Longest unsigned integral type in the target machine.
int64_t type
Next type in memory length.
int32_t type
Next type in memory length.
int16_t type
Next type in memory length.
int64_t type
Next type in memory length.
int32_t type
Next type in memory length.
int64_t type
Next type in memory length.
int64_t type
Next type in memory length.