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> 37 PXR_NAMESPACE_OPEN_SCOPE
40 class 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 #if PY_MAJOR_VERSION < 3 130 .def(
"itervalues", &This::_GetValueIterator)
131 .def(
"iterkeys", &This::_GetKeyIterator)
132 .def(
"iteritems", &This::_GetItemIterator)
133 .def(
"values", &This::_GetValues)
134 .def(
"keys", &This::_GetKeys)
135 .def(
"items", &This::_GetItems)
136 .def(
"has_key", &This::_HasKey)
138 .def(
"values", &This::_GetValueIterator)
139 .def(
"keys", &This::_GetKeyIterator)
140 .def(
"items", &This::_GetItemIterator)
142 .def(
"clear", &Type::clear)
143 .def(
"get", &This::_PyGet)
144 .def(
"get", &This::_PyGetDefault)
145 .def(
"pop", &This::_Pop)
146 .def(
"popitem", &This::_PopItem)
147 .def(
"setdefault", &This::_SetDefault)
148 .def(
"update", &This::_UpdateDict)
149 .def(
"update", &This::_UpdateList)
150 .def(
"copy", &This::_Copy)
151 .add_property(
"expired", &Type::IsExpired)
152 .def(TfPyBoolBuiltinFuncName, &This::_NonZero)
157 class_<_Iterator<_ExtractItem> >
158 ((name +
"_Iterator").c_str(), no_init)
159 .def(
"__iter__", &This::template _Iterator<_ExtractItem>::GetCopy)
160 .def(TfPyIteratorNextMethodName, &This::template _Iterator<_ExtractItem>::GetNext)
163 class_<_Iterator<_ExtractKey> >
164 ((name +
"_KeyIterator").c_str(), no_init)
165 .def(
"__iter__", &This::template _Iterator<_ExtractKey>::GetCopy)
166 .def(TfPyIteratorNextMethodName, &This::template _Iterator<_ExtractKey>::GetNext)
169 class_<_Iterator<_ExtractValue> >
170 ((name +
"_ValueIterator").c_str(), no_init)
171 .def(
"__iter__", &This::template _Iterator<_ExtractValue>::GetCopy)
172 .def(TfPyIteratorNextMethodName, &This::template _Iterator<_ExtractValue>::GetNext)
176 static std::string _GetName()
178 std::string name =
"MapEditProxy_" +
179 ArchGetDemangled<typename Type::Type>();
188 static std::string _GetRepr(
const Type& x)
200 static std::string _GetStr(
const Type& x)
202 std::string result(
"{");
203 if (x && ! x.empty()) {
204 const_iterator i = x.begin(), n = x.end();
214 static mapped_type _GetItem(
const Type& x,
const key_type& key)
216 const_iterator i = x.find(key);
219 return mapped_type();
226 static void _SetItem(Type& x,
const key_type& key,
const mapped_type& value)
228 std::pair<typename Type::iterator, bool> i =
229 x.insert(value_type(key, value));
230 if (! i.second && i.first !=
typename Type::iterator()) {
231 i.first->second = value;
235 static void _DelItem(Type& x,
const key_type& key)
240 static bool _HasKey(
const Type& x,
const key_type& key)
242 return x.count(key) != 0;
245 static _Iterator<_ExtractItem>
246 _GetItemIterator(
const boost::python::object& x)
248 return _Iterator<_ExtractItem>(x);
251 static _Iterator<_ExtractKey>
252 _GetKeyIterator(
const boost::python::object& x)
254 return _Iterator<_ExtractKey>(x);
257 static _Iterator<_ExtractValue>
258 _GetValueIterator(
const boost::python::object& x)
260 return _Iterator<_ExtractValue>(x);
263 static boost::python::object _PyGet(
const Type& x,
const key_type& key)
265 const_iterator i = x.find(key);
266 return i == x.end() ? boost::python::object() :
267 boost::python::object(i->second);
270 static mapped_type _PyGetDefault(
const Type& x,
const key_type& key,
271 const mapped_type& def)
273 const_iterator i = x.find(key);
274 return i == x.end() ? def : i->second;
278 static boost::python::list _Get(
const Type& x)
280 boost::python::list result;
281 for (const_iterator i = x.begin(), n = x.end(); i != n; ++i) {
282 result.append(E::Get(i));
287 #if PY_MAJOR_VERSION < 3 288 static boost::python::list _GetItems(
const Type& x)
290 return _Get<_ExtractItem>(x);
293 static boost::python::list _GetKeys(
const Type& x)
295 return _Get<_ExtractKey>(x);
298 static boost::python::list _GetValues(
const Type& x)
300 return _Get<_ExtractValue>(x);
304 static mapped_type _Pop(Type& x,
const key_type& key)
306 iterator i = x.find(key);
309 return mapped_type();
312 mapped_type result = i->second;
318 static boost::python::tuple _PopItem(Type& x)
322 return boost::python::tuple();
325 iterator i = x.begin();
326 value_type result = *i;
328 return boost::python::make_tuple(result.first, result.second);
332 static mapped_type _SetDefault(Type& x,
const key_type& key,
333 const mapped_type& def)
335 const_iterator i = x.find(key);
344 static void _Update(Type& x,
const std::vector<pair_type>& values)
348 x[i->first] = i->second;
352 static void _UpdateDict(Type& x,
const boost::python::dict& d)
354 _UpdateList(x, d.items());
357 static void _UpdateList(Type& x,
const boost::python::list& pairs)
359 using namespace boost::python;
361 std::vector<pair_type> values;
362 for (
int i = 0, n = len(pairs); i != n; ++i) {
363 values.push_back(pair_type(
364 extract<key_type>(pairs[i][0])(),
365 extract<mapped_type>(pairs[i][1])()));
370 static void _Copy(Type& x,
const typename Type::Type& other)
375 static bool _NonZero(
const Type& x)
377 return static_cast<bool>(x);
381 PXR_NAMESPACE_CLOSE_SCOPE
383 #endif // PXR_USD_SDF_PY_MAP_EDIT_PROXY_H TF_API std::string TfStringPrintf(const char *fmt,...)
Returns a string formed by a printf()-like specification.
A simple iterator adapter for STL containers.
Definitions of basic string utilities in tf.
TF_API void TfPyThrowKeyError(std::string const &msg)
Raises a python KeyError and throws a C++ exception.
Miscellaneous Utilities for dealing with script.
Demangle C++ typenames generated by the typeid() facility.
std::string TfPyRepr(T const &t)
Return repr(t).
#define TF_FOR_ALL(iter, c)
Macro for iterating over a container.
TF_API void TfPyThrowStopIteration(std::string const &msg)
Raises a python StopIteration exception and throws a C++ exception.
#define TF_PY_REPR_PREFIX
A macro which expands to the proper repr prefix for a library.
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.