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.
 
 
 
 

87 lines
2.3 KiB

  1. /*!
  2. * \file selection.h
  3. * \brief Template meta-programming type selections.
  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_selection_h__
  21. #define __utl_meta_selection_h__
  22. #include <utl/core/impl.h>
  23. #include <utl/meta/integral.h>
  24. /*!
  25. * \ingroup meta
  26. * \defgroup type selection
  27. * Type selection support header
  28. */
  29. //! @{
  30. namespace utl {
  31. namespace meta{
  32. /*!
  33. * Type selection
  34. */
  35. //! @{
  36. //! if_, if_c
  37. //! @{
  38. namespace detail {
  39. template <bool If, typename...>
  40. struct if_c_ {
  41. using type = nil_; //!< avoid ill formed result
  42. };
  43. template<typename Then>
  44. struct if_c_<true, Then> {
  45. using type = Then;
  46. };
  47. template<typename Then, typename Else>
  48. struct if_c_<true, Then, Else> {
  49. using type = Then;
  50. };
  51. template<typename Then, typename Else>
  52. struct if_c_<false, Then, Else> {
  53. using type = Else;
  54. };
  55. }
  56. //! Select one type or another depending on a compile-time Boolean.
  57. template <bool B, typename... Args>
  58. using if_c = eval<detail::if_c_<B, Args...>>;
  59. //! Select one type or another depending on a compile-time Boolean type
  60. template <typename If, typename... Args>
  61. using if_ = if_c<If::type::value, Args...>;
  62. //! @}
  63. /*!
  64. * Named type selectors
  65. */
  66. //! @{
  67. //! Select the first type of a type sequence
  68. template <typename T1, typename ...> using first_of = T1;
  69. //! Select the second type of a type sequence
  70. template <typename T1, typename T2, typename ...> using second_of = T2;
  71. //! @}
  72. }}
  73. //! @}
  74. #endif /* __utl_meta_selection_h__ */