Micro template library A library for building device drivers
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.
 
 
 
 

162 lines
5.3 KiB

  1. /*!
  2. * \file utl/meta/basic.h
  3. * \brief Template meta-programming basic definitions
  4. *
  5. * \copyright
  6. * Copyright (C) 2018 Christos Choutouridis <christos@choutouridis.net>\n
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation, either version 3
  10. * of the License, or (at your option) any later version.\n
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.\n
  15. * You should have received a copy of the GNU Lesser General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. #ifndef __utl_meta_basic_h__
  20. #define __utl_meta_basic_h__
  21. #include <utl/core/impl.h>
  22. #include <type_traits>
  23. #include <utility>
  24. /*!
  25. * \ingroup meta
  26. * \defgroup basic Basic
  27. * Basic definitions
  28. */
  29. //! @{
  30. namespace utl {
  31. namespace meta {
  32. /*!
  33. * meta's empty type
  34. *
  35. * utl::meta's nil type is not pure nil. It's a recursive "de-referencable nil.
  36. * Each time someone applies \c \::type to it, he gets back nil_. This way we can prevent
  37. * a lot of compilation errors in a wrong meta:: handling.
  38. */
  39. struct nil_ {
  40. using type = nil_;
  41. };
  42. //! Type alias for \c Tp::type. Used to evaluate/extract return type of metafunctions
  43. //! \tparam Tp The metafunction to evaluate
  44. //! \return The inner \::type
  45. template <typename Tp>
  46. using eval = typename Tp::type;
  47. //! Type alias for \c Tp::type. Used to evaluate/extract return type of metafunctions
  48. template <typename Tp>
  49. using eval_t = typename Tp::type;
  50. //! Type alias for \c Tp::value. Used to evaluate/extract return value of metafunctions
  51. template <typename Tp>
  52. using eval_v = typename Tp::value;
  53. //!
  54. //! integral_ is a holder class for a compile-time value of an integral type.
  55. //!
  56. //! Every Integral Constant is also a null-ary Metafunction, returning itself.\n
  57. //! An integral constant object is implicitly convertible to the corresponding
  58. //! run-time value of the wrapped integral type
  59. template <typename Tp, Tp v>
  60. using integral_ = std::integral_constant<Tp, v>;
  61. //! \name Wrappers for basic types
  62. //! @{
  63. //! bool_ type: integral constant wrapper for bool
  64. template<bool v>
  65. using bool_ = integral_<bool, v>;
  66. using true_ = bool_<true>; //!< The type used as a compile-time boolean with true value.
  67. using false_ = bool_<false>; //!< The type used as a compile-time boolean with false value.
  68. //! int8_ type: integral constant wrapper for \c int8_t
  69. template<int8_t v>
  70. using int8_ = integral_<int8_t, v>;
  71. //! uint8_ type: integral constant wrapper for \c uint8_t
  72. template<uint8_t v>
  73. using uint8_ = integral_<uint8_t, v>;
  74. //! int16_ type: integral constant wrapper for \c int16_t
  75. template<int16_t v>
  76. using int16_ = integral_<int16_t, v>;
  77. //! uint16_ type: integral constant wrapper for \c uint16_t
  78. template<uint16_t v>
  79. using uint16_ = integral_<uint16_t, v>;
  80. //! int32_ type: integral constant wrapper for \c int32_t
  81. template<int32_t v>
  82. using int32_ = integral_<int32_t, v>;
  83. //! uint32_ type: integral constant wrapper for \c uint32_t
  84. template<uint32_t v>
  85. using uint32_ = integral_<uint32_t, v>;
  86. //! char_ type: integral constant wrapper for \c char
  87. template<char v>
  88. using char_ = integral_<char, v>;
  89. //! int_ type: integral constant wrapper for \c int
  90. template<int v>
  91. using int_ = integral_<int, v>;
  92. //! long_ type: integral constant wrapper for \c long
  93. template<long v>
  94. using long_ = integral_<long, v>;
  95. //! index_ type: integral constant wrapper for \c index_t a.k.a std::size_t
  96. template<index_t v>
  97. using index_ = integral_<index_t, v>;
  98. //! size_ type: integral constant wrapper for \c size_t a.k.a std::size_t
  99. template<size_t v>
  100. using size_ = integral_<size_t, v>;
  101. //! Computes the size of the type \p Tp.
  102. //! Complexity \f$ O(1) \f$.
  103. template <typename Tp>
  104. using sizeof_ = size_<sizeof(Tp)>;
  105. //! Computes the alignment required for any instance of the type \p Tp.
  106. //! Complexity \f$ O(1) \f$.
  107. template <typename Tp>
  108. using alignof_ = size_<alignof(Tp)>;
  109. //! @}
  110. //! The last position we can express for indexing
  111. using Npos = size_<index_t(-1)>;
  112. //! \name integer sequence
  113. //! @{
  114. template< class Tp, Tp... Ints >
  115. using integer_sequence = std::integer_sequence<Tp, Ints...>;
  116. template<typename Tp, Tp Num>
  117. using make_integer_sequence = std::make_integer_sequence<Tp, Num>;
  118. //! Alias template index_sequence
  119. template<index_t... Idx>
  120. using index_sequence = integer_sequence<index_t, Idx...>;
  121. //! Alias template make_index_sequence
  122. template<index_t Num>
  123. using make_index_sequence = make_integer_sequence <index_t, Num>;
  124. //! Alias template index_sequence_for
  125. template<typename... Types>
  126. using index_sequence_for = make_index_sequence<sizeof...(Types)>;
  127. //! @}
  128. }}
  129. //!@}
  130. #endif /* __utl_meta_basic_h__ */