31 #include "pxr/base/arch/defines.h"
32 #include "pxr/base/arch/demangle.h"
33 #include "pxr/base/tf/preprocessorUtils.h"
34 #include "pxr/base/tf/safeTypeCompare.h"
35 #include "pxr/base/tf/api.h"
37 #include <boost/operators.hpp>
38 #include <boost/preprocessor/punctuation/comma_if.hpp>
39 #include <boost/preprocessor/stringize.hpp>
40 #include <boost/type_traits/is_enum.hpp>
41 #include <boost/utility/enable_if.hpp>
46 #include <type_traits>
49 PXR_NAMESPACE_OPEN_SCOPE
140 class TfEnum : boost::totally_ordered<TfEnum>
145 : _typeInfo(&typeid(int)), _value(0)
152 typename boost::enable_if<boost::is_enum<T> >::type * = 0)
153 : _typeInfo(&typeid(T)), _value(int(value))
162 TfEnum(
const std::type_info& ti,
int value)
163 : _typeInfo(&ti), _value(value)
169 return t._value == _value &&
178 return _typeInfo->before(*t._typeInfo) ||
179 (!t._typeInfo->before(*_typeInfo) && _value < t._value);
184 typename boost::enable_if<boost::is_enum<T>,
bool>::type
186 return int(value) == _value && IsA<T>();
191 typename boost::enable_if<boost::is_enum<T>,
bool>::type
193 return int(value) != _value || !IsA<T>();
198 friend typename boost::enable_if<boost::is_enum<T>,
bool>::type
205 friend typename boost::enable_if<boost::is_enum<T>,
bool>::type
218 bool IsA(
const std::type_info& t)
const {
243 template <
typename T>
246 _FatalGetValueError(
typeid(T));
252 template <
typename T,
253 typename =
typename std::enable_if<
254 std::is_integral<T>::value ||
255 std::is_enum<T>::value>::type
301 TF_API
static std::vector<std::string>
GetAllNames(
const std::type_info &ti);
322 static const std::type_info *
GetTypeFromName(
const std::string& typeName);
340 const std::string &name,
341 bool *foundIt = NULL);
354 bool *foundIt = NULL);
372 const std::string &displayName=
"");
377 const std::string &displayName=
"")
379 _AddName(val, valName, displayName);
382 template <
typename T>
383 static TfEnum IntegralEnum(T value) {
385 e._typeInfo = &
typeid(T);
386 e._value = int(value);
392 explicit TfEnum(
int value)
393 : _typeInfo(&typeid(int)), _value(value)
398 explicit TfEnum(
size_t value)
399 : _typeInfo(&typeid(size_t)), _value(static_cast<int>(value))
404 void _FatalGetValueError(std::type_info
const& typeInfo)
const;
406 const std::type_info* _typeInfo;
439 #define TF_ADD_ENUM_NAME(VAL, ...) \
440 TfEnum::_AddName(VAL, \
441 BOOST_PP_STRINGIZE(VAL) \
442 BOOST_PP_COMMA_IF(TF_NUM_ARGS(__VA_ARGS__)) \
445 PXR_NAMESPACE_CLOSE_SCOPE
friend boost::enable_if< boost::is_enum< T >, bool >::type operator!=(T val, TfEnum const &e)
Compare a literal enum value val of enum type T with TfEnum e.
static std::vector< std::string > GetAllNames(TfEnum val)
Returns a vector of all the names associated with an enum type.
T GetValue() const
Returns the enum value for the enum type T.
const std::type_info & GetType() const
Returns the type of the enum value, as an std::type_info.
static TF_API bool IsKnownEnumType(const std::string &typeName)
Returns true if typeName is a known enum type.
bool IsA(const std::type_info &t) const
True if *this has been assigned any enumerated value of type T with typeid(T)==t. ...
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.
An enum class that records both enum type and enum value.
bool operator<(const TfEnum &t) const
Less than comparison.
static void AddName(TfEnum val, const std::string &valName, const std::string &displayName="")
Associates a name with an enumerated value.
static TF_API std::string GetDisplayName(TfEnum val)
Returns the display name for an enumerated value.
friend boost::enable_if< boost::is_enum< T >, bool >::type operator==(T val, TfEnum const &e)
Compare a literal enum value val of enum type T with TfEnum e.
TfEnum(const std::type_info &ti, int value)
Initializes value to integral value value with enum type ti.
static TF_API std::string GetName(TfEnum val)
Returns the name associated with an enumerated value.
static TF_API TfEnum GetValueFromFullName(const std::string &fullname, bool *foundIt=NULL)
Returns the enumerated value for a fully-qualified name.
boost::enable_if< boost::is_enum< T >, bool >::type operator==(T value) const
True if *this has been assigned with value.
static TF_API std::string GetFullName(TfEnum val)
Returns the fully-qualified name for an enumerated value.
boost::enable_if< boost::is_enum< T >, bool >::type operator!=(T value) const
False if *this has been assigned with value.
GF_API std::ostream & operator<<(std::ostream &, const GfBBox3d &)
Output a GfBBox3d using the format [(range) matrix zeroArea].
static T GetValueFromName(const std::string &name, bool *foundIt=NULL)
Returns the enumerated value for a name.
TfEnum()
Default constructor assigns integer value zero.
static TF_API void _AddName(TfEnum val, const std::string &valName, const std::string &displayName="")
Associates a name with an enumerated value.
bool IsA() const
True if *this has been assigned any enumerated value of type T.
TfEnum(T value, typename boost::enable_if< boost::is_enum< T > >::type *=0)
Initializes value to enum variable value of enum type T.
bool operator==(const TfEnum &t) const
True if *this and t have both the same type and value.
const int & GetValueAsInt() const
Returns the integral value of the enum value.
bool TfSafeTypeCompare(const std::type_info &t1, const std::type_info &t2)
Safely compare std::type_info structures.