7#ifndef PXR_USD_SDF_PY_MAP_EDIT_PROXY_H
8#define PXR_USD_SDF_PY_MAP_EDIT_PROXY_H
13#include "pxr/usd/sdf/changeBlock.h"
18#include "pxr/external/boost/python.hpp"
20PXR_NAMESPACE_OPEN_SCOPE
23class SdfPyWrapMapEditProxy {
26 typedef typename Type::key_type key_type;
27 typedef typename Type::mapped_type mapped_type;
28 typedef typename Type::value_type value_type;
29 typedef typename Type::iterator iterator;
30 typedef typename Type::const_iterator const_iterator;
31 typedef SdfPyWrapMapEditProxy<Type> This;
33 SdfPyWrapMapEditProxy()
35 TfPyWrapOnce<Type>(&This::_Wrap);
39 typedef std::pair<key_type, mapped_type> pair_type;
42 static pxr_boost::python::object Get(
const const_iterator& i)
44 return pxr_boost::python::make_tuple(i->first, i->second);
49 static pxr_boost::python::object Get(
const const_iterator& i)
51 return pxr_boost::python::object(i->first);
55 struct _ExtractValue {
56 static pxr_boost::python::object Get(
const const_iterator& i)
58 return pxr_boost::python::object(i->second);
65 _Iterator(
const pxr_boost::python::object&
object) :
67 _owner(pxr_boost::python::extract<const Type&>(object)),
74 _Iterator<E> GetCopy()
const
79 pxr_boost::python::object GetNext()
84 pxr_boost::python::object result = E::Get(_cur);
90 pxr_boost::python::object _object;
98 using namespace pxr_boost::python;
100 std::string name = _GetName();
103 class_<Type>(name.c_str())
104 .def(
"__repr__", &This::_GetRepr)
105 .def(
"__str__", &This::_GetStr)
106 .def(
"__len__", &Type::size)
107 .def(
"__getitem__", &This::_GetItem)
108 .def(
"__setitem__", &This::_SetItem)
109 .def(
"__delitem__", &This::_DelItem)
110 .def(
"__contains__", &This::_HasKey)
111 .def(
"__iter__", &This::_GetKeyIterator)
112 .def(
"values", &This::_GetValueIterator)
113 .def(
"keys", &This::_GetKeyIterator)
114 .def(
"items", &This::_GetItemIterator)
115 .def(
"clear", &Type::clear)
116 .def(
"get", &This::_PyGet)
117 .def(
"get", &This::_PyGetDefault)
118 .def(
"pop", &This::_Pop)
119 .def(
"popitem", &This::_PopItem)
120 .def(
"setdefault", &This::_SetDefault)
121 .def(
"update", &This::_UpdateDict)
122 .def(
"update", &This::_UpdateList)
123 .def(
"copy", &This::_Copy)
124 .add_property(
"expired", &Type::IsExpired)
125 .def(
"__bool__", &This::_IsValid)
130 class_<_Iterator<_ExtractItem> >
131 ((name +
"_Iterator").c_str(), no_init)
132 .def(
"__iter__", &This::template _Iterator<_ExtractItem>::GetCopy)
133 .def(
"__next__", &This::template _Iterator<_ExtractItem>::GetNext)
136 class_<_Iterator<_ExtractKey> >
137 ((name +
"_KeyIterator").c_str(), no_init)
138 .def(
"__iter__", &This::template _Iterator<_ExtractKey>::GetCopy)
139 .def(
"__next__", &This::template _Iterator<_ExtractKey>::GetNext)
142 class_<_Iterator<_ExtractValue> >
143 ((name +
"_ValueIterator").c_str(), no_init)
144 .def(
"__iter__", &This::template _Iterator<_ExtractValue>::GetCopy)
145 .def(
"__next__", &This::template _Iterator<_ExtractValue>::GetNext)
149 static std::string _GetName()
151 std::string name =
"MapEditProxy_" +
152 ArchGetDemangled<typename Type::Type>();
161 static std::string _GetRepr(
const Type& x)
173 static std::string _GetStr(
const Type& x)
175 std::string result(
"{");
176 if (x && ! x.empty()) {
177 const_iterator i = x.begin(), n = x.end();
187 static mapped_type _GetItem(
const Type& x,
const key_type& key)
189 const_iterator i = x.find(key);
192 return mapped_type();
199 static void _SetItem(Type& x,
const key_type& key,
const mapped_type& value)
201 std::pair<typename Type::iterator, bool> i =
202 x.insert(value_type(key, value));
203 if (! i.second && i.first !=
typename Type::iterator()) {
204 i.first->second = value;
208 static void _DelItem(Type& x,
const key_type& key)
213 static bool _HasKey(
const Type& x,
const key_type& key)
215 return x.count(key) != 0;
218 static _Iterator<_ExtractItem>
219 _GetItemIterator(
const pxr_boost::python::object& x)
221 return _Iterator<_ExtractItem>(x);
224 static _Iterator<_ExtractKey>
225 _GetKeyIterator(
const pxr_boost::python::object& x)
227 return _Iterator<_ExtractKey>(x);
230 static _Iterator<_ExtractValue>
231 _GetValueIterator(
const pxr_boost::python::object& x)
233 return _Iterator<_ExtractValue>(x);
236 static pxr_boost::python::object _PyGet(
const Type& x,
const key_type& key)
238 const_iterator i = x.find(key);
239 return i == x.end() ? pxr_boost::python::object() :
240 pxr_boost::python::object(i->second);
243 static mapped_type _PyGetDefault(
const Type& x,
const key_type& key,
244 const mapped_type& def)
246 const_iterator i = x.find(key);
247 return i == x.end() ? def : i->second;
251 static pxr_boost::python::list _Get(
const Type& x)
253 pxr_boost::python::list result;
254 for (const_iterator i = x.begin(), n = x.end(); i != n; ++i) {
255 result.append(E::Get(i));
260 static mapped_type _Pop(Type& x,
const key_type& key)
262 iterator i = x.find(key);
265 return mapped_type();
268 mapped_type result = i->second;
274 static pxr_boost::python::tuple _PopItem(Type& x)
278 return pxr_boost::python::tuple();
281 iterator i = x.begin();
282 value_type result = *i;
284 return pxr_boost::python::make_tuple(result.first, result.second);
288 static mapped_type _SetDefault(Type& x,
const key_type& key,
289 const mapped_type& def)
291 const_iterator i = x.find(key);
300 static void _Update(Type& x,
const std::vector<pair_type>& values)
304 x[i->first] = i->second;
308 static void _UpdateDict(Type& x,
const pxr_boost::python::dict& d)
310 _UpdateList(x, d.items());
313 static void _UpdateList(Type& x,
const pxr_boost::python::list& pairs)
315 using namespace pxr_boost::python;
317 std::vector<pair_type> values;
318 for (
int i = 0, n = len(pairs); i != n; ++i) {
319 values.push_back(pair_type(
320 extract<key_type>(pairs[i][0])(),
321 extract<mapped_type>(pairs[i][1])()));
326 static void _Copy(Type& x,
const typename Type::Type& other)
331 static bool _IsValid(
const Type& x)
333 return static_cast<bool>(x);
337PXR_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 pxr_boost::python::error_already_...
TF_API void TfPyThrowKeyError(const char *msg)
Raises a Python KeyError with the given error msg and throws a pxr_boost::python::error_already_set e...
#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.