24 #ifndef PXR_USD_SDF_PY_LIST_PROXY_H 25 #define PXR_USD_SDF_PY_LIST_PROXY_H 30 #include "pxr/usd/sdf/changeBlock.h" 33 #include "pxr/base/tf/pyLock.h" 35 #include "pxr/base/tf/pyResultConversions.h" 38 #include <boost/python.hpp> 39 #include <boost/python/slice.hpp> 41 PXR_NAMESPACE_OPEN_SCOPE
44 class SdfPyWrapListProxy {
47 typedef typename Type::TypePolicy TypePolicy;
48 typedef typename Type::value_type value_type;
49 typedef typename Type::value_vector_type value_vector_type;
50 typedef SdfPyWrapListProxy<Type> This;
54 TfPyWrapOnce<Type>(&This::_Wrap);
60 using namespace boost::python;
62 class_<Type>(_GetName().c_str(), no_init)
63 .def(
"__str__", &This::_GetStr)
64 .def(
"__len__", &Type::size)
65 .def(
"__getitem__", &This::_GetItemIndex)
66 .def(
"__getitem__", &This::_GetItemSlice)
67 .def(
"__setitem__", &This::_SetItemIndex)
68 .def(
"__setitem__", &This::_SetItemSlice)
69 .def(
"__delitem__", &This::_DelItemIndex)
70 .def(
"__delitem__", &This::_DelItemSlice)
71 .def(
"__delitem__", &Type::Remove)
72 .def(
"count", &Type::Count)
73 .def(
"copy", &Type::operator value_vector_type,
74 return_value_policy<TfPySequenceToList>())
75 .def(
"index", &This::_FindIndex)
76 .def(
"clear", &Type::clear)
77 .def(
"insert", &This::_Insert)
78 .def(
"append", &Type::push_back)
79 .def(
"remove", &Type::Remove)
80 .def(
"replace", &Type::Replace)
81 .def(
"ApplyList", &Type::ApplyList)
82 .def(
"ApplyEditsToList", &This::_ApplyEditsToList)
83 .add_property(
"expired", &This::_IsExpired)
90 .def(
self == other<value_vector_type>())
91 .def(
self != other<value_vector_type>())
92 .def(
self < other<value_vector_type>())
93 .def(
self <= other<value_vector_type>())
94 .def(
self > other<value_vector_type>())
95 .def(
self >= other<value_vector_type>())
99 static std::string _GetName()
101 std::string name =
"ListProxy_" +
102 ArchGetDemangled<TypePolicy>();
111 static std::string _GetStr(
const Type& x)
113 return TfPyRepr(static_cast<value_vector_type>(x));
116 static value_type _GetItemIndex(
const Type& x,
int index)
121 static boost::python::list _GetItemSlice(
const Type& x,
122 const boost::python::slice& index)
124 using namespace boost::python;
130 slice::range<typename Type::const_iterator> range =
131 index.get_indicies(x.begin(), x.end());
132 for (; range.start != range.stop; range.start += range.step) {
133 result.append(*range.start);
135 result.append(*range.start);
137 catch (
const std::invalid_argument&) {
145 static void _SetItemIndex(Type& x,
int index,
const value_type& value)
150 static void _SetItemSlice(Type& x,
const boost::python::slice& index,
151 const value_vector_type& values)
153 using namespace boost::python;
155 if (! x._Validate()) {
160 size_t start, step, count;
162 slice::range<typename Type::iterator> range =
163 index.get_indicies(x.begin(), x.end());
164 start = range.start - x.begin();
166 count = 1 + (range.stop - range.start) / range.step;
168 catch (
const std::invalid_argument&) {
170 extract<int> e(index.start());
178 x._Edit(start, count, values);
182 if (count != values.size()) {
185 "to extended slice of size %zd",
186 values.size(), count).c_str());
188 else if (step == 1) {
189 x._Edit(start, count, values);
193 for (
size_t i = 0, j = start; i != count; j += step, ++i) {
194 x._Edit(j, 1, value_vector_type(1, values[i]));
200 static void _DelItemIndex(Type& x,
int i)
203 1, value_vector_type());
206 static void _DelItemSlice(Type& x,
const boost::python::slice& index)
208 using namespace boost::python;
213 slice::range<typename Type::iterator> range =
214 index.get_indicies(x.begin(), x.end());
215 size_t start = range.start - x.begin();
216 size_t step = range.step;
217 size_t count = 1 + (range.stop - range.start) / range.step;
221 x._Edit(start, count, value_vector_type());
225 value_vector_type empty;
226 for (
size_t j = start; count > 0; j += step - 1, --count) {
227 x._Edit(j, 1, empty);
231 catch (
const std::invalid_argument&) {
237 static int _FindIndex(
const Type& x,
const value_type& value)
240 return static_cast<int>(x.Find(value));
247 static void _Insert(Type& x,
int index,
const value_type& value)
250 index += x._GetSize();
252 if (index < 0 || index > static_cast<int>(x._GetSize())) {
255 x._Edit(index, 0, value_vector_type(1, value));
258 static bool _IsExpired(
const Type& x)
260 return x.IsExpired();
263 static value_vector_type _ApplyEditsToList(Type& x,
264 const value_vector_type& values)
266 value_vector_type newValues = values;
267 x.ApplyEditsToList(&newValues);
272 PXR_NAMESPACE_CLOSE_SCOPE
274 #endif // PXR_USD_SDF_PY_LIST_PROXY_H TF_API std::string TfStringPrintf(const char *fmt,...)
Returns a string formed by a printf()-like specification.
TF_API void TfPyThrowValueError(std::string const &msg)
Raises a python ValueError and throws a C++ exception.
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 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).
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.