COMMS
Template library intended to help with implementation of communication protocols.
Namespaces | Classes | Typedefs | Functions
comms::util Namespace Reference

Namespace for various utility classes and/or functions used by COMMS library. More...

Detailed Description

Namespace for various utility classes and/or functions used by COMMS library.

Namespaces

 alloc
 Namespace for various allocation classes.
 
 detect
 Namespace for various compile-time detection operations of whether a type has certain member types and/or member functions.
 
 traits
 Namespace for various utilities related trait classes.
 

Classes

class  ArrayView
 Describes an object that can refer to a constant contiguous sequence of other objects. More...
 
struct  Conditional
 Replacement to std::conditional. More...
 
struct  IsInTuple
 Check whether TType type is included in the tuple TTuple. More...
 
struct  IsTuple
 Check whether provided type is a variant of std::tuple. More...
 
class  ScopeGuard
 Implements Scope Guard Idiom. More...
 
class  StaticString
 Replacement to std::string when no dynamic memory allocation is allowed. More...
 
class  StaticVector
 Replacement to std::vector when no dynamic memory allocation is allowed. More...
 
class  StringView
 Describes an object that can refer to a constant contiguous sequence of char-like objects with the first element of the sequence at position zero. More...
 
struct  TupleAsAlignedUnion
 Calculated "aligned union" storage type for all the types in provided tuple. More...
 
struct  TupleCat
 Provides the type of std::tuple_cat operation. More...
 

Typedefs

template<bool TCond, typename TTrue , typename TFalse >
using ConditionalT = typename Conditional< TCond >::template Type< TTrue, TFalse >
 Replacement to std::conditional.
 
template<typename TTuple >
using TupleAsAlignedUnionT = typename TupleAsAlignedUnion< TTuple >::Type
 Alias to TupleAsAlignedUnion::Type.
 

Functions

template<typename T , typename TIter >
void assign (T &obj, TIter from, TIter to)
 Assigns a new value to provided object. More...
 
template<typename TType >
constexpr bool isTuple ()
 Check whether provided type is a variant of std::tuple. More...
 
template<typename T , std::size_t TSize, typename TIter >
readBig (TIter &iter)
 Read part of integral value from the input area using big endian notation. More...
 
template<typename T , typename TIter >
readBig (TIter &iter)
 Read integral value from the input area using big endian notation. More...
 
template<typename T , typename TIter >
readBig (TIter &iter, std::size_t size)
 Read integral value from the input area using big endian notation. More...
 
template<typename T , typename TIter >
readData (TIter &iter, const traits::endian::Big &endian)
 Same as readBig<T, TIter>()
 
template<typename T , std::size_t TSize, typename TIter >
readData (TIter &iter, const traits::endian::Big &endian)
 Same as readBig<T, TSize, TIter>()
 
template<typename T , typename TIter >
readData (TIter &iter, const traits::endian::Little &endian)
 Same as readLittle<T, TIter>()
 
template<typename T , std::size_t TSize, typename TIter >
readData (TIter &iter, const traits::endian::Little &endian)
 Same as readData<T, TSize, TIter>()
 
template<typename T , typename TIter >
readData (TIter &iter, std::size_t size, const traits::endian::Big &endian)
 Same as readBig<T, TIter>()
 
template<typename T , typename TIter >
readData (TIter &iter, std::size_t size, const traits::endian::Little &endian)
 Same as readBig<T, TIter>()
 
template<typename T , std::size_t TSize, typename TIter >
readLittle (TIter &iter)
 Read part of integral value from the input area using little endian notation. More...
 
template<typename T , typename TIter >
readLittle (TIter &iter)
 Read integral value from the input area using little endian notation. More...
 
template<typename T , typename TIter >
readLittle (TIter &iter, std::size_t size)
 Read integral value from the input area using little endian notation. More...
 
template<typename TTuple , typename TValue , typename TFunc >
constexpr TValue tupleAccumulate (TTuple &&tuple, const TValue &value, TFunc &&func)
 Performs "accumulate" algorithm on every element of the tuple. More...
 
