24#ifndef PXR_BASE_TF_PY_ENUM_H
25#define PXR_BASE_TF_PY_ENUM_H
32#include "pxr/base/tf/api.h"
33#include "pxr/base/tf/pyObjWrapper.h"
35#include "pxr/base/tf/type.h"
40#include "pxr/base/tf/hashmap.h"
45#include <boost/python/class.hpp>
46#include <boost/python/converter/from_python.hpp>
47#include <boost/python/converter/registered.hpp>
48#include <boost/python/converter/rvalue_from_python_data.hpp>
49#include <boost/python/list.hpp>
50#include <boost/python/object.hpp>
51#include <boost/python/operators.hpp>
52#include <boost/python/refcount.hpp>
53#include <boost/python/scope.hpp>
54#include <boost/python/to_python_converter.hpp>
55#include <boost/python/tuple.hpp>
59PXR_NAMESPACE_OPEN_SCOPE
70class Tf_PyEnumRegistry {
73 typedef Tf_PyEnumRegistry This;
77 virtual ~Tf_PyEnumRegistry();
82 TF_API
static This &GetInstance() {
87 void RegisterValue(
TfEnum const &e, boost::python::object
const &obj);
90 void RegisterEnumConversions() {
92 boost::python::to_python_converter<T, _EnumToPython<T> >();
99 PyObject *_ConvertEnumToPython(
TfEnum const &e);
101 template <
typename T>
102 struct _EnumFromPython {
104 boost::python::converter::registry::insert
105 (&convertible, &construct, boost::python::type_id<T>());
107 static void *convertible(PyObject *obj) {
108 TfHashMap<PyObject *, TfEnum, _ObjectHash>
const &o2e =
109 Tf_PyEnumRegistry::GetInstance()._objectsToEnums;
110 TfHashMap<PyObject *, TfEnum, _ObjectHash>::const_iterator
115 if (std::is_same<T, TfEnum>::value ||
116 (std::is_integral<T>::value && !std::is_enum<T>::value))
117 return i != o2e.end() ? obj : 0;
119 return (i != o2e.end() && i->second.IsA<T>()) ? obj : 0;
121 static void construct(PyObject *src, boost::python::converter::
122 rvalue_from_python_stage1_data *data) {
124 ((boost::python::converter::
125 rvalue_from_python_storage<T> *)data)->storage.bytes;
126 new (storage) T(_GetEnumValue(src, (T *)0));
127 data->convertible = storage;
132 template <
typename U>
133 static U _GetEnumValue(PyObject *src, U *) {
134 return U(Tf_PyEnumRegistry::GetInstance()._objectsToEnums[src].
138 return Tf_PyEnumRegistry::GetInstance()._objectsToEnums[src];
143 struct _EnumToPython {
144 static PyObject *convert(T t) {
145 return Tf_PyEnumRegistry
146 ::GetInstance()._ConvertEnumToPython(
TfEnum(t));
153 size_t operator()(PyObject *o)
const {
154 return reinterpret_cast<size_t>(o);
158 TfHashMap<TfEnum, PyObject *, TfHash> _enumsToObjects;
159 TfHashMap<PyObject *, TfEnum, _ObjectHash> _objectsToEnums;
167std::string Tf_PyEnumRepr(boost::python::object
const &self);
171struct Tf_PyEnumWrapper :
public Tf_PyEnum
173 typedef Tf_PyEnumWrapper This;
175 Tf_PyEnumWrapper(std::string
const &n,
TfEnum const &val) :
176 name(n), value(val) {}
177 long GetValue()
const {
178 return value.GetValueAsInt();
180 std::string GetName()
const{
183 std::string GetDisplayName()
const {
186 std::string GetFullName()
const {
189 friend bool operator ==(Tf_PyEnumWrapper
const &self,
191 return self.value.GetValueAsInt() == other;
194 friend bool operator ==(Tf_PyEnumWrapper
const &lhs,
195 Tf_PyEnumWrapper
const &rhs) {
196 return lhs.value == rhs.value;
199 friend bool operator !=(Tf_PyEnumWrapper
const &lhs,
200 Tf_PyEnumWrapper
const &rhs) {
201 return !(lhs == rhs);
204 friend bool operator <(Tf_PyEnumWrapper
const &lhs,
205 Tf_PyEnumWrapper
const &rhs)
211 if (!lhs.value.IsA(rhs.value.GetType()))
215 return lhs.GetValue() < rhs.GetValue();
218 friend bool operator >(Tf_PyEnumWrapper
const& lhs,
219 Tf_PyEnumWrapper
const& rhs)
224 friend bool operator <=(Tf_PyEnumWrapper
const& lhs,
225 Tf_PyEnumWrapper
const& rhs)
230 friend bool operator >=(Tf_PyEnumWrapper
const& lhs,
231 Tf_PyEnumWrapper
const& rhs)
243 friend TfEnum operator |(Tf_PyEnumWrapper
const &lhs,
244 Tf_PyEnumWrapper
const &rhs) {
245 if (lhs.value.IsA(rhs.value.GetType())) {
246 return TfEnum(lhs.value.GetType(),
247 lhs.value.GetValueAsInt() |
248 rhs.value.GetValueAsInt());
253 friend TfEnum operator |(Tf_PyEnumWrapper
const &lhs,
long rhs) {
254 return TfEnum(lhs.value.GetType(), lhs.value.GetValueAsInt() | rhs);
256 friend TfEnum operator |(
long lhs, Tf_PyEnumWrapper
const &rhs) {
257 return TfEnum(rhs.value.GetType(), lhs | rhs.value.GetValueAsInt());
260 friend TfEnum operator &(Tf_PyEnumWrapper
const &lhs,
261 Tf_PyEnumWrapper
const &rhs) {
262 if (lhs.value.IsA(rhs.value.GetType())) {
263 return TfEnum(lhs.value.GetType(),
264 lhs.value.GetValueAsInt() &
265 rhs.value.GetValueAsInt());
270 friend TfEnum operator &(Tf_PyEnumWrapper
const &lhs,
long rhs) {
271 return TfEnum(lhs.value.GetType(), lhs.value.GetValueAsInt() & rhs);
273 friend TfEnum operator &(
long lhs, Tf_PyEnumWrapper
const &rhs) {
274 return TfEnum(rhs.value.GetType(), lhs & rhs.value.GetValueAsInt());
277 friend TfEnum operator ^(Tf_PyEnumWrapper
const &lhs,
278 Tf_PyEnumWrapper
const &rhs) {
279 if (lhs.value.IsA(rhs.value.GetType())) {
280 return TfEnum(lhs.value.GetType(),
281 lhs.value.GetValueAsInt() ^
282 rhs.value.GetValueAsInt());
287 friend TfEnum operator ^(Tf_PyEnumWrapper
const &lhs,
long rhs) {
288 return TfEnum(lhs.value.GetType(), lhs.value.GetValueAsInt() ^ rhs);
290 friend TfEnum operator ^(
long lhs, Tf_PyEnumWrapper
const &rhs) {
291 return TfEnum(rhs.value.GetType(), lhs ^ rhs.value.GetValueAsInt());
294 friend TfEnum operator ~(Tf_PyEnumWrapper
const &rhs) {
295 return TfEnum(rhs.value.GetType(), ~rhs.value.GetValueAsInt());
304struct Tf_TypedPyEnumWrapper : Tf_PyEnumWrapper
306 Tf_TypedPyEnumWrapper(std::string
const &n,
TfEnum const &val) :
307 Tf_PyEnumWrapper(n, val) {}
309 static boost::python::object GetValueFromName(
const std::string& name) {
311 const TfEnum value = TfEnum::GetValueFromName<T>(name, &found);
313 ? boost::python::object(value)
314 : boost::python::object();
325std::string Tf_PyCleanEnumName(std::string name,
326 bool stripPackageName =
false);
331void Tf_PyEnumAddAttribute(boost::python::scope &s,
332 const std::string &name,
333 const boost::python::object &value);
379template <typename T, bool IsScopedEnum = !std::is_convertible<T, int>::value>
383 typedef boost::python::class_<
384 Tf_TypedPyEnumWrapper<T>, boost::python::bases<Tf_PyEnumWrapper> >
395 using namespace boost::python;
397 const bool explicitName = !name.empty();
400 std::string enumName = explicitName ? name :
406 if (baseName == enumName)
407 baseName = std::string();
416 if (!baseName.empty()) {
417 baseName = Tf_PyCleanEnumName(
421 enumName = Tf_PyCleanEnumName(
429 if (!baseName.empty()) {
432 baseName += enumName;
436 _EnumPyClassType enumClass(enumName.c_str(), no_init);
437 enumClass.def(
"GetValueFromName", &Tf_TypedPyEnumWrapper<T>::GetValueFromName, arg(
"name"));
438 enumClass.staticmethod(
"GetValueFromName");
439 enumClass.setattr(
"_baseName", baseName);
442 Tf_PyEnumRegistry::GetInstance().RegisterEnumConversions<T>();
450 const bool stripPackageName = baseName.empty();
451 _ExportValues(stripPackageName, enumClass);
456 const TfType &type = TfType::Find<T>();
466 void _ExportValues(
bool stripPackageName, _EnumPyClassType &enumClass) {
467 boost::python::list valueList;
469 for (
const std::string& name : TfEnum::GetAllNames<T>()) {
470 bool success =
false;
471 TfEnum enumValue = TfEnum::GetValueFromName<T>(name, &success);
476 const std::string cleanedName =
477 Tf_PyCleanEnumName(name, stripPackageName);
480 Tf_TypedPyEnumWrapper<T> wrappedValue(cleanedName, enumValue);
481 boost::python::object pyValue(wrappedValue);
484 Tf_PyEnumRegistry::GetInstance().RegisterValue(enumValue, pyValue);
487 std::string valueName = wrappedValue.GetName();
490 boost::python::scope s(enumClass);
491 Tf_PyEnumAddAttribute(s, valueName, pyValue);
494 boost::python::scope s;
495 Tf_PyEnumAddAttribute(s, valueName, pyValue);
498 valueList.append(pyValue);
502 enumClass.setattr(
"allValues", boost::python::tuple(valueList));
507PXR_NAMESPACE_CLOSE_SCOPE
A simple iterator adapter for STL containers.
Miscellaneous Utilities for dealing with script.
TF_API void TfPyThrowTypeError(const char *msg)
Raises a Python TypeError with the given error msg and throws a boost::python::error_already_set exce...
An enum class that records both enum type and enum value.
static TF_API std::string GetFullName(TfEnum val)
Returns the fully-qualified name for an enumerated value.
static TF_API std::string GetDisplayName(TfEnum val)
Returns the display name for an enumerated value.
Manage a single instance of an object (see.
static T & GetInstance()
Return a reference to an object of type T, creating it if necessary.
TfType represents a dynamic runtime type.
TF_API void DefinePythonClass(const TfPyObjWrapper &classObj) const
Define the Python class object corresponding to this TfType.
bool IsUnknown() const
Return true if this is the unknown type, representing a type unknown to the TfType system.
Demangle C++ typenames generated by the typeid() facility.
std::string ArchGetDemangled()
Return demangled RTTI generated-type name.
TF_API std::string TfStringReplace(const std::string &source, const std::string &from, const std::string &to)
Replaces all occurrences of string from with to in source.
TF_API std::string TfStringGetBeforeSuffix(const std::string &name, char delimiter='.')
Returns everything up to the suffix of a string.
TF_API std::string TfStringGetSuffix(const std::string &name, char delimiter='.')
Returns the suffix of a string.
Manage a single instance of an object.
Definitions of basic string utilities in tf.
Used to wrap enum types for script.
TfPyWrapEnum(std::string const &name=std::string())
Construct an enum wrapper object.