walberla::uid::UID< T > Class Template Reference

Detailed Description

template<typename T>
class walberla::uid::UID< T >

Class for creating, storing, and managing unique identifiers.

Unique identifiers (UID) can be compared and follow a strict total order (==, !=, >, >=, <, <=). Additionally, every UID is associated with a unique identifier string, meaning that two UIDs that share the same numerical representation (uid_) are also associated with the same identifier string. Furthermore, it is not possible to create two UIDs that have different numerical representations but are both associated with the same identifier string. In other words: two UIDs / UID objects either have the same numerical representation (uid_) and the same identifier string or they have a different numerical representation and a different identifier string.

In addition to being constructed via constructor, UIDs can also be constructed via two static member functions:

getOrConstruct( const std::string& identifier ) and get( const std::string& identifier )

For more information on how to create UIDs see the documentation of constructors and these two functions (UIDs can also be copied / copy constructed).

The template T specifies a class that is used to generate the numerical representations (uid_) for all UID<T>s. T must be an implementation of a UID generator class (see "UIDGenerators.h"). Every UID generator implements a static member function "uint_type generatUID()" which returns a new UID every time it is called. Additionally, UID generators implement a static member function "uint_type toIndex( const uint_type uid )" which translates the numerical representations of a given UID into an index which, for example, can be used for array access/storage. Moreover, UID generators implement a static member function "uint_type toBitMask( const uint_type uid )" which translates the numerical UID representations into a bit mask. For further information see the documentation of the two member functions "toIndex()" and "toBitMask()" of this class.

The class UID is thread safe and follows a multiple-readers / single-writer pattern.

#include <InitializationManager.h>

Public Types

using uint_type = typename T::uint_type
 

Public Member Functions

 UID (const char *const identifier, const bool newUid=false, const bool appendUIDtoIdentifier=false)
 For documentation see member function 'init'. More...
 
 UID (const std::string &identifier, const bool newUid=false, const bool appendUIDtoIdentifier=false)
 For documentation see member function 'init'. More...
 
 UID (const bool createAnonymousUID=false)
 The default constructor (-> createAnonymousUID is false by default!) is used to create uninitialized, invalid UID objects (by just declaring a variable without initializing it at declaration: "UID<T> uid; [...]; uid = [...];"). More...
 
uint_type getUid () const
 
const std::string & getIdentifier () const
 
uint_type toIndex () const
 Returns '0' for the first UID created by T::generateUID(), '1' for the second, '2' for the third, etc. More...
 
uint_type toBitMask () const
 Returns '[...] 0001' for the first UID created by T::generateUID(), '[...] 0010' for the second, '[...] 0100' for the third, etc. More...
 
void toStream (std::ostream &os) const
 
std::string toString () const
 
bool operator== (const UID< T > &uid) const
 
bool operator!= (const UID< T > &uid) const
 
bool operator> (const UID< T > &uid) const
 
bool operator>= (const UID< T > &uid) const
 
bool operator< (const UID< T > &uid) const
 
bool operator<= (const UID< T > &uid) const
 

Static Public Member Functions

static bool exists (const std::string &identifier)
 
static UID< T > getOrConstruct (const std::string &identifier)
 Either returns a copy of an existing UID or creates and returns a completely new UID depending on whether a UID that is associated with the identifier string "identifier" already exists or not. More...
 
static UID< T > get (const std::string &identifier)
 Returns the UID that is associated with the identifier string "identifier". More...
 

Private Member Functions

 UID (const uint_type uid)
 Must not be made public! Do not use this constructor. More...
 
void init (const std::string &identifier, const bool newUid, const bool appendUIDtoIdentifier)
 This function is called by two constructors in order to create new UIDs or to retrieve copies of existing UIDs. More...
 

Static Private Member Functions

static std::map< uint_type, std::string > & uidToString ()
 
static std::map< std::string, uint_type > & stringToUid ()
 
static std::shared_timed_mutex & uidToStringMutex ()
 
static std::shared_timed_mutex & stringToUidMutex ()
 

Private Attributes

uint_type uid_
 
bool valid_
 

Member Typedef Documentation

◆ uint_type

template<typename T >
using walberla::uid::UID< T >::uint_type = typename T::uint_type

Constructor & Destructor Documentation

◆ UID() [1/4]

template<typename T >
walberla::uid::UID< T >::UID ( const char *const  identifier,
const bool  newUid = false,
const bool  appendUIDtoIdentifier = false 
)
inline

For documentation see member function 'init'.

Do not delete this constructor! This constructor is required, so that writing "UID<TYPE> uid("id")" does not lead to calling the constructor "UID( const bool createAnonymousUID )".

◆ UID() [2/4]

template<typename T >
walberla::uid::UID< T >::UID ( const std::string &  identifier,
const bool  newUid = false,
const bool  appendUIDtoIdentifier = false 
)
inline

For documentation see member function 'init'.

◆ UID() [3/4]

template<typename T >
walberla::uid::UID< T >::UID ( const bool  createAnonymousUID = false)
explicit

