Loading...
Searching...
No Matches
eternalString.h
Go to the documentation of this file.
1//
2// Copyright 2026 Pixar
3//
4// Licensed under the terms set forth in the LICENSE.txt file available at
5// https://openusd.org/license.
6//
7#ifndef PXR_BASE_TF_ETERNAL_STRING_H
8#define PXR_BASE_TF_ETERNAL_STRING_H
9
11
12#include "pxr/pxr.h"
13#include "pxr/base/tf/api.h"
14
15#include <cstddef>
16#include <iosfwd>
17#include <string>
18#include <string_view>
19
20PXR_NAMESPACE_OPEN_SCOPE
21
103{
104public:
107
122 TF_API static TfEternalString Immortalize(std::string_view content);
123
126 char const *c_str() const { return _str->c_str(); }
127 char const *data() const { return _str->data(); }
128 size_t size() const { return _str->size(); }
129 size_t length() const { return _str->size(); }
130 bool empty() const { return _str->empty(); }
132
134 std::string const &GetString() const { return *_str; }
135
137 std::string_view GetStringView() const { return *_str; }
138
166 operator std::string const &() const { return *_str; }
167 operator std::string_view() const { return *_str; }
169
179
180 friend std::string operator+(TfEternalString a, std::string_view b) {
181 return _Cat(a.data(), a.size(), b.data(), b.size());
182 }
183 friend std::string operator+(std::string_view a, TfEternalString b) {
184 return _Cat(a.data(), a.size(), b.data(), b.size());
185 }
186 friend std::string operator+(TfEternalString a, TfEternalString b) {
187 return _Cat(a.data(), a.size(), b.data(), b.size());
188 }
189
194 return a._str == b._str;
195 }
196 friend bool operator==(TfEternalString a, std::string_view b) {
197 return a.GetStringView() == b;
198 }
199 friend bool operator==(std::string_view a, TfEternalString b) {
200 return a == b.GetStringView();
201 }
202 friend bool operator!=(TfEternalString a, TfEternalString b) {
203 return !(a == b);
204 }
205 friend bool operator!=(TfEternalString a, std::string_view b) {
206 return !(a == b);
207 }
208 friend bool operator!=(std::string_view a, TfEternalString b) {
209 return !(a == b);
210 }
211
218 template <class HashState>
219 friend void TfHashAppend(HashState &h, TfEternalString s) {
220 h.Append(s._str);
221 }
222
223 TF_API friend std::ostream &operator<<(std::ostream &o, TfEternalString s);
224
226
227private:
228 explicit TfEternalString(std::string const *str) : _str(str) {}
229
230 TF_API static std::string
231 _Cat(char const *a, size_t aLen, char const *b, size_t bLen);
232
233 std::string const *_str;
234};
235
236PXR_NAMESPACE_CLOSE_SCOPE
237
238#endif // PXR_BASE_TF_ETERNAL_STRING_H
An immutable, interned string whose character data is guaranteed to remain valid at a fixed address f...
friend bool operator==(TfEternalString a, TfEternalString b)
Compare by address, which interning makes a content identity in both directions – see the class docum...
friend void TfHashAppend(HashState &h, TfEternalString s)
Hash by address, which interning makes a content identity – see the class documentation.
std::string const & GetString() const
Return the underlying string.
std::string_view GetStringView() const
Return a view of the characters.
static TF_API TfEternalString Immortalize(std::string_view content)
Return the TfEternalString naming content, interning its characters if they are not interned already.
TF_API TfEternalString()
Construct the empty eternal string.