This document is for a version of USD that is under development. See this page for the current release.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
enum.h
Go to the documentation of this file.
1//
2// Copyright 2016 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_ENUM_H
8#define PXR_BASE_TF_ENUM_H
9
12
13#include "pxr/pxr.h"
14#include "pxr/base/arch/defines.h"
16#include "pxr/base/tf/hash.h"
17#include "pxr/base/tf/preprocessorUtilsLite.h"
19#include "pxr/base/tf/api.h"
20
21
22#include <iosfwd>
23#include <string>
24#include <typeinfo>
25#include <type_traits>
26#include <vector>
27
28PXR_NAMESPACE_OPEN_SCOPE
29
120{
121public:
124 : _typeInfo(&typeid(int)), _value(0)
125 {
126 }
127
129 template <class T>
130 TfEnum(T value,
131 std::enable_if_t<std::is_enum<T>::value> * = 0)
132 : _typeInfo(&typeid(T)), _value(int(value))
133 {
134 }
135
141 TfEnum(const std::type_info& ti, int value)
142 : _typeInfo(&ti), _value(value)
143 {
144 }
145
147 bool operator==(const TfEnum& t) const {
148 return t._value == _value &&
149 TfSafeTypeCompare(*t._typeInfo, *_typeInfo);
150 }
151
154 bool operator!=(const TfEnum& t) const {
155 return !(*this == t);
156 }
157
162 bool operator<(const TfEnum& t) const {
163 return _typeInfo->before(*t._typeInfo) ||
164 (!t._typeInfo->before(*_typeInfo) && _value < t._value);
165 }
166
169 bool operator<=(const TfEnum& t) const {
170 return !(t < *this);
171 }
172
175 bool operator>(const TfEnum& t) const {
176 return t < *this;
177 }
178
181 bool operator>=(const TfEnum& t) const {
182 return !(*this < t);
183 }
184
186 template <class T>
187 std::enable_if_t<std::is_enum<T>::value, bool>
188 operator==(T value) const {
189 return int(value) == _value && IsA<T>();
190 }
191
193 template <class T>
194 std::enable_if_t<std::is_enum<T>::value, bool>
195 operator!=(T value) const {
196 return int(value) != _value || !IsA<T>();
197 }
198
200 template <class T>
201 friend std::enable_if_t<std::is_enum<T>::value, bool>
202 operator==(T val, TfEnum const &e) {
203 return e == val;
204 }
205
207 template <class T>
208 friend std::enable_if_t<std::is_enum<T>::value, bool>
209 operator!=(T val, TfEnum const &e) {
210 return e != val;
211 }
212
214 template <class T>
215 bool IsA() const {
216 return TfSafeTypeCompare(*_typeInfo, typeid(T));
217 }
218
221 bool IsA(const std::type_info& t) const {
222 return TfSafeTypeCompare(*_typeInfo, t);
223 }
224
226 const std::type_info& GetType() const {
227 return *_typeInfo;
228 }
229
231 const int& GetValueAsInt() const {
232 return _value;
233 }
234
246 template <typename T>
247 T GetValue() const {
248 if (!IsA<T>())
249 _FatalGetValueError(typeid(T));
250
251 return T(_value);
252 }
253
255 template <typename T,
256 typename = typename std::enable_if<
257 std::is_integral<T>::value ||
258 std::is_enum<T>::value>::type
259 >
260 operator T() const
261 {
262 return T(_value);
263 }
264
272
276 TF_API static std::string GetName(TfEnum val);
277
283 TF_API static std::string GetFullName(TfEnum val);
284
289 TF_API static std::string GetDisplayName(TfEnum val);
290
299 static std::vector<std::string> GetAllNames(TfEnum val) {
300 return GetAllNames(val.GetType());
301 }
302
304 TF_API static std::vector<std::string> GetAllNames(const std::type_info &ti);
305
314 template <class T>
315 static std::vector<std::string> GetAllNames() {
316 return GetAllNames(typeid(T));
317 }
318
324 TF_API
325 static const std::type_info *GetTypeFromName(const std::string& typeName);
326
332 template <class T>
333 static T GetValueFromName(const std::string &name, bool *foundIt = NULL) {
334 TfEnum e = GetValueFromName(typeid(T), name, foundIt);
335 return T(e.GetValueAsInt());
336 }
337
341 TF_API
342 static TfEnum GetValueFromName(const std::type_info& ti,
343 const std::string &name,
344 bool *foundIt = NULL);
345
355 TF_API
356 static TfEnum GetValueFromFullName(const std::string &fullname,
357 bool *foundIt = NULL);
358
363 TF_API
364 static bool IsKnownEnumType(const std::string& typeName);
365
367
373 TF_API
374 static void _AddName(TfEnum val, char const *valName,
375 char const *displayName);
376
377 // Helper for TF_ADD_ENUM_NAME() dealing with empty vs nonempty __VA_ARGS__
378 static char const *
379 _NameIdent(char const *str = nullptr) { return str; }
380
383 static void AddName(TfEnum val, const std::string &valName,
384 const std::string &displayName="")
385 {
386 _AddName(val, valName.c_str(), displayName.c_str());
387 }
388
389 template <typename T>
390 static TfEnum IntegralEnum(T value) {
391 TfEnum e;
392 e._typeInfo = &typeid(T);
393 e._value = int(value);
394 return e;
395 }
396
397private:
398 // Internal constructor for int values.
399 explicit TfEnum(int value)
400 : _typeInfo(&typeid(int)), _value(value)
401 {
402 }
403
404 // Internal constructor for size_t values.
405 explicit TfEnum(size_t value)
406 : _typeInfo(&typeid(size_t)), _value(static_cast<int>(value))
407 {
408 }
409
410 TF_API
411 void _FatalGetValueError(std::type_info const& typeInfo) const;
412
413 const std::type_info* _typeInfo;
414 int _value;
415};
416
417// TfHash support. Make the enum parameter be a deduced template type but
418// enable the overload only for TfEnum. This disables implicit conversion from
419// ordinary enum types to TfEnum.
420template <class HashState, class Enum>
421std::enable_if_t<std::is_same<Enum, TfEnum>::value>
422TfHashAppend(HashState &h, Enum const &e)
423{
424 h.Append(TfHashAsCStr(e.GetType().name()));
425 h.Append(e.GetValueAsInt());
426}
427
430TF_API std::ostream& operator<<(std::ostream& out, const TfEnum & e);
431
457#define TF_ADD_ENUM_NAME(VAL, ...) \
458 TfEnum::_AddName(VAL, TF_PP_STRINGIZE(VAL), \
459 TfEnum::_NameIdent(__VA_ARGS__));
460
461PXR_NAMESPACE_CLOSE_SCOPE
462
463#endif // PXR_BASE_TF_ENUM_H
An enum class that records both enum type and enum value.
Definition: enum.h:120
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:123
bool operator>(const TfEnum &t) const
Greater than operator.
Definition: enum.h:175
bool operator==(const TfEnum &t) const
True if *this and t have both the same type and value.
Definition: enum.h:147
static TF_API std::string GetName(TfEnum val)
Returns the name associated 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:141
static void AddName(TfEnum val, const std::string &valName, const std::string &displayName="")
Associates a name with an enumerated value.
Definition: enum.h:383
const std::type_info & GetType() const
Returns the type of the enum value, as an std::type_info.
Definition: enum.h:226
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:162
static T GetValueFromName(const std::string &name, bool *foundIt=NULL)
Returns the enumerated value for a name.
Definition: enum.h:333
static std::vector< std::string > GetAllNames(TfEnum val)
Returns a vector of all the names associated with an enum type.
Definition: enum.h:299
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:215
static TF_API void _AddName(TfEnum val, char const *valName, char const *displayName)
Associates a name with an enumerated value.
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:209
bool operator<=(const TfEnum &t) const
Less than or equal operator.
Definition: enum.h:169
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:221
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:202
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:195
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:130
bool operator>=(const TfEnum &t) const
Greater than or equal operator.
Definition: enum.h:181
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:188
bool operator!=(const TfEnum &t) const
Inequality operator.
Definition: enum.h:154
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:315
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:247
const int & GetValueAsInt() const
Returns the integral value of the enum value.
Definition: enum.h:231
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:158