template<std::size_t TFrom, std::size_t TUntil, typename TTuple , typename TValue , typename TFunc >
constexpr TValue tupleAccumulateFromUntil (TTuple &&tuple, const TValue &value, TFunc &&func)
 Performs "accumulate" algorithm on every element of the tuple. More...
 
template<typename TTuple , typename TFunc >
void tupleForEach (TTuple &&tuple, TFunc &&func)
 Invoke provided functor for every element in the tuple. More...
 
template<std::size_t TIdx, typename TTuple , typename TFunc >
void tupleForEachFrom (TTuple &&tuple, TFunc &&func)
 Invoke provided functor for every element in the tuple starting from element with specified index. More...
 
template<std::size_t TFromIdx, std::size_t TUntilIdx, typename TTuple , typename TFunc >
void tupleForEachFromUntil (TTuple &&tuple, TFunc &&func)
 Invoke provided functor for every element in the tuple which indices are in range [TFromIdx, TUntilIdx). More...
 
template<typename TTuple , typename TFunc >
void tupleForEachType (TFunc &&func)
 Invoke provided functor for every type in the tuple. More...
 
template<std::size_t TIdx, typename TTuple , typename TFunc >
void tupleForEachUntil (TTuple &&tuple, TFunc &&func)
 Invoke provided functor for every element in the tuple until element with specified index is reached. More...
 
template<typename TTuple , typename TFunc >
void tupleForEachWithIdx (TTuple &&tuple, TFunc &&func)
 Invoke provided functor for every element in the tuple while providing information about element index in the tuple. More...
 
template<typename TTuple , typename TFunc >
void tupleForEachWithTemplateParamIdx (TTuple &&tuple, TFunc &&func)
 Invoke provided functor for every element in the tuple while providing information about element index in the tuple as a template parameter. More...
 
template<typename TTuple , typename TFunc >
void tupleForSelectedType (std::size_t idx, TFunc &&func)
 Invoke provided functor for a selected type when element index is known only at run time. More...
 
template<typename TTail , typename TTuple >
constexpr bool tupleIsTailOf ()
 Compile time check of whether one tuple is a "tail" of another. More...
 
template<typename TTuple , typename TValue , typename TFunc >
constexpr TValue tupleTypeAccumulate (const TValue &value, TFunc &&func)
 Performs "accumulate" algorithm on every type of the tuple. More...
 
template<std::size_t TFrom, std::size_t TUntil, typename TTuple , typename TValue , typename TFunc >
constexpr TValue tupleTypeAccumulateFromUntil (const TValue &value, TFunc &&func)
 Performs "accumulate" algorithm on specified types inside the tuple. More...
 
template<typename TTuple , typename TFunc >
constexpr bool tupleTypeIsAnyOf (TFunc &&func)
 Compile time check of whether any type within a tuple has a certain condition. More...
 
template<typename T , typename TIter >
void writeBig (T value, std::size_t size, TIter &iter)
 Write part of integral value into the output area using big endian notation. More...
 
template<std::size_t TSize, typename T , typename TIter >
void writeBig (T value, TIter &iter)
 Write part of integral value into the output area using big endian notation. More...
 
template<typename T , typename TIter >
void writeBig (T value, TIter &iter)
 Write integral value into the output area using big endian notation. More...
 
template<typename T , typename TIter >
void writeData (T value, std::size_t size, TIter &iter, const traits::endian::Big &endian)
 Same as writeBig<T, TIter>()
 
template<typename T , typename TIter >
void writeData (T value, std::size_t size, TIter &iter, const traits::endian::Little &endian)
 Same as writeLittle<T, TIter>()
 
template<typename T , typename TIter >
void writeData (T value, TIter &iter, const traits::endian::Big &endian)
 Same as writeBig<T, TIter>()
 
template<std::size_t TSize, typename T , typename TIter >
void writeData (T value, TIter &iter, const traits::endian::Big &endian)
 Same as writeBig<TSize, T, TIter>()
 
template<typename T , typename TIter >
void writeData (T value, TIter &iter, const traits::endian::Little &endian)
 Same as writeLittle<T, TIter>()
 
