24 #ifndef PXR_USD_SDF_PY_CHILDREN_VIEW_H 25 #define PXR_USD_SDF_PY_CHILDREN_VIEW_H 34 #include <boost/python.hpp> 36 PXR_NAMESPACE_OPEN_SCOPE
38 template <
class _View>
39 class SdfPyWrapChildrenView {
42 typedef typename View::ChildPolicy ChildPolicy;
43 typedef typename View::Predicate Predicate;
44 typedef typename View::key_type key_type;
45 typedef typename View::value_type value_type;
46 typedef typename View::const_iterator const_iterator;
47 typedef SdfPyWrapChildrenView<View> This;
49 SdfPyWrapChildrenView()
51 TfPyWrapOnce<View>(&This::_Wrap);
56 static boost::python::object Get(
const View& x,
const const_iterator& i)
58 return boost::python::make_tuple(x.key(i), *i);
63 static boost::python::object Get(
const View& x,
const const_iterator& i)
65 return boost::python::object(x.key(i));
69 struct _ExtractValue {
70 static boost::python::object Get(
const View& x,
const const_iterator& i)
72 return boost::python::object(*i);
79 _Iterator(
const boost::python::object&
object) :
81 _owner(boost::python::extract<const View&>(object)),
88 _Iterator<E> GetCopy()
const 93 boost::python::object GetNext()
98 boost::python::object result = E::Get(_owner, _cur);
104 boost::python::object _object;
112 using namespace boost::python;
114 std::string name = _GetName();
121 class_<View>(name.c_str(), no_init)
122 .def(
"__repr__", &This::_GetRepr)
123 .def(
"__len__", &View::size)
124 .def(
"__getitem__", &This::_GetItemByKey)
125 .def(
"__getitem__", &This::_GetItemByIndex)
126 .def(
"get", &This::_PyGet)
127 .def(
"__contains__", &This::_HasKey)
128 .def(
"__contains__", &This::_HasValue)
129 .def(
"__iter__", &This::_GetValueIterator)
130 #if PY_MAJOR_VERSION < 3 131 .def(
"has_key", &This::_HasKey)
132 .def(
"itervalues", &This::_GetValueIterator)
133 .def(
"iterkeys", &This::_GetKeyIterator)
134 .def(
"iteritems", &This::_GetItemIterator)
135 .def(
"items", &This::_GetItems)
136 .def(
"keys", &This::_GetKeys)
137 .def(
"values", &This::_GetValues)
139 .def(
"items", &This::_GetItemIterator)
140 .def(
"keys", &This::_GetKeyIterator)
141 .def(
"values", &This::_GetValueIterator)
143 .def(
"index", &This::_FindIndexByKey)
144 .def(
"index", &This::_FindIndexByValue)
149 class_<_Iterator<_ExtractItem> >
150 ((name +
"_Iterator").c_str(), no_init)
151 .def(
"__iter__", &This::template _Iterator<_ExtractItem>::GetCopy)
152 .def(TfPyIteratorNextMethodName, &This::template _Iterator<_ExtractItem>::GetNext)
155 class_<_Iterator<_ExtractKey> >
156 ((name +
"_KeyIterator").c_str(), no_init)
157 .def(
"__iter__", &This::template _Iterator<_ExtractKey>::GetCopy)
158 .def(TfPyIteratorNextMethodName, &This::template _Iterator<_ExtractKey>::GetNext)
161 class_<_Iterator<_ExtractValue> >
162 ((name +
"_ValueIterator").c_str(), no_init)
163 .def(
"__iter__", &This::template _Iterator<_ExtractValue>::GetCopy)
164 .def(TfPyIteratorNextMethodName, &This::template _Iterator<_ExtractValue>::GetNext)
168 static std::string _GetName()
170 std::string name =
"ChildrenView_" +
171 ArchGetDemangled<ChildPolicy>() +
"_" +
172 ArchGetDemangled<Predicate>();
181 static std::string _GetRepr(
const View& x)
183 std::string result(
"{");
185 const_iterator i = x.begin(), n = x.end();
195 static value_type _GetItemByKey(
const View& x,
const key_type& key)
197 const_iterator i = x.find(key);
207 static value_type _GetItemByIndex(
const View& x,
size_t index)
209 if (index >= x.size()) {
215 static boost::python::object _PyGet(
const View& x,
const key_type& key)
217 const_iterator i = x.find(key);
218 return i == x.end() ? boost::python::object() :
219 boost::python::object(*i);
222 static bool _HasKey(
const View& x,
const key_type& key)
224 return x.find(key) != x.end();
227 static bool _HasValue(
const View& x,
const value_type& value)
229 return x.find(value) != x.end();
233 _Iterator<_ExtractItem> _GetItemIterator(
const boost::python::object& x)
235 return _Iterator<_ExtractItem>(x);
239 _Iterator<_ExtractKey> _GetKeyIterator(
const boost::python::object& x)
241 return _Iterator<_ExtractKey>(x);
245 _Iterator<_ExtractValue> _GetValueIterator(
const boost::python::object& x)
247 return _Iterator<_ExtractValue>(x);
251 static boost::python::list _Get(
const View& x)
253 boost::python::list result;
254 for (const_iterator i = x.begin(), n = x.end(); i != n; ++i) {
255 result.append(E::Get(x, i));
260 #if PY_MAJOR_VERSION < 3 261 static boost::python::list _GetItems(
const View& x)
263 return _Get<_ExtractItem>(x);
266 static boost::python::list _GetKeys(
const View& x)
268 return _Get<_ExtractKey>(x);
271 static boost::python::list _GetValues(
const View& x)
273 return _Get<_ExtractValue>(x);
277 static int _FindIndexByKey(
const View& x,
const key_type& key)
279 size_t i = std::distance(x.begin(), x.find(key));
280 return i == x.size() ? -1 : i;
283 static int _FindIndexByValue(
const View& x,
const value_type& value)
285 size_t i = std::distance(x.begin(), x.find(value));
286 return i == x.size() ? -1 : i;
290 PXR_NAMESPACE_CLOSE_SCOPE
292 #endif // PXR_USD_SDF_PY_CHILDREN_VIEW_H 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 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.