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.
 
 
 
 

152 lines
4.9 KiB

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