You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

183 lines
7.5 KiB

8 years ago
  1. /*
  2. pybind11/descr.h: Helper type for concatenating type signatures
  3. either at runtime (C++11) or compile time (C++14)
  4. Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
  5. All rights reserved. Use of this source code is governed by a
  6. BSD-style license that can be found in the LICENSE file.
  7. */
  8. #pragma once
  9. #include "common.h"
  10. NAMESPACE_BEGIN(pybind11)
  11. NAMESPACE_BEGIN(detail)
  12. #if defined(PYBIND11_CPP14) /* Concatenate type signatures at compile time using C++14 */
  13. template <size_t Size1, size_t Size2> class descr {
  14. template <size_t Size1_, size_t Size2_> friend class descr;
  15. public:
  16. constexpr descr(char const (&text) [Size1+1], const std::type_info * const (&types)[Size2+1])
  17. : descr(text, types,
  18. make_index_sequence<Size1>(),
  19. make_index_sequence<Size2>()) { }
  20. constexpr const char *text() const { return m_text; }
  21. constexpr const std::type_info * const * types() const { return m_types; }
  22. template <size_t OtherSize1, size_t OtherSize2>
  23. constexpr descr<Size1 + OtherSize1, Size2 + OtherSize2> operator+(const descr<OtherSize1, OtherSize2> &other) const {
  24. return concat(other,
  25. make_index_sequence<Size1>(),
  26. make_index_sequence<Size2>(),
  27. make_index_sequence<OtherSize1>(),
  28. make_index_sequence<OtherSize2>());
  29. }
  30. protected:
  31. template <size_t... Indices1, size_t... Indices2>
  32. constexpr descr(
  33. char const (&text) [Size1+1],
  34. const std::type_info * const (&types) [Size2+1],
  35. index_sequence<Indices1...>, index_sequence<Indices2...>)
  36. : m_text{text[Indices1]..., '\0'},
  37. m_types{types[Indices2]..., nullptr } {}
  38. template <size_t OtherSize1, size_t OtherSize2, size_t... Indices1,
  39. size_t... Indices2, size_t... OtherIndices1, size_t... OtherIndices2>
  40. constexpr descr<Size1 + OtherSize1, Size2 + OtherSize2>
  41. concat(const descr<OtherSize1, OtherSize2> &other,
  42. index_sequence<Indices1...>, index_sequence<Indices2...>,
  43. index_sequence<OtherIndices1...>, index_sequence<OtherIndices2...>) const {
  44. return descr<Size1 + OtherSize1, Size2 + OtherSize2>(
  45. { m_text[Indices1]..., other.m_text[OtherIndices1]..., '\0' },
  46. { m_types[Indices2]..., other.m_types[OtherIndices2]..., nullptr }
  47. );
  48. }
  49. protected:
  50. char m_text[Size1 + 1];
  51. const std::type_info * m_types[Size2 + 1];
  52. };
  53. template <size_t Size> constexpr descr<Size - 1, 0> _(char const(&text)[Size]) {
  54. return descr<Size - 1, 0>(text, { nullptr });
  55. }
  56. template <size_t Rem, size_t... Digits> struct int_to_str : int_to_str<Rem/10, Rem%10, Digits...> { };
  57. template <size_t...Digits> struct int_to_str<0, Digits...> {
  58. static constexpr auto digits = descr<sizeof...(Digits), 0>({ ('0' + Digits)..., '\0' }, { nullptr });
  59. };
  60. // Ternary description (like std::conditional)
  61. template <bool B, size_t Size1, size_t Size2>
  62. constexpr enable_if_t<B, descr<Size1 - 1, 0>> _(char const(&text1)[Size1], char const(&)[Size2]) {
  63. return _(text1);
  64. }
  65. template <bool B, size_t Size1, size_t Size2>
  66. constexpr enable_if_t<!B, descr<Size2 - 1, 0>> _(char const(&)[Size1], char const(&text2)[Size2]) {
  67. return _(text2);
  68. }
  69. template <bool B, size_t SizeA1, size_t SizeA2, size_t SizeB1, size_t SizeB2>
  70. constexpr enable_if_t<B, descr<SizeA1, SizeA2>> _(descr<SizeA1, SizeA2> d, descr<SizeB1, SizeB2>) { return d; }
  71. template <bool B, size_t SizeA1, size_t SizeA2, size_t SizeB1, size_t SizeB2>
  72. constexpr enable_if_t<!B, descr<SizeB1, SizeB2>> _(descr<SizeA1, SizeA2>, descr<SizeB1, SizeB2> d) { return d; }
  73. template <size_t Size> auto constexpr _() -> decltype(int_to_str<Size / 10, Size % 10>::digits) {
  74. return int_to_str<Size / 10, Size % 10>::digits;
  75. }
  76. template <typename Type> constexpr descr<1, 1> _() {
  77. return descr<1, 1>({ '%', '\0' }, { &typeid(Type), nullptr });
  78. }
  79. inline constexpr descr<0, 0> concat() { return _(""); }
  80. template <size_t Size1, size_t Size2, typename... Args> auto constexpr concat(descr<Size1, Size2> descr) { return descr; }
  81. template <size_t Size1, size_t Size2, typename... Args> auto constexpr concat(descr<Size1, Size2> descr, Args&&... args) { return descr + _(", ") + concat(args...); }
  82. template <size_t Size1, size_t Size2> auto constexpr type_descr(descr<Size1, Size2> descr) { return _("{") + descr + _("}"); }
  83. #define PYBIND11_DESCR constexpr auto
  84. #else /* Simpler C++11 implementation based on run-time memory allocation and copying */
  85. class descr {
  86. public:
  87. PYBIND11_NOINLINE descr(const char *text, const std::type_info * const * types) {
  88. size_t nChars = len(text), nTypes = len(types);
  89. m_text = new char[nChars];
  90. m_types = new const std::type_info *[nTypes];
  91. memcpy(m_text, text, nChars * sizeof(char));
  92. memcpy(m_types, types, nTypes * sizeof(const std::type_info *));
  93. }
  94. PYBIND11_NOINLINE descr friend operator+(descr &&d1, descr &&d2) {
  95. descr r;
  96. size_t nChars1 = len(d1.m_text), nTypes1 = len(d1.m_types);
  97. size_t nChars2 = len(d2.m_text), nTypes2 = len(d2.m_types);
  98. r.m_text = new char[nChars1 + nChars2 - 1];
  99. r.m_types = new const std::type_info *[nTypes1 + nTypes2 - 1];
  100. memcpy(r.m_text, d1.m_text, (nChars1-1) * sizeof(char));
  101. memcpy(r.m_text + nChars1 - 1, d2.m_text, nChars2 * sizeof(char));
  102. memcpy(r.m_types, d1.m_types, (nTypes1-1) * sizeof(std::type_info *));
  103. memcpy(r.m_types + nTypes1 - 1, d2.m_types, nTypes2 * sizeof(std::type_info *));
  104. delete[] d1.m_text; delete[] d1.m_types;
  105. delete[] d2.m_text; delete[] d2.m_types;
  106. return r;
  107. }
  108. char *text() { return m_text; }
  109. const std::type_info * * types() { return m_types; }
  110. protected:
  111. PYBIND11_NOINLINE descr() { }
  112. template <typename T> static size_t len(const T *ptr) { // return length including null termination
  113. const T *it = ptr;
  114. while (*it++ != (T) 0)
  115. ;
  116. return static_cast<size_t>(it - ptr);
  117. }
  118. const std::type_info **m_types = nullptr;
  119. char *m_text = nullptr;
  120. };
  121. /* The 'PYBIND11_NOINLINE inline' combinations below are intentional to get the desired linkage while producing as little object code as possible */
  122. PYBIND11_NOINLINE inline descr _(const char *text) {
  123. const std::type_info *types[1] = { nullptr };
  124. return descr(text, types);
  125. }
  126. template <bool B> PYBIND11_NOINLINE enable_if_t<B, descr> _(const char *text1, const char *) { return _(text1); }
  127. template <bool B> PYBIND11_NOINLINE enable_if_t<!B, descr> _(char const *, const char *text2) { return _(text2); }
  128. template <bool B> PYBIND11_NOINLINE enable_if_t<B, descr> _(descr d, descr) { return d; }
  129. template <bool B> PYBIND11_NOINLINE enable_if_t<!B, descr> _(descr, descr d) { return d; }
  130. template <typename Type> PYBIND11_NOINLINE descr _() {
  131. const std::type_info *types[2] = { &typeid(Type), nullptr };
  132. return descr("%", types);
  133. }
  134. template <size_t Size> PYBIND11_NOINLINE descr _() {
  135. const std::type_info *types[1] = { nullptr };
  136. return descr(std::to_string(Size).c_str(), types);
  137. }
  138. PYBIND11_NOINLINE inline descr concat() { return _(""); }
  139. PYBIND11_NOINLINE inline descr concat(descr &&d) { return d; }
  140. template <typename... Args> PYBIND11_NOINLINE descr concat(descr &&d, Args&&... args) { return std::move(d) + _(", ") + concat(std::forward<Args>(args)...); }
  141. PYBIND11_NOINLINE inline descr type_descr(descr&& d) { return _("{") + std::move(d) + _("}"); }
  142. #define PYBIND11_DESCR ::pybind11::detail::descr
  143. #endif
  144. NAMESPACE_END(detail)
  145. NAMESPACE_END(pybind11)