24#ifndef PXR_BASE_TF_ENUM_H
25#define PXR_BASE_TF_ENUM_H
31#include "pxr/base/arch/defines.h"
34#include "pxr/base/tf/preprocessorUtilsLite.h"
36#include "pxr/base/tf/api.h"
45PXR_NAMESPACE_OPEN_SCOPE
141 : _typeInfo(&typeid(int)), _value(0)
148 std::enable_if_t<std::is_enum<T>::value> * = 0)
149 : _typeInfo(&typeid(T)), _value(int(value))
158 TfEnum(
const std::type_info& ti,
int value)
159 : _typeInfo(&ti), _value(value)
165 return t._value == _value &&
172 return !(*
this == t);
180 return _typeInfo->before(*t._typeInfo) ||
181 (!t._typeInfo->before(*_typeInfo) && _value < t._value);
204 std::enable_if_t<std::is_enum<T>::value,
bool>
206 return int(value) == _value && IsA<T>();
211 std::enable_if_t<std::is_enum<T>::value,
bool>
213 return int(value) != _value || !IsA<T>();
218 friend std::enable_if_t<std::is_enum<T>::value,
bool>
225 friend std::enable_if_t<std::is_enum<T>::value,
bool>
238 bool IsA(
const std::type_info& t)
const {
263 template <
typename T>
266 _FatalGetValueError(
typeid(T));
272 template <
typename T,
273 typename =
typename std::enable_if<
274 std::is_integral<T>::value ||
275 std::is_enum<T>::value>::type
321 TF_API
static std::vector<std::string>
GetAllNames(
const std::type_info &ti);
360 const std::string &name,
361 bool *foundIt = NULL);
374 bool *foundIt = NULL);
392 const std::string &displayName=
"");
397 const std::string &displayName=
"")
399 _AddName(val, valName, displayName);
402 template <
typename T>
403 static TfEnum IntegralEnum(T value) {
405 e._typeInfo = &
typeid(T);
406 e._value = int(value);
412 explicit TfEnum(
int value)
413 : _typeInfo(&typeid(int)), _value(value)
418 explicit TfEnum(
size_t value)
419 : _typeInfo(&typeid(size_t)), _value(static_cast<int>(value))
424 void _FatalGetValueError(std::type_info
const& typeInfo)
const;
426 const std::type_info* _typeInfo;
433template <
class HashState,
class Enum>
434std::enable_if_t<std::is_same<Enum, TfEnum>::value>
435TfHashAppend(HashState &h, Enum
const &e)
438 h.Append(e.GetValueAsInt());
470#define TF_ADD_ENUM_NAME(VAL, ...) \
471 TfEnum::_AddName(VAL, TF_PP_STRINGIZE(VAL), \
472 std::string{__VA_ARGS__});
474PXR_NAMESPACE_CLOSE_SCOPE
An enum class that records both enum type and enum value.
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.
bool operator>(const TfEnum &t) const
Greater than operator.
bool operator==(const TfEnum &t) const
True if *this and t have both the same type and value.
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.
static void AddName(TfEnum val, const std::string &valName, const std::string &displayName="")
Associates a name with an enumerated value.
const std::type_info & GetType() const
Returns the type of the enum value, as an std::type_info.
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.
static T GetValueFromName(const std::string &name, bool *foundIt=NULL)
Returns the enumerated value for a name.
static std::vector< std::string > GetAllNames(TfEnum val)
Returns a vector of all the names associated with an enum type.
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.
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.
bool operator<=(const TfEnum &t) const
Less than or equal operator.
bool IsA(const std::type_info &t) const
True if *this has been assigned any enumerated value of type T with typeid(T)==t.
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.
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.
TfEnum(T value, std::enable_if_t< std::is_enum< T >::value > *=0)
Initializes value to enum variable value of enum type T.
bool operator>=(const TfEnum &t) const
Greater than or equal operator.
std::enable_if_t< std::is_enum< T >::value, bool > operator==(T value) const
True if *this has been assigned with value.
bool operator!=(const TfEnum &t) const
Inequality operator.
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.
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.
const int & GetValueAsInt() const
Returns the integral value of the enum value.
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.