24 #ifndef PXR_BASE_TF_PY_ENUM_H
25 #define PXR_BASE_TF_PY_ENUM_H
45 #include <hboost/python/class.hpp>
46 #include <hboost/python/converter/from_python.hpp>
47 #include <hboost/python/converter/registered.hpp>
48 #include <hboost/python/converter/rvalue_from_python_data.hpp>
49 #include <hboost/python/list.hpp>
50 #include <hboost/python/object.hpp>
51 #include <hboost/python/operators.hpp>
52 #include <hboost/python/refcount.hpp>
53 #include <hboost/python/scope.hpp>
54 #include <hboost/python/to_python_converter.hpp>
55 #include <hboost/python/tuple.hpp>
92 hboost::python::to_python_converter<T, _EnumToPython<T> >();
99 PyObject *_ConvertEnumToPython(
TfEnum const &e);
101 template <
typename T>
102 struct _EnumFromPython {
105 (&convertible, &construct, hboost::python::type_id<T>());
107 static void *convertible(PyObject *obj) {
117 return i != o2e.
end() ? obj : 0;
119 return (i != o2e.
end() && i->second.IsA<
T>()) ? obj : 0;
121 static void construct(PyObject *
src, hboost::python::converter::
122 rvalue_from_python_stage1_data *
data) {
124 ((hboost::python::converter::
125 rvalue_from_python_storage<T> *)data)->storage.bytes;
126 new (
storage)
T(_GetEnumValue(src, (
T *)0));
132 template <
typename U>
133 static U _GetEnumValue(PyObject *src, U *) {
143 struct _EnumToPython {
153 size_t operator()(PyObject *o)
const {
154 return reinterpret_cast<size_t>(o);
178 return value.GetValueAsInt();
191 return self.value.GetValueAsInt() == other;
201 return !(lhs == rhs);
303 template <
typename T>
326 bool stripPackageName =
false);
383 typedef hboost::python::class_<
395 using namespace hboost::python;
397 const bool explicitName = !
name.empty();
406 if (baseName == enumName)
416 if (!baseName.empty()) {
429 if (!baseName.empty()) {
432 baseName += enumName;
436 _EnumPyClassType enumClass(enumName.c_str(), no_init);
438 enumClass.staticmethod(
"GetValueFromName");
439 enumClass.setattr(
"_baseName", baseName);
450 const bool stripPackageName = baseName.empty();
451 _ExportValues(stripPackageName, enumClass);
457 if (!
type.IsUnknown())
458 type.DefinePythonClass(enumClass);
466 void _ExportValues(
bool stripPackageName, _EnumPyClassType &enumClass) {
467 hboost::python::list valueList;
470 bool success =
false;
471 TfEnum enumValue = TfEnum::GetValueFromName<T>(
name, &success);
490 hboost::python::scope
s(enumClass);
494 hboost::python::scope
s;
498 valueList.append(pyValue);
502 enumClass.setattr(
"allValues", hboost::python::tuple(valueList));
509 #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 GLchar *const * string
GLsizei const GLfloat * value
TF_API void RegisterValue(TfEnum const &e, hboost::python::object const &obj)
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 >
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)
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
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)
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
static TF_API std::string GetDisplayName(TfEnum val)
#define PXR_NAMESPACE_CLOSE_SCOPE
TF_API void Tf_PyEnumAddAttribute(hboost::python::scope &s, const std::string &name, const hboost::python::object &value)
std::string GetDisplayName() const
static hboost::python::object GetValueFromName(const std::string &name)
std::string GetName() const
TF_API std::string TfStringGetBeforeSuffix(const std::string &name, char delimiter= '.')
TF_API std::string Tf_PyEnumRepr(hboost::python::object const &self)
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)