![]() |
The ZX Ecosystem v5.1.0;_GUI_v3.1.0
|
Miscellaneous macros, types, consts and functions for general C++11 programs.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Modules | |
Instantiated templates for the FIntTraits type | |
Instantiated templates for the NextIntType type | |
Instantiated templates for the LongestType type | |
Classes | |
class | FIntTraits< T > |
Collect diverse traits for the fixed integer type T. More... | |
class | NextIntType< NUMOFBITS > |
Provide the first signed integer type that has more than NUMOFBITS in memory. More... | |
class | LongestType< T0, T1, sizet0smallerthansizet1 > |
Select the longest type in memory of two given types T0 and T1. More... | |
class | NaturalRange< BaseIntegerType, MAXVAL > |
An unsigned integer type that is bound-protected. More... | |
class | Interval< ELEMTYPE > |
An interval, typically numeric, either closed, opened or semi-closed. More... | |
class | IterableEnum< T > |
A generic enum for adding iteration capabilities to C++11 enum class. More... | |
class | IterableEnum< T >::Iterator |
The iterator of IterableEnum. More... | |
class | ThreadSafer< Unsafe > |
Makes an unsafe class thread-safe. More... | |
class | ThreadSafer< Unsafe >::Proxy |
A proxy to access objects in a safe manner. Not used directly. More... | |
Macros | |
#define | BINTOSTREAM(v, n) (std::bitset<n>{v}) |
Format V in binary for feeding it to a stream, up to the bit number N-1. More... | |
#define | JUSTFILENAME |
Strips FILE from entire path. More... | |
#define | DEBUGPREFFIX(txt) |
Generate a string for debugging uses (see the Debugging module). More... | |
#define | RUNTIMEEXCEP(txt) |
Raise a runtime exception with the given std::string TXT + additional info. More... | |
#define | INTERNALERROR(txt) RUNTIMEEXCEP("Internal error - " + txt) |
Raise a runtime exception indicating internal error. More... | |
Enumerations | |
enum class | IntervalCl : uint8_t { CLOSED , OPENED , LEFT_CLOSED , RIGHT_CLOSED } |
Kinds of closedness for intervals. More... | |
Functions | |
template<typename NumType > | |
NumType | string_hex_to_num (const std::string &txt) |
Return the unsigned int. value of a number expressed in hexadecimal form. More... | |
template<typename NumType > | |
std::string | num_to_string_hex (NumType n, bool padding=true) |
Convert an integral number to string in hexadecimal format, without preffix. More... | |
template<typename PointedType > | |
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. More... | |
LongestUnsignedType | pointer_to_number (const void *p) noexcept |
Return the numerical value (address) of the given pointer. More... | |
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]) More... | |
template<typename T , unsigned char N = sizeof(T)*CHAR_BIT> | |
T | reverse_bits (T v) |
Reverse the bits in an integral value, up to bit number N-1. More... | |
template<typename T > | |
unsigned char | count_bits_set (T v) |
Count the number of bits that are set in an integral value. More... | |
template<typename T , unsigned char N = sizeof(T)*CHAR_BIT> | |
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. More... | |
template<typename T , unsigned char N = sizeof(T)*CHAR_BIT> | |
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. More... | |
template<typename T > | |
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. More... | |
template<typename T > | |
T | substract_bits (T b1, T b2) |
Substract bits of B2 from B1. More... | |
template<typename T > | |
bool | new_bits_set (T b1, T b2) |
Return TRUE if B1 has some bit set that B2 has not, indepently on other bits. More... | |
template<unsigned char N> | |
constexpr unsigned char | bits_of_constexprvalue () |
Return the number of bits needed by a constexpr value N. More... | |
template<typename T > | |
unsigned char | bits_of_value (T v) |
The same as bits_of_constexprvalue(v) but for any variable value. More... | |
template<class TIter , class TBody > | |
void | for_within (TIter i_first, TIter i_last, const TBody &body) |
Iterate through consecutive values without using any value outside the range. More... | |
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. More... | |
LongestUnsignedType | utime_now (void) |
Return the current time since epoch in microseconds. More... | |
Variables | |
static const double | PI = std::acos(-1.0) |
The PI constant. More... | |
class FIntTraits |
Collect diverse traits for the fixed integer type T.
This is the default case: undefined traits.
Definition at line 123 of file CppAddons.h.
class NextIntType |
Provide the first signed integer type that has more than NUMOFBITS in memory.
This is the default case of NextIntType. Undefined.
Definition at line 245 of file CppAddons.h.
class LongestType |
Select the longest type in memory of two given types T0 and T1.
This is the case of sizeof(T0) >= sizeof(T1)
Definition at line 339 of file CppAddons.h.
Public Types | |
typedef T0 | type |
Longest type of both. More... | |
typedef T0 LongestType< T0, T1, sizet0smallerthansizet1 >::type |
Longest type of both.
Definition at line 343 of file CppAddons.h.
class NaturalRange |
An unsigned integer type that is bound-protected.
A wrapper of some unsigned type that can take values from 0 to MAXVAL (protected with exceptions against out of range values) and can be converted, both expliticly and implicitly, to that integer type.
Definition at line 385 of file CppAddons.h.
Public Types | |
typedef BaseIntegerType | basetype |
Just syntactic sugar. More... | |
Public Member Functions | |
NaturalRange (BaseIntegerType v) | |
Constructor from a value of the base integer type. More... | |
---------------------— Methods & operators -----------------— | |
These methods/operators need not be overriden in derived classes. Not declared virtual for that reason; therefore not declared final (cause virtual would be needed for that). | |
operator BaseIntegerType (void) const noexcept | |
Implicit conversion to the base integer type. More... | |
BaseIntegerType | numeric (void) const noexcept |
Explicit conversion to the base integer type. More... | |
NaturalRange & | operator++ (void) |
Implicit conversion to the base integer type. More... | |
NaturalRange & | operator++ (int) |
Implicit conversion to the base integer type. More... | |
NaturalRange & | operator-- (void) |
Implicit conversion to the base integer type. More... | |
NaturalRange & | operator-- (int) |
Implicit conversion to the base integer type. More... | |
NaturalRange & | operator+= (BaseIntegerType o) |
Implicit conversion to the base integer type. More... | |
NaturalRange & | operator-= (BaseIntegerType o) |
Implicit conversion to the base integer type. More... | |
Static Protected Member Functions | |
static void | outOfRange (void) |
Raise a run time exception "out of range". More... | |
Protected Attributes | |
BaseIntegerType | baseval_ |
The base type value of the number. More... | |
typedef BaseIntegerType NaturalRange< BaseIntegerType, MAXVAL >::basetype |
Just syntactic sugar.
Definition at line 389 of file CppAddons.h.
|
inline |
Constructor from a value of the base integer type.
Exception if the value is outside the valid range.
Definition at line 391 of file CppAddons.h.
|
inlinenoexcept |
Implicit conversion to the base integer type.
Definition at line 401 of file CppAddons.h.
|
inlinenoexcept |
Explicit conversion to the base integer type.
Definition at line 404 of file CppAddons.h.
|
inline |
Implicit conversion to the base integer type.
Definition at line 407 of file CppAddons.h.
|
inline |
Implicit conversion to the base integer type.
Definition at line 412 of file CppAddons.h.
|
inline |
Implicit conversion to the base integer type.
Definition at line 417 of file CppAddons.h.
|
inline |
Implicit conversion to the base integer type.
Definition at line 422 of file CppAddons.h.
|
inline |
Implicit conversion to the base integer type.
Definition at line 427 of file CppAddons.h.
|
inline |
Implicit conversion to the base integer type.
Definition at line 432 of file CppAddons.h.
|
inlinestaticprotected |
Raise a run time exception "out of range".
Definition at line 441 of file CppAddons.h.
|
protected |
The base type value of the number.
Definition at line 444 of file CppAddons.h.
class Interval |
An interval, typically numeric, either closed, opened or semi-closed.
ELEMTYPE must have minus, < and == operators.
Definition at line 469 of file CppAddons.h.
Public Member Functions | |
Interval (void) | |
Default constructor: empty interval. More... | |
Interval (ELEMTYPE mi, ELEMTYPE ma, IntervalCl cl=IntervalCl::CLOSED) | |
Constructor. More... | |
ELEMTYPE | minimum (void) const noexcept |
Return the minimum value. More... | |
ELEMTYPE | maximum (void) const noexcept |
Return the maximum value. More... | |
ELEMTYPE | width (void) const noexcept |
Return the distance from the minimum to the maximum. More... | |
IntervalCl | closedness (void) const noexcept |
< Return the closedness of the interval. More... | |
bool | closedMin (void) const noexcept |
Return the closedness of the minimum bound. More... | |
bool | closedMax (void) const noexcept |
Return the closedness of the maximum bound. More... | |
bool | empty (void) const noexcept |
Return true if the interval is empty. More... | |
Interval | intersect (const Interval &oth) const noexcept |
< Return the interval that is the intersection of both, or empty if none. More... | |
bool | containPoint (ELEMTYPE e) const noexcept |
< Return true if E is inside the interval. More... | |
std::string | to_string (void) const |
< Return a string describing the interval. More... | |
|
inline |
Default constructor: empty interval.
Definition at line 473 of file CppAddons.h.
|
inline |
Constructor.
Definition at line 481 of file CppAddons.h.
|
inlinenoexcept |
Return the minimum value.
Definition at line 496 of file CppAddons.h.
|
inlinenoexcept |
Return the maximum value.
Definition at line 499 of file CppAddons.h.
|
inlinenoexcept |
Return the distance from the minimum to the maximum.
Definition at line 502 of file CppAddons.h.
|
inlinenoexcept |
< Return the closedness of the interval.
Definition at line 505 of file CppAddons.h.
|
inlinenoexcept |
Return the closedness of the minimum bound.
Definition at line 509 of file CppAddons.h.
|
inlinenoexcept |
Return the closedness of the maximum bound.
Definition at line 512 of file CppAddons.h.
|
inlinenoexcept |
Return true if the interval is empty.
Closed intervals will never be empty because they include both extremes even when they are equal.
Definition at line 515 of file CppAddons.h.
|
inlinenoexcept |
< Return the interval that is the intersection of both, or empty if none.
Definition at line 519 of file CppAddons.h.
|
inlinenoexcept |
< Return true if E is inside the interval.
Definition at line 545 of file CppAddons.h.
|
inline |
< Return a string describing the interval.
Definition at line 551 of file CppAddons.h.
class IterableEnum |
A generic enum for adding iteration capabilities to C++11 enum class.
T is the enum class type, that must contain, after all its constants, a constant call "FIRST" equal to the first constant and a constant call "LAST" equal to the last constant, in that order.
Usage:
-Define a enum class with FIRST and LAST, e.g., enum class MyEnum { one, two, three, FIRST = one, LAST = three}; -Enhance that enum with this template, e.g., typedef IterableEnum<MyEnum> MyEnhancedEnum; -Use the methods of the enhanced enum singleton object, e.g., for (auto e: MyEnhancedEnum::theIterable() ) { ... }
NOTE:
-The basic enum class cannot contain any constant with value greater than LAST.
Definition at line 599 of file CppAddons.h.
Static Public Member Functions | |
static IterableEnum & | theIterable (void) noexcept |
Return the singleton. More... | |
Public Member Functions | |
IterableEnum (const IterableEnum &)=default | |
Copies allowed. | |
IterableEnum (IterableEnum &&)=default | |
Moves allowed. | |
IterableEnum & | operator= (const IterableEnum &o) |
Assignment allowed, but do nothing. More... | |
IterableEnum & | operator= (IterableEnum &&o) |
Assignment-moves allowed, but do nothing. More... | |
Iterator | begin (void) const noexcept |
Return an iterator pointing to the first constant of the enum class T. More... | |
Iterator | end (void) const noexcept |
Return an iterator pointing beyond the last constant of enum class T. More... | |
|
inlinestaticnoexcept |
Return the singleton.
Definition at line 640 of file CppAddons.h.
|
inline |
Assignment allowed, but do nothing.
Definition at line 649 of file CppAddons.h.
|
inline |
Assignment-moves allowed, but do nothing.
Definition at line 651 of file CppAddons.h.
|
inlinenoexcept |
Return an iterator pointing to the first constant of the enum class T.
Definition at line 656 of file CppAddons.h.
|
inlinenoexcept |
Return an iterator pointing beyond the last constant of enum class T.
Definition at line 659 of file CppAddons.h.
class IterableEnum::Iterator |
The iterator of IterableEnum.
Definition at line 606 of file CppAddons.h.
Public Member Functions | |
Iterator (T val=T::FIRST) noexcept | |
Iterator from val.: point to it. More... | |
T | operator* (void) const noexcept |
De-referencing the iterator must give the value. More... | |
Iterator & | operator++ (void) noexcept |
Increment iterator (pre-fix: first increment, then return) More... | |
Iterator | operator++ (int) noexcept |
Postfix: return, then increment. More... | |
bool | operator!= (const Iterator &o) const noexcept |
Iterator comparison. More... | |
bool | operator== (const Iterator &o) const noexcept |
Iterator equality, based on the enum value of the iterator. More... | |
|
inlinenoexcept |
Iterator from val.: point to it.
Definition at line 610 of file CppAddons.h.
|
inlinenoexcept |
De-referencing the iterator must give the value.
Definition at line 616 of file CppAddons.h.
|
inlinenoexcept |
Increment iterator (pre-fix: first increment, then return)
Definition at line 619 of file CppAddons.h.
|
inlinenoexcept |
Postfix: return, then increment.
Definition at line 623 of file CppAddons.h.
|
inlinenoexcept |
Iterator comparison.
Definition at line 627 of file CppAddons.h.
|
inlinenoexcept |
Iterator equality, based on the enum value of the iterator.
Definition at line 630 of file CppAddons.h.
class ThreadSafer |
Makes an unsafe class thread-safe.
This is a form of the Execute-Around Pointer idiom, that allows to call all methods of a class with automatic calls to code at the beginning and/or end without replicating all of them in a wrapper.
Usage: create an object of this template class to protect an existing unsafe object. Use it from diverse threads by calling safe() and the -> operator.
Definition at line 687 of file CppAddons.h.
Public Member Functions | |
ThreadSafer (Unsafe &obj) | |
Constructor. Create the needed infrastructure for making obj safe. More... | |
ThreadSafer (const ThreadSafer &)=delete | |
ThreadSafer (ThreadSafer &&)=delete | |
ThreadSafer & | operator= (const ThreadSafer &)=delete |
ThreadSafer & | operator= (ThreadSafer &&)=delete |
Proxy | safe (void) |
Allows safe access to the object. More... | |
|
inline |
Constructor. Create the needed infrastructure for making obj safe.
Definition at line 719 of file CppAddons.h.
|
delete |
Only one ThreadSafer should exist for any object that must be made safe, and shared for any accesses to that object. OBJ must outlive this ThreadSafer.
|
inline |
Allows safe access to the object.
The returned proxy locks the object until deleted, thus it should be used in a temporary, e.g., mythreadsafer.safe()->operation();
Definition at line 732 of file CppAddons.h.
class ThreadSafer::Proxy |
A proxy to access objects in a safe manner. Not used directly.
Definition at line 692 of file CppAddons.h.
Public Member Functions | |
Proxy (Unsafe *obj, std::mutex &mux) | |
Constructor. More... | |
Proxy (Proxy &&oth) | |
Move constructor. More... | |
~Proxy (void) | |
Destructor. More... | |
Proxy (const Proxy &)=delete | |
Proxy & | operator= (const Proxy &)=delete |
Proxy & | operator= (Proxy &&)=delete |
Unsafe * | operator-> (void) |
Accessor. More... | |
|
inline |
Constructor.
Definition at line 696 of file CppAddons.h.
|
inline |
Move constructor.
Definition at line 699 of file CppAddons.h.
|
inline |
Destructor.
Definition at line 702 of file CppAddons.h.
|
inline |
Accessor.
Definition at line 709 of file CppAddons.h.
#define BINTOSTREAM | ( | v, | |
n | |||
) | (std::bitset<n>{v}) |
#include <CppAddons.h>
Format V in binary for feeding it to a stream, up to the bit number N-1.
Definition at line 54 of file CppAddons.h.
#define JUSTFILENAME |
#include <CppAddons.h>
Strips FILE from entire path.
https://stackoverflow.com/questions/8487986/file-macro-shows-full-path
Definition at line 65 of file CppAddons.h.
#define DEBUGPREFFIX | ( | txt | ) |
#include <CppAddons.h>
Generate a string for debugging uses (see the Debugging module).
Set the TXT (std::string) variable to a string that contains abundant information about the current point of execution of a program.
Definition at line 72 of file CppAddons.h.
#define RUNTIMEEXCEP | ( | txt | ) |
#include <CppAddons.h>
Raise a runtime exception with the given std::string TXT + additional info.
Definition at line 93 of file CppAddons.h.
#define INTERNALERROR | ( | txt | ) | RUNTIMEEXCEP("Internal error - " + txt) |
#include <CppAddons.h>
Raise a runtime exception indicating internal error.
Definition at line 99 of file CppAddons.h.
|
strong |
#include <CppAddons.h>
Kinds of closedness for intervals.
Definition at line 458 of file CppAddons.h.
NumType string_hex_to_num | ( | const std::string & | txt | ) |
#include <CppAddons.h>
Return the unsigned int. value of a number expressed in hexadecimal form.
The hexadecimal string may contain letters a-f in upper or lower case, and the calculation ends when, scanning from its end to its start, a non-digit, non a-f character is found.
Definition at line 784 of file CppAddons.h.
std::string num_to_string_hex | ( | NumType | n, |
bool | padding = true |
||
) |
#include <CppAddons.h>
Convert an integral number to string in hexadecimal format, without preffix.
If PADDING, fill with 0 the most significant part of the number up to the length of the greatest number of that size; otherwise, get rid of those 0's.
Definition at line 818 of file CppAddons.h.
std::string hex_dump | ( | const PointedType * | data, |
size_t | howmany, | ||
bool | toupp = true , |
||
bool | sepelems = true |
||
) |
#include <CppAddons.h>
Return a string that represents all hex codes of the pointed bytes.
Each element is separated from the next one by a space if SEPELEMS == TRUE.
Definition at line 843 of file CppAddons.h.
|
inlinenoexcept |
#include <CppAddons.h>
Return the numerical value (address) of the given pointer.
Definition at line 871 of file CppAddons.h.
|
constexpr |
#include <CppAddons.h>
Return a byte with its (N+1) least-significant bits set (N must be in [0,7])
Definition at line 882 of file CppAddons.h.
T reverse_bits | ( | T | v | ) |
#include <CppAddons.h>
Reverse the bits in an integral value, up to bit number N-1.
Definition at line 898 of file CppAddons.h.
unsigned char count_bits_set | ( | T | v | ) |
#include <CppAddons.h>
Count the number of bits that are set in an integral value.
Definition at line 918 of file CppAddons.h.
unsigned char most_significant_bit_set | ( | T | b | ) |
#include <CppAddons.h>
Return the index of the MSb set in B, or the number of bits in T if none.
Definition at line 937 of file CppAddons.h.
unsigned char least_significant_bit_set | ( | T | b | ) |
#include <CppAddons.h>
Return the index of the LSb set in B, or the number of bits in T if none.
Definition at line 958 of file CppAddons.h.
bool more_bits_set | ( | T | b1, |
T | b2 | ||
) |
#include <CppAddons.h>
Return TRUE if B1 has the same bits set as B2 plus at least 1 more bit set.
Definition at line 979 of file CppAddons.h.
T substract_bits | ( | T | b1, |
T | b2 | ||
) |
#include <CppAddons.h>
Substract bits of B2 from B1.
The result will have each bit set iff B1 has the same bit set but B2 has that bit unset; otherwise, the result will have that bit unset. Not defined as operator for not overloading other existing '-' operator for type T.
Definition at line 999 of file CppAddons.h.
bool new_bits_set | ( | T | b1, |
T | b2 | ||
) |
#include <CppAddons.h>
Return TRUE if B1 has some bit set that B2 has not, indepently on other bits.
Definition at line 1012 of file CppAddons.h.
|
constexpr |
#include <CppAddons.h>
Return the number of bits needed by a constexpr value N.
Definition at line 1025 of file CppAddons.h.
unsigned char bits_of_value | ( | T | v | ) |
#include <CppAddons.h>
The same as bits_of_constexprvalue(v) but for any variable value.
Definition at line 1037 of file CppAddons.h.
void for_within | ( | TIter | i_first, |
TIter | i_last, | ||
const TBody & | body | ||
) |
#include <CppAddons.h>
Iterate through consecutive values without using any value outside the range.
This kind of counting is not possible with C-like loops without adding extra code that modifies the loop behavior (like we do in this function).
Usage (example):
for_within<uint8_t>(0,255,[](uint8_t i) <-- specialization to force the type (otherwise, int in this example) { <-- work to do with the iteration index I });
Definition at line 1061 of file CppAddons.h.
|
inline |
#include <CppAddons.h>
Pad S up to LEN characters PADDING either to the left (TRUE) or right.
If S is equal to or longer than LEN, do nothing.
Definition at line 1097 of file CppAddons.h.
|
inline |
#include <CppAddons.h>
Return the current time since epoch in microseconds.
Definition at line 1116 of file CppAddons.h.
|
static |
#include <CppAddons.h>
The PI constant.
Cannot be constexpr since acos is not constexpr. static makes the constant a new value in each .cpp that includes this, and also avoids collisions with others versions of the same constant in other .cpps at linkage time, since static is internal.
Definition at line 764 of file CppAddons.h.