28#include "openPMD/backend/Container.hpp"
29#include "openPMD/backend/MeshRecordComponent.hpp"
30#include "openPMD/backend/PatchRecord.hpp"
31#include "openPMD/backend/PatchRecordComponent.hpp"
33#include "openPMD/binding/python/Common.hpp"
38#include <pybind11/attr.h>
50template <
typename Map,
typename... Args>
51py::class_<Map, std::unique_ptr<Map>, Args...> declare_container(
53 std::string
const &name,
54 std::optional<std::string> docstring = std::nullopt)
56 using holder_type = std::unique_ptr<Map>;
57 using KeyType =
typename Map::key_type;
58 using MappedType =
typename Map::mapped_type;
59 using Class_ = py::class_<Map, holder_type, Args...>;
64 auto tinfo = py::detail::get_type_info(
typeid(MappedType));
65 bool local = !tinfo || tinfo->module_local;
68 tinfo = py::detail::get_type_info(
typeid(KeyType));
69 local = !tinfo || tinfo->module_local;
72 Class_ cl = docstring.has_value()
74 scope, name.c_str(), docstring->c_str(),
75 py::module_local(local))
76 : Class_(scope, name.c_str(), py::module_local(local));
81 py::detail::map_if_insertion_operator<Map, Class_>(cl, name);
85 [](
const Map &m) ->
bool {
return !m.empty(); },
86 "Check whether the container is nonempty");
90 [](Map &m) {
return py::make_key_iterator(m.begin(), m.end()); },
92 py::keep_alive<0, 1>());
97 [](
const Map &m) {
return m.size(); },
98 "Number of elements in the container to iterate.");
100 cl.def(
"__repr__", [name](Map
const &m) {
101 std::stringstream stream;
102 stream <<
"<openPMD." << name <<
" with ";
103 if (
size_t num_entries = m.size(); num_entries == 1)
105 stream <<
"1 entry and ";
109 stream << num_entries <<
" entries and ";
112 stream << m.numAttributes() <<
" attribute(s)>";
119template <
typename Map,
typename Class_>
120Class_ finalize_container(Class_ cl,
bool skip_getitem =
false)
122 using KeyType =
typename Map::key_type;
123 using MappedType =
typename Map::mapped_type;
127 [](Map &m) {
return py::make_iterator(m.begin(), m.end()); },
129 py::keep_alive<0, 1>());
136 [](Map &m, KeyType
const &k) -> MappedType {
return m[k]; },
140 py::return_value_policy::move,
141 py::keep_alive<0, 1>());
145 py::detail::map_assignment<Map, Class_>(cl);
147 cl.def(
"__delitem__", [](Map &m, KeyType
const &k) {
150 throw py::key_error();
154 cl.def(
"__len__", &Map::size);
156 cl.def(
"_ipython_key_completions_", [](Map &m) {
158 for (
const auto &myPair : m)
159 l.append(myPair.first);
Public definitions of openPMD-api.
Definition Date.cpp:29