SMTK  @SMTK_VERSION@
Simulation Modeling Tool Kit
Public Member Functions | Static Public Member Functions | Static Public Attributes | Static Protected Member Functions | Protected Attributes | Static Protected Attributes | List of all members
smtk::string::Token Class Reference

A string token identified by an integer. More...

#include <Token.h>

Collaboration diagram for smtk::string::Token:
[legend]

Public Member Functions

 Token (const char *data=nullptr, std::size_t size=std::string::npos)
 Construct a token from a string literal.
 
 Token (const std::string &data)
 Construct a token from a std::string.
 
constexpr Token (Hash tokenId) noexcept
 Construct a token given its hash value. More...
 
Hash id () const
 Return the token's ID (usually its hash but possibly not in the case of collisions).
 
const std::string & data () const
 Return the string corresponding to the token.
 
bool hasData () const
 Return true if a string corresponding to the token exists.
 
bool valid () const
 Return true if the token's ID has been set (false if not).
 
bool operator== (const Token &other) const
 Fast equality comparison (compares hashes, not strings).
 
bool operator!= (const Token &other) const
 Fast inequality comparison (compares hashes, not strings).
 
bool operator< (const Token &other) const
 Slow, but unsurprising string comparison (preserves lexical string ordering).
 
bool operator> (const Token &other) const
 
bool operator<= (const Token &other) const
 
bool operator>= (const Token &other) const
 

Static Public Member Functions

static Managermanager ()
 Return the database of strings and their tokens (hashes).
 
static constexpr Hash stringHash (const char *data, std::size_t size) noexcept
 Return the hash of a string This is used internally but also by the ""_token() literal operator.
 
static Token fromHash (Hash h)
 Construct a token given only its hash. More...
 

Static Public Attributes

static constexpr Hash Invalid = Manager::Invalid
 An invalid hash, used with the constructor above to make an invalid token.
 

Static Protected Member Functions

template<typename T >
static constexpr std::enable_if< sizeof(T)==4, T >::type hash_fnv1a_const (const char *str, std::size_t size, T value) noexcept
 
template<typename T >
static constexpr std::enable_if< sizeof(T)==4, std::size_t >::type hash_fnv1a_seed ()
 
template<typename T >
static constexpr std::enable_if< sizeof(T)==8, std::size_t >::type hash_fnv1a_const (const char *str, std::size_t size, uint64_t value) noexcept
 
template<typename T >
static constexpr std::enable_if< sizeof(T)==8, std::size_t >::type hash_fnv1a_seed ()
 

Protected Attributes

Hash m_id
 

Static Protected Attributes

static constexpr uint32_t hash32a_const = 0x811c9dc5
 
static constexpr uint32_t hash32b_const = 0x1000193
 
static constexpr uint64_t hash64a_const = 0xcbf29ce484222325
 
static constexpr uint64_t hash64b_const = 0x100000001b3
 
static std::shared_ptr< Managers_manager
 

Detailed Description

A string token identified by an integer.

Often, it is useful for a common, variable-length string to be referenced indirectly rather than storing copies of the string. This class has a static database of hashes; each instance of the class holds only a fixed-size hash value. The string value can be produced in O(1) time by looking the hash up from the static database.

Constructor & Destructor Documentation

◆ Token()

constexpr smtk::string::Token::Token ( Hash  tokenId)
inlineconstexprnoexcept

Construct a token given its hash value.

NOTE: This will NOT insert a string into the manager as other constructors do.

Member Function Documentation

◆ fromHash()

Token smtk::string::Token::fromHash ( Hash  h)
static

Construct a token given only its hash.

This variant checks that the string manager holds data for the hash and will throw an exception if it does not.


The documentation for this class was generated from the following files: