7#ifndef PXR_USD_SDF_PY_CHILDREN_PROXY_H
8#define PXR_USD_SDF_PY_CHILDREN_PROXY_H
15#include "pxr/base/tf/pyError.h"
18#include "pxr/external/boost/python.hpp"
19#include "pxr/external/boost/python/slice.hpp"
21PXR_NAMESPACE_OPEN_SCOPE
24class SdfPyChildrenProxy {
27 typedef SdfChildrenProxy<View> Proxy;
28 typedef typename Proxy::key_type key_type;
29 typedef typename Proxy::mapped_type mapped_type;
30 typedef typename Proxy::mapped_vector_type mapped_vector_type;
31 typedef typename Proxy::size_type size_type;
32 typedef SdfPyChildrenProxy<View> This;
34 SdfPyChildrenProxy(
const Proxy& proxy) : _proxy(proxy)
39 SdfPyChildrenProxy(
const View& view,
const std::string& type,
40 int permission = Proxy::CanSet |
43 _proxy(view, type, permission)
48 bool operator==(
const This& other)
const
50 return _proxy == other._proxy;
53 bool operator!=(
const This& other)
const
55 return _proxy != other._proxy;
59 typedef typename Proxy::const_iterator _const_iterator;
60 typedef typename View::const_iterator _view_const_iterator;
63 static pxr_boost::python::object Get(
const _const_iterator& i)
65 return pxr_boost::python::make_tuple(i->first, i->second);
70 static pxr_boost::python::object Get(
const _const_iterator& i)
72 return pxr_boost::python::object(i->first);
76 struct _ExtractValue {
77 static pxr_boost::python::object Get(
const _const_iterator& i)
79 return pxr_boost::python::object(i->second);
86 _Iterator(
const pxr_boost::python::object&
object) :
88 _owner(pxr_boost::python::extract<const This&>(object)()._proxy)
90 _cur = _owner.begin();
93 _Iterator<E> GetCopy()
const
98 pxr_boost::python::object GetNext()
100 if (_cur == _owner.end()) {
103 pxr_boost::python::object result = E::Get(_cur);
109 pxr_boost::python::object _object;
111 _const_iterator _cur;
116 TfPyWrapOnce<This>(&This::_Wrap);
121 using namespace pxr_boost::python;
123 std::string name = _GetName();
126 class_<This>(name.c_str(), no_init)
152 class_<_Iterator<_ExtractItem> >
153 ((name +
"_Iterator").c_str(), no_init)
154 .def(
"__iter__", &This::template _Iterator<_ExtractItem>::GetCopy)
155 .def(
"__next__", &This::template _Iterator<_ExtractItem>::GetNext)
158 class_<_Iterator<_ExtractKey> >
159 ((name +
"_KeyIterator").c_str(), no_init)
160 .def(
"__iter__", &This::template _Iterator<_ExtractKey>::GetCopy)
161 .def(
"__next__", &This::template _Iterator<_ExtractKey>::GetNext)
164 class_<_Iterator<_ExtractValue> >
165 ((name +
"_ValueIterator").c_str(), no_init)
166 .def(
"__iter__", &This::template _Iterator<_ExtractValue>::GetCopy)
167 .def(
"__next__", &This::template _Iterator<_ExtractValue>::GetNext)
171 static std::string _GetName()
173 std::string name =
"ChildrenProxy_" +
174 ArchGetDemangled<View>();
183 const View& _GetView()
const
193 std::string _GetRepr()
const
195 std::string result(
"{");
196 if (! _proxy.empty()) {
197 _const_iterator i = _proxy.begin(), n = _proxy.end();
200 result +=
", " +
TfPyRepr(i->first) +
208 size_type _GetSize()
const
210 return _proxy.size();
213 mapped_type _GetItemByKey(
const key_type& key)
const
215 _view_const_iterator i = _GetView().find(key);
216 if (i == _GetView().end()) {
218 return mapped_type();
225 mapped_type _GetItemByIndex(
int index)
const
228 return _GetView()[index];
231 void _SetItemByKey(
const key_type& key,
const mapped_type& value)
234 _proxy._GetType().c_str());
237 void _SetItemBySlice(
const pxr_boost::python::slice& slice,
238 const mapped_vector_type& values)
246 _proxy._Copy(values);
250 void _DelItemByKey(
const key_type& key)
252 if (_GetView().find(key) == _GetView().end()) {
258 void _DelItemByIndex(
int index)
260 _proxy._Erase(_GetView().key(_GetItemByIndex(index)));
265 _proxy._Copy(mapped_vector_type());
268 void _AppendItem(
const mapped_type& value)
270 _proxy._Insert(value, _proxy.size());
273 void _InsertItemByIndex(
int index,
const mapped_type& value)
276 index = index < (int)_proxy.size()
280 _proxy._Insert(value, index);
283 pxr_boost::python::object _PyGet(
const key_type& key)
const
285 _view_const_iterator i = _GetView().find(key);
286 return i == _GetView().end() ? pxr_boost::python::object() :
287 pxr_boost::python::object(*i);
290 pxr_boost::python::object _PyGetDefault(
const key_type& key,
291 const mapped_type& def)
const
293 _view_const_iterator i = _GetView().find(key);
294 return i == _GetView().end() ? pxr_boost::python::object(def) :
295 pxr_boost::python::object(*i);
298 bool _HasKey(
const key_type& key)
const
300 return _GetView().find(key) != _GetView().end();
303 bool _HasValue(
const mapped_type& value)
const
305 return _GetView().find(value) != _GetView().end();
309 _Iterator<_ExtractItem> _GetItemIterator(
const pxr_boost::python::object &x)
311 return _Iterator<_ExtractItem>(x);
315 _Iterator<_ExtractKey> _GetKeyIterator(
const pxr_boost::python::object &x)
317 return _Iterator<_ExtractKey>(x);
321 _Iterator<_ExtractValue> _GetValueIterator(
const pxr_boost::python::object &x)
323 return _Iterator<_ExtractValue>(x);
327 pxr_boost::python::list _Get()
const
329 pxr_boost::python::list result;
330 for (_const_iterator i = _proxy.begin(), n = _proxy.end(); i != n; ++i){
331 result.append(E::Get(i));
336 int _FindIndexByKey(
const key_type& key)
const
338 size_t i = std::distance(_GetView().begin(), _GetView().find(key));
339 return i == _GetView().size() ? -1 : i;
342 int _FindIndexByValue(
const mapped_type& value)
const
344 size_t i = std::distance(_GetView().begin(), _GetView().find(value));
345 return i == _GetView().size() ? -1 : i;
351 template <
class E>
friend class _Iterator;
354PXR_NAMESPACE_CLOSE_SCOPE
Miscellaneous Utilities for dealing with script.
TF_API void TfPyThrowIndexError(const char *msg)
Raises a Python IndexError with the given error msg and throws a pxr_boost::python::error_already_set...
TF_API void TfPyThrowStopIteration(const char *msg)
Raises a Python StopIteration with the given error msg and throws a pxr_boost::python::error_already_...
TF_API bool TfPyIsNone(pxr_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).
std::string TfPyRepr(T const &t)
Return repr(t).
Demangle C++ typenames generated by the typeid() facility.
#define TF_CODING_ERROR(fmt, args)
Issue an internal programming error, but continue execution.
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.
Definitions of basic string utilities in tf.
A boost.python call policy class which, when applied to a wrapped function, will create an error mark...