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.
 
 
 
 

160 lines
5.2 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. template <typename Tp>
  44. using eval = typename Tp::type;
  45. //! Type alias for \c Tp::type. Used to evaluate/extract return value of metafunctions
  46. template <typename Tp>
  47. using eval_t = typename Tp::type;
  48. //! Type alias for \c Tp::type. Used to evaluate/extract return type of metafunctions
  49. template <typename Tp>
  50. using eval_v = typename Tp::value;
  51. //!
  52. //! integral_ is a holder class for a compile-time value of an integral type.
  53. //!
  54. //! Every Integral Constant is also a null-ary Metafunction, returning itself.\n
  55. //! An integral constant object is implicitly convertible to the corresponding
  56. //! run-time value of the wrapped integral type
  57. template <typename Tp, Tp v>
  58. using integral_ = std::integral_constant<Tp, v>;
  59. //! \name Wrappers for basic types
  60. //! @{
  61. //! bool_ type: integral constant wrapper for bool
  62. template<bool v>
  63. using bool_ = integral_<bool, v>;
  64. using true_ = bool_<true>; //!< The type used as a compile-time boolean with true value.
  65. using false_ = bool_<false>; //!< The type used as a compile-time boolean with false value.
  66. //! int8_ type: integral constant wrapper for \c int8_t
  67. template<int8_t v>
  68. using int8_ = integral_<int8_t, v>;
  69. //! uint8_ type: integral constant wrapper for \c uint8_t
  70. template<uint8_t v>
  71. using uint8_ = integral_<uint8_t, v>;
  72. //! int16_ type: integral constant wrapper for \c int16_t
  73. template<int16_t v>
  74. using int16_ = integral_<int16_t, v>;
  75. //! uint16_ type: integral constant wrapper for \c uint16_t
  76. template<uint16_t v>
  77. using uint16_ = integral_<uint16_t, v>;
  78. //! int32_ type: integral constant wrapper for \c int32_t
  79. template<int32_t v>
  80. using int32_ = integral_<int32_t, v>;
  81. //! uint32_ type: integral constant wrapper for \c uint32_t
  82. template<uint32_t v>
  83. using uint32_ = integral_<uint32_t, v>;
  84. //! char_ type: integral constant wrapper for \c char
  85. template<char v>
  86. using char_ = integral_<char, v>;
  87. //! int_ type: integral constant wrapper for \c int
  88. template<int v>
  89. using int_ = integral_<int, v>;
  90. //! long_ type: integral constant wrapper for \c long
  91. template<long v>
  92. using long_ = integral_<long, v>;
  93. //! index_ type: integral constant wrapper for \c index_t a.k.a std::size_t
  94. template<index_t v>
  95. using index_ = integral_<index_t, v>;
  96. //! size_ type: integral constant wrapper for \c size_t a.k.a std::size_t
  97. template<size_t v>
  98. using size_ = integral_<size_t, v>;
  99. //! Computes the size of the type \p Tp.
  100. //! Complexity \f$ O(1) \f$.
  101. template <typename Tp>
  102. using sizeof_ = size_<sizeof(Tp)>;
  103. //! Computes the alignment required for any instance of the type \p Tp.
  104. //! Complexity \f$ O(1) \f$.
  105. template <typename Tp>
  106. using alignof_ = size_<alignof(Tp)>;
  107. //! @}
  108. //! The last position we can express for indexing
  109. using Npos = size_<index_t(-1)>;
  110. //! \name integer sequence
  111. //! @{
  112. template< class Tp, Tp... Ints >
  113. using integer_sequence = std::integer_sequence<Tp, Ints...>;
  114. template<typename Tp, Tp Num>
  115. using make_integer_sequence = std::make_integer_sequence<Tp, Num>;
  116. //! Alias template index_sequence
  117. template<index_t... Idx>
  118. using index_sequence = integer_sequence<index_t, Idx...>;
  119. //! Alias template make_index_sequence
  120. template<index_t Num>
  121. using make_index_sequence = make_integer_sequence <index_t, Num>;
  122. //! Alias template index_sequence_for
  123. template<typename... Types>
  124. using index_sequence_for = make_index_sequence<sizeof...(Types)>;
  125. //! @}
  126. }}
  127. //!@}
  128. #endif /* __utl_meta_basic_h__ */