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>
38PXR_NAMESPACE_OPEN_SCOPE
41class 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)
65 bool operator==(
const This& other)
const
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)
169 class_<_Iterator<_ExtractItem> >
170 ((name +
"_Iterator").c_str(), no_init)
171 .def(
"__iter__", &This::template _Iterator<_ExtractItem>::GetCopy)
172 .def(
"__next__", &This::template _Iterator<_ExtractItem>::GetNext)
175 class_<_Iterator<_ExtractKey> >
176 ((name +
"_KeyIterator").c_str(), no_init)
177 .def(
"__iter__", &This::template _Iterator<_ExtractKey>::GetCopy)
178 .def(
"__next__", &This::template _Iterator<_ExtractKey>::GetNext)
181 class_<_Iterator<_ExtractValue> >
182 ((name +
"_ValueIterator").c_str(), no_init)
183 .def(
"__iter__", &This::template _Iterator<_ExtractValue>::GetCopy)
184 .def(
"__next__", &This::template _Iterator<_ExtractValue>::GetNext)
188 static std::string _GetName()
190 std::string name =
"ChildrenProxy_" +
191 ArchGetDemangled<View>();
200 const View& _GetView()
const
210 std::string _GetRepr()
const
212 std::string result(
"{");
213 if (! _proxy.empty()) {
214 _const_iterator i = _proxy.begin(), n = _proxy.end();
217 result +=
", " +
TfPyRepr(i->first) +
225 size_type _GetSize()
const
227 return _proxy.size();
230 mapped_type _GetItemByKey(
const key_type& key)
const
232 _view_const_iterator i = _GetView().find(key);
233 if (i == _GetView().end()) {
235 return mapped_type();
242 mapped_type _GetItemByIndex(
int index)
const
245 return _GetView()[index];
248 void _SetItemByKey(
const key_type& key,
const mapped_type& value)
251 _proxy._GetType().c_str());
254 void _SetItemBySlice(
const boost::python::slice& slice,
255 const mapped_vector_type& values)
263 _proxy._Copy(values);
267 void _DelItemByKey(
const key_type& key)
269 if (_GetView().find(key) == _GetView().end()) {
275 void _DelItemByIndex(
int index)
277 _proxy._Erase(_GetView().key(_GetItemByIndex(index)));
282 _proxy._Copy(mapped_vector_type());
285 void _AppendItem(
const mapped_type& value)
287 _proxy._Insert(value, _proxy.size());
290 void _InsertItemByIndex(
int index,
const mapped_type& value)
293 index = index < (int)_proxy.size()
297 _proxy._Insert(value, index);
300 boost::python::object _PyGet(
const key_type& key)
const
302 _view_const_iterator i = _GetView().find(key);
303 return i == _GetView().end() ? boost::python::object() :
304 boost::python::object(*i);
307 boost::python::object _PyGetDefault(
const key_type& key,
308 const mapped_type& def)
const
310 _view_const_iterator i = _GetView().find(key);
311 return i == _GetView().end() ? boost::python::object(def) :
312 boost::python::object(*i);
315 bool _HasKey(
const key_type& key)
const
317 return _GetView().find(key) != _GetView().end();
320 bool _HasValue(
const mapped_type& value)
const
322 return _GetView().find(value) != _GetView().end();
326 _Iterator<_ExtractItem> _GetItemIterator(
const boost::python::object &x)
328 return _Iterator<_ExtractItem>(x);
332 _Iterator<_ExtractKey> _GetKeyIterator(
const boost::python::object &x)
334 return _Iterator<_ExtractKey>(x);
338 _Iterator<_ExtractValue> _GetValueIterator(
const boost::python::object &x)
340 return _Iterator<_ExtractValue>(x);
344 boost::python::list _Get()
const
346 boost::python::list result;
347 for (_const_iterator i = _proxy.begin(), n = _proxy.end(); i != n; ++i){
348 result.append(E::Get(i));
353 int _FindIndexByKey(
const key_type& key)
const
355 size_t i = std::distance(_GetView().begin(), _GetView().find(key));
356 return i == _GetView().size() ? -1 : i;
359 int _FindIndexByValue(
const mapped_type& value)
const
361 size_t i = std::distance(_GetView().begin(), _GetView().find(value));
362 return i == _GetView().size() ? -1 : i;
368 template <
class E>
friend class _Iterator;
371PXR_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 boost::python::error_already_set exc...
TF_API void TfPyThrowStopIteration(const char *msg)
Raises a Python StopIteration with the given error msg and throws a boost::python::error_already_set ...
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).
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...