|
| 1 | +/* Copyright 2018 Axel Huebl |
| 2 | + * |
| 3 | + * This file is part of openPMD-api. |
| 4 | + * |
| 5 | + * openPMD-api is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of of either the GNU General Public License or |
| 7 | + * the GNU Lesser General Public License as published by |
| 8 | + * the Free Software Foundation, either version 3 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * openPMD-api is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU General Public License and the GNU Lesser General Public License |
| 15 | + * for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License |
| 18 | + * and the GNU Lesser General Public License along with openPMD-api. |
| 19 | + * If not, see <http://www.gnu.org/licenses/>. |
| 20 | + */ |
| 21 | +#include <pybind11/pybind11.h> |
| 22 | +#include <pybind11/stl_bind.h> |
| 23 | +#include <pybind11/stl.h> |
| 24 | + |
| 25 | +#include "openPMD/backend/Attributable.hpp" |
| 26 | +#include "openPMD/backend/Attribute.hpp" |
| 27 | +#include "openPMD/auxiliary/Variant.hpp" |
| 28 | + |
| 29 | +#include <string> |
| 30 | +#include <vector> |
| 31 | +#include <array> |
| 32 | + |
| 33 | + |
| 34 | +// std::variant |
| 35 | +// https://pybind11.readthedocs.io/en/stable/advanced/cast/stl.html |
| 36 | +namespace pybind11 { |
| 37 | +namespace detail { |
| 38 | + template< typename... Ts > |
| 39 | + struct type_caster< variantSrc::variant< Ts... > > : |
| 40 | + variant_caster< variantSrc::variant< Ts... > > |
| 41 | + {}; |
| 42 | + |
| 43 | +} // namespace detail |
| 44 | +} // namespace pybind11 |
| 45 | + |
| 46 | +namespace py = pybind11; |
| 47 | +using namespace openPMD; |
| 48 | + |
| 49 | +using PyAttributeKeys = std::vector< std::string >; |
| 50 | +//PYBIND11_MAKE_OPAQUE(PyAttributeKeys) |
| 51 | + |
| 52 | +void init_Attributable(py::module &m) { |
| 53 | + py::class_<Attributable>(m, "Attributable") |
| 54 | + .def(py::init<>()) |
| 55 | + .def(py::init<Attributable const &>()) |
| 56 | + |
| 57 | + .def("__repr__", |
| 58 | + [](Attributable const & attr) { |
| 59 | + return "<openPMD.Attributable with'" + std::to_string(attr.numAttributes()) + "' attributes>"; |
| 60 | + } |
| 61 | + ) |
| 62 | + |
| 63 | + .def_property_readonly( |
| 64 | + "attributes", |
| 65 | + []( Attributable & attr ) |
| 66 | + { |
| 67 | + return attr.attributes(); |
| 68 | + }, |
| 69 | + // ref + keepalive |
| 70 | + py::return_value_policy::reference_internal |
| 71 | + ) |
| 72 | + |
| 73 | + // C++ pass-through API |
| 74 | + .def("set_attribute", &Attributable::setAttribute< char >) |
| 75 | + .def("set_attribute", &Attributable::setAttribute< unsigned char >) |
| 76 | + .def("set_attribute", &Attributable::setAttribute< int16_t >) |
| 77 | + .def("set_attribute", &Attributable::setAttribute< int32_t >) |
| 78 | + .def("set_attribute", &Attributable::setAttribute< int64_t >) |
| 79 | + .def("set_attribute", &Attributable::setAttribute< uint16_t >) |
| 80 | + .def("set_attribute", &Attributable::setAttribute< uint32_t >) |
| 81 | + .def("set_attribute", &Attributable::setAttribute< uint64_t >) |
| 82 | + .def("set_attribute", &Attributable::setAttribute< float >) |
| 83 | + .def("set_attribute", &Attributable::setAttribute< double >) |
| 84 | + .def("set_attribute", &Attributable::setAttribute< long double >) |
| 85 | + .def("set_attribute", &Attributable::setAttribute< std::string >) |
| 86 | + .def("set_attribute", &Attributable::setAttribute< std::vector< char > >) |
| 87 | + .def("set_attribute", &Attributable::setAttribute< std::vector< unsigned char > >) |
| 88 | + .def("set_attribute", &Attributable::setAttribute< std::vector< int16_t > >) |
| 89 | + .def("set_attribute", &Attributable::setAttribute< std::vector< int32_t > >) |
| 90 | + .def("set_attribute", &Attributable::setAttribute< std::vector< int64_t > >) |
| 91 | + .def("set_attribute", &Attributable::setAttribute< std::vector< uint16_t > >) |
| 92 | + .def("set_attribute", &Attributable::setAttribute< std::vector< uint32_t > >) |
| 93 | + .def("set_attribute", &Attributable::setAttribute< std::vector< uint64_t > >) |
| 94 | + .def("set_attribute", &Attributable::setAttribute< std::vector< float > >) |
| 95 | + .def("set_attribute", &Attributable::setAttribute< std::vector< double > >) |
| 96 | + .def("set_attribute", &Attributable::setAttribute< std::vector< long double > >) |
| 97 | + .def("set_attribute", &Attributable::setAttribute< std::vector< std::string > >) |
| 98 | + .def("set_attribute", &Attributable::setAttribute< std::array< double, 7 > >) |
| 99 | + .def("set_attribute", &Attributable::setAttribute< bool >) |
| 100 | + |
| 101 | + .def("get_attribute", []( Attributable & attr, std::string const& key ) { |
| 102 | + auto v = attr.getAttribute(key); |
| 103 | + return v.getResource(); |
| 104 | + }) |
| 105 | + .def("delete_attribute", &Attributable::deleteAttribute) |
| 106 | + .def("contains_attribute", &Attributable::containsAttribute) |
| 107 | + |
| 108 | + .def("__len__", &Attributable::numAttributes) |
| 109 | + |
| 110 | + // @todo _ipython_key_completions_ if we find a way to add a [] interface |
| 111 | + |
| 112 | + .def_property_readonly("comment", &Attributable::comment) |
| 113 | + .def("set_comment", &Attributable::setComment) |
| 114 | + ; |
| 115 | + |
| 116 | + py::bind_vector< PyAttributeKeys >( |
| 117 | + m, |
| 118 | + "Attribute_Keys" |
| 119 | + ); |
| 120 | +} |
0 commit comments