template<std::size_t TSize, typename T , typename TIter >
void writeData (T value, TIter &iter, const traits::endian::Little &endian)
 Same as writeLittle<TSize, T, TIter>()
 
template<typename T , typename TIter >
void writeLittle (T value, std::size_t size, TIter &iter)
 Write part of integral value into the output area using little endian notation. More...
 
template<std::size_t TSize, typename T , typename TIter >
void writeLittle (T value, TIter &iter)
 Write part of integral value into the output area using little endian notation. More...
 
template<typename T , typename TIter >
void writeLittle (T value, TIter &iter)
 Write integral value into the output area using big endian notation. More...
 

Function Documentation

◆ assign()

template<typename T , typename TIter >
void comms::util::assign ( T &  obj,
TIter  from,
TIter  to 
)

Assigns a new value to provided object.

The function detects at compile-time presence of assign() member function and uses it. In case the assign() member function does not exist (for types like std::string_view or std::span) the function checks for presence of a constructor that can receive a pointer to the first element + number of elements (size) and uses it instead to create temporary object and then uses move / copy constructor to assign the value.

static const std::string Str("hello");
std::string s1;
comms::util::assign(s1, Str.begin(), Str.end()) // <-- equivalent to s1.assign(Str.begin(), Str.end());
std::string_view s2;
comms::util::assign(s2, Str.begin(), Str.end()) // <-- equivalent to s2 = std::string_view(&(*Str.begin()), std::distance(Str.begin(), Str.end()));
void assign(T &obj, TIter from, TIter to)
Assigns a new value to provided object.
Definition: assign.h:39
Main namespace for all classes / functions of COMMS library.

◆ isTuple()

template<typename TType >
constexpr bool comms::util::isTuple ( )
constexpr

Check whether provided type is a variant of std::tuple.

Template Parameters
TTypeType to check.

◆ readBig() [1/3]

template<typename T , std::size_t TSize, typename TIter >
T comms::util::readBig ( TIter &  iter)

Read part of integral value from the input area using big endian notation.

Template Parameters
TType to read.
TSizeNumber of bytes to read.
Parameters
[in,out]iterInput iterator.
Returns
Read value
Precondition
TSize <= sizeof(T).
The iterator must be valid and can be successfully dereferenced and incremented at least TSize times.
Postcondition
The iterator is advanced.

◆ readBig() [2/3]

template<typename T , typename TIter >
T comms::util::readBig ( TIter &  iter)

Read integral value from the input area using big endian notation.

Template Parameters
TType to read.
Parameters
[in,out]iterInput iterator.
Returns
Read value
Precondition
The iterator must be valid and can be successfully dereferenced and incremented at least sizeof(T) times.
Postcondition
The iterator is advanced.

◆ readBig() [3/3]

template<typename T , typename TIter >
T comms::util::readBig ( TIter &  iter,
std::size_t  size 
)

Read integral value from the input area using big endian notation.

Template Parameters
TType to read.
Parameters
[in,out]iterInput iterator.
[in]sizelength to read
Returns
Read value
Precondition
The iterator must be valid and can be successfully dereferenced and incremented at least sizeof(T) times.
Postcondition
The iterator is advanced.

◆ readLittle() [1/3]

template<typename T , std::size_t TSize, typename TIter >
T comms::util::readLittle ( TIter &  iter)

Read part of integral value from the input area using little endian notation.

Template Parameters
TType to read.
TSizeNumber of bytes to read.
Parameters
[in,out]iterInput iterator.
Returns
Read value
Precondition
TSize <= sizeof(T).
The iterator must be valid and can be successfully dereferenced and incremented at least TSize times.
Postcondition
The iterator is advanced.

◆ readLittle() [2/3]

template<typename T , typename TIter >
T comms::util::readLittle ( TIter &  iter)

Read integral value from the input area using little endian notation.

Template Parameters
TType to read.
Parameters
[in,out]iterInput iterator.
Returns
Read value
Precondition
The iterator must be valid and can be successfully dereferenced and incremented at least sizeof(T) times.
Postcondition
The iterator is advanced.

