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 Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #ifndef PXR_USD_SDF_PY_LIST_EDITOR_PROXY_H
25 #define PXR_USD_SDF_PY_LIST_EDITOR_PROXY_H
26 
27 /// \file sdf/pyListEditorProxy.h
28 
29 #include "pxr/pxr.h"
31 #include "pxr/usd/sdf/listOp.h"
33 
34 #include "pxr/base/arch/demangle.h"
35 #include "pxr/base/tf/diagnostic.h"
36 #include "pxr/base/tf/pyCall.h"
38 #include "pxr/base/tf/pyLock.h"
39 #include "pxr/base/tf/pyUtils.h"
41 #include <hboost/python.hpp>
42 
44 
46 public:
47  template <class T, class V>
48  class ApplyHelper {
49  public:
50  ApplyHelper(const T& owner, const hboost::python::object& callback) :
51  _owner(owner),
52  _callback(callback)
53  {
54  // Do nothing
55  }
56 
57  std::optional<V> operator()(SdfListOpType op, const V& value)
58  {
59  using namespace hboost::python;
60 
61  TfPyLock pyLock;
62  object result = _callback(_owner, value, op);
63  if (! TfPyIsNone(result)) {
64  extract<V> e(result);
65  if (e.check()) {
66  return std::optional<V>(e());
67  }
68  else {
69  TF_CODING_ERROR("ApplyEditsToList callback has "
70  "incorrect return type.");
71  }
72  }
73  return std::optional<V>();
74  }
75 
76  private:
77  const T& _owner;
79  };
80 
81  template <class V>
82  class ModifyHelper {
83  public:
85  _callback(callback)
86  {
87  // Do nothing
88  }
89 
90  std::optional<V> operator()(const V& value)
91  {
92  using namespace hboost::python;
93 
94  TfPyLock pyLock;
95  object result = _callback(value);
96  if (! TfPyIsNone(result)) {
97  extract<V> e(result);
98  if (e.check()) {
99  return std::optional<V>(e());
100  }
101  else {
102  TF_CODING_ERROR("ModifyItemEdits callback has "
103  "incorrect return type.");
104  }
105  }
106  return std::optional<V>();
107  }
108 
109  private:
111  };
112 };
113 
114 template <class T>
116 public:
117  typedef T Type;
118  typedef typename Type::TypePolicy TypePolicy;
119  typedef typename Type::value_type value_type;
120  typedef typename Type::value_vector_type value_vector_type;
121  typedef typename Type::ApplyCallback ApplyCallback;
122  typedef typename Type::ModifyCallback ModifyCallback;
125 
127  {
128  TfPyWrapOnce<Type>(&This::_Wrap);
130  }
131 
132 private:
133  static void _Wrap()
134  {
135  using namespace hboost::python;
136 
137  class_<Type>(_GetName().c_str(), no_init)
138  .def("__str__", &This::_GetStr)
139  .add_property("isExpired", &Type::IsExpired)
140  .add_property("explicitItems",
141  &Type::GetExplicitItems,
142  &This::_SetExplicitProxy)
143  .add_property("addedItems",
144  &Type::GetAddedItems,
145  &This::_SetAddedProxy)
146  .add_property("prependedItems",
147  &Type::GetPrependedItems,
148  &This::_SetPrependedProxy)
149  .add_property("appendedItems",
150  &Type::GetAppendedItems,
151  &This::_SetAppendedProxy)
152  .add_property("deletedItems",
153  &Type::GetDeletedItems,
154  &This::_SetDeletedProxy)
155  .add_property("orderedItems",
156  &Type::GetOrderedItems,
157  &This::_SetOrderedProxy)
158  .def("GetAddedOrExplicitItems", &Type::GetAppliedItems,
159  return_value_policy<TfPySequenceToTuple>()) // deprecated
160  .def("GetAppliedItems", &Type::GetAppliedItems,
161  return_value_policy<TfPySequenceToTuple>())
162  .add_property("isExplicit", &Type::IsExplicit)
163  .add_property("isOrderedOnly", &Type::IsOrderedOnly)
164  .def("ApplyEditsToList",
165  &This::_ApplyEditsToList,
166  return_value_policy<TfPySequenceToList>())
167  .def("ApplyEditsToList",
168  &This::_ApplyEditsToList2,
169  return_value_policy<TfPySequenceToList>())
170 
171  .def("CopyItems", &Type::CopyItems)
172  .def("ClearEdits", &Type::ClearEdits)
173  .def("ClearEditsAndMakeExplicit", &Type::ClearEditsAndMakeExplicit)
174  .def("ContainsItemEdit", &Type::ContainsItemEdit,
175  (arg("item"), arg("onlyAddOrExplicit")=false))
176  .def("RemoveItemEdits", &Type::RemoveItemEdits)
177  .def("ReplaceItemEdits", &Type::ReplaceItemEdits)
178  .def("ModifyItemEdits", &This::_ModifyEdits)
179 
180  // New API (see bug 8710)
181  .def("Add", &Type::Add)
182  .def("Prepend", &Type::Prepend)
183  .def("Append", &Type::Append)
184  .def("Remove", &Type::Remove)
185  .def("Erase", &Type::Erase)
186  ;
187  }
188 
189  static std::string _GetName()
190  {
191  std::string name = "ListEditorProxy_" +
192  ArchGetDemangled<TypePolicy>();
193  name = TfStringReplace(name, " ", "_");
194  name = TfStringReplace(name, ",", "_");
195  name = TfStringReplace(name, "::", "_");
196  name = TfStringReplace(name, "<", "_");
197  name = TfStringReplace(name, ">", "_");
198  return name;
199  }
200 
201  static std::string _GetStr(const Type& x)
202  {
203  return x._listEditor ? TfStringify(*x._listEditor) : std::string();
204  }
205 
206  static void _SetExplicitProxy(Type& x, const value_vector_type& v)
207  {
208  x.GetExplicitItems() = v;
209  }
210 
211  static void _SetAddedProxy(Type& x, const value_vector_type& v)
212  {
213  x.GetAddedItems() = v;
214  }
215 
216  static void _SetPrependedProxy(Type& x, const value_vector_type& v)
217  {
218  x.GetPrependedItems() = v;
219  }
220 
221  static void _SetAppendedProxy(Type& x, const value_vector_type& v)
222  {
223  x.GetAppendedItems() = v;
224  }
225 
226  static void _SetDeletedProxy(Type& x, const value_vector_type& v)
227  {
228  x.GetDeletedItems() = v;
229  }
230 
231  static void _SetOrderedProxy(Type& x, const value_vector_type& v)
232  {
233  x.GetOrderedItems() = v;
234  }
235 
236  static value_vector_type _ApplyEditsToList(const Type& x,
237  const value_vector_type& v)
238  {
239  value_vector_type tmp = v;
240  x.ApplyEditsToList(&tmp);
241  return tmp;
242  }
243 
244  static value_vector_type _ApplyEditsToList2(const Type& x,
245  const value_vector_type& v,
246  const hboost::python::object& cb)
247  {
248  value_vector_type tmp = v;
249  x.ApplyEditsToList(&tmp,
251  return tmp;
252  }
253 
254  static void _ModifyEdits(Type& x, const hboost::python::object& cb)
255  {
257  }
258 };
259 
261 
262 #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
Type::ApplyCallback ApplyCallback
#define TF_CODING_ERROR
**But if you need a result
Definition: thread.h:613
ModifyHelper(const hboost::python::object &callback)
auto arg(const Char *name, const T &arg) -> detail::named_arg< Char, T >
Definition: core.h:1736
uint64 value_type
Definition: GA_PrimCompat.h:29
Type::value_vector_type value_vector_type
SdfListOpType
Definition: listOp.h:46
SdfPyWrapListEditorProxy< Type > This
std::string TfStringify(const T &v)
Definition: stringUtils.h:572
GLuint const GLchar * name
Definition: glcorearb.h:786
GLint GLenum GLint x
Definition: glcorearb.h:409
std::optional< V > operator()(const V &value)
Type::ModifyCallback ModifyCallback
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1432
std::optional< V > operator()(SdfListOpType op, const V &value)
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
Definition: core.h:1131
ApplyHelper(const T &owner, const hboost::python::object &callback)
TF_API bool TfPyIsNone(hboost::python::object const &obj)
Return true iff obj is None.
TF_API std::string TfStringReplace(const std::string &source, const std::string &from, const std::string &to)