24#ifndef PXR_BASE_TF_PY_NOTICE_WRAPPER_H
25#define PXR_BASE_TF_PY_NOTICE_WRAPPER_H
31#include "pxr/base/tf/type.h"
32#include "pxr/base/tf/pyLock.h"
33#include "pxr/base/tf/pyObjectFinder.h"
34#include "pxr/base/tf/wrapTypeHelpers.h"
36#include <boost/python/bases.hpp>
37#include <boost/python/class.hpp>
38#include <boost/python/extract.hpp>
39#include <boost/python/handle.hpp>
45PXR_NAMESPACE_OPEN_SCOPE
47struct Tf_PyNoticeObjectGenerator {
48 typedef Tf_PyNoticeObjectGenerator This;
49 typedef boost::python::object (*MakeObjectFunc)(
TfNotice const &);
53 static void Register() {
55 (*_generators)[
typeid(T).name()] = This::_Generate<T>;
59 TF_API
static boost::python::object Invoke(
TfNotice const &n);
64 static boost::python::object _Generate(
TfNotice const &n) {
66 return boost::python::object(
static_cast<T
const &
>(n));
69 static MakeObjectFunc _Lookup(
TfNotice const &n);
75struct TfPyNoticeWrapperBase :
public TfType::PyPolymorphicBase {
76 TF_API
virtual ~TfPyNoticeWrapperBase();
77 virtual boost::python::handle<> GetNoticePythonObject()
const = 0;
80template <
class Notice>
81struct Tf_PyNoticeObjectFinder :
public Tf_PyObjectFinderBase {
82 virtual ~Tf_PyNoticeObjectFinder() {}
83 virtual boost::python::object Find(
void const *objPtr)
const {
84 using namespace boost::python;
86 Notice
const *wrapper =
static_cast<Notice
const *
>(objPtr);
87 return wrapper ? object(wrapper->GetNoticePythonObject()) : object();
91template <
typename NoticeType,
typename BaseType>
92struct TfPyNoticeWrapper :
public NoticeType,
public TfPyNoticeWrapperBase {
94 static_assert(std::is_base_of<TfNotice, NoticeType>::value
95 || std::is_same<TfNotice, NoticeType>::value,
96 "Notice type must be derived from or equal to TfNotice.");
98 static_assert(std::is_base_of<TfNotice, BaseType>::value
99 || std::is_same<TfNotice, BaseType>::value,
100 "BaseType type must be derived from or equal to TfNotice.");
102 static_assert(std::is_base_of<BaseType, NoticeType>::value
103 || (std::is_same<NoticeType, TfNotice>::value
104 && std::is_same<BaseType, TfNotice>::value),
105 "BaseType type must be a base of notice, unless both "
106 "BaseType and Notice type are equal to TfNotice.");
110 typedef TfPyNoticeWrapper<NoticeType, BaseType> This;
114 using Bases = std::conditional_t<std::is_same<NoticeType, TfNotice>::value,
115 boost::python::bases<>,
116 boost::python::bases<BaseType>>;
118 typedef boost::python::class_<NoticeType, This, Bases> ClassType;
120 static ClassType Wrap(std::string
const &name = std::string()) {
121 std::string wrappedName = name;
122 if (wrappedName.empty()) {
124 wrappedName = TfType::Find<NoticeType>().GetTypeName();
128 Tf_PyNoticeObjectGenerator::Register<NoticeType>();
129 Tf_RegisterPythonObjectFinderInternal
130 (
typeid(TfPyNoticeWrapper),
131 new Tf_PyNoticeObjectFinder<TfPyNoticeWrapper>);
132 return ClassType(wrappedName.c_str(), boost::python::no_init)
137 virtual boost::python::handle<> GetNoticePythonObject()
const {
139 return boost::python::handle<>(boost::python::borrowed(_self));
144 template <
typename... Args>
145 TfPyNoticeWrapper(PyObject *self, Args... args)
146 : NoticeType(args...)
154#define TF_INSTANTIATE_NOTICE_WRAPPER(T, Base) \
155TF_REGISTRY_FUNCTION(TfType) \
157 TfType::Define< TfPyNoticeWrapper<T, Base>, \
158 TfType::Bases<Base> >(); \
161PXR_NAMESPACE_CLOSE_SCOPE
The base class for objects used to notify interested parties (listeners) when events have occurred.
Convenience class for accessing the Python Global Interpreter Lock.
Create or return a previously created object instance of global data.
TF_API std::string TfStringGetSuffix(const std::string &name, char delimiter='.')
Returns the suffix of a string.
Definitions of basic string utilities in tf.
A boost.python visitor that associates the Python class object created by the wrapping with the TfTyp...