◆ readLittle() [3/3]

template<typename T , typename TIter >
T comms::util::readLittle ( TIter &  iter,
std::size_t  size 
)

Read integral value from the input area using little endian notation.

Template Parameters
TType to read.
Parameters
[in,out]iterInput iterator.
[in]sizelength to read
Returns
Read value
Precondition
The iterator must be valid and can be successfully dereferenced and incremented at least sizeof(T) times.
Postcondition
The iterator is advanced.

◆ tupleAccumulate()

template<typename TTuple , typename TValue , typename TFunc >
constexpr TValue comms::util::tupleAccumulate ( TTuple &&  tuple,
const TValue &  value,
TFunc &&  func 
)
constexpr

Performs "accumulate" algorithm on every element of the tuple.

The algorithm invokes operator() of the provided functor object with initial value and first element of the tuple, then provides the returned value as a parameter to the next invocation of operator() and second element in the tuple, and so on until all elements in the tuple is handled.

Parameters
[in]tupleReference (l- or r-value) to tuple object.
[in]valueInitial value.
[in]funcFunctor object. The class must provide operator() with the following signature:
struct MyFunc
{
template <typename TValue, typename TTupleElem>
TValue operator()(const TValue& value, TTupleElem&& elem) {...}
};
Returns
Returns the result of the last invocation of the functor's operator().

◆ tupleAccumulateFromUntil()

template<std::size_t TFrom, std::size_t TUntil, typename TTuple , typename TValue , typename TFunc >
constexpr TValue comms::util::tupleAccumulateFromUntil ( TTuple &&  tuple,
const TValue &  value,
TFunc &&  func 
)
constexpr

Performs "accumulate" algorithm on every element of the tuple.

Similar to tupleAccumulate(), but allows specifying range of indices of tuple elements.

Template Parameters
TFromIndex of the first tuple element to evaluate
TUntilIndex of the one past the last tuple element to evaluate.
Parameters
[in]tupleReference (l- or r-value) to tuple object.
[in]valueInitial value.
[in]funcFunctor object. The class must provide operator() with the following signature:
struct MyFunc
{
template <typename TValue, typename TTupleElem>
TValue operator()(const TValue& value, TTupleElem&& elem) {...}
};
Returns
Returns the result of the last invocation of the functor's operator().

◆ tupleForEach()

template<typename TTuple , typename TFunc >
void comms::util::tupleForEach ( TTuple &&  tuple,
TFunc &&  func 
)

Invoke provided functor for every element in the tuple.

The functor object class must define operator() with following signature:

struct MyFunc
{
template <typename TTupleElem>
void operator()(TTupleElem&& elem) {...}
};
Parameters
[in]tupleReference (l- or r-value) to tuple object.
[in]funcFunctor object.

◆ tupleForEachFrom()

template<std::size_t TIdx, typename TTuple , typename TFunc >
void comms::util::tupleForEachFrom ( TTuple &&  tuple,
TFunc &&  func 
)

Invoke provided functor for every element in the tuple starting from element with specified index.

Very similar to tupleForEach() function, but also receives index of the first element as a template parameter.

Template Parameters
TIdxIndex of the first element to invoke functor on.
Parameters
[in]tupleReference (l- or r-value) to tuple object.
[in]funcFunctor object.
Precondition
TIdx must be less than number of elements in the tuple.

◆ tupleForEachFromUntil()

template<std::size_t TFromIdx, std::size_t TUntilIdx, typename TTuple , typename TFunc >
void comms::util::tupleForEachFromUntil ( TTuple &&  tuple,
TFunc &&  func 
)

Invoke provided functor for every element in the tuple which indices are in range [TFromIdx, TUntilIdx).

Very similar to tupleForEach() function, but also receives indices of the first and last elements as a template parameters.

Template Parameters
TFromIdxIndex of the first element to invoke functor on.
TUntilIdxIndex of the last (not included) element.
Parameters
[in]tupleReference (l- or r-value) to tuple object.
[in]funcFunctor object.
Precondition
TFromIdx must be less than number of elements in the tuple.
TUntilIdx mustn't exceed number of elements in the tuple.
TFormIdx <= TUntilIdx

◆ tupleForEachType()

template<typename TTuple , typename TFunc >
void comms::util::tupleForEachType ( TFunc &&  func)

Invoke provided functor for every type in the tuple.

The functor object class must define operator() with following signature:

struct MyFunc
{
template <typename TTupleElem>
void operator()() {...}
};
Parameters
[in]funcFunctor object.

◆ tupleForEachUntil()

template<std::size_t TIdx, typename TTuple , typename TFunc >
void comms::util::tupleForEachUntil ( TTuple &&  tuple,
TFunc &&  func 
)

Invoke provided functor for every element in the tuple until element with specified index is reached.

Very similar to tupleForEach() function, but also receives index of the last element as a template parameter. The provided functor is NOT invoked for the element with index TIdx.

Template Parameters
TIdxIndex of the last (not included) element.
Parameters
[in]tupleReference (l- or r-value) to tuple object.
[in]funcFunctor object.
Precondition
TIdx mustn't exceed number of elements in the tuple.

◆ tupleForEachWithIdx()

template<typename TTuple , typename TFunc >
void comms::util::tupleForEachWithIdx ( TTuple &&  tuple,
TFunc &&  func 
)

Invoke provided functor for every element in the tuple while providing information about element index in the tuple.

Very similar to tupleForEach(), but the operator() in the functor receives additional information about index of the element. The functor object class must define operator() with following signature:

struct MyFunc
{
template <typename TTupleElem>
void operator()(TTupleElem&& elem, std::size_t idx) {...}
};
Parameters
[in]tupleReference (l- or r-value) to tuple object.
[in]funcFunctor object.

◆ tupleForEachWithTemplateParamIdx()

template<typename TTuple , typename TFunc >
void comms::util::tupleForEachWithTemplateParamIdx ( TTuple &&  tuple,
TFunc &&  func 
)

Invoke provided functor for every element in the tuple while providing information about element index in the tuple as a template parameter.

Very similar to tupleForEachWithIdx(), but the operator() in the functor receives additional information about index of the element as a template parameter instead of as argument to the function. The functor object class must define operator() with following signature:

struct MyFunc
{
template <std::size_t TIdx, typename TTupleElem>
void operator()(TTupleElem&& elem) {...}
};
Parameters
[in]tupleReference (l- or r-value) to tuple object.
[in]funcFunctor object.

◆ tupleForSelectedType()

template<typename TTuple , typename TFunc >
void comms::util::tupleForSelectedType ( std::size_t  idx,
TFunc &&  func 
)

Invoke provided functor for a selected type when element index is known only at run time.

The functor object class must define operator() with following signature:

struct MyFunc
{
// TIdx - index of the type inside the tuple
// TTupleElem - type inside the tuple
template <std::size_t TIdx, typename TTupleElem>
void operator()() {...}
};
Parameters
[in]idxIndex of the type in the tuple
[in]funcFunctor object.

◆ tupleIsTailOf()

template<typename TTail , typename TTuple >
constexpr bool comms::util::tupleIsTailOf ( )
constexpr

Compile time check of whether one tuple is a "tail" of another.

Template Parameters
TTailTail tuple
TTupleContaining tuple

◆ tupleTypeAccumulate()

template<typename TTuple , typename TValue , typename TFunc >
constexpr TValue comms::util::tupleTypeAccumulate ( const TValue &  value,
TFunc &&  func 
)
constexpr

Performs "accumulate" algorithm on every type of the tuple.

Very similar to tupleAccumulate(), but without actual tuple object, provides only type information to operator() of the functor.

Parameters
[in]valueInitial value.
[in]funcFunctor object. The class must provide operator() with the following signature:
struct MyFunc
{
template <typename TTupleElem, typename TValue>
TValue operator()(const TValue& value) {...}
};
Returns
Returns the result of the last invocation of the functor's operator().

◆ tupleTypeAccumulateFromUntil()

template<std::size_t TFrom, std::size_t TUntil, typename TTuple , typename TValue , typename TFunc >
constexpr TValue comms::util::tupleTypeAccumulateFromUntil ( const TValue &  value,
TFunc &&  func 
)
constexpr

Performs "accumulate" algorithm on specified types inside the tuple.

Very similar to tupleTypeAccumulate(), but allows specifying range of indices of tuple elements.

Template Parameters
TFromIndex of the first tuple type to evaluate
TUntilIndex of the one past the last tuple type to evaluate.
Parameters
[in]valueInitial value.
[in]funcFunctor object. The class must provide operator() with the following signature:
struct MyFunc
{
template <typename TTupleElem, typename TValue>
TValue operator()(const TValue& value) {...}
};
Returns
Returns the result of the last invocation of the functor's operator().

◆ tupleTypeIsAnyOf()

template<typename TTuple , typename TFunc >
constexpr bool comms::util::tupleTypeIsAnyOf ( TFunc &&  func)
constexpr

Compile time check of whether any type within a tuple has a certain condition.

The functor object class must define operator() with following signature:

struct MyFunc
{
// TTupleElem - type inside the tuple
template <typename TTupleElem>
constexpr bool operator()() const {...}
};
Template Parameters
TTupleTuple
TFuncFunctor object type

◆ writeBig() [1/3]

template<typename T , typename TIter >
void comms::util::writeBig ( value,
std::size_t  size,
TIter &  iter 
)

Write part of integral value into the output area using big endian notation.

Parameters
[in]valueIntegral type value to be written.
[in]sizeNumber of bytes to write.
[in,out]iterOutput iterator.
Precondition
TSize <= sizeof(T).
The iterator must be valid and can be successfully dereferenced and incremented at least TSize times.
Postcondition
The iterator is advanced.

◆ writeBig() [2/3]

template<std::size_t TSize, typename T , typename TIter >
void comms::util::writeBig ( value,
TIter &  iter 
)

Write part of integral value into the output area using big endian notation.

Template Parameters
TSizeNumber of bytes to write.
Parameters
[in]valueIntegral type value to be written.
[in,out]iterOutput iterator.
Precondition
TSize <= sizeof(T).
The iterator must be valid and can be successfully dereferenced and incremented at least TSize times.
Postcondition
The iterator is advanced.

◆ writeBig() [3/3]

template<typename T , typename TIter >
void comms::util::writeBig ( value,
TIter &  iter 
)

Write integral value into the output area using big endian notation.

Parameters
[in]valueIntegral type value to be written.
[in,out]iterOutput iterator.
Precondition
The iterator must be valid and can be successfully dereferenced and incremented at least sizeof(T) times.
Postcondition
The iterator is advanced.

◆ writeLittle() [1/3]

template<typename T , typename TIter >
void comms::util::writeLittle ( value,
std::size_t  size,
TIter &  iter 
)

Write part of integral value into the output area using little endian notation.

Parameters
[in]valueIntegral type value to be written.
[in]sizeNumber of bytes to write.
[in,out]iterOutput iterator.
Precondition
TSize <= sizeof(T).
The iterator must be valid and can be successfully dereferenced and incremented at least TSize times.
Postcondition
The iterator is advanced.

◆ writeLittle() [2/3]

template<std::size_t TSize, typename T , typename TIter >
void comms::util::writeLittle ( value,
TIter &  iter 
)

Write part of integral value into the output area using little endian notation.

Template Parameters
TSizeNumber of bytes to write.
Parameters
[in]valueIntegral type value to be written.
[in,out]iterOutput iterator.
Precondition
TSize <= sizeof(T).
The iterator must be valid and can be successfully dereferenced and incremented at least TSize times.
Postcondition
The iterator is advanced.

◆ writeLittle() [3/3]

template<typename T , typename TIter >
void comms::util::writeLittle ( value,
TIter &  iter 
)

Write integral value into the output area using big endian notation.

Parameters
[in]valueIntegral type value to be written.
[in,out]iterOutput iterator.
Precondition
The iterator must be valid and can be successfully dereferenced and incremented at least sizeof(T) times.
Postcondition
The iterator is advanced.