7#ifndef PXR_BASE_TF_TOKEN_H
8#define PXR_BASE_TF_TOKEN_H
17#include "pxr/base/tf/api.h"
20#include "pxr/base/tf/hashset.h"
21#include "pxr/base/tf/pointerAndBits.h"
29PXR_NAMESPACE_OPEN_SCOPE
73 enum _ImmortalTag { Immortal };
114 TF_API
explicit TfToken(std::string
const& s);
120 TF_API
TfToken(std::string
const& s, _ImmortalTag);
146 inline size_t Hash()
const;
150 size_t operator()(
TfToken const& token)
const {
return token.
Hash(); }
158 typedef TfHashSet<TfToken, TfToken::HashFunctor>
HashSet;
166 typedef std::set<TfToken, TfTokenFastArbitraryLessThan>
Set;
170 _Rep
const *rep = _rep.
Get();
171 return rep ? rep->_str.size() : 0;
180 _Rep
const *rep = _rep.
Get();
181 return rep ? rep->_str.c_str() :
"";
191 _Rep
const *rep = _rep.
Get();
192 return rep ? rep->_str : _GetEmptyString();
197 std::swap(_rep, other._rep);
203 return _rep.
Get() == o._rep.
Get();
208 return !(*
this == o);
232 return !(*
this == o);
243 return !(*
this == o);
262 auto lrep = _rep.
Get(), rrep = r._rep.
Get();
263 uint64_t lcc = lrep->_compareCode, rcc = rrep->_compareCode;
264 return lcc < rcc || (lcc == rcc && lrep->_str < rrep->_str);
285 operator std::string
const& ()
const {
return GetString(); }
295 if (!_rep.
BitsAs<
bool>()) {
300 bool immortal = !(_rep->_refCount.load(std::memory_order_relaxed) & 1);
312 template <
class HashState>
315 h.Append(token._rep.
Get());
324 void _AddRef()
const {
325 if (!_rep.
BitsAs<
bool>()) {
330 if (!_rep->IncrementAndCheckCounted()) {
336 void _RemoveRef()
const {
337 if (!_rep.
BitsAs<
bool>()) {
348 explicit _Rep(std::string &&str,
350 uint64_t compareCode)
352 , _compareCode(compareCode)
353 , _str(
std::move(str))
354 , _cstr(_str.c_str()) {}
356 explicit _Rep(std::string
const &str,
358 uint64_t compareCode)
359 : _Rep(
std::string(str), setNum, compareCode) {}
361 explicit _Rep(
char const *str,
363 uint64_t compareCode)
364 : _Rep(
std::string(str), setNum, compareCode) {}
371 _Rep(_Rep
const &rhs)
372 : _refCount(rhs._refCount.load(
std::memory_order_relaxed))
373 , _setNum(rhs._setNum)
374 , _compareCode(rhs._compareCode)
376 , _cstr(rhs._str.c_str() == rhs._cstr ? _str.c_str() : rhs._cstr) {}
378 _Rep &operator=(_Rep
const &rhs) {
379 _refCount = rhs._refCount.load(std::memory_order_relaxed);
380 _setNum = rhs._setNum;
381 _compareCode = rhs._compareCode;
383 _cstr = (rhs._str.c_str() == rhs._cstr ? _str.c_str() : rhs._cstr);
387 inline bool IncrementAndCheckCounted()
const {
390 return _refCount.fetch_add(2, std::memory_order_relaxed) & 1;
393 inline void Decrement()
const {
396 _refCount.fetch_sub(2, std::memory_order_release);
399 mutable std::atomic_uint _refCount;
401 uint64_t _compareCode;
407 friend struct Tf_TokenRegistry;
409 TF_API
static std::string
const& _GetEmptyString();
423 inline bool operator()(
TfToken const &lhs,
TfToken const &rhs)
const {
424 return lhs._rep.
Get() < rhs._rep.
Get();
429TF_API std::vector<TfToken>
433TF_API std::vector<std::string>
442PXR_NAMESPACE_CLOSE_SCOPE
A user-extensible hashing mechanism for use with runtime hash tables.
This class stores a T * and a small integer in the space of a T *.
void SetBits(Integral val) noexcept
Set the stored bits. No static range checking is performed.
constexpr uintptr_t GetLiteral() const noexcept
Retrieve the raw underlying value.
constexpr T * Get() const noexcept
Retrieve the pointer.
constexpr Integral BitsAs() const noexcept
Retrieve the stored bits as the integral type Integral.
Token for efficient comparison, assignment, and hashing of known strings.
static TF_API TfToken Find(std::string const &s)
Find the token for the given string, if one exists.
friend bool operator!=(char const *o, TfToken const &t)
This is an overloaded member function, provided for convenience. It differs from the above function o...
std::set< TfToken, TfTokenFastArbitraryLessThan > Set
Predefined type for set of tokens, for when faster lookup is desired, without paying the memory or in...
TfToken(TfToken &&rhs) noexcept
Move constructor.
size_t size() const
Return the size of the string that this token represents.
TF_API TfToken(std::string const &s, _ImmortalTag)
This is an overloaded member function, provided for convenience. It differs from the above function o...
char const * data() const
Synonym for GetText().
bool operator!=(char const *o) const
Inequality operator for char strings.
bool operator<=(TfToken const &o) const
Less-than-or-equal operator that compares tokenized strings lexicographically.
TfHashSet< TfToken, TfToken::HashFunctor > HashSet
Predefined type for TfHashSet of tokens, since it's so awkward to manually specify.
bool operator>(TfToken const &o) const
Greater-than operator that compares tokenized strings lexicographically.
friend bool operator!=(std::string const &o, TfToken const &t)
This is an overloaded member function, provided for convenience. It differs from the above function o...
TF_API TfToken(char const *s, _ImmortalTag)
This is an overloaded member function, provided for convenience. It differs from the above function o...
friend bool operator==(const char *o, TfToken const &t)
This is an overloaded member function, provided for convenience. It differs from the above function o...
TfToken & operator=(TfToken const &rhs) noexcept
Copy assignment.
bool operator!=(TfToken const &o) const
Equality operator.
char const * GetText() const
Return the text that this token represents.
friend TF_API std::ostream & operator<<(std::ostream &stream, TfToken const &)
Stream insertion.
void Swap(TfToken &other)
Swap this token with another.
TF_API bool operator==(std::string const &o) const
Equality operator for char strings.
bool operator==(TfToken const &o) const
Equality operator.
friend bool operator==(std::string const &o, TfToken const &t)
This is an overloaded member function, provided for convenience. It differs from the above function o...
bool IsEmpty() const
Returns true iff this token contains the empty string "".
TF_API TfToken(std::string const &s)
Acquire a token for the given string.
std::string const & GetString() const
Return the string that this token represents.
TF_API bool operator==(const char *) const
Equality operator for char strings.
TF_API TfToken(char const *s)
Acquire a token for the given string.
bool operator>=(TfToken const &o) const
Greater-than-or-equal operator that compares tokenized strings lexicographically.
constexpr TfToken() noexcept=default
Create the empty token, containing the empty string.
bool operator<(TfToken const &r) const
Less-than operator that compares tokenized strings lexicographically.
size_t Hash() const
Return a size_t hash for this token.
bool IsImmortal() const
Returns true iff this is an immortal token.
friend void TfHashAppend(HashState &h, TfToken const &token)
TfHash support.
bool operator!=(std::string const &o) const
Inequality operator for string's.
Stripped down version of diagnostic.h that doesn't define std::string.
Functor to use for hash maps from tokens to other things.
Fast but non-lexicographical (in fact, arbitrary) less-than comparison for TfTokens.
size_t hash_value(const TfToken &x)
Overload hash_value for TfToken.
std::vector< TfToken > TfTokenVector
Convenience types.
TF_API std::vector< std::string > TfToStringVector(const std::vector< TfToken > &tv)
Convert the vector of TfToken tv into a vector of strings.
TF_API std::vector< TfToken > TfToTokenVector(const std::vector< std::string > &sv)
Convert the vector of strings sv into a vector of TfToken.