Drake
cpp_param_pybind.h
Go to the documentation of this file.
1 #pragma once
2 
3 /// @file
4 /// Provides a mechanism to map C++ types to canonical Python types.
5 
6 #include <string>
7 #include <typeinfo>
8 #include <vector>
9 
10 #include "pybind11/pybind11.h"
11 
14 
15 namespace drake {
16 namespace pydrake {
17 namespace internal {
18 
19 // Gets singleton for type aliases from `cpp_param`.
20 py::object GetParamAliases();
21 
22 // Gets Python type object given `std::type_info`.
23 // @throws std::runtime_error if type is neither aliased nor registered in
24 // `pybind11`.
25 py::object GetPyParamScalarImpl(const std::type_info& tinfo);
26 
27 // Gets Python type for a C++ type (base case).
28 template <typename T>
29 inline py::object GetPyParamScalarImpl(type_pack<T> = {}) {
30  return GetPyParamScalarImpl(typeid(T));
31 }
32 
33 // Gets Python literal for a C++ literal (specialization).
34 template <typename T, T Value>
35 inline py::object GetPyParamScalarImpl(
36  type_pack<std::integral_constant<T, Value>> = {}) {
37  return py::cast(Value);
38 }
39 
40 } // namespace internal
41 
42 /// Gets the canonical Python parameters for each C++ type.
43 /// @returns Python tuple of canonical parameters.
44 /// @throws std::runtime_error on the first type it encounters that is neither
45 /// aliased nor registered in `pybind11`.
46 template <typename ... Ts>
47 inline py::tuple GetPyParam(type_pack<Ts...> = {}) {
48  return py::make_tuple(internal::GetPyParamScalarImpl(type_pack<Ts>{})...);
49 }
50 
51 } // namespace pydrake
52 } // namespace drake
py::object GetParamAliases()
Definition: cpp_param_pybind.cc:51
Definition: bullet_model.cc:22
double T
Definition: benchmarks_py.cc:14
Provides a tag to pass a parameter packs for ease of inference.
Definition: type_pack.h:16
py::tuple GetPyParam(type_pack< Ts... >={})
Gets the canonical Python parameters for each C++ type.
Definition: cpp_param_pybind.h:47
py::object GetPyParamScalarImpl(const std::type_info &tinfo)
Definition: cpp_param_pybind.cc:62
Basic meta-programming utilities for types, focused on template parameter packs.