24 #ifndef PXR_BASE_TF_TOKEN_H
25 #define PXR_BASE_TF_TOKEN_H
34 #include "pxr/base/tf/api.h"
37 #include "pxr/base/tf/hashset.h"
38 #include "pxr/base/tf/pointerAndBits.h"
46 PXR_NAMESPACE_OPEN_SCOPE
90 enum _ImmortalTag { Immortal };
131 TF_API
explicit TfToken(std::string
const& s);
137 TF_API
TfToken(std::string
const& s, _ImmortalTag);
144 TF_API
explicit TfToken(
char const* s);
150 TF_API
TfToken(
char const* s, _ImmortalTag);
163 inline size_t Hash()
const;
167 size_t operator()(
TfToken const& token)
const {
return token.
Hash(); }
175 typedef TfHashSet<TfToken, TfToken::HashFunctor>
HashSet;
183 typedef std::set<TfToken, TfTokenFastArbitraryLessThan>
Set;
187 _Rep
const *rep = _rep.
Get();
188 return rep ? rep->_str.size() : 0;
197 _Rep
const *rep = _rep.
Get();
198 return rep ? rep->_str.c_str() :
"";
208 _Rep
const *rep = _rep.
Get();
209 return rep ? rep->_str : _GetEmptyString();
220 return _rep.
Get() == o._rep.
Get();
225 return !(*
this == o);
230 TF_API
bool operator==(std::string
const& o)
const;
249 return !(*
this == o);
260 return !(*
this == o);
273 auto lrep = _rep.
Get(), rrep = r._rep.
Get();
274 uint64_t lcc = lrep->_compareCode, rcc = rrep->_compareCode;
275 return lcc < rcc || (lcc == rcc && lrep->_str < rrep->_str);
299 operator std::string
const& ()
const {
return GetString(); }
311 template <
class HashState>
314 h.Append(token._rep.
Get());
323 void _AddRef()
const {
324 if (_rep.
BitsAs<
bool>()) {
326 if (!_rep->IncrementIfCounted()) {
333 void _RemoveRef()
const {
334 if (_rep.
BitsAs<
bool>()) {
336 if (_rep->_isCounted) {
337 if (_rep->_refCount.load(std::memory_order_relaxed) == 1) {
338 _PossiblyDestroyRep();
354 _rep->_refCount.fetch_sub(1, std::memory_order_relaxed);
363 void TF_API _PossiblyDestroyRep()
const;
367 explicit _Rep(
char const *s) : _str(s), _cstr(_str.c_str()) {}
368 explicit _Rep(std::string
const &s) : _str(s), _cstr(_str.c_str()) {}
375 _Rep(_Rep
const &rhs) : _str(rhs._str),
376 _cstr(rhs._str.c_str() != rhs._cstr ?
377 rhs._cstr : _str.c_str()),
378 _compareCode(rhs._compareCode),
379 _refCount(rhs._refCount.load()),
380 _isCounted(rhs._isCounted),
381 _setNum(rhs._setNum) {}
384 _cstr = (rhs._str.c_str() != rhs._cstr ? rhs._cstr : _str.c_str());
385 _compareCode = rhs._compareCode;
386 _refCount = rhs._refCount.load();
387 _isCounted = rhs._isCounted;
388 _setNum = rhs._setNum;
392 inline bool IncrementIfCounted()
const {
393 const bool isCounted = _isCounted;
395 _refCount.fetch_add(1, std::memory_order_relaxed);
402 mutable uint64_t _compareCode;
403 mutable std::atomic_int _refCount;
404 mutable bool _isCounted;
405 mutable unsigned char _setNum;
409 friend struct Tf_TokenRegistry;
411 TF_API
static std::string
const& _GetEmptyString();
425 inline bool operator()(
TfToken const &lhs,
TfToken const &rhs)
const {
426 return lhs._rep.
Get() < rhs._rep.
Get();
431 TF_API std::vector<TfToken>
435 TF_API std::vector<std::string>
444 PXR_NAMESPACE_CLOSE_SCOPE
446 #endif // PXR_BASE_TF_TOKEN_H
std::string const & GetString() const
Return the string that this token represents.
friend void TfHashAppend(HashState &h, TfToken const &token)
TfHash support.
TfToken & operator=(TfToken const &rhs) noexcept
Copy assignment.
bool operator!=(char const *o) const
Inequality operator for char strings.
friend TF_API std::ostream & operator<<(std::ostream &stream, TfToken const &)
Stream insertion.
bool operator<(TfToken const &r) const
Less-than operator that compares tokenized strings lexicographically.
constexpr T * Get() const noexcept
Retrieve the pointer.
bool operator==(TfToken const &o) const
Equality operator.
static TF_API TfToken Find(std::string const &s)
Find the token for the given string, if one exists.
Fast but non-lexicographical (in fact, arbitrary) less-than comparison for TfTokens.
size_t size() const
Return the size of the string that this token represents.
size_t Hash() const
Return a size_t hash for this token.
bool operator>=(TfToken const &o) const
Greater-than-or-equal operator that compares tokenized strings lexicographically. ...
TfToken(TfToken const &rhs) noexcept
Copy constructor.
bool operator!=(TfToken const &o) const
Equality operator.
Functor to use for hash maps from tokens to other things.
TF_API std::vector< TfToken > TfToTokenVector(const std::vector< std::string > &sv)
Convert the vector of strings sv into a vector of TfToken.
constexpr uintptr_t GetLiteral() const noexcept
Retrieve the raw underlying value.
friend bool operator!=(std::string const &o, TfToken const &t)
void SetBits(Integral val) noexcept
Set the stored bits. No static range checking is performed.
A user-extensible hashing mechanism for use with runtime hash tables.
bool operator<=(TfToken const &o) const
Less-than-or-equal operator that compares tokenized strings lexicographically.
TfToken(TfToken &&rhs) noexcept
Move constructor.
Token for efficient comparison, assignment, and hashing of known strings.
std::set< TfToken, TfTokenFastArbitraryLessThan > Set
Predefined type for set of tokens, for when faster lookup is desired, without paying the memory or in...
void swap(UsdStageLoadRules &l, UsdStageLoadRules &r)
Swap the contents of rules l and r.
bool operator!=(std::string const &o) const
Inequality operator for string's.
std::vector< TfToken > TfTokenVector
Convenience types.
bool operator>(TfToken const &o) const
Greater-than operator that compares tokenized strings lexicographically.
friend bool operator==(std::string const &o, TfToken const &t)
friend bool operator!=(char const *o, TfToken const &t)
TF_API std::vector< std::string > TfToStringVector(const std::vector< TfToken > &tv)
Convert the vector of TfToken tv into a vector of strings.
TfHashSet< TfToken, TfToken::HashFunctor > HashSet
Predefined type for TfHashSet of tokens, since it's so awkward to manually specify.
bool IsEmpty() const
Returns true iff this token contains the empty string "".
constexpr Integral BitsAs() const noexcept
Retrieve the stored bits as the integral type Integral.
void Swap(TfToken &other)
Swap this token with another.
friend bool operator==(const char *o, TfToken const &t)
constexpr TfToken() noexcept
Create the empty token, containing the empty string.
char const * data() const
Synonym for GetText().
bool IsImmortal() const
Returns true iff this is an immortal token.
Stripped down version of diagnostic.h that doesn't define std::string.
char const * GetText() const
Return the text that this token represents.