Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:06:11

0001 /*
0002     pybind11/detail/descr.h: Helper type for concatenating type signatures at compile time
0003 
0004     Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
0005 
0006     All rights reserved. Use of this source code is governed by a
0007     BSD-style license that can be found in the LICENSE file.
0008 */
0009 
0010 #pragma once
0011 
0012 #include "common.h"
0013 
0014 PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
0015 PYBIND11_NAMESPACE_BEGIN(detail)
0016 
0017 #if !defined(_MSC_VER)
0018 #    define PYBIND11_DESCR_CONSTEXPR static constexpr
0019 #else
0020 #    define PYBIND11_DESCR_CONSTEXPR const
0021 #endif
0022 
0023 /* Concatenate type signatures at compile time */
0024 template <size_t N, typename... Ts>
0025 struct descr {
0026     char text[N + 1]{'\0'};
0027 
0028     constexpr descr() = default;
0029     // NOLINTNEXTLINE(google-explicit-constructor)
0030     constexpr descr(char const (&s)[N + 1]) : descr(s, make_index_sequence<N>()) {}
0031 
0032     template <size_t... Is>
0033     constexpr descr(char const (&s)[N + 1], index_sequence<Is...>) : text{s[Is]..., '\0'} {}
0034 
0035     template <typename... Chars>
0036     // NOLINTNEXTLINE(google-explicit-constructor)
0037     constexpr descr(char c, Chars... cs) : text{c, static_cast<char>(cs)..., '\0'} {}
0038 
0039     static constexpr std::array<const std::type_info *, sizeof...(Ts) + 1> types() {
0040         return {{&typeid(Ts)..., nullptr}};
0041     }
0042 };
0043 
0044 template <size_t N1, size_t N2, typename... Ts1, typename... Ts2, size_t... Is1, size_t... Is2>
0045 constexpr descr<N1 + N2, Ts1..., Ts2...> plus_impl(const descr<N1, Ts1...> &a,
0046                                                    const descr<N2, Ts2...> &b,
0047                                                    index_sequence<Is1...>,
0048                                                    index_sequence<Is2...>) {
0049     PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(b);
0050     return {a.text[Is1]..., b.text[Is2]...};
0051 }
0052 
0053 template <size_t N1, size_t N2, typename... Ts1, typename... Ts2>
0054 constexpr descr<N1 + N2, Ts1..., Ts2...> operator+(const descr<N1, Ts1...> &a,
0055                                                    const descr<N2, Ts2...> &b) {
0056     return plus_impl(a, b, make_index_sequence<N1>(), make_index_sequence<N2>());
0057 }
0058 
0059 template <size_t N>
0060 constexpr descr<N - 1> const_name(char const (&text)[N]) {
0061     return descr<N - 1>(text);
0062 }
0063 constexpr descr<0> const_name(char const (&)[1]) { return {}; }
0064 
0065 template <size_t Rem, size_t... Digits>
0066 struct int_to_str : int_to_str<Rem / 10, Rem % 10, Digits...> {};
0067 template <size_t... Digits>
0068 struct int_to_str<0, Digits...> {
0069     // WARNING: This only works with C++17 or higher.
0070     static constexpr auto digits = descr<sizeof...(Digits)>(('0' + Digits)...);
0071 };
0072 
0073 // Ternary description (like std::conditional)
0074 template <bool B, size_t N1, size_t N2>
0075 constexpr enable_if_t<B, descr<N1 - 1>> const_name(char const (&text1)[N1], char const (&)[N2]) {
0076     return const_name(text1);
0077 }
0078 template <bool B, size_t N1, size_t N2>
0079 constexpr enable_if_t<!B, descr<N2 - 1>> const_name(char const (&)[N1], char const (&text2)[N2]) {
0080     return const_name(text2);
0081 }
0082 
0083 template <bool B, typename T1, typename T2>
0084 constexpr enable_if_t<B, T1> const_name(const T1 &d, const T2 &) {
0085     return d;
0086 }
0087 template <bool B, typename T1, typename T2>
0088 constexpr enable_if_t<!B, T2> const_name(const T1 &, const T2 &d) {
0089     return d;
0090 }
0091 
0092 template <size_t Size>
0093 auto constexpr const_name() -> remove_cv_t<decltype(int_to_str<Size / 10, Size % 10>::digits)> {
0094     return int_to_str<Size / 10, Size % 10>::digits;
0095 }
0096 
0097 template <typename Type>
0098 constexpr descr<1, Type> const_name() {
0099     return {'%'};
0100 }
0101 
0102 // If "_" is defined as a macro, py::detail::_ cannot be provided.
0103 // It is therefore best to use py::detail::const_name universally.
0104 // This block is for backward compatibility only.
0105 // (The const_name code is repeated to avoid introducing a "_" #define ourselves.)
0106 #ifndef _
0107 #    define PYBIND11_DETAIL_UNDERSCORE_BACKWARD_COMPATIBILITY
0108 template <size_t N>
0109 constexpr descr<N - 1> _(char const (&text)[N]) {
0110     return const_name<N>(text);
0111 }
0112 template <bool B, size_t N1, size_t N2>
0113 constexpr enable_if_t<B, descr<N1 - 1>> _(char const (&text1)[N1], char const (&text2)[N2]) {
0114     return const_name<B, N1, N2>(text1, text2);
0115 }
0116 template <bool B, size_t N1, size_t N2>
0117 constexpr enable_if_t<!B, descr<N2 - 1>> _(char const (&text1)[N1], char const (&text2)[N2]) {
0118     return const_name<B, N1, N2>(text1, text2);
0119 }
0120 template <bool B, typename T1, typename T2>
0121 constexpr enable_if_t<B, T1> _(const T1 &d1, const T2 &d2) {
0122     return const_name<B, T1, T2>(d1, d2);
0123 }
0124 template <bool B, typename T1, typename T2>
0125 constexpr enable_if_t<!B, T2> _(const T1 &d1, const T2 &d2) {
0126     return const_name<B, T1, T2>(d1, d2);
0127 }
0128 
0129 template <size_t Size>
0130 auto constexpr _() -> remove_cv_t<decltype(int_to_str<Size / 10, Size % 10>::digits)> {
0131     return const_name<Size>();
0132 }
0133 template <typename Type>
0134 constexpr descr<1, Type> _() {
0135     return const_name<Type>();
0136 }
0137 #endif // #ifndef _
0138 
0139 constexpr descr<0> concat() { return {}; }
0140 
0141 template <size_t N, typename... Ts>
0142 constexpr descr<N, Ts...> concat(const descr<N, Ts...> &descr) {
0143     return descr;
0144 }
0145 
0146 #ifdef __cpp_fold_expressions
0147 template <size_t N1, size_t N2, typename... Ts1, typename... Ts2>
0148 constexpr descr<N1 + N2 + 2, Ts1..., Ts2...> operator,(const descr<N1, Ts1...> &a,
0149                                                        const descr<N2, Ts2...> &b) {
0150     return a + const_name(", ") + b;
0151 }
0152 
0153 template <size_t N, typename... Ts, typename... Args>
0154 constexpr auto concat(const descr<N, Ts...> &d, const Args &...args) {
0155     return (d, ..., args);
0156 }
0157 #else
0158 template <size_t N, typename... Ts, typename... Args>
0159 constexpr auto concat(const descr<N, Ts...> &d,
0160                       const Args &...args) -> decltype(std::declval<descr<N + 2, Ts...>>()
0161                                                        + concat(args...)) {
0162     return d + const_name(", ") + concat(args...);
0163 }
0164 #endif
0165 
0166 template <size_t N, typename... Ts>
0167 constexpr descr<N + 2, Ts...> type_descr(const descr<N, Ts...> &descr) {
0168     return const_name("{") + descr + const_name("}");
0169 }
0170 
0171 PYBIND11_NAMESPACE_END(detail)
0172 PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)