type_list.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /**
  2. * Tencent is pleased to support the open source community by making Tars available.
  3. *
  4. * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
  5. *
  6. * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  7. * in compliance with the License. You may obtain a copy of the License at
  8. *
  9. * https://opensource.org/licenses/BSD-3-Clause
  10. *
  11. * Unless required by applicable law or agreed to in writing, software distributed
  12. * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  13. * CONDITIONS OF ANY KIND, either express or implied. See the License for the
  14. * specific language governing permissions and limitations under the License.
  15. */
  16. #ifndef _TARS_TYPE_LIST_H_
  17. #define _TARS_TYPE_LIST_H_
  18. namespace tars {
  19. namespace tl {
  20. template <typename... Types>
  21. struct TypeList {};
  22. // get the Nth type in a given type list.
  23. template <size_t N, typename List>
  24. struct TypeAtImpl;
  25. template <size_t N, typename T, typename... List>
  26. struct TypeAtImpl<N, TypeList<T, List...> >
  27. : TypeAtImpl<N-1, TypeList<List...> > {};
  28. template <typename T, typename... List>
  29. struct TypeAtImpl<0, TypeList<T, List...> > {
  30. using Type = T;
  31. };
  32. // throw error while empty list.
  33. template <> struct TypeAtImpl<0, TypeList<> > {};
  34. template <size_t N, typename... List>
  35. using TypeAt = typename TypeAtImpl<N, TypeList<List...> >::Type;
  36. // index of a type in a given type list.
  37. template <typename T, typename List>
  38. struct IndexOfImpl;
  39. template <typename T>
  40. struct IndexOfImpl<T, TypeList<> > {
  41. enum { value = -1 };
  42. };
  43. template <typename T, typename... List>
  44. struct IndexOfImpl<T, TypeList<T, List...> > {
  45. enum { value = 0 };
  46. };
  47. template <typename T, typename U, typename... List>
  48. struct IndexOfImpl<T, TypeList<U, List...> > {
  49. private:
  50. enum { temp = IndexOfImpl<T, TypeList<List...> >::value };
  51. public:
  52. enum { value = ((temp == -1) ? -1 : (1 + temp)) };
  53. };
  54. template <typename T, typename List>
  55. using IndexOf = IndexOfImpl<T, List>;
  56. // drop first N types in a given type list.
  57. template <size_t N, typename List>
  58. struct DropTypeListItemImpl;
  59. template <size_t N, typename T, typename... List>
  60. struct DropTypeListItemImpl<N, TypeList<T, List...> >
  61. : DropTypeListItemImpl<N-1, TypeList<List...> > {};
  62. template <typename T, typename... List>
  63. struct DropTypeListItemImpl<0, TypeList<T, List...> > {
  64. using Type = TypeList<T, List...>;
  65. };
  66. template <> struct DropTypeListItemImpl<0, TypeList<> > {
  67. using Type = TypeList<>;
  68. };
  69. template <size_t N, typename List>
  70. using DropTypeListItem = typename DropTypeListItemImpl<N, List>::Type;
  71. // take first N types in a given type list.
  72. template <size_t N, typename List, typename... TList>
  73. struct TakeTypeListItemImpl;
  74. template <size_t N, typename T, typename... List, typename... TList>
  75. struct TakeTypeListItemImpl<N, TypeList<T, List...>, TList...>
  76. : TakeTypeListItemImpl<N-1, TypeList<List...>, TList..., T> {};
  77. template <typename T, typename... List, typename... TList>
  78. struct TakeTypeListItemImpl<0, TypeList<T, List...>, TList...> {
  79. using Type = TypeList<TList...>;
  80. };
  81. template <typename... TList>
  82. struct TakeTypeListItemImpl<0, TypeList<>, TList...> {
  83. using Type = TypeList<TList...>;
  84. };
  85. template <size_t N, typename List>
  86. using TakeTypeListItem = typename TakeTypeListItemImpl<N, List>::Type;
  87. // concat type lists.
  88. template <typename List1, typename List2>
  89. struct ConcatTypeListImpl;
  90. template <typename... ListA, typename... ListB>
  91. struct ConcatTypeListImpl<TypeList<ListA...>, TypeList<ListB...> > {
  92. using Type = TypeList<ListA..., ListB...>;
  93. };
  94. template <typename List1, typename List2>
  95. using ConcatTypeList = typename ConcatTypeListImpl<List1, List2>::Type;
  96. } // end namespace tl(type list)
  97. } // end namespace tars
  98. #endif