HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
pyListEditorProxy.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_USD_SDF_PY_LIST_EDITOR_PROXY_H
8 #define PXR_USD_SDF_PY_LIST_EDITOR_PROXY_H
9 
10 /// \file sdf/pyListEditorProxy.h
11 
12 #include "pxr/pxr.h"
14 #include "pxr/usd/sdf/listOp.h"
16 
17 #include "pxr/base/arch/demangle.h"
18 #include "pxr/base/tf/diagnostic.h"
19 #include "pxr/base/tf/pyCall.h"
21 #include "pxr/base/tf/pyLock.h"
22 #include "pxr/base/tf/pyUtils.h"
24 #include "pxr/external/boost/python.hpp"
25 
27 
29 public:
30  template <class T, class V>
31  class ApplyHelper {
32  public:
33  ApplyHelper(const T& owner, const pxr_boost::python::object& callback) :
34  _owner(owner),
35  _callback(callback)
36  {
37  // Do nothing
38  }
39 
40  std::optional<V> operator()(SdfListOpType op, const V& value)
41  {
42  using namespace pxr_boost::python;
43 
44  TfPyLock pyLock;
45  object result = _callback(_owner, value, op);
46  if (! TfPyIsNone(result)) {
47  extract<V> e(result);
48  if (e.check()) {
49  return std::optional<V>(e());
50  }
51  else {
52  TF_CODING_ERROR("ApplyEditsToList callback has "
53  "incorrect return type.");
54  }
55  }
56  return std::optional<V>();
57  }
58 
59  private:
60  const T& _owner;
62  };
63 
64  template <class V>
65  class ModifyHelper {
66  public:
68  _callback(callback)
69  {
70  // Do nothing
71  }
72 
73  std::optional<V> operator()(const V& value)
74  {
75  using namespace pxr_boost::python;
76 
77  TfPyLock pyLock;
78  object result = _callback(value);
79  if (! TfPyIsNone(result)) {
80  extract<V> e(result);
81  if (e.check()) {
82  return std::optional<V>(e());
83  }
84  else {
85  TF_CODING_ERROR("ModifyItemEdits callback has "
86  "incorrect return type.");
87  }
88  }
89  return std::optional<V>();
90  }
91 
92  private:
94  };
95 };
96 
97 template <class T>
99 public:
100  typedef T Type;
101  typedef typename Type::TypePolicy TypePolicy;
102  typedef typename Type::value_type value_type;
103  typedef typename Type::value_vector_type value_vector_type;
104  typedef typename Type::ApplyCallback ApplyCallback;
105  typedef typename Type::ModifyCallback ModifyCallback;
108 
110  {
111  TfPyWrapOnce<Type>(&This::_Wrap);
113  }
114 
115 private:
116  static void _Wrap()
117  {
118  using namespace pxr_boost::python;
119 
120  class_<Type>(_GetName().c_str(), no_init)
121  .def("__str__", &This::_GetStr)
122  .add_property("isExpired", &Type::IsExpired)
123  .add_property("explicitItems",
124  &Type::GetExplicitItems,
125  &This::_SetExplicitProxy)
126  .add_property("addedItems",
127  &Type::GetAddedItems,
128  &This::_SetAddedProxy)
129  .add_property("prependedItems",
130  &Type::GetPrependedItems,
131  &This::_SetPrependedProxy)
132  .add_property("appendedItems",
133  &Type::GetAppendedItems,
134  &This::_SetAppendedProxy)
135  .add_property("deletedItems",
136  &Type::GetDeletedItems,
137  &This::_SetDeletedProxy)
138  .add_property("orderedItems",
139  &Type::GetOrderedItems,
140  &This::_SetOrderedProxy)
141  .def("GetAddedOrExplicitItems", &Type::GetAppliedItems,
142  return_value_policy<TfPySequenceToTuple>()) // deprecated
143  .def("GetAppliedItems", &Type::GetAppliedItems,
144  return_value_policy<TfPySequenceToTuple>())
145  .add_property("isExplicit", &Type::IsExplicit)
146  .add_property("isOrderedOnly", &Type::IsOrderedOnly)
147  .def("ApplyEditsToList",
148  &This::_ApplyEditsToList,
149  return_value_policy<TfPySequenceToList>())
150  .def("ApplyEditsToList",
151  &This::_ApplyEditsToList2,
152  return_value_policy<TfPySequenceToList>())
153 
154  .def("CopyItems", &Type::CopyItems)
155  .def("ClearEdits", &Type::ClearEdits)
156  .def("ClearEditsAndMakeExplicit", &Type::ClearEditsAndMakeExplicit)
157  .def("ContainsItemEdit", &Type::ContainsItemEdit,
158  (arg("item"), arg("onlyAddOrExplicit")=false))
159  .def("RemoveItemEdits", &Type::RemoveItemEdits)
160  .def("ReplaceItemEdits", &Type::ReplaceItemEdits)
161  .def("ModifyItemEdits", &This::_ModifyEdits)
162 
163  // New API (see bug 8710)
164  .def("Add", &Type::Add)
165  .def("Prepend", &Type::Prepend)
166  .def("Append", &Type::Append)
167  .def("Remove", &Type::Remove)
168  .def("Erase", &Type::Erase)
169  ;
170  }
171 
172  static std::string _GetName()
173  {
174  std::string name = "ListEditorProxy_" +
175  ArchGetDemangled<TypePolicy>();
176  name = TfStringReplace(name, " ", "_");
177  name = TfStringReplace(name, ",", "_");
178  name = TfStringReplace(name, "::", "_");
179  name = TfStringReplace(name, "<", "_");
180  name = TfStringReplace(name, ">", "_");
181  return name;
182  }
183 
184  static std::string _GetStr(const Type& x)
185  {
186  return x._listEditor ? TfStringify(*x._listEditor) : std::string();
187  }
188 
189  static void _SetExplicitProxy(Type& x, const value_vector_type& v)
190  {
191  x.GetExplicitItems() = v;
192  }
193 
194  static void _SetAddedProxy(Type& x, const value_vector_type& v)
195  {
196  x.GetAddedItems() = v;
197  }
198 
199  static void _SetPrependedProxy(Type& x, const value_vector_type& v)
200  {
201  x.GetPrependedItems() = v;
202  }
203 
204  static void _SetAppendedProxy(Type& x, const value_vector_type& v)
205  {
206  x.GetAppendedItems() = v;
207  }
208 
209  static void _SetDeletedProxy(Type& x, const value_vector_type& v)
210  {
211  x.GetDeletedItems() = v;
212  }
213 
214  static void _SetOrderedProxy(Type& x, const value_vector_type& v)
215  {
216  x.GetOrderedItems() = v;
217  }
218 
219  static value_vector_type _ApplyEditsToList(const Type& x,
220  const value_vector_type& v)
221  {
222  value_vector_type tmp = v;
223  x.ApplyEditsToList(&tmp);
224  return tmp;
225  }
226 
227  static value_vector_type _ApplyEditsToList2(const Type& x,
228  const value_vector_type& v,
229  const pxr_boost::python::object& cb)
230  {
231  value_vector_type tmp = v;
232  x.ApplyEditsToList(&tmp,
234  return tmp;
235  }
236 
237  static void _ModifyEdits(Type& x, const pxr_boost::python::object& cb)
238  {
240  }
241 };
242 
244 
245 #endif // PXR_USD_SDF_PY_LIST_EDITOR_PROXY_H
SdfListProxy< TypePolicy > ListProxy
const GLdouble * v
Definition: glcorearb.h:837
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
GLsizei const GLfloat * value
Definition: glcorearb.h:824
Type::ApplyCallback ApplyCallback
#define TF_CODING_ERROR
**But if you need a result
Definition: thread.h:622
auto arg(const Char *name, const T &arg) -> detail::named_arg< Char, T >
Definition: core.h:1859
uint64 value_type
Definition: GA_PrimCompat.h:29
Type::value_vector_type value_vector_type
TF_API bool TfPyIsNone(pxr_boost::python::object const &obj)
Return true iff obj is None.
SdfListOpType
Definition: listOp.h:29
SdfPyWrapListEditorProxy< Type > This
std::string TfStringify(const T &v)
Definition: stringUtils.h:556
GLuint const GLchar * name
Definition: glcorearb.h:786
GLint GLenum GLint x
Definition: glcorearb.h:409
std::optional< V > operator()(const V &value)
ApplyHelper(const T &owner, const pxr_boost::python::object &callback)
Type::ModifyCallback ModifyCallback
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
std::optional< V > operator()(SdfListOpType op, const V &value)
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
ModifyHelper(const pxr_boost::python::object &callback)
TF_API std::string TfStringReplace(const std::string &source, const std::string &from, const std::string &to)