24#ifndef PXR_USD_SDF_PY_MAP_EDIT_PROXY_H
25#define PXR_USD_SDF_PY_MAP_EDIT_PROXY_H
30#include "pxr/usd/sdf/changeBlock.h"
35#include <boost/python.hpp>
37PXR_NAMESPACE_OPEN_SCOPE
40class SdfPyWrapMapEditProxy {
43 typedef typename Type::key_type key_type;
44 typedef typename Type::mapped_type mapped_type;
45 typedef typename Type::value_type value_type;
46 typedef typename Type::iterator iterator;
47 typedef typename Type::const_iterator const_iterator;
48 typedef SdfPyWrapMapEditProxy<Type> This;
50 SdfPyWrapMapEditProxy()
52 TfPyWrapOnce<Type>(&This::_Wrap);
56 typedef std::pair<key_type, mapped_type> pair_type;
59 static boost::python::object Get(
const const_iterator& i)
61 return boost::python::make_tuple(i->first, i->second);
66 static boost::python::object Get(
const const_iterator& i)
68 return boost::python::object(i->first);
72 struct _ExtractValue {
73 static boost::python::object Get(
const const_iterator& i)
75 return boost::python::object(i->second);
82 _Iterator(
const boost::python::object&
object) :
84 _owner(boost::python::extract<const Type&>(object)),
91 _Iterator<E> GetCopy()
const
96 boost::python::object GetNext()
101 boost::python::object result = E::Get(_cur);
107 boost::python::object _object;
115 using namespace boost::python;
117 std::string name = _GetName();
120 class_<Type>(name.c_str())
121 .def(
"__repr__", &This::_GetRepr)
122 .def(
"__str__", &This::_GetStr)
123 .def(
"__len__", &Type::size)
124 .def(
"__getitem__", &This::_GetItem)
125 .def(
"__setitem__", &This::_SetItem)
126 .def(
"__delitem__", &This::_DelItem)
127 .def(
"__contains__", &This::_HasKey)
128 .def(
"__iter__", &This::_GetKeyIterator)
129 .def(
"values", &This::_GetValueIterator)
130 .def(
"keys", &This::_GetKeyIterator)
131 .def(
"items", &This::_GetItemIterator)
132 .def(
"clear", &Type::clear)
133 .def(
"get", &This::_PyGet)
134 .def(
"get", &This::_PyGetDefault)
135 .def(
"pop", &This::_Pop)
136 .def(
"popitem", &This::_PopItem)
137 .def(
"setdefault", &This::_SetDefault)
138 .def(
"update", &This::_UpdateDict)
139 .def(
"update", &This::_UpdateList)
140 .def(
"copy", &This::_Copy)
141 .add_property(
"expired", &Type::IsExpired)
142 .def(
"__bool__", &This::_IsValid)
147 class_<_Iterator<_ExtractItem> >
148 ((name +
"_Iterator").c_str(), no_init)
149 .def(
"__iter__", &This::template _Iterator<_ExtractItem>::GetCopy)
150 .def(
"__next__", &This::template _Iterator<_ExtractItem>::GetNext)
153 class_<_Iterator<_ExtractKey> >
154 ((name +
"_KeyIterator").c_str(), no_init)
155 .def(
"__iter__", &This::template _Iterator<_ExtractKey>::GetCopy)
156 .def(
"__next__", &This::template _Iterator<_ExtractKey>::GetNext)
159 class_<_Iterator<_ExtractValue> >
160 ((name +
"_ValueIterator").c_str(), no_init)
161 .def(
"__iter__", &This::template _Iterator<_ExtractValue>::GetCopy)
162 .def(
"__next__", &This::template _Iterator<_ExtractValue>::GetNext)
166 static std::string _GetName()
168 std::string name =
"MapEditProxy_" +
169 ArchGetDemangled<typename Type::Type>();
178 static std::string _GetRepr(
const Type& x)
190 static std::string _GetStr(
const Type& x)
192 std::string result(
"{");
193 if (x && ! x.empty()) {
194 const_iterator i = x.begin(), n = x.end();
204 static mapped_type _GetItem(
const Type& x,
const key_type& key)
206 const_iterator i = x.find(key);
209 return mapped_type();
216 static void _SetItem(Type& x,
const key_type& key,
const mapped_type& value)
218 std::pair<typename Type::iterator, bool> i =
219 x.insert(value_type(key, value));
220 if (! i.second && i.first !=
typename Type::iterator()) {
221 i.first->second = value;
225 static void _DelItem(Type& x,
const key_type& key)
230 static bool _HasKey(
const Type& x,
const key_type& key)
232 return x.count(key) != 0;
235 static _Iterator<_ExtractItem>
236 _GetItemIterator(
const boost::python::object& x)
238 return _Iterator<_ExtractItem>(x);
241 static _Iterator<_ExtractKey>
242 _GetKeyIterator(
const boost::python::object& x)
244 return _Iterator<_ExtractKey>(x);
247 static _Iterator<_ExtractValue>
248 _GetValueIterator(
const boost::python::object& x)
250 return _Iterator<_ExtractValue>(x);
253 static boost::python::object _PyGet(
const Type& x,
const key_type& key)
255 const_iterator i = x.find(key);
256 return i == x.end() ? boost::python::object() :
257 boost::python::object(i->second);
260 static mapped_type _PyGetDefault(
const Type& x,
const key_type& key,
261 const mapped_type& def)
263 const_iterator i = x.find(key);
264 return i == x.end() ? def : i->second;
268 static boost::python::list _Get(
const Type& x)
270 boost::python::list result;
271 for (const_iterator i = x.begin(), n = x.end(); i != n; ++i) {
272 result.append(E::Get(i));
277 static mapped_type _Pop(Type& x,
const key_type& key)
279 iterator i = x.find(key);
282 return mapped_type();
285 mapped_type result = i->second;
291 static boost::python::tuple _PopItem(Type& x)
295 return boost::python::tuple();
298 iterator i = x.begin();
299 value_type result = *i;
301 return boost::python::make_tuple(result.first, result.second);
305 static mapped_type _SetDefault(Type& x,
const key_type& key,
306 const mapped_type& def)
308 const_iterator i = x.find(key);
317 static void _Update(Type& x,
const std::vector<pair_type>& values)
321 x[i->first] = i->second;
325 static void _UpdateDict(Type& x,
const boost::python::dict& d)
327 _UpdateList(x, d.items());
330 static void _UpdateList(Type& x,
const boost::python::list& pairs)
332 using namespace boost::python;
334 std::vector<pair_type> values;
335 for (
int i = 0, n = len(pairs); i != n; ++i) {
336 values.push_back(pair_type(
337 extract<key_type>(pairs[i][0])(),
338 extract<mapped_type>(pairs[i][1])()));
343 static void _Copy(Type& x,
const typename Type::Type& other)
348 static bool _IsValid(
const Type& x)
350 return static_cast<bool>(x);
354PXR_NAMESPACE_CLOSE_SCOPE
A simple iterator adapter for STL containers.
Miscellaneous Utilities for dealing with script.
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 void TfPyThrowKeyError(const char *msg)
Raises a Python KeyError with the given error msg and throws a boost::python::error_already_set excep...
#define TF_PY_REPR_PREFIX
A macro which expands to the proper repr prefix for a library.
std::string TfPyRepr(T const &t)
Return repr(t).
Demangle C++ typenames generated by the typeid() facility.
#define TF_FOR_ALL(iter, c)
Macro for iterating over a container.
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.