7#ifndef PXR_USD_SDF_PY_CHILDREN_VIEW_H
8#define PXR_USD_SDF_PY_CHILDREN_VIEW_H
17#include "pxr/external/boost/python.hpp"
19PXR_NAMESPACE_OPEN_SCOPE
22class SdfPyWrapChildrenView {
25 typedef typename View::ChildPolicy ChildPolicy;
26 typedef typename View::Predicate Predicate;
27 typedef typename View::key_type key_type;
28 typedef typename View::value_type value_type;
29 typedef typename View::const_iterator const_iterator;
30 typedef SdfPyWrapChildrenView<View> This;
32 SdfPyWrapChildrenView()
34 TfPyWrapOnce<View>(&This::_Wrap);
39 static pxr_boost::python::object Get(
const View& x,
const const_iterator& i)
41 return pxr_boost::python::make_tuple(x.key(i), *i);
46 static pxr_boost::python::object Get(
const View& x,
const const_iterator& i)
48 return pxr_boost::python::object(x.key(i));
52 struct _ExtractValue {
53 static pxr_boost::python::object Get(
const View& x,
const const_iterator& i)
55 return pxr_boost::python::object(*i);
62 _Iterator(
const pxr_boost::python::object&
object) :
64 _owner(pxr_boost::python::extract<const View&>(object)),
71 _Iterator<E> GetCopy()
const
76 pxr_boost::python::object GetNext()
81 pxr_boost::python::object result = E::Get(_owner, _cur);
87 pxr_boost::python::object _object;
95 using namespace pxr_boost::python;
97 std::string name = _GetName();
104 class_<View>(name.c_str(), no_init)
105 .def(
"__repr__", &This::_GetRepr)
106 .def(
"__len__", &View::size)
107 .def(
"__getitem__", &This::_GetItemByKey)
108 .def(
"__getitem__", &This::_GetItemByIndex)
109 .def(
"get", &This::_PyGet)
110 .def(
"__contains__", &This::_HasKey)
111 .def(
"__contains__", &This::_HasValue)
112 .def(
"__iter__", &This::_GetValueIterator)
113 .def(
"items", &This::_GetItemIterator)
114 .def(
"keys", &This::_GetKeyIterator)
115 .def(
"values", &This::_GetValueIterator)
116 .def(
"index", &This::_FindIndexByKey)
117 .def(
"index", &This::_FindIndexByValue)
122 class_<_Iterator<_ExtractItem> >
123 ((name +
"_Iterator").c_str(), no_init)
124 .def(
"__iter__", &This::template _Iterator<_ExtractItem>::GetCopy)
125 .def(
"__next__", &This::template _Iterator<_ExtractItem>::GetNext)
128 class_<_Iterator<_ExtractKey> >
129 ((name +
"_KeyIterator").c_str(), no_init)
130 .def(
"__iter__", &This::template _Iterator<_ExtractKey>::GetCopy)
131 .def(
"__next__", &This::template _Iterator<_ExtractKey>::GetNext)
134 class_<_Iterator<_ExtractValue> >
135 ((name +
"_ValueIterator").c_str(), no_init)
136 .def(
"__iter__", &This::template _Iterator<_ExtractValue>::GetCopy)
137 .def(
"__next__", &This::template _Iterator<_ExtractValue>::GetNext)
141 static std::string _GetName()
143 std::string name =
"ChildrenView_" +
144 ArchGetDemangled<ChildPolicy>() +
"_" +
145 ArchGetDemangled<Predicate>();
154 static std::string _GetRepr(
const View& x)
156 std::string result(
"{");
158 const_iterator i = x.begin(), n = x.end();
168 static value_type _GetItemByKey(
const View& x,
const key_type& key)
170 const_iterator i = x.find(key);
180 static value_type _GetItemByIndex(
const View& x,
size_t index)
182 if (index >= x.size()) {
188 static pxr_boost::python::object _PyGet(
const View& x,
const key_type& key)
190 const_iterator i = x.find(key);
191 return i == x.end() ? pxr_boost::python::object() :
192 pxr_boost::python::object(*i);
195 static bool _HasKey(
const View& x,
const key_type& key)
197 return x.find(key) != x.end();
200 static bool _HasValue(
const View& x,
const value_type& value)
202 return x.find(value) != x.end();
206 _Iterator<_ExtractItem> _GetItemIterator(
const pxr_boost::python::object& x)
208 return _Iterator<_ExtractItem>(x);
212 _Iterator<_ExtractKey> _GetKeyIterator(
const pxr_boost::python::object& x)
214 return _Iterator<_ExtractKey>(x);
218 _Iterator<_ExtractValue> _GetValueIterator(
const pxr_boost::python::object& x)
220 return _Iterator<_ExtractValue>(x);
224 static pxr_boost::python::list _Get(
const View& x)
226 pxr_boost::python::list result;
227 for (const_iterator i = x.begin(), n = x.end(); i != n; ++i) {
228 result.append(E::Get(x, i));
233 static int _FindIndexByKey(
const View& x,
const key_type& key)
235 size_t i = std::distance(x.begin(), x.find(key));
236 return i == x.size() ? -1 : i;
239 static int _FindIndexByValue(
const View& x,
const value_type& value)
241 size_t i = std::distance(x.begin(), x.find(value));
242 return i == x.size() ? -1 : i;
246PXR_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_...
std::string TfPyRepr(T const &t)
Return repr(t).
Demangle C++ typenames generated by the typeid() facility.
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.