The ZX Ecosystem v5.1.0;_GUI_v3.1.0
Loading...
Searching...
No Matches
Modules | Classes | Macros | Enumerations | Functions | Variables
C++11 Add-ons

Description

Miscellaneous macros, types, consts and functions for general C++11
programs.

http://jafma.net

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.

Collaboration diagram for C++11 Add-ons:

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>
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 >
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 Documentation

◆ FIntTraits

class FIntTraits
template<typename T>
class FIntTraits< T >

Collect diverse traits for the fixed integer type T.

This is the default case: undefined traits.

Definition at line 123 of file CppAddons.h.

◆ NextIntType

class NextIntType
template<uint8_t NUMOFBITS>
class NextIntType< NUMOFBITS >

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.

◆ LongestType

class LongestType
template<typename T0, typename T1, bool sizet0smallerthansizet1>
class LongestType< T0, T1, sizet0smallerthansizet1 >

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...
 

Member Typedef Documentation

◆ type

template<typename T0 , typename T1 , bool sizet0smallerthansizet1>
typedef T0 LongestType< T0, T1, sizet0smallerthansizet1 >::type

Longest type of both.

Definition at line 343 of file CppAddons.h.

◆ NaturalRange

class NaturalRange
template<typename BaseIntegerType, BaseIntegerType MAXVAL>
class NaturalRange< BaseIntegerType, MAXVAL >

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...
 
NaturalRangeoperator++ (void)
 Implicit conversion to the base integer type. More...
 
NaturalRangeoperator++ (int)
 Implicit conversion to the base integer type. More...
 
NaturalRangeoperator-- (void)
 Implicit conversion to the base integer type. More...
 
NaturalRangeoperator-- (int)
 Implicit conversion to the base integer type. More...
 
NaturalRangeoperator+= (BaseIntegerType o)
 Implicit conversion to the base integer type. More...
 
