7 #ifndef PXR_BASE_TF_PY_ENUM_H
8 #define PXR_BASE_TF_PY_ENUM_H
28 #include "pxr/external/boost/python/class.hpp"
29 #include "pxr/external/boost/python/converter/from_python.hpp"
30 #include "pxr/external/boost/python/converter/registered.hpp"
31 #include "pxr/external/boost/python/converter/rvalue_from_python_data.hpp"
32 #include "pxr/external/boost/python/list.hpp"
33 #include "pxr/external/boost/python/object.hpp"
34 #include "pxr/external/boost/python/operators.hpp"
35 #include "pxr/external/boost/python/refcount.hpp"
36 #include "pxr/external/boost/python/scope.hpp"
37 #include "pxr/external/boost/python/to_python_converter.hpp"
38 #include "pxr/external/boost/python/tuple.hpp"
75 pxr_boost::python::to_python_converter<T, _EnumToPython<T> >();
82 PyObject *_ConvertEnumToPython(
TfEnum const &e);
85 struct _EnumFromPython {
88 (&convertible, &construct, pxr_boost::python::type_id<T>());
90 static void *convertible(PyObject *obj) {
100 return i != o2e.
end() ? obj : 0;
102 return (i != o2e.
end() && i->second.IsA<
T>()) ? obj : 0;
104 static void construct(PyObject *
src, pxr_boost::python::converter::
105 rvalue_from_python_stage1_data *
data) {
107 ((pxr_boost::python::converter::
108 rvalue_from_python_storage<T> *)data)->storage.bytes;
109 new (
storage)
T(_GetEnumValue(src, (
T *)0));
115 template <
typename U>
116 static U _GetEnumValue(PyObject *src, U *) {
126 struct _EnumToPython {
136 size_t operator()(PyObject *o)
const {
137 return reinterpret_cast<size_t>(o);
161 return value.GetValueAsInt();
174 return self.value.GetValueAsInt() == other;
184 return !(lhs == rhs);
286 template <
typename T>
309 bool stripPackageName =
false);
315 const std::string &
name,
366 typedef pxr_boost::python::class_<
378 using namespace pxr_boost::python;
380 const bool explicitName = !
name.empty();
383 std::string enumName = explicitName ?
name :
389 if (baseName == enumName)
390 baseName = std::string();
399 if (!baseName.empty()) {
412 if (!baseName.empty()) {
415 baseName += enumName;
419 _EnumPyClassType enumClass(enumName.c_str(), no_init);
421 enumClass.staticmethod(
"GetValueFromName");
422 enumClass.setattr(
"_baseName", baseName);
433 const bool stripPackageName = baseName.empty();
434 _ExportValues(stripPackageName, enumClass);
440 if (!
type.IsUnknown())
441 type.DefinePythonClass(enumClass);
449 void _ExportValues(
bool stripPackageName, _EnumPyClassType &enumClass) {
450 pxr_boost::python::list valueList;
452 for (
const std::string&
name : TfEnum::GetAllNames<T>()) {
453 bool success =
false;
454 TfEnum enumValue = TfEnum::GetValueFromName<T>(
name, &success);
459 const std::string cleanedName =
470 std::string valueName = wrappedValue.GetName();
473 pxr_boost::python::scope
s(enumClass);
477 pxr_boost::python::scope
s;
481 valueList.append(pyValue);
485 enumClass.setattr(
"allValues", pxr_boost::python::tuple(valueList));
492 #endif // PXR_BASE_TF_PY_ENUM_H
TfPyWrapEnum(std::string const &name=std::string())
Tf_PyEnumWrapper(std::string const &n, TfEnum const &val)
getFileOption("OpenEXR:storage") storage
TF_API std::string TfStringGetSuffix(const std::string &name, char delimiter= '.')
GLsizei const GLfloat * value
ARCH_API std::string ArchGetDemangled(const std::string &typeName)
std::string GetFullName() const
Tf_TypedPyEnumWrapper(std::string const &n, TfEnum const &val)
Tto convert(const Tfrom &source)
friend TfEnum operator|(Tf_PyEnumWrapper const &lhs, Tf_PyEnumWrapper const &rhs)
OIIO_FORCEINLINE vbool4 insert(const vbool4 &a, bool val)
Helper: substitute val for a[i].
auto arg(const Char *name, const T &arg) -> detail::named_arg< Char, T >
static pxr_boost::python::object GetValueFromName(const std::string &name)
friend bool operator<(Tf_PyEnumWrapper const &lhs, Tf_PyEnumWrapper const &rhs)
friend TfEnum operator^(Tf_PyEnumWrapper const &lhs, Tf_PyEnumWrapper const &rhs)
friend bool operator<=(Tf_PyEnumWrapper const &lhs, Tf_PyEnumWrapper const &rhs)
friend bool operator==(Tf_PyEnumWrapper const &self, long other)
friend bool operator>(Tf_PyEnumWrapper const &lhs, Tf_PyEnumWrapper const &rhs)
GLint GLint GLsizei GLint GLenum GLenum type
TF_API std::string Tf_PyCleanEnumName(std::string name, bool stripPackageName=false)
const std::type_info & GetType() const
Returns the type of the enum value, as an std::type_info.
void RegisterEnumConversions()
TF_API void TfPyThrowTypeError(const char *msg)
static TF_API This & GetInstance()
static TF_API std::string GetFullName(TfEnum val)
TF_API_TEMPLATE_CLASS(TfSingleton< Tf_PyEnumRegistry >)
const int & GetValueAsInt() const
Returns the integral value of the enum value.
GLuint const GLchar * name
TF_API void RegisterValue(TfEnum const &e, pxr_boost::python::object const &obj)
TF_API std::string Tf_PyEnumRepr(pxr_boost::python::object const &self)
friend TfEnum operator&(Tf_PyEnumWrapper const &lhs, Tf_PyEnumWrapper const &rhs)
friend TfEnum operator~(Tf_PyEnumWrapper const &rhs)
friend bool operator>=(Tf_PyEnumWrapper const &lhs, Tf_PyEnumWrapper const &rhs)
TF_API void Tf_PyEnumAddAttribute(pxr_boost::python::scope &s, const std::string &name, const pxr_boost::python::object &value)
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
static TF_API std::string GetDisplayName(TfEnum val)
#define PXR_NAMESPACE_CLOSE_SCOPE
std::string GetDisplayName() const
std::string GetName() const
TF_API std::string TfStringGetBeforeSuffix(const std::string &name, char delimiter= '.')
bool IsA() const
True if *this has been assigned any enumerated value of type T.
TF_API std::string TfStringReplace(const std::string &source, const std::string &from, const std::string &to)
friend bool operator!=(Tf_PyEnumWrapper const &lhs, Tf_PyEnumWrapper const &rhs)