Skip to content

Commit 0e5bf54

Browse files
committed
utils: add type_info helpers
1 parent 52092f6 commit 0e5bf54

File tree

4 files changed

+149
-0
lines changed

4 files changed

+149
-0
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ set(${PROJECT_NAME}_HEADERS
275275
include/eigenpy/variant.hpp
276276
include/eigenpy/std-unique-ptr.hpp
277277
include/eigenpy/swig.hpp
278+
include/eigenpy/type_info.hpp
278279
include/eigenpy/version.hpp)
279280

280281
list(
@@ -349,6 +350,7 @@ set(${PROJECT_NAME}_SOURCES
349350
src/scipy-type.cpp
350351
src/std-vector.cpp
351352
src/optional.cpp
353+
src/type_info.cpp
352354
src/version.cpp)
353355

354356
add_library(${PROJECT_NAME} SHARED ${${PROJECT_NAME}_SOURCES}

include/eigenpy/type_info.hpp

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
///
2+
/// Copyright (c) 2024 INRIA
3+
///
4+
5+
#ifndef __eigenpy_type_info_hpp__
6+
#define __eigenpy_type_info_hpp__
7+
8+
#include "eigenpy/fwd.hpp"
9+
10+
#include <boost/type_index.hpp>
11+
#include <typeinfo>
12+
#include <typeindex>
13+
14+
namespace eigenpy {
15+
16+
template <typename T>
17+
boost::typeindex::type_index type_info(const T& value) {
18+
return boost::typeindex::type_id_runtime(value);
19+
}
20+
21+
template <typename T>
22+
void expose_boost_type_info() {
23+
boost::python::def(
24+
"type_info",
25+
+[](const T& value) -> boost::typeindex::type_index {
26+
return boost::typeindex::type_id_runtime(value);
27+
},
28+
bp::arg("value"),
29+
"Returns information of the type of value as a "
30+
"boost::typeindex::type_index (can work without RTTI).");
31+
boost::python::def(
32+
"boost_type_info",
33+
+[](const T& value) -> boost::typeindex::type_index {
34+
return boost::typeindex::type_id_runtime(value);
35+
},
36+
bp::arg("value"),
37+
"Returns information of the type of value as a "
38+
"boost::typeindex::type_index (can work without RTTI).");
39+
}
40+
41+
template <typename T>
42+
void expose_std_type_info() {
43+
boost::python::def(
44+
"std_type_info",
45+
+[](const T& value) -> std::type_index { return typeid(value); },
46+
bp::arg("value"),
47+
"Returns information of the type of value as a std::type_index.");
48+
}
49+
50+
///
51+
/// \brief Add the Python method type_info to query information of a type.
52+
///
53+
template <class C>
54+
struct TypeInfoVisitor : public bp::def_visitor<TypeInfoVisitor<C> > {
55+
template <class PyClass>
56+
void visit(PyClass& cl) const {
57+
cl.def("type_info", &boost_type_info, bp::arg("self"),
58+
"Queries information of the type of *this as a "
59+
"boost::typeindex::type_index (can work without RTTI).");
60+
cl.def("boost_type_info", &boost_type_info, bp::arg("self"),
61+
"Queries information of the type of *this as a "
62+
"boost::typeindex::type_index (can work without RTTI).");
63+
cl.def("std_type_info", &std_type_info, bp::arg("self"),
64+
"Queries information of the type of *this as a std::type_index.");
65+
}
66+
67+
private:
68+
static boost::typeindex::type_index boost_type_info(const C& self) {
69+
return boost::typeindex::type_id<C>();
70+
}
71+
72+
static std::type_index std_type_info(const C& self) { return typeid(C); }
73+
};
74+
75+
} // namespace eigenpy
76+
77+
#endif // __eigenpy_type_info_hpp__

src/eigenpy.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ void exposeMatrixComplexDouble();
3636
void exposeMatrixComplexLongDouble();
3737

3838
void exposeNoneType();
39+
void exposeTypeInfo();
3940

4041
/* Enable Eigen-Numpy serialization for a set of standard MatrixBase instances.
4142
*/
@@ -84,6 +85,7 @@ void enableEigenPy() {
8485
exposeMatrixComplexLongDouble();
8586

8687
exposeNoneType();
88+
exposeTypeInfo();
8789
}
8890

8991
bool withTensorSupport() {

src/type_info.cpp

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
///
2+
/// Copyright 2024 INRIA
3+
///
4+
5+
#include <typeinfo>
6+
#include <typeindex>
7+
8+
#include <boost/python.hpp>
9+
#include <boost/type_index.hpp>
10+
11+
namespace bp = boost::python;
12+
13+
void exposeStdTypeIndex() {
14+
typedef std::type_index Self;
15+
bp::class_<Self>(
16+
"std_type_index",
17+
"The class type_index holds implementation-specific information about a "
18+
"type, including the name of the type and means to compare two types for "
19+
"equality or collating order.",
20+
bp::no_init)
21+
.def(bp::self == bp::self)
22+
.def(bp::self >= bp::self)
23+
.def(bp::self > bp::self)
24+
.def(bp::self < bp::self)
25+
.def(bp::self <= bp::self)
26+
.def("hash_code", &Self::hash_code, bp::arg("self"),
27+
"Returns an unspecified value (here denoted by hash code) such that "
28+
"for all std::type_info objects referring to the same type, their "
29+
"hash code is the same.")
30+
.def("name", &Self::name, bp::arg("self"),
31+
"Returns an implementation defined null-terminated character string "
32+
"containing the name of the type. No guarantees are given; in "
33+
"particular, the returned string can be identical for several types "
34+
"and change between invocations of the same program.");
35+
}
36+
37+
void exposeBoostTypeIndex() {
38+
typedef boost::typeindex::type_index Self;
39+
bp::class_<Self>(
40+
"boost_type_index",
41+
"The class type_index holds implementation-specific information about a "
42+
"type, including the name of the type and means to compare two types for "
43+
"equality or collating order.",
44+
bp::no_init)
45+
.def(bp::self == bp::self)
46+
.def(bp::self >= bp::self)
47+
.def(bp::self > bp::self)
48+
.def(bp::self < bp::self)
49+
.def(bp::self <= bp::self)
50+
.def("hash_code", &Self::hash_code, bp::arg("self"),
51+
"Returns an unspecified value (here denoted by hash code) such that "
52+
"for all std::type_info objects referring to the same type, their "
53+
"hash code is the same.")
54+
.def("name", &Self::name, bp::arg("self"),
55+
"Returns an implementation defined null-terminated character string "
56+
"containing the name of the type. No guarantees are given; in "
57+
"particular, the returned string can be identical for several types "
58+
"and change between invocations of the same program.")
59+
.def("pretty_name", &Self::pretty_name, bp::arg("self"),
60+
"Human readible name.");
61+
}
62+
63+
namespace eigenpy {
64+
void exposeTypeInfo() {
65+
exposeStdTypeIndex();
66+
exposeBoostTypeIndex();
67+
}
68+
} // namespace eigenpy

0 commit comments

Comments
 (0)