From 7a26fbbf7c8964f1327cf598a58c5711a414d764 Mon Sep 17 00:00:00 2001 From: ManifoldFR <wilson.jallet@polytechnique.org> Date: Tue, 4 Oct 2022 16:39:03 +0200 Subject: [PATCH] move std-map.hpp (std-map) Fix include guard * apply pre-commit formatting * rename namespace --- include/eigenpy/std-map.hpp | 63 ++++++++++++++++++++++++++++++ python/utils/std-map.hpp | 77 ------------------------------------- 2 files changed, 63 insertions(+), 77 deletions(-) create mode 100644 include/eigenpy/std-map.hpp delete mode 100644 python/utils/std-map.hpp diff --git a/include/eigenpy/std-map.hpp b/include/eigenpy/std-map.hpp new file mode 100644 index 00000000..4af4f3b0 --- /dev/null +++ b/include/eigenpy/std-map.hpp @@ -0,0 +1,63 @@ +// +// Copyright (c) 2020 INRIA +// + +#ifndef __eigenpy_utils_map_hpp__ +#define __eigenpy_utils_map_hpp__ + +#include <boost/python/suite/indexing/map_indexing_suite.hpp> + +namespace eigenpy { +namespace details { +template <typename Container> +struct overload_base_get_item_for_std_map + : public boost::python::def_visitor< + overload_base_get_item_for_std_map<Container> > { + typedef typename Container::value_type value_type; + typedef typename Container::value_type::second_type data_type; + typedef typename Container::key_type key_type; + typedef typename Container::key_type index_type; + + template <class Class> + void visit(Class& cl) const { + cl.def("__getitem__", &base_get_item); + } + + private: + static boost::python::object base_get_item( + boost::python::back_reference<Container&> container, PyObject* i_) { + namespace bp = ::boost::python; + + index_type idx = convert_index(container.get(), i_); + typename Container::iterator i = container.get().find(idx); + if (i == container.get().end()) { + PyErr_SetString(PyExc_KeyError, "Invalid key"); + bp::throw_error_already_set(); + } + + typename bp::to_python_indirect<data_type&, + bp::detail::make_reference_holder> + convert; + return bp::object(bp::handle<>(convert(i->second))); + } + + static index_type convert_index(Container& /*container*/, PyObject* i_) { + namespace bp = ::boost::python; + bp::extract<key_type const&> i(i_); + if (i.check()) { + return i(); + } else { + bp::extract<key_type> i(i_); + if (i.check()) return i(); + } + + PyErr_SetString(PyExc_TypeError, "Invalid index type"); + bp::throw_error_already_set(); + return index_type(); + } +}; + +} // namespace details +} // namespace eigenpy + +#endif // ifndef __eigenpy_utils_map_hpp__ diff --git a/python/utils/std-map.hpp b/python/utils/std-map.hpp deleted file mode 100644 index c5a5340f..00000000 --- a/python/utils/std-map.hpp +++ /dev/null @@ -1,77 +0,0 @@ -// -// Copyright (c) 2020 INRIA -// - -#ifndef __pinocchio_python_utils_map_hpp__ -#define __pinocchio_python_utils_map_hpp__ - -#include <boost/python/suite/indexing/map_indexing_suite.hpp> - -namespace pinocchio -{ - namespace python - { - namespace details - { - template<typename Container> - struct overload_base_get_item_for_std_map - : public boost::python::def_visitor< overload_base_get_item_for_std_map<Container> > - { - typedef typename Container::value_type value_type; - typedef typename Container::value_type::second_type data_type; - typedef typename Container::key_type key_type; - typedef typename Container::key_type index_type; - - template <class Class> - void visit(Class& cl) const - { - cl - .def("__getitem__", &base_get_item); - } - - private: - - static boost::python::object - base_get_item(boost::python::back_reference<Container&> container, PyObject* i_) - { - namespace bp = ::boost::python; - - index_type idx = convert_index(container.get(), i_); - typename Container::iterator i = container.get().find(idx); - if (i == container.get().end()) - { - PyErr_SetString(PyExc_KeyError, "Invalid key"); - bp::throw_error_already_set(); - } - - typename bp::to_python_indirect<data_type&,bp::detail::make_reference_holder> convert; - return bp::object(bp::handle<>(convert(i->second))); - } - - static index_type - convert_index(Container& /*container*/, PyObject* i_) - { - namespace bp = ::boost::python; - bp::extract<key_type const&> i(i_); - if (i.check()) - { - return i(); - } - else - { - bp::extract<key_type> i(i_); - if (i.check()) - return i(); - } - - PyErr_SetString(PyExc_TypeError, "Invalid index type"); - bp::throw_error_already_set(); - return index_type(); - } - }; - - } - } -} - -#endif // ifndef __pinocchio_python_utils_map_hpp__ -- GitLab