descr.h
Go to the documentation of this file.
1 /*
2  pybind11/detail/descr.h: Helper type for concatenating type signatures at compile time
3 
4  Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
5 
6  All rights reserved. Use of this source code is governed by a
7  BSD-style license that can be found in the LICENSE file.
8 */
9 
10 #pragma once
11 
12 #include "common.h"
13 
16 
17 #if !defined(_MSC_VER)
18 # define PYBIND11_DESCR_CONSTEXPR static constexpr
19 #else
20 # define PYBIND11_DESCR_CONSTEXPR const
21 #endif
22 
23 /* Concatenate type signatures at compile time */
24 template <size_t N, typename... Ts>
25 struct descr {
26  char text[N + 1];
27 
28  constexpr descr() : text{'\0'} { }
29  constexpr descr(char const (&s)[N+1]) : descr(s, make_index_sequence<N>()) { }
30 
31  template <size_t... Is>
32  constexpr descr(char const (&s)[N+1], index_sequence<Is...>) : text{s[Is]..., '\0'} { }
33 
34  template <typename... Chars>
35  constexpr descr(char c, Chars... cs) : text{c, static_cast<char>(cs)..., '\0'} { }
36 
37  static constexpr std::array<const std::type_info *, sizeof...(Ts) + 1> types() {
38  return {{&typeid(Ts)..., nullptr}};
39  }
40 };
41 
42 template <size_t N1, size_t N2, typename... Ts1, typename... Ts2, size_t... Is1, size_t... Is2>
43 constexpr descr<N1 + N2, Ts1..., Ts2...> plus_impl(const descr<N1, Ts1...> &a, const descr<N2, Ts2...> &b,
45  return {a.text[Is1]..., b.text[Is2]...};
46 }
47 
48 template <size_t N1, size_t N2, typename... Ts1, typename... Ts2>
49 constexpr descr<N1 + N2, Ts1..., Ts2...> operator+(const descr<N1, Ts1...> &a, const descr<N2, Ts2...> &b) {
51 }
52 
53 template <size_t N>
54 constexpr descr<N - 1> _(char const(&text)[N]) { return descr<N - 1>(text); }
55 constexpr descr<0> _(char const(&)[1]) { return {}; }
56 
57 template <size_t Rem, size_t... Digits> struct int_to_str : int_to_str<Rem/10, Rem%10, Digits...> { };
58 template <size_t...Digits> struct int_to_str<0, Digits...> {
59  static constexpr auto digits = descr<sizeof...(Digits)>(('0' + Digits)...);
60 };
61 
62 // Ternary description (like std::conditional)
63 template <bool B, size_t N1, size_t N2>
64 constexpr enable_if_t<B, descr<N1 - 1>> _(char const(&text1)[N1], char const(&)[N2]) {
65  return _(text1);
66 }
67 template <bool B, size_t N1, size_t N2>
68 constexpr enable_if_t<!B, descr<N2 - 1>> _(char const(&)[N1], char const(&text2)[N2]) {
69  return _(text2);
70 }
71 
72 template <bool B, typename T1, typename T2>
73 constexpr enable_if_t<B, T1> _(const T1 &d, const T2 &) { return d; }
74 template <bool B, typename T1, typename T2>
75 constexpr enable_if_t<!B, T2> _(const T1 &, const T2 &d) { return d; }
76 
77 template <size_t Size> auto constexpr _() -> decltype(int_to_str<Size / 10, Size % 10>::digits) {
78  return int_to_str<Size / 10, Size % 10>::digits;
79 }
80 
81 template <typename Type> constexpr descr<1, Type> _() { return {'%'}; }
82 
83 constexpr descr<0> concat() { return {}; }
84 
85 template <size_t N, typename... Ts>
86 constexpr descr<N, Ts...> concat(const descr<N, Ts...> &descr) { return descr; }
87 
88 template <size_t N, typename... Ts, typename... Args>
89 constexpr auto concat(const descr<N, Ts...> &d, const Args &...args)
90  -> decltype(std::declval<descr<N + 2, Ts...>>() + concat(args...)) {
91  return d + _(", ") + concat(args...);
92 }
93 
94 template <size_t N, typename... Ts>
95 constexpr descr<N + 2, Ts...> type_descr(const descr<N, Ts...> &descr) {
96  return _("{") + descr + _("}");
97 }
98 
int array[24]
constexpr descr(char const (&s)[N+1], index_sequence< Is... >)
Definition: descr.h:32
Scalar * b
Definition: benchVecAdd.cpp:17
constexpr descr(char const (&s)[N+1])
Definition: descr.h:29
Definition: pytypes.h:1322
Scalar Scalar * c
Definition: benchVecAdd.cpp:17
constexpr descr< N1+N2, Ts1..., Ts2... > operator+(const descr< N1, Ts1... > &a, const descr< N2, Ts2... > &b)
Definition: descr.h:49
Pose2 T2(M_PI/2.0, Point2(0.0, 2.0))
#define N
Definition: gksort.c:12
char text[N+1]
Definition: descr.h:26
Array33i a
Definition: descr.h:25
RealScalar s
constexpr descr< 0 > concat()
Definition: descr.h:83
constexpr descr(char c, Chars...cs)
Definition: descr.h:35
constexpr descr< N+2, Ts... > type_descr(const descr< N, Ts... > &descr)
Definition: descr.h:95
constexpr descr< N1+N2, Ts1..., Ts2... > plus_impl(const descr< N1, Ts1... > &a, const descr< N2, Ts2... > &b, index_sequence< Is1... >, index_sequence< Is2... >)
Definition: descr.h:43
constexpr descr()
Definition: descr.h:28
constexpr descr< N-1 > _(char const (&text)[N])
Definition: descr.h:54
static constexpr std::array< const std::type_info *, sizeof...(Ts)+1 > types()
Definition: descr.h:37
typename std::enable_if< B, T >::type enable_if_t
from cpp_future import (convenient aliases from C++14/17)
Pose2 T1(M_PI/4.0, Point2(sqrt(0.5), sqrt(0.5)))
typename make_index_sequence_impl< N >::type make_index_sequence
#define PYBIND11_NAMESPACE_END(name)
std::vector< char > Chars
Definition: QPSParser.cpp:47
#define PYBIND11_NAMESPACE_BEGIN(name)


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:41:58