NaturalRangeoperator-= (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...
 

Member Typedef Documentation

◆ basetype

template<typename BaseIntegerType , BaseIntegerType MAXVAL>
typedef BaseIntegerType NaturalRange< BaseIntegerType, MAXVAL >::basetype

Just syntactic sugar.

Definition at line 389 of file CppAddons.h.

Constructor & Destructor Documentation

◆ NaturalRange()

template<typename BaseIntegerType , BaseIntegerType MAXVAL>
NaturalRange< BaseIntegerType, MAXVAL >::NaturalRange ( BaseIntegerType  v)
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.

Member Function Documentation

◆ operator BaseIntegerType()

template<typename BaseIntegerType , BaseIntegerType MAXVAL>
NaturalRange< BaseIntegerType, MAXVAL >::operator BaseIntegerType ( void  ) const
inlinenoexcept

Implicit conversion to the base integer type.

Definition at line 401 of file CppAddons.h.

◆ numeric()

template<typename BaseIntegerType , BaseIntegerType MAXVAL>
BaseIntegerType NaturalRange< BaseIntegerType, MAXVAL >::numeric ( void  ) const
inlinenoexcept

Explicit conversion to the base integer type.

Definition at line 404 of file CppAddons.h.

◆ operator++() [1/2]

template<typename BaseIntegerType , BaseIntegerType MAXVAL>
NaturalRange & NaturalRange< BaseIntegerType, MAXVAL >::operator++ ( void  )
inline

Implicit conversion to the base integer type.

Definition at line 407 of file CppAddons.h.

◆ operator++() [2/2]

template<typename BaseIntegerType , BaseIntegerType MAXVAL>
NaturalRange & NaturalRange< BaseIntegerType, MAXVAL >::operator++ ( int  )
inline

Implicit conversion to the base integer type.

Definition at line 412 of file CppAddons.h.

◆ operator--() [1/2]

template<typename BaseIntegerType , BaseIntegerType MAXVAL>
NaturalRange & NaturalRange< BaseIntegerType, MAXVAL >::operator-- ( void  )
inline

Implicit conversion to the base integer type.

Definition at line 417 of file CppAddons.h.

◆ operator--() [2/2]

template<typename BaseIntegerType , BaseIntegerType MAXVAL>
NaturalRange & NaturalRange< BaseIntegerType, MAXVAL >::operator-- ( int  )
inline

Implicit conversion to the base integer type.

Definition at line 422 of file CppAddons.h.

◆ operator+=()

template<typename BaseIntegerType , BaseIntegerType MAXVAL>
NaturalRange & NaturalRange< BaseIntegerType, MAXVAL >::operator+= ( BaseIntegerType  o)
inline

Implicit conversion to the base integer type.

Definition at line 427 of file CppAddons.h.

◆ operator-=()

template<typename BaseIntegerType , BaseIntegerType MAXVAL>
NaturalRange & NaturalRange< BaseIntegerType, MAXVAL >::operator-= ( BaseIntegerType  o)
inline

Implicit conversion to the base integer type.

Definition at line 432 of file CppAddons.h.

◆ outOfRange()

template<typename BaseIntegerType , BaseIntegerType MAXVAL>
static void NaturalRange< BaseIntegerType, MAXVAL >::outOfRange ( void  )
inlinestaticprotected

Raise a run time exception "out of range".

Definition at line 441 of file CppAddons.h.

Member Data Documentation

◆ baseval_

template<typename BaseIntegerType , BaseIntegerType MAXVAL>
BaseIntegerType NaturalRange< BaseIntegerType, MAXVAL >::baseval_
protected

The base type value of the number.

Definition at line 444 of file CppAddons.h.

◆ Interval

class Interval
template<typename ELEMTYPE>
class Interval< ELEMTYPE >

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...
 

Constructor & Destructor Documentation

◆ Interval() [1/2]

template<typename ELEMTYPE >
Interval< ELEMTYPE >::Interval ( void  )
inline

Default constructor: empty interval.

Definition at line 473 of file CppAddons.h.

◆ Interval() [2/2]

template<typename ELEMTYPE >
Interval< ELEMTYPE >::Interval ( ELEMTYPE  mi,
ELEMTYPE  ma,
IntervalCl  cl = IntervalCl::CLOSED 
)
inline

Constructor.

Definition at line 481 of file CppAddons.h.

Member Function Documentation

◆ minimum()

template<typename ELEMTYPE >
ELEMTYPE Interval< ELEMTYPE >::minimum ( void  ) const
inlinenoexcept

Return the minimum value.

Definition at line 496 of file CppAddons.h.

◆ maximum()

template<typename ELEMTYPE >
ELEMTYPE Interval< ELEMTYPE >::maximum ( void  ) const
inlinenoexcept

Return the maximum value.

Definition at line 499 of file CppAddons.h.

◆ width()

template<typename ELEMTYPE >
ELEMTYPE Interval< ELEMTYPE >::width ( void  ) const
inlinenoexcept

Return the distance from the minimum to the maximum.

Definition at line 502 of file CppAddons.h.

◆ closedness()

template<typename ELEMTYPE >
IntervalCl Interval< ELEMTYPE >::closedness ( void  ) const
inlinenoexcept

< Return the closedness of the interval.

Definition at line 505 of file CppAddons.h.

◆ closedMin()

template<typename ELEMTYPE >
bool Interval< ELEMTYPE >::closedMin ( void  ) const
inlinenoexcept

Return the closedness of the minimum bound.

Definition at line 509 of file CppAddons.h.

◆ closedMax()

template<typename ELEMTYPE >
bool Interval< ELEMTYPE >::closedMax ( void  ) const
inlinenoexcept

Return the closedness of the maximum bound.

Definition at line 512 of file CppAddons.h.

◆ empty()

template<typename ELEMTYPE >
bool Interval< ELEMTYPE >::empty ( void  ) const
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.

◆ intersect()

template<typename ELEMTYPE >
Interval Interval< ELEMTYPE >::intersect ( const Interval< ELEMTYPE > &  oth) const
inlinenoexcept

< Return the interval that is the intersection of both, or empty if none.

Definition at line 519 of file CppAddons.h.

◆ containPoint()

template<typename ELEMTYPE >
bool Interval< ELEMTYPE >::containPoint ( ELEMTYPE  e) const
inlinenoexcept

< Return true if E is inside the interval.

Definition at line 545 of file CppAddons.h.

◆ to_string()

template<typename ELEMTYPE >
std::string Interval< ELEMTYPE >::to_string ( void  ) const
inline

< Return a string describing the interval.

Definition at line 551 of file CppAddons.h.

◆ IterableEnum

class IterableEnum
template<typename T>
class IterableEnum< T >

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 IterableEnumtheIterable (void) noexcept
 Return the singleton. More...
 

Public Member Functions

 IterableEnum (const IterableEnum &)=default
 Copies allowed.
 
 IterableEnum (IterableEnum &&)=default
 Moves allowed.
 
IterableEnumoperator= (const IterableEnum &o)
 Assignment allowed, but do nothing. More...
 
IterableEnumoperator= (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...
 

Member Function Documentation

◆ theIterable()

template<typename T >
static IterableEnum & IterableEnum< T >::theIterable ( void  )
inlinestaticnoexcept

Return the singleton.

Definition at line 640 of file CppAddons.h.

◆ operator=() [1/2]

template<typename T >
IterableEnum & IterableEnum< T >::operator= ( const IterableEnum< T > &  o)
inline

Assignment allowed, but do nothing.

Definition at line 649 of file CppAddons.h.

◆ operator=() [2/2]

template<typename T >
IterableEnum & IterableEnum< T >::operator= ( IterableEnum< T > &&  o)
inline

Assignment-moves allowed, but do nothing.

Definition at line 651 of file CppAddons.h.

◆ begin()

template<typename T >
Iterator IterableEnum< T >::begin ( void  ) const
inlinenoexcept

Return an iterator pointing to the first constant of the enum class T.

Definition at line 656 of file CppAddons.h.

◆ end()

template<typename T >
Iterator IterableEnum< T >::end ( void  ) const
inlinenoexcept

Return an iterator pointing beyond the last constant of enum class T.

Definition at line 659 of file CppAddons.h.

◆ IterableEnum::Iterator

class IterableEnum::Iterator
template<typename T>
class IterableEnum< T >::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...
 
operator* (void) const noexcept
 De-referencing the iterator must give the value. More...
 
Iteratoroperator++ (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...
 

Constructor & Destructor Documentation

◆ Iterator()

template<typename T >
IterableEnum< T >::Iterator::Iterator ( val = T::FIRST)
inlinenoexcept

Iterator from val.: point to it.

Definition at line 610 of file CppAddons.h.

Member Function Documentation

◆ operator*()

template<typename T >
T IterableEnum< T >::Iterator::operator* ( void  ) const
inlinenoexcept

De-referencing the iterator must give the value.

Definition at line 616 of file CppAddons.h.

◆ operator++() [1/2]

template<typename T >
Iterator & IterableEnum< T >::Iterator::operator++ ( void  )
inlinenoexcept

Increment iterator (pre-fix: first increment, then return)

Definition at line 619 of file CppAddons.h.

◆ operator++() [2/2]

template<typename T >
Iterator IterableEnum< T >::Iterator::operator++ ( int  )
inlinenoexcept

Postfix: return, then increment.

Definition at line 623 of file CppAddons.h.

◆ operator!=()

template<typename T >
bool IterableEnum< T >::Iterator::operator!= ( const Iterator o) const
inlinenoexcept

Iterator comparison.

Definition at line 627 of file CppAddons.h.

◆ operator==()

template<typename T >
bool IterableEnum< T >::Iterator::operator== ( const Iterator o) const
inlinenoexcept

Iterator equality, based on the enum value of the iterator.

Definition at line 630 of file CppAddons.h.

◆ ThreadSafer

class ThreadSafer
template<class Unsafe>
class ThreadSafer< Unsafe >

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
 
ThreadSaferoperator= (const ThreadSafer &)=delete
 
ThreadSaferoperator= (ThreadSafer &&)=delete
 
Proxy safe (void)
 Allows safe access to the object. More...
 

Constructor & Destructor Documentation

◆ ThreadSafer() [1/2]

template<class Unsafe >
ThreadSafer< Unsafe >::ThreadSafer ( Unsafe &  obj)
inline

Constructor. Create the needed infrastructure for making obj safe.

Definition at line 719 of file CppAddons.h.

◆ ThreadSafer() [2/2]

template<class Unsafe >
ThreadSafer< Unsafe >::ThreadSafer ( const ThreadSafer< Unsafe > &  )
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.

Member Function Documentation

◆ safe()

template<class Unsafe >
Proxy ThreadSafer< Unsafe >::safe ( void  )
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.

◆ ThreadSafer::Proxy

class ThreadSafer::Proxy
template<class Unsafe>
class ThreadSafer< Unsafe >::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
 
Proxyoperator= (const Proxy &)=delete
 
Proxyoperator= (Proxy &&)=delete
 
Unsafe * operator-> (void)
 Accessor. More...
 

Constructor & Destructor Documentation

◆ Proxy() [1/2]

template<class Unsafe >
ThreadSafer< Unsafe >::Proxy::Proxy ( Unsafe *  obj,
std::mutex &  mux 
)
inline

Constructor.

Definition at line 696 of file CppAddons.h.

◆ Proxy() [2/2]

template<class Unsafe >
ThreadSafer< Unsafe >::Proxy::Proxy ( Proxy &&  oth)
inline

Move constructor.

Definition at line 699 of file CppAddons.h.

◆ ~Proxy()

template<class Unsafe >
ThreadSafer< Unsafe >::Proxy::~Proxy ( void  )
inline

Destructor.

Definition at line 702 of file CppAddons.h.

Member Function Documentation

◆ operator->()

template<class Unsafe >
Unsafe * ThreadSafer< Unsafe >::Proxy::operator-> ( void  )
inline

Accessor.

Definition at line 709 of file CppAddons.h.

Macro Definition Documentation

◆ BINTOSTREAM

#define BINTOSTREAM (   v,
 
)    (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.

◆ JUSTFILENAME

#define JUSTFILENAME

#include <CppAddons.h>

Value:
(strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : \
(strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : \
__FILE__) )

Strips FILE from entire path.

https://stackoverflow.com/questions/8487986/file-macro-shows-full-path

Definition at line 65 of file CppAddons.h.

◆ DEBUGPREFFIX

#define DEBUGPREFFIX (   txt)

#include <CppAddons.h>

Value:
{ \
std::ostringstream stringStream; \
stringStream << \
" [" << JUSTFILENAME << ":" << __FUNCTION__ \
<< "@" << __LINE__ \
<< "]";\
txt = stringStream.str(); \
}
#define JUSTFILENAME
Strips FILE from entire path.
Definition: CppAddons.h:65

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.

◆ RUNTIMEEXCEP

#define RUNTIMEEXCEP (   txt)

#include <CppAddons.h>

Value:
{ std::string pdebugpreffix;\
DEBUGPREFFIX(pdebugpreffix);\
throw(std::runtime_error(pdebugpreffix + ": " + \
txt)); }

Raise a runtime exception with the given std::string TXT + additional info.

Definition at line 93 of file CppAddons.h.

◆ INTERNALERROR

#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.

Enumeration Type Documentation

◆ IntervalCl

enum class IntervalCl : uint8_t
strong

#include <CppAddons.h>

Kinds of closedness for intervals.

Definition at line 458 of file CppAddons.h.

Function Documentation

◆ string_hex_to_num()

template<typename NumType >
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.

◆ num_to_string_hex()

template<typename NumType >
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.

◆ hex_dump()

template<typename PointedType >
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.

◆ pointer_to_number()

LongestUnsignedType pointer_to_number ( const void *  p)
inlinenoexcept

#include <CppAddons.h>

Return the numerical value (address) of the given pointer.

Definition at line 871 of file CppAddons.h.

◆ all_bits_set_up_to_n()

constexpr uint8_t all_bits_set_up_to_n ( uint8_t  n)
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.

◆ reverse_bits()

template<typename T , unsigned char N = sizeof(T)*CHAR_BIT>
T reverse_bits ( 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.

◆ count_bits_set()

template<typename T >
unsigned char count_bits_set ( v)

#include <CppAddons.h>

Count the number of bits that are set in an integral value.

Definition at line 918 of file CppAddons.h.

◆ most_significant_bit_set()

template<typename T , unsigned char N = sizeof(T)*CHAR_BIT>
unsigned char most_significant_bit_set ( 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.

◆ least_significant_bit_set()

template<typename T , unsigned char N = sizeof(T)*CHAR_BIT>
unsigned char least_significant_bit_set ( 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.

◆ more_bits_set()

template<typename T >
bool more_bits_set ( b1,
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.

◆ substract_bits()

template<typename T >
T substract_bits ( b1,
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.

◆ new_bits_set()

template<typename T >
bool new_bits_set ( b1,
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.

◆ bits_of_constexprvalue()

template<unsigned char N>
constexpr unsigned char bits_of_constexprvalue ( )
constexpr

#include <CppAddons.h>

Return the number of bits needed by a constexpr value N.

Definition at line 1025 of file CppAddons.h.

◆ bits_of_value()

template<typename T >
unsigned char bits_of_value ( v)

#include <CppAddons.h>

The same as bits_of_constexprvalue(v) but for any variable value.

Definition at line 1037 of file CppAddons.h.

◆ for_within()

template<class TIter , class TBody >
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.

◆ pad_string()

void pad_string ( std::string &  s,
size_t  len,
bool  leftorright = true,
char  padding = '\t' 
)
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.

◆ utime_now()

LongestUnsignedType utime_now ( void  )
inline

#include <CppAddons.h>

Return the current time since epoch in microseconds.

Definition at line 1116 of file CppAddons.h.

Variable Documentation

◆ PI

const double PI = std::acos(-1.0)
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.