7#ifndef PXR_USD_SDF_PY_LIST_PROXY_H
8#define PXR_USD_SDF_PY_LIST_PROXY_H
13#include "pxr/usd/sdf/changeBlock.h"
16#include "pxr/base/tf/pyLock.h"
18#include "pxr/base/tf/pyResultConversions.h"
21#include "pxr/external/boost/python.hpp"
22#include "pxr/external/boost/python/slice.hpp"
24PXR_NAMESPACE_OPEN_SCOPE
27class SdfPyWrapListProxy {
30 typedef typename Type::TypePolicy TypePolicy;
31 typedef typename Type::value_type value_type;
32 typedef typename Type::value_vector_type value_vector_type;
33 typedef SdfPyWrapListProxy<Type> This;
37 TfPyWrapOnce<Type>(&This::_Wrap);
43 using namespace pxr_boost::python;
45 class_<Type>(_GetName().c_str(), no_init)
46 .def(
"__str__", &This::_GetStr)
47 .def(
"__len__", &Type::size)
48 .def(
"__getitem__", &This::_GetItemIndex)
49 .def(
"__getitem__", &This::_GetItemSlice)
50 .def(
"__setitem__", &This::_SetItemIndex)
51 .def(
"__setitem__", &This::_SetItemSlice)
52 .def(
"__delitem__", &This::_DelItemIndex)
53 .def(
"__delitem__", &This::_DelItemSlice)
54 .def(
"__delitem__", &Type::Remove)
55 .def(
"count", &Type::Count)
56 .def(
"copy", &Type::operator value_vector_type,
57 return_value_policy<TfPySequenceToList>())
58 .def(
"index", &This::_FindIndex)
59 .def(
"clear", &Type::clear)
60 .def(
"insert", &This::_Insert)
61 .def(
"append", &Type::push_back)
62 .def(
"remove", &Type::Remove)
63 .def(
"replace", &Type::Replace)
64 .def(
"ApplyList", &Type::ApplyList)
65 .def(
"ApplyEditsToList", &This::_ApplyEditsToList)
66 .add_property(
"expired", &This::_IsExpired)
67 .add_static_property(
"invalidIndex", &This::_GetInvalidIndex)
74 .def(self == other<value_vector_type>())
75 .def(self != other<value_vector_type>())
76 .def(self < other<value_vector_type>())
77 .def(self <= other<value_vector_type>())
78 .def(self > other<value_vector_type>())
79 .def(self >= other<value_vector_type>())
83 static std::string _GetName()
85 std::string name =
"ListProxy_" +
86 ArchGetDemangled<TypePolicy>();
95 static std::string _GetStr(
const Type& x)
97 return TfPyRepr(
static_cast<value_vector_type
>(x));
100 static value_type _GetItemIndex(
const Type& x,
int index)
105 static pxr_boost::python::list _GetItemSlice(
const Type& x,
106 const pxr_boost::python::slice& index)
108 using namespace pxr_boost::python;
114 slice::range<typename Type::const_iterator> range =
115 index.get_indicies(x.begin(), x.end());
116 for (; range.start != range.stop; range.start += range.step) {
117 result.append(*range.start);
119 result.append(*range.start);
121 catch (
const std::invalid_argument&) {
129 static void _SetItemIndex(Type& x,
int index,
const value_type& value)
134 static void _SetItemSlice(Type& x,
const pxr_boost::python::slice& index,
135 const value_vector_type& values)
137 using namespace pxr_boost::python;
139 if (! x._Validate()) {
144 size_t start, step, count;
146 slice::range<typename Type::iterator> range =
147 index.get_indicies(x.begin(), x.end());
148 start = range.start - x.begin();
150 count = 1 + (range.stop - range.start) / range.step;
152 catch (
const std::invalid_argument&) {
154 extract<int> e(index.start());
162 x._Edit(start, count, values);
166 if (count != values.size()) {
169 "to extended slice of size %zd",
170 values.size(), count).c_str());
172 else if (step == 1) {
173 x._Edit(start, count, values);
177 for (
size_t i = 0, j = start; i != count; j += step, ++i) {
178 x._Edit(j, 1, value_vector_type(1, values[i]));
184 static void _DelItemIndex(Type& x,
int i)
187 1, value_vector_type());
190 static void _DelItemSlice(Type& x,
const pxr_boost::python::slice& index)
192 using namespace pxr_boost::python;
197 slice::range<typename Type::iterator> range =
198 index.get_indicies(x.begin(), x.end());
199 size_t start = range.start - x.begin();
200 size_t step = range.step;
201 size_t count = 1 + (range.stop - range.start) / range.step;
205 x._Edit(start, count, value_vector_type());
209 value_vector_type empty;
210 for (
size_t j = start; count > 0; j += step - 1, --count) {
211 x._Edit(j, 1, empty);
215 catch (
const std::invalid_argument&) {
221 static int _GetInvalidIndex()
229 static int _FindIndex(
const Type& x,
const value_type& value)
232 const size_t index = x.Find(value);
233 return index == Type::invalidIndex
235 : static_cast<int>(index);
238 return _GetInvalidIndex();
242 static void _Insert(Type& x,
int index,
const value_type& value)
245 index += x._GetSize();
247 if (index < 0 || index >
static_cast<int>(x._GetSize())) {
250 x._Edit(index, 0, value_vector_type(1, value));
253 static bool _IsExpired(
const Type& x)
255 return x.IsExpired();
258 static value_vector_type _ApplyEditsToList(Type& x,
259 const value_vector_type& values)
261 value_vector_type newValues = values;
262 x.ApplyEditsToList(&newValues);
267PXR_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 bool TfPyIsNone(pxr_boost::python::object const &obj)
Return true iff obj is None.
TF_API void TfPyThrowValueError(const char *msg)
Raises a Python ValueError with the given error msg and throws a pxr_boost::python::error_already_set...
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.
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.
TF_API std::string TfStringPrintf(const char *fmt,...)
Returns a string formed by a printf()-like specification.
Definitions of basic string utilities in tf.