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.
 
 
 
 

126 lines
3.9 KiB

  1. /*!
  2. * \file integralconstant.h
  3. * \brief Template meta-programming integral constant
  4. *
  5. * Copyright (C) 2018 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. /*!
  24. * \ingroup meta
  25. * \defgroup integral
  26. * integral constant support header
  27. */
  28. //! @{
  29. namespace utl {
  30. namespace meta {
  31. /*!
  32. * Empty type
  33. * utl::meta's nil type is not pure nil. It's a recursive "de-referencable nil.
  34. * Each time someone applies ::type to it, he gets back nil_. This way we can prevent
  35. * a lot of compilation errors in a wrong meta:: handling.
  36. */
  37. struct nil_ {
  38. using type = nil_;
  39. };
  40. //! Type alias for \p _Tp::type. Used to extract return type of metafunctions
  41. template <typename _Tp>
  42. using type_ = typename _Tp::type;
  43. //! integral_constant
  44. //! An Integral Constant is a holder class for a compile-time value of an integral type.
  45. //! Every Integral Constant is also a nullary Metafunction, returning itself.
  46. //! An integral constant object is implicitly convertible to the corresponding
  47. //! run-time value of the wrapped integral type
  48. //! @{
  49. template <typename _Tp, _Tp _v>
  50. struct integral_constant {
  51. using value_type = _Tp;
  52. using type = integral_constant<_Tp, _v>;
  53. constexpr operator value_type() const noexcept {
  54. return value;
  55. }
  56. constexpr value_type operator()() const noexcept {
  57. return value;
  58. }
  59. static constexpr _Tp value = _v;
  60. };
  61. template<typename _Tp, _Tp _v>
  62. constexpr _Tp integral_constant<_Tp, _v>::value;
  63. //! @}
  64. //! Wrappers for basic types
  65. //! @{
  66. //! integral constant
  67. template <typename _Tp, _Tp _v>
  68. using integral_c = integral_constant<_Tp, _v>;
  69. //! bool_ type: integral constant wrapper for bool
  70. template<bool _v>
  71. using bool_ = integral_c<bool, _v>;
  72. using true_ = bool_<true>; //!< The type used as a compile-time boolean with true value.
  73. using false_ = bool_<false>; //!< The type used as a compile-time boolean with false value.
  74. //! char_ type: integral constant wrapper for \c char
  75. template<char _v>
  76. using char_ = integral_c<char, _v>;
  77. //! int_ type: integral constant wrapper for \c int
  78. template<int _v>
  79. using int_ = integral_c<int, _v>;
  80. //! long_ type: integral constant wrapper for \c long
  81. template<long _v>
  82. using long_ = integral_c<long, _v>;
  83. //! index_t_ type: integral constant wrapper for \c index_t a.k.a std::size_t
  84. template<index_t _v>
  85. using index_t_ = integral_c<index_t, _v>;
  86. //! size_t_ type: integral constant wrapper for \c size_t a.k.a std::size_t
  87. template<size_t _v>
  88. using size_t_ = integral_constant<size_t, _v>;
  89. //! Computes the size of the type \p _Tp.
  90. //! Complexity \f$ O(1) \f$.
  91. template <typename _Tp>
  92. using sizeof_ = size_t_<sizeof(_Tp)>;
  93. //! Computes the alignment required for any instance of the type \p _Tp.
  94. //! Complexity \f$ O(1) \f$.
  95. template <typename _Tp>
  96. using alignof_ = size_t_<alignof(_Tp)>;
  97. //! @}
  98. //! The last position we can express for indexing
  99. using Npos = size_t_<index_t(-1)>;
  100. }}
  101. //!@}
  102. #endif /* __utl_meta_integralconstant_h__ */