-
Notifications
You must be signed in to change notification settings - Fork 47
Add helper for type_info #502
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+195
−0
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0e5bf54
utils: add type_info helpers
jcarpent 048784c
test: add tests for type_info
jcarpent b1ca46f
test: fix
jcarpent 2a9b3b5
typeinfo: fix arguments for runtime outputs
jcarpent 42b309f
typeinfo: add pretty_name to std::type_index
jcarpent 1ba3500
changelog: sync
jcarpent e576541
test/type_info: fix issue on Windows
jcarpent 6552b9a
core: avoid duplicate registration
jcarpent File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| /// | ||
| /// Copyright (c) 2024 INRIA | ||
| /// | ||
|
|
||
| #ifndef __eigenpy_type_info_hpp__ | ||
| #define __eigenpy_type_info_hpp__ | ||
|
|
||
| #include "eigenpy/fwd.hpp" | ||
|
|
||
| #include <boost/type_index.hpp> | ||
| #include <typeinfo> | ||
| #include <typeindex> | ||
|
|
||
| namespace eigenpy { | ||
|
|
||
| template <typename T> | ||
| boost::typeindex::type_index type_info(const T& value) { | ||
| return boost::typeindex::type_id_runtime(value); | ||
| } | ||
|
|
||
| template <typename T> | ||
| void expose_boost_type_info() { | ||
| boost::python::def( | ||
| "type_info", | ||
| +[](const T& value) -> boost::typeindex::type_index { | ||
| return boost::typeindex::type_id_runtime(value); | ||
| }, | ||
| bp::arg("value"), | ||
| "Returns information of the type of value as a " | ||
| "boost::typeindex::type_index (can work without RTTI)."); | ||
| boost::python::def( | ||
| "boost_type_info", | ||
| +[](const T& value) -> boost::typeindex::type_index { | ||
| return boost::typeindex::type_id_runtime(value); | ||
| }, | ||
| bp::arg("value"), | ||
| "Returns information of the type of value as a " | ||
| "boost::typeindex::type_index (can work without RTTI)."); | ||
| } | ||
|
|
||
| template <typename T> | ||
| void expose_std_type_info() { | ||
| boost::python::def( | ||
| "std_type_info", | ||
| +[](const T& value) -> std::type_index { return typeid(value); }, | ||
| bp::arg("value"), | ||
| "Returns information of the type of value as a std::type_index."); | ||
| } | ||
|
|
||
| /// | ||
| /// \brief Add the Python method type_info to query information of a type. | ||
| /// | ||
| template <class C> | ||
| struct TypeInfoVisitor : public bp::def_visitor<TypeInfoVisitor<C> > { | ||
| template <class PyClass> | ||
| void visit(PyClass& cl) const { | ||
| cl.def("type_info", &boost_type_info, bp::arg("self"), | ||
| "Queries information of the type of *this as a " | ||
| "boost::typeindex::type_index (can work without RTTI)."); | ||
| cl.def("boost_type_info", &boost_type_info, bp::arg("self"), | ||
| "Queries information of the type of *this as a " | ||
| "boost::typeindex::type_index (can work without RTTI)."); | ||
| cl.def("std_type_info", &std_type_info, bp::arg("self"), | ||
| "Queries information of the type of *this as a std::type_index."); | ||
| } | ||
|
|
||
| private: | ||
| static boost::typeindex::type_index boost_type_info(const C& self) { | ||
| return boost::typeindex::type_id_runtime(self); | ||
| } | ||
|
|
||
| static std::type_index std_type_info(const C& self) { return typeid(self); } | ||
| }; | ||
|
|
||
| } // namespace eigenpy | ||
|
|
||
| #endif // __eigenpy_type_info_hpp__ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| /// | ||
| /// Copyright 2024 INRIA | ||
| /// | ||
|
|
||
| #include <typeinfo> | ||
| #include <typeindex> | ||
|
|
||
| #include <boost/python.hpp> | ||
| #include <boost/type_index.hpp> | ||
|
|
||
| #include "eigenpy/registration.hpp" | ||
|
|
||
| namespace bp = boost::python; | ||
|
|
||
| namespace eigenpy { | ||
|
|
||
| void exposeStdTypeIndex() { | ||
| typedef std::type_index Self; | ||
| if (register_symbolic_link_to_registered_type<Self>()) return; | ||
|
|
||
| bp::class_<Self>( | ||
jcarpent marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "std_type_index", | ||
| "The class type_index holds implementation-specific information about a " | ||
| "type, including the name of the type and means to compare two types for " | ||
| "equality or collating order.", | ||
| bp::no_init) | ||
| .def(bp::self == bp::self) | ||
| .def(bp::self >= bp::self) | ||
| .def(bp::self > bp::self) | ||
| .def(bp::self < bp::self) | ||
| .def(bp::self <= bp::self) | ||
| .def("hash_code", &Self::hash_code, bp::arg("self"), | ||
| "Returns an unspecified value (here denoted by hash code) such that " | ||
| "for all std::type_info objects referring to the same type, their " | ||
| "hash code is the same.") | ||
| .def("name", &Self::name, bp::arg("self"), | ||
| "Returns an implementation defined null-terminated character string " | ||
| "containing the name of the type. No guarantees are given; in " | ||
| "particular, the returned string can be identical for several types " | ||
| "and change between invocations of the same program.") | ||
| .def( | ||
| "pretty_name", | ||
| +[](const Self &value) -> std::string { | ||
| return boost::core::demangle(value.name()); | ||
| }, | ||
| bp::arg("self"), "Human readible name."); | ||
| } | ||
|
|
||
| void exposeBoostTypeIndex() { | ||
| typedef boost::typeindex::type_index Self; | ||
| if (register_symbolic_link_to_registered_type<Self>()) return; | ||
|
|
||
| bp::class_<Self>( | ||
| "boost_type_index", | ||
| "The class type_index holds implementation-specific information about a " | ||
| "type, including the name of the type and means to compare two types for " | ||
| "equality or collating order.", | ||
| bp::no_init) | ||
| .def(bp::self == bp::self) | ||
| .def(bp::self >= bp::self) | ||
| .def(bp::self > bp::self) | ||
| .def(bp::self < bp::self) | ||
| .def(bp::self <= bp::self) | ||
| .def("hash_code", &Self::hash_code, bp::arg("self"), | ||
| "Returns an unspecified value (here denoted by hash code) such that " | ||
| "for all std::type_info objects referring to the same type, their " | ||
| "hash code is the same.") | ||
| .def("name", &Self::name, bp::arg("self"), | ||
| "Returns an implementation defined null-terminated character string " | ||
| "containing the name of the type. No guarantees are given; in " | ||
| "particular, the returned string can be identical for several types " | ||
| "and change between invocations of the same program.") | ||
| .def("pretty_name", &Self::pretty_name, bp::arg("self"), | ||
| "Human readible name."); | ||
| } | ||
|
|
||
| void exposeTypeInfo() { | ||
| exposeStdTypeIndex(); | ||
| exposeBoostTypeIndex(); | ||
| } | ||
| } // namespace eigenpy | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import type_info | ||
|
|
||
| d = type_info.Dummy() | ||
| assert "Dummy" in d.type_info().pretty_name() | ||
|
|
||
| assert type_info.type_info(1).pretty_name() == "int" | ||
| assert "basic_string" in type_info.type_info("toto").pretty_name() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| /* | ||
| * Copyright 2024 INRIA | ||
| */ | ||
|
|
||
| #include <iostream> | ||
|
|
||
| #include "eigenpy/eigenpy.hpp" | ||
| #include "eigenpy/type_info.hpp" | ||
|
|
||
| struct Dummy {}; | ||
|
|
||
| BOOST_PYTHON_MODULE(type_info) { | ||
| using namespace Eigen; | ||
| namespace bp = boost::python; | ||
| eigenpy::enableEigenPy(); | ||
|
|
||
| eigenpy::expose_boost_type_info<int>(); | ||
| eigenpy::expose_boost_type_info<std::string>(); | ||
|
|
||
| bp::class_<Dummy>("Dummy").def(eigenpy::TypeInfoVisitor<Dummy>()); | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.