-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #502 from jcarpent/topic/type_info
Add helper for type_info
- Loading branch information
Showing
8 changed files
with
195 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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>( | ||
"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 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 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 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>()); | ||
} |