24 #ifndef PXR_USD_SDF_PY_CHILDREN_PROXY_H 25 #define PXR_USD_SDF_PY_CHILDREN_PROXY_H 32 #include "pxr/base/tf/pyError.h" 35 #include <boost/python.hpp> 36 #include <boost/python/slice.hpp> 38 PXR_NAMESPACE_OPEN_SCOPE
40 template <
class _View>
41 class SdfPyChildrenProxy {
44 typedef SdfChildrenProxy<View> Proxy;
45 typedef typename Proxy::key_type key_type;
46 typedef typename Proxy::mapped_type mapped_type;
47 typedef typename Proxy::mapped_vector_type mapped_vector_type;
48 typedef typename Proxy::size_type size_type;
49 typedef SdfPyChildrenProxy<View> This;
51 SdfPyChildrenProxy(
const Proxy& proxy) : _proxy(proxy)
56 SdfPyChildrenProxy(
const View& view,
const std::string& type,
57 int permission = Proxy::CanSet |
60 _proxy(view, type, permission)
67 return _proxy == other._proxy;
70 bool operator!=(
const This& other)
const 72 return _proxy != other._proxy;
76 typedef typename Proxy::const_iterator _const_iterator;
77 typedef typename View::const_iterator _view_const_iterator;
80 static boost::python::object Get(
const _const_iterator& i)
82 return boost::python::make_tuple(i->first, i->second);
87 static boost::python::object Get(
const _const_iterator& i)
89 return boost::python::object(i->first);
93 struct _ExtractValue {
94 static boost::python::object Get(
const _const_iterator& i)
96 return boost::python::object(i->second);
103 _Iterator(
const boost::python::object&
object) :
105 _owner(boost::python::extract<const This&>(object)()._proxy)
107 _cur = _owner.begin();
110 _Iterator<E> GetCopy()
const 115 boost::python::object GetNext()
117 if (_cur == _owner.end()) {
120 boost::python::object result = E::Get(_cur);
126 boost::python::object _object;
128 _const_iterator _cur;
133 TfPyWrapOnce<This>(&This::_Wrap);
138 using namespace boost::python;
140 std::string name = _GetName();
143 class_<This>(name.c_str(), no_init)
160 #
if PY_MAJOR_VERSION < 3
179 class_<_Iterator<_ExtractItem> >
180 ((name +
"_Iterator").c_str(), no_init)
181 .def(
"__iter__", &This::template _Iterator<_ExtractItem>::GetCopy)
182 .def(TfPyIteratorNextMethodName, &This::template _Iterator<_ExtractItem>::GetNext)
185 class_<_Iterator<_ExtractKey> >
186 ((name +
"_KeyIterator").c_str(), no_init)
187 .def(
"__iter__", &This::template _Iterator<_ExtractKey>::GetCopy)
188 .def(TfPyIteratorNextMethodName, &This::template _Iterator<_ExtractKey>::GetNext)
191 class_<_Iterator<_ExtractValue> >
192 ((name +
"_ValueIterator").c_str(), no_init)
193 .def(
"__iter__", &This::template _Iterator<_ExtractValue>::GetCopy)
194 .def(TfPyIteratorNextMethodName, &This::template _Iterator<_ExtractValue>::GetNext)
198 static std::string _GetName()
200 std::string name =
"ChildrenProxy_" +
201 ArchGetDemangled<View>();
210 const View& _GetView()
const 220 std::string _GetRepr()
const 222 std::string result(
"{");
223 if (! _proxy.empty()) {
224 _const_iterator i = _proxy.begin(), n = _proxy.end();
227 result +=
", " +
TfPyRepr(i->first) +
235 size_type _GetSize()
const 237 return _proxy.size();
240 mapped_type _GetItemByKey(
const key_type& key)
const 242 _view_const_iterator i = _GetView().find(key);
243 if (i == _GetView().end()) {
245 return mapped_type();
252 mapped_type _GetItemByIndex(
int index)
const 255 return _GetView()[index];
258 void _SetItemByKey(
const key_type& key,
const mapped_type& value)
261 _proxy._GetType().c_str());
264 void _SetItemBySlice(
const boost::python::slice& slice,
265 const mapped_vector_type& values)
273 _proxy._Copy(values);
277 void _DelItemByKey(
const key_type& key)
279 if (_GetView().find(key) == _GetView().end()) {
285 void _DelItemByIndex(
int index)
287 _proxy._Erase(_GetView().key(_GetItemByIndex(index)));
292 _proxy._Copy(mapped_vector_type());
295 void _AppendItem(
const mapped_type& value)
297 _proxy._Insert(value, _proxy.size());
300 void _InsertItemByIndex(
int index,
const mapped_type& value)
303 index = index < (int)_proxy.size()
307 _proxy._Insert(value, index);
310 boost::python::object _PyGet(
const key_type& key)
const 312 _view_const_iterator i = _GetView().find(key);
313 return i == _GetView().end() ? boost::python::object() :
314 boost::python::object(*i);
317 boost::python::object _PyGetDefault(
const key_type& key,
318 const mapped_type& def)
const 320 _view_const_iterator i = _GetView().find(key);
321 return i == _GetView().end() ? boost::python::object(def) :
322 boost::python::object(*i);
325 bool _HasKey(
const key_type& key)
const 327 return _GetView().find(key) != _GetView().end();
330 bool _HasValue(
const mapped_type& value)
const 332 return _GetView().find(value) != _GetView().end();
336 _Iterator<_ExtractItem> _GetItemIterator(
const boost::python::object &x)
338 return _Iterator<_ExtractItem>(x);
342 _Iterator<_ExtractKey> _GetKeyIterator(
const boost::python::object &x)
344 return _Iterator<_ExtractKey>(x);
348 _Iterator<_ExtractValue> _GetValueIterator(
const boost::python::object &x)
350 return _Iterator<_ExtractValue>(x);
354 boost::python::list _Get()
const 356 boost::python::list result;
357 for (_const_iterator i = _proxy.begin(), n = _proxy.end(); i != n; ++i){
358 result.append(E::Get(i));
363 #if PY_MAJOR_VERSION < 3 364 boost::python::list _GetItems()
const 366 return _Get<_ExtractItem>();
369 boost::python::list _GetKeys()
const 371 return _Get<_ExtractKey>();
374 boost::python::list _GetValues()
const 376 return _Get<_ExtractValue>();
380 int _FindIndexByKey(
const key_type& key)
const 382 size_t i = std::distance(_GetView().begin(), _GetView().find(key));
383 return i == _GetView().size() ? -1 : i;
386 int _FindIndexByValue(
const mapped_type& value)
const 388 size_t i = std::distance(_GetView().begin(), _GetView().find(value));
389 return i == _GetView().size() ? -1 : i;
395 template <
class E>
friend class _Iterator;
398 PXR_NAMESPACE_CLOSE_SCOPE
400 #endif // PXR_USD_SDF_PY_CHILDREN_PROXY_H A boost.python call policy class which, when applied to a wrapped function, will create an error mark...
#define TF_CODING_ERROR(fmt, args)
Issue an internal programming error, but continue execution.
Definitions of basic string utilities in tf.
Miscellaneous Utilities for dealing with script.
TF_API void TfPyThrowIndexError(std::string const &msg)
Raises a python IndexError and throws a C++ exception.
Demangle C++ typenames generated by the typeid() facility.
std::string TfPyRepr(T const &t)
Return repr(t).
TF_API void TfPyThrowStopIteration(std::string const &msg)
Raises a python StopIteration exception and throws a C++ exception.
TF_API bool TfPyIsNone(boost::python::object const &obj)
Return true iff obj is None.
TF_API int64_t TfPyNormalizeIndex(int64_t index, uint64_t size, bool throwError=false)
Return a positive index in the range [0,size).
VT_API bool operator==(VtDictionary const &, VtDictionary const &)
Equality comparison.
TF_API std::string TfStringReplace(const std::string &source, const std::string &from, const std::string &to)
Replaces all occurrences of string from with to in source.