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
172 :
public Tf_PyEnum, boost::totally_ordered<Tf_PyEnumWrapper>
174 typedef Tf_PyEnumWrapper This;
176 Tf_PyEnumWrapper(std::string
const &n,
TfEnum const &val) :
177 name(n), value(val) {}
178 long GetValue()
const {
179 return value.GetValueAsInt();
181 std::string GetName()
const{
184 std::string GetDisplayName()
const {
187 std::string GetFullName()
const {
190 friend bool operator ==(Tf_PyEnumWrapper
const &self,
192 return self.value.GetValueAsInt() == other;
195 friend bool operator ==(Tf_PyEnumWrapper
const &lhs,
196 Tf_PyEnumWrapper
const &rhs) {
197 return lhs.value == rhs.value;
200 friend bool operator <(Tf_PyEnumWrapper
const &lhs,
201 Tf_PyEnumWrapper
const &rhs)
207 if (!lhs.value.IsA(rhs.value.GetType()))
211 return lhs.GetValue() < rhs.GetValue();
221 friend TfEnum operator |(Tf_PyEnumWrapper
const &lhs,
222 Tf_PyEnumWrapper
const &rhs) {
223 if (lhs.value.IsA(rhs.value.GetType())) {
224 return TfEnum(lhs.value.GetType(),
225 lhs.value.GetValueAsInt() |
226 rhs.value.GetValueAsInt());
231 friend TfEnum operator |(Tf_PyEnumWrapper
const &lhs,
long rhs) {
232 return TfEnum(lhs.value.GetType(), lhs.value.GetValueAsInt() | rhs);
234 friend TfEnum operator |(
long lhs, Tf_PyEnumWrapper
const &rhs) {
235 return TfEnum(rhs.value.GetType(), lhs | rhs.value.GetValueAsInt());
238 friend TfEnum operator &(Tf_PyEnumWrapper
const &lhs,
239 Tf_PyEnumWrapper
const &rhs) {
240 if (lhs.value.IsA(rhs.value.GetType())) {
241 return TfEnum(lhs.value.GetType(),
242 lhs.value.GetValueAsInt() &
243 rhs.value.GetValueAsInt());
248 friend TfEnum operator &(Tf_PyEnumWrapper
const &lhs,
long rhs) {
249 return TfEnum(lhs.value.GetType(), lhs.value.GetValueAsInt() & rhs);
251 friend TfEnum operator &(
long lhs, Tf_PyEnumWrapper
const &rhs) {
252 return TfEnum(rhs.value.GetType(), lhs & rhs.value.GetValueAsInt());
255 friend TfEnum operator ^(Tf_PyEnumWrapper
const &lhs,
256 Tf_PyEnumWrapper
const &rhs) {
257 if (lhs.value.IsA(rhs.value.GetType())) {
258 return TfEnum(lhs.value.GetType(),
259 lhs.value.GetValueAsInt() ^
260 rhs.value.GetValueAsInt());
265 friend TfEnum operator ^(Tf_PyEnumWrapper
const &lhs,
long rhs) {
266 return TfEnum(lhs.value.GetType(), lhs.value.GetValueAsInt() ^ rhs);
268 friend TfEnum operator ^(
long lhs, Tf_PyEnumWrapper
const &rhs) {
269 return TfEnum(rhs.value.GetType(), lhs ^ rhs.value.GetValueAsInt());
272 friend TfEnum operator ~(Tf_PyEnumWrapper
const &rhs) {
273 return TfEnum(rhs.value.GetType(), ~rhs.value.GetValueAsInt());
282struct Tf_TypedPyEnumWrapper : Tf_PyEnumWrapper
284 Tf_TypedPyEnumWrapper(std::string
const &n,
TfEnum const &val) :
285 Tf_PyEnumWrapper(n, val) {}
287 static boost::python::object GetValueFromName(
const std::string& name) {
289 const TfEnum value = TfEnum::GetValueFromName<T>(name, &found);
291 ? boost::python::object(value)
292 : boost::python::object();
303std::string Tf_PyCleanEnumName(std::string name,
304 bool stripPackageName =
false);
309void Tf_PyEnumAddAttribute(boost::python::scope &s,
310 const std::string &name,
311 const boost::python::object &value);
357template <typename T, bool IsScopedEnum = !std::is_convertible<T, int>::value>
361 typedef boost::python::class_<
362 Tf_TypedPyEnumWrapper<T>, boost::python::bases<Tf_PyEnumWrapper> >
373 using namespace boost::python;
375 const bool explicitName = !name.empty();
378 std::string enumName = explicitName ? name :
384 if (baseName == enumName)
385 baseName = std::string();
394 if (!baseName.empty()) {
395 baseName = Tf_PyCleanEnumName(
399 enumName = Tf_PyCleanEnumName(
407 if (!baseName.empty()) {
410 baseName += enumName;
414 _EnumPyClassType enumClass(enumName.c_str(), no_init);
415 enumClass.def(
"GetValueFromName", &Tf_TypedPyEnumWrapper<T>::GetValueFromName, arg(
"name"));
416 enumClass.staticmethod(
"GetValueFromName");
417 enumClass.setattr(
"_baseName", baseName);
420 Tf_PyEnumRegistry::GetInstance().RegisterEnumConversions<T>();
428 const bool stripPackageName = baseName.empty();
429 _ExportValues(stripPackageName, enumClass);
434 const TfType &type = TfType::Find<T>();
444 void _ExportValues(
bool stripPackageName, _EnumPyClassType &enumClass) {
445 boost::python::list valueList;
447 for (
const std::string& name : TfEnum::GetAllNames<T>()) {
448 bool success =
false;
449 TfEnum enumValue = TfEnum::GetValueFromName<T>(name, &success);
454 const std::string cleanedName =
455 Tf_PyCleanEnumName(name, stripPackageName);
458 Tf_TypedPyEnumWrapper<T> wrappedValue(cleanedName, enumValue);
459 boost::python::object pyValue(wrappedValue);
462 Tf_PyEnumRegistry::GetInstance().RegisterValue(enumValue, pyValue);
465 std::string valueName = wrappedValue.GetName();
468 boost::python::scope s(enumClass);
469 Tf_PyEnumAddAttribute(s, valueName, pyValue);
472 boost::python::scope s;
473 Tf_PyEnumAddAttribute(s, valueName, pyValue);
476 valueList.append(pyValue);
480 enumClass.setattr(
"allValues", boost::python::tuple(valueList));
485PXR_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.