The default constructor (-> createAnonymousUID is false by default!) is used to create uninitialized, invalid UID objects (by just declaring a variable without initializing it at declaration: "UID<T> uid; [...]; uid = [...];").

In debug mode, every operation involving such an uninitialized UID will trigger an assertion and fail. If createAnonymousUID is true, a valid UID with an unspecified identifier string is created.

◆ UID() [4/4]

template<typename T >
walberla::uid::UID< T >::UID ( const uint_type  uid)
inlineexplicitprivate

Must not be made public! Do not use this constructor.

This constructor is intended for internal use only.

Member Function Documentation

◆ exists()

template<typename T >
static bool walberla::uid::UID< T >::exists ( const std::string &  identifier)
inlinestatic

◆ get()

template<typename T >
UID< T > walberla::uid::UID< T >::get ( const std::string &  identifier)
inlinestatic

Returns the UID that is associated with the identifier string "identifier".

In debug mode, this function will trigger an assertion and fail if no UID exists that is associated with this identifier string.

◆ getIdentifier()

template<typename T >
const std::string & walberla::uid::UID< T >::getIdentifier
inline

◆ getOrConstruct()

template<typename T >
UID< T > walberla::uid::UID< T >::getOrConstruct ( const std::string &  identifier)
static

Either returns a copy of an existing UID or creates and returns a completely new UID depending on whether a UID that is associated with the identifier string "identifier" already exists or not.

◆ getUid()

template<typename T >
uint_type walberla::uid::UID< T >::getUid ( ) const
inline

◆ init()

template<typename T >
void walberla::uid::UID< T >::init ( const std::string &  identifier,
const bool  newUid,
const bool  appendUIDtoIdentifier 
)
private

This function is called by two constructors in order to create new UIDs or to retrieve copies of existing UIDs.

If newUid is true, a completely new UID is created and associated with the string "identifier". In debug mode, creation of a new UID will trigger an assertion and fail if another UID with the same identifier string already exists. If newUid is false (-> default value, can be omitted), a copy of an existing UID is returned if the identifier string already exists, otherwise a new UID is created and associated with "identifier" (this behavior is identical to the static member function "getOrConstruct"). If newUid is true AND appendUIDtoIdentifier is also true, the newly created UID is appended to the provided identifier string.

Must not be made public! Do not use this function. This function is intended for internal use only.

◆ operator!=()

template<typename T >
bool walberla::uid::UID< T >::operator!= ( const UID< T > &  uid) const
inline

◆ operator<()

template<typename T >
bool walberla::uid::UID< T >::operator< ( const UID< T > &  uid) const
inline

◆ operator<=()

template<typename T >
bool walberla::uid::UID< T >::operator<= ( const UID< T > &  uid) const
inline

◆ operator==()

template<typename T >
bool walberla::uid::UID< T >::operator== ( const UID< T > &  uid) const
inline

◆ operator>()

template<typename T >
bool walberla::uid::UID< T >::operator> ( const UID< T > &  uid) const
inline

◆ operator>=()

template<typename T >
bool walberla::uid::UID< T >::operator>= ( const UID< T > &  uid) const
inline

◆ stringToUid()

template<typename T >
static std::map< std::string, uint_type >& walberla::uid::UID< T >::stringToUid ( )
inlinestaticprivate

◆ stringToUidMutex()

template<typename T >
static std::shared_timed_mutex& walberla::uid::UID< T >::stringToUidMutex ( )
inlinestaticprivate

◆ toBitMask()

template<typename T >
uint_type walberla::uid::UID< T >::toBitMask ( ) const
inline

Returns '[...] 0001' for the first UID created by T::generateUID(), '[...] 0010' for the second, '[...] 0100' for the third, etc.

The value returned by this function may or may not be equal to "uid_" - depending on the underlying UID generator T.

◆ toIndex()

template<typename T >
uint_type walberla::uid::UID< T >::toIndex ( ) const
inline

Returns '0' for the first UID created by T::generateUID(), '1' for the second, '2' for the third, etc.

The value returned by this function may or may not be equal to "uid_" - depending on the underlying UID generator T.

◆ toStream()

template<typename T >
void walberla::uid::UID< T >::toStream ( std::ostream &  os) const
inline

◆ toString()

template<typename T >
std::string walberla::uid::UID< T >::toString
inline

◆ uidToString()

template<typename T >
static std::map< uint_type, std::string >& walberla::uid::UID< T >::uidToString ( )
inlinestaticprivate

◆ uidToStringMutex()

template<typename T >
static std::shared_timed_mutex& walberla::uid::UID< T >::uidToStringMutex ( )
inlinestaticprivate

Member Data Documentation

◆ uid_

template<typename T >
uint_type walberla::uid::UID< T >::uid_
private

◆ valid_

template<typename T >
bool walberla::uid::UID< T >::valid_
private

The documentation for this class was generated from the following file:
static UID< T > getOrConstruct(const std::string &identifier)
Either returns a copy of an existing UID or creates and returns a completely new UID depending on whe...
Definition: UID.h:289
static UID< T > get(const std::string &identifier)
Returns the UID that is associated with the identifier string "identifier".
Definition: UID.h:320