Utilities
Utilities header only library
utils::StrongId< PhantomType > Struct Template Reference

Template class implementation of a Named Strong Id (a Strong Type with an underlying unsigned integer type (uint32_t)). More...

Inheritance diagram for utils::StrongId< PhantomType >:
[legend]

Public Types

using ValueType = uint32_t
 

Public Member Functions

 StrongId (size_t value)
 Construct from size_t.
 
 StrongId (int value)
 Construct from int.
 
StrongId Next () const
 An id following this one. Incremented by 1.
 
std::string ToString () const
 Convert the id to string.
 
- Public Member Functions inherited from utils::StrongType< uint32_t, PhantomType >
 StrongType (uint32_t value)
 
std::enable_if_t< std::is_arithmetic_v< RT >, RT > As () const
 Cast to a specified type. Only work for arithmetic types.
 
bool operator== (const StrongType &other) const
 
bool operator!= (const StrongType &other) const
 
bool operator< (const StrongType &other) const
 
bool operator> (const StrongType &other) const
 
const uint32_t & operator* () const
 Get underlying value using the * operator.
 

Static Public Member Functions

static std::vector< StrongId< PhantomType > > CreateIds (const std::vector< unsigned > &ids)
 Creates vector of specific strong ids values from the vector of unsigned values.
 

Additional Inherited Members

- Protected Attributes inherited from utils::StrongType< uint32_t, PhantomType >
uint32_t value_
 The actual underlying value.
 

Detailed Description

template<typename PhantomType>
struct utils::StrongId< PhantomType >

Template class implementation of a Named Strong Id (a Strong Type with an underlying unsigned integer type (uint32_t)).

Note
https://www.fluentcpp.com/2016/12/08/strong-types-for-strong-interfaces/ Strong Id is an integral type (uint32_t) specific for the given usage (usually as an id of a class). It prevents implicit conversions to and from other types, as well as comparisons to other types.
Template Parameters
PhantomTypeAn unused type specific for each Strong Type to ensure the Strong Type is different from every other type.