Loading...
Searching...
No Matches
enum.h
Go to the documentation of this file.
1//
2// Copyright 2016 Pixar
3//
4// Licensed under the Apache License, Version 2.0 (the "Apache License")
5// with the following modification; you may not use this file except in
6// compliance with the Apache License and the following modification to it:
7// Section 6. Trademarks. is deleted and replaced with:
8//
9// 6. Trademarks. This License does not grant permission to use the trade
10// names, trademarks, service marks, or product names of the Licensor
11// and its affiliates, except as required to comply with Section 4(c) of
12// the License and to reproduce the content of the NOTICE file.
13//
14// You may obtain a copy of the Apache License at
15//
16// http://www.apache.org/licenses/LICENSE-2.0
17//
18// Unless required by applicable law or agreed to in writing, software
19// distributed under the Apache License with the above modification is
20// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21// KIND, either express or implied. See the Apache License for the specific
22// language governing permissions and limitations under the Apache License.
23//
24#ifndef PXR_BASE_TF_ENUM_H
25#define PXR_BASE_TF_ENUM_H
26
29
30#include "pxr/pxr.h"
31#include "pxr/base/arch/defines.h"
33#include "pxr/base/tf/hash.h"
34#include "pxr/base/tf/preprocessorUtilsLite.h"
36#include "pxr/base/tf/api.h"
37
38
39#include <iosfwd>
40#include <string>
41#include <typeinfo>
42#include <type_traits>
43#include <vector>
44
45PXR_NAMESPACE_OPEN_SCOPE
46
137{
138public:
141 : _typeInfo(&typeid(int)), _value(0)
142 {
143 }
144
146 template <class T>
147 TfEnum(T value,
148 std::enable_if_t<std::is_enum<T>::value> * = 0)
149 : _typeInfo(&typeid(T)), _value(int(value))
150 {
151 }
152
158 TfEnum(const std::type_info& ti, int value)
159 : _typeInfo(&ti), _value(value)
160 {
161 }
162
164 bool operator==(const TfEnum& t) const {
165 return t._value == _value &&
166 TfSafeTypeCompare(*t._typeInfo, *_typeInfo);
167 }
168
171 bool operator!=(const TfEnum& t) const {
172 return !(*this == t);
173 }
174
179 bool operator<(const TfEnum& t) const {
180 return _typeInfo->before(*t._typeInfo) ||
181 (!t._typeInfo->before(*_typeInfo) && _value < t._value);
182 }
183
186 bool operator<=(const TfEnum& t) const {
187 return !(t < *this);
188 }
189
192 bool operator>(const TfEnum& t) const {
193 return t < *this;
194 }
195
198 bool operator>=(const TfEnum& t) const {
199 return !(*this < t);
200 }
201
203 template <class T>
204 std::enable_if_t<std::is_enum<T>::value, bool>
205 operator==(T value) const {
206 return int(value) == _value && IsA<T>();
207 }
208
210 template <class T>
211 std::enable_if_t<std::is_enum<T>::value, bool>
212 operator!=(T value) const {
213 return int(value) != _value || !IsA<T>();
214 }
215
217 template <class T>
218 friend std::enable_if_t<std::is_enum<T>::value, bool>
219 operator==(T val, TfEnum const &e) {
220 return e == val;
221 }
222
224 template <class T>
225 friend std::enable_if_t<std::is_enum<T>::value, bool>
226 operator!=(T val, TfEnum const &e) {
227 return e != val;
228 }
229
231 template <class T>
232 bool IsA() const {
233 return TfSafeTypeCompare(*_typeInfo, typeid(T));
234 }
235
238 bool IsA(const std::type_info& t) const {
239 return TfSafeTypeCompare(*_typeInfo, t);
240 }
241
243 const std::type_info& GetType() const {
244 return *_typeInfo;
245 }
246
248 const int& GetValueAsInt() const {
249 return _value;
250 }
251
263 template <typename T>
264 T GetValue() const {
265 if (!IsA<T>())
266 _FatalGetValueError(typeid(T));
267
268 return T(_value);
269 }
270
272 template <typename T,
273 typename = typename std::enable_if<
274 std::is_integral<T>::value ||
275 std::is_enum<T>::value>::type
276 >
277 operator T() const
278 {
279 return T(_value);
280 }
281
289
293 TF_API static std::string GetName(TfEnum val);
294
300 TF_API static std::string GetFullName(TfEnum val);
301
306 TF_API static std::string GetDisplayName(TfEnum val);
307
316 static std::vector<std::string> GetAllNames(TfEnum val) {
317 return GetAllNames(val.GetType());
318 }
319
321 TF_API static std::vector<std::string> GetAllNames(const std::type_info &ti);
322
331 template <class T>
332 static std::vector<std::string> GetAllNames() {
333 return GetAllNames(typeid(T));
334 }
335
341 TF_API
342 static const std::type_info *GetTypeFromName(const std::string& typeName);
343
349 template <class T>
350 static T GetValueFromName(const std::string &name, bool *foundIt = NULL) {
351 TfEnum e = GetValueFromName(typeid(T), name, foundIt);
352 return T(e.GetValueAsInt());
353 }
354
358 TF_API
359 static TfEnum GetValueFromName(const std::type_info& ti,
360 const std::string &name,
361 bool *foundIt = NULL);
362
372 TF_API
373 static TfEnum GetValueFromFullName(const std::string &fullname,
374 bool *foundIt = NULL);
375
380 TF_API
381 static bool IsKnownEnumType(const std::string& typeName);
382
384
390 TF_API
391 static void _AddName(TfEnum val, const std::string &valName,
392 const std::string &displayName="");
393
396 static void AddName(TfEnum val, const std::string &valName,
397 const std::string &displayName="")
398 {
399 _AddName(val, valName, displayName);
400 }
401
402 template <typename T>
403 static TfEnum IntegralEnum(T value) {
404 TfEnum e;
405 e._typeInfo = &typeid(T);
406 e._value = int(value);
407 return e;
408 }
409
410private:
411 // Internal constructor for int values.
412 explicit TfEnum(int value)
413 : _typeInfo(&typeid(int)), _value(value)
414 {
415 }
416
417 // Internal constructor for size_t values.
418 explicit TfEnum(size_t value)
419 : _typeInfo(&typeid(size_t)), _value(static_cast<int>(value))
420 {
421 }
422
423 TF_API
424 void _FatalGetValueError(std::type_info const& typeInfo) const;
425
426 const std::type_info* _typeInfo;
427 int _value;
428};
429
430// TfHash support. Make the enum parameter be a deduced template type but
431// enable the overload only for TfEnum. This disables implicit conversion from
432// ordinary enum types to TfEnum.
433template <class HashState, class Enum>
434std::enable_if_t<std::is_same<Enum, TfEnum>::value>
435TfHashAppend(HashState &h, Enum const &e)
436{
437 h.Append(TfHashAsCStr(e.GetType().name()));
438 h.Append(e.GetValueAsInt());
439}
440
443TF_API std::ostream& operator<<(std::ostream& out, const TfEnum & e);
444
470#define TF_ADD_ENUM_NAME(VAL, ...) \
471 TfEnum::_AddName(VAL, TF_PP_STRINGIZE(VAL), \
472 std::string{__VA_ARGS__});
473
474PXR_NAMESPACE_CLOSE_SCOPE
475
476#endif // PXR_BASE_TF_ENUM_H
An enum class that records both enum type and enum value.
Definition: enum.h:137
static TF_API TfEnum GetValueFromName(const std::type_info &ti, const std::string &name, bool *foundIt=NULL)
Returns the enumerated value for a name.
TfEnum()
Default constructor assigns integer value zero.
Definition: enum.h:140
bool operator>(const TfEnum &t) const
Greater than operator.
Definition: enum.h:192
bool operator==(const TfEnum &t) const
True if *this and t have both the same type and value.
Definition: enum.h:164
static TF_API std::string GetName(TfEnum val)
Returns the name associated with an enumerated value.
static TF_API void _AddName(TfEnum val, const std::string &valName, const std::string &displayName="")
Associates a name with an enumerated value.
TfEnum(const std::type_info &ti, int value)
Initializes value to integral value value with enum type ti.
Definition: enum.h:158
static void AddName(TfEnum val, const std::string &valName, const std::string &displayName="")
Associates a name with an enumerated value.
Definition: enum.h:396
const std::type_info & GetType() const
Returns the type of the enum value, as an std::type_info.
Definition: enum.h:243
static TF_API TfEnum GetValueFromFullName(const std::string &fullname, bool *foundIt=NULL)
Returns the enumerated value for a fully-qualified name.
static TF_API std::vector< std::string > GetAllNames(const std::type_info &ti)
This is an overloaded member function, provided for convenience. It differs from the above function o...
bool operator<(const TfEnum &t) const
Less than comparison.
Definition: enum.h:179
static T GetValueFromName(const std::string &name, bool *foundIt=NULL)
Returns the enumerated value for a name.
Definition: enum.h:350
static std::vector< std::string > GetAllNames(TfEnum val)
Returns a vector of all the names associated with an enum type.
Definition: enum.h:316
static TF_API std::string GetFullName(TfEnum val)
Returns the fully-qualified name for an enumerated value.
bool IsA() const
True if *this has been assigned any enumerated value of type T.
Definition: enum.h:232
friend std::enable_if_t< std::is_enum< T >::value, bool > operator!=(T val, TfEnum const &e)
Compare a literal enum value val of enum type T with TfEnum e.
Definition: enum.h:226
bool operator<=(const TfEnum &t) const
Less than or equal operator.
Definition: enum.h:186
bool IsA(const std::type_info &t) const
True if *this has been assigned any enumerated value of type T with typeid(T)==t.
Definition: enum.h:238
friend std::enable_if_t< std::is_enum< T >::value, bool > operator==(T val, TfEnum const &e)
Compare a literal enum value val of enum type T with TfEnum e.
Definition: enum.h:219
static TF_API bool IsKnownEnumType(const std::string &typeName)
Returns true if typeName is a known enum type.
std::enable_if_t< std::is_enum< T >::value, bool > operator!=(T value) const
False if *this has been assigned with value.
Definition: enum.h:212
TfEnum(T value, std::enable_if_t< std::is_enum< T >::value > *=0)
Initializes value to enum variable value of enum type T.
Definition: enum.h:147
bool operator>=(const TfEnum &t) const
Greater than or equal operator.
Definition: enum.h:198
std::enable_if_t< std::is_enum< T >::value, bool > operator==(T value) const
True if *this has been assigned with value.
Definition: enum.h:205
bool operator!=(const TfEnum &t) const
Inequality operator.
Definition: enum.h:171
static TF_API std::string GetDisplayName(TfEnum val)
Returns the display name for an enumerated value.
static std::vector< std::string > GetAllNames()
Returns a vector of all the names associated with an enum type.
Definition: enum.h:332
static TF_API const std::type_info * GetTypeFromName(const std::string &typeName)
Returns the typeid for a given enum type name.
T GetValue() const
Returns the enum value for the enum type T.
Definition: enum.h:264
const int & GetValueAsInt() const
Returns the integral value of the enum value.
Definition: enum.h:248
Demangle C++ typenames generated by the typeid() facility.
GF_API std::ostream & operator<<(std::ostream &, const GfBBox3d &)
Output a GfBBox3d using the format [(range) matrix zeroArea].
Safely compare C++ RTTI type structures.
bool TfSafeTypeCompare(const std::type_info &t1, const std::type_info &t2)
Safely compare std::type_info structures.
TfCStrHashWrapper TfHashAsCStr(char const *cstr)
Indicate that a char pointer is intended to be hashed as a C-style null terminated string.
Definition: hash.h:175