24 #include "pybind11/pybind11.h" 25 #include "pybind11/stl.h" 42 template <
typename... Ts>
43 struct type_caster<
boost::variant<Ts...>> : variant_caster<boost::variant<Ts...>> {};
45 struct visit_helper<boost::variant> {
46 template <
typename... Args>
47 static auto call(Args&&...
args) -> decltype(boost::apply_visitor(
args...)) {
48 return boost::apply_visitor(
args...);
59 namespace typehandling {
66 class Publicist :
public MutableGenericMap<K> {
68 using typename GenericMap<K>::ConstValueReference;
69 using GenericMap<K>::unsafeLookup;
70 using MutableGenericMap<K>::unsafeErase;
74 py::object
get(GenericMap<K>&
self, K
const&
key) {
75 auto callable = static_cast<typename Publicist<K>::ConstValueReference (GenericMap<K>::*)(K)
const>(
76 &Publicist<K>::unsafeLookup);
77 return py::cast((
self.*callable)(
key));
81 void declareGenericMap(utils::python::WrapperCollection& wrappers,
std::string const& suffix,
83 using Class = GenericMap<K>;
84 using PyClass = py::class_<Class, std::shared_ptr<Class>>;
89 "An abstract `~collections.abc.Mapping` for use when sharing a map between C++ and Python.\n" +
91 wrappers.wrapType(PyClass(wrappers.module, className.
c_str(), docstring.
c_str()), [](
auto& mod,
99 cls.def(
"__contains__",
static_cast<bool (Class::*)(K const&) const
>(&
Class::contains),
"key"_a);
101 cls.def(
"__getitem__",
102 [](Class&
self, K
const& key) {
104 return get(
self,
key);
105 }
catch (pex::exceptions::OutOfRangeError
const& e) {
108 buffer <<
"Unknown key: " <<
key;
114 "key"_a, py::return_value_policy::copy);
116 [](Class&
self, K
const& key, py::object
const& def) {
118 return get(
self,
key);
119 }
catch (pex::exceptions::OutOfRangeError
const& e) {
125 "key"_a,
"default"_a = py::none(), py::return_value_policy::copy);
127 [](Class
const&
self) {
return py::make_iterator(
self.
keys().begin(),
self.
keys().
end()); },
128 py::keep_alive<0, 1>());
129 cls.def(
"__len__", &Class::size);
130 cls.def(
"__bool__", [](Class
const&
self) {
return !
self.empty(); });
137 template <
typename V,
class PyClass>
138 void declareMutableGenericMapTypedMethods(PyClass&
cls) {
140 cls.def(
"__setitem__",
141 [](Class&
self,
typename Class::key_type
const& key, V
const& value) {
145 auto callable = &Publicist<typename Class::key_type>::unsafeErase;
146 (
self.*callable)(key);
148 self.insert(key, value);
155 template <
typename K>
156 void declareMutableGenericMap(utils::python::WrapperCollection& wrappers,
std::string const& suffix,
158 using Class = MutableGenericMap<K>;
159 using PyClass = py::class_<Class, std::shared_ptr<Class>, GenericMap<K>>;
161 std::string className =
"MutableGenericMap" + suffix;
164 "An abstract `~collections.abc.MutableMapping` for use when sharing a map between C++ and " 167 wrappers.wrapType(PyClass(wrappers.module, className.
c_str(), docstring.
c_str()),
168 [](
auto& mod,
auto& cls) {
170 declareMutableGenericMapTypedMethods<std::shared_ptr<Storable>>(
cls);
171 declareMutableGenericMapTypedMethods<bool>(
cls);
172 declareMutableGenericMapTypedMethods<std::int32_t>(
cls);
173 declareMutableGenericMapTypedMethods<std::int64_t>(
cls);
174 declareMutableGenericMapTypedMethods<float>(
cls);
175 declareMutableGenericMapTypedMethods<double>(
cls);
176 declareMutableGenericMapTypedMethods<std::string>(
cls);
177 cls.def(
"__delitem__",
178 [](Class&
self, K
const& key) {
180 auto callable = &Publicist<K>::unsafeErase;
181 (
self.*callable)(key);
184 buffer <<
"Unknown key: " <<
key;
185 throw py::key_error(buffer.
str());
189 cls.def(
"popitem", [](Class&
self) {
191 K key =
self.keys().back();
193 auto callable = &Publicist<K>::unsafeErase;
194 (
self.*callable)(key);
197 throw py::key_error(
"Cannot pop from empty GenericMap.");
200 cls.def(
"clear", &Class::clear);
207 declareGenericMap<std::string>(wrappers,
"S",
"strings");
208 declareMutableGenericMap<std::string>(wrappers,
"S",
"strings");
bool contains(VertexIterator const begin, VertexIterator const end, UnitVector3d const &v)
static auto call(Args &&... args) -> decltype(boost::apply_visitor(args...))
A base class for image defects.
T throw_with_nested(T... args)
A helper class for subdividing pybind11 module across multiple translation units (i.e.
std::string declareGenericMapRestrictions(std::string const &className, std::string const &keyName)
void wrapGenericMap(utils::python::WrapperCollection &wrappers)