traits_bag.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_TRAITS_BAG_H_
  5. #define BASE_TRAITS_BAG_H_
  6. #include <initializer_list>
  7. #include <tuple>
  8. #include <type_traits>
  9. #include <utility>
  10. #include "base/optional.h"
  11. #include "base/parameter_pack.h"
  12. #include "base/template_util.h"
  13. // A bag of Traits (structs / enums / etc...) can be an elegant alternative to
  14. // the builder pattern and multiple default arguments for configuring things.
  15. // Traits are terser than the builder pattern and can be evaluated at compile
  16. // time, however they require the use of variadic templates which complicates
  17. // matters. This file contains helpers that make Traits easier to use.
  18. //
  19. // WARNING: Trait bags are currently too heavy for non-constexpr usage in prod
  20. // code due to template bloat, although adding NOINLINE to template constructors
  21. // configured via trait bags can help.
  22. //
  23. // E.g.
  24. // struct EnableFeatureX {};
  25. // struct UnusedTrait {};
  26. // enum Color { RED, BLUE };
  27. //
  28. // struct ValidTraits {
  29. // ValidTraits(EnableFeatureX);
  30. // ValidTraits(Color);
  31. // };
  32. // ...
  33. // DoSomethingAwesome(); // Use defaults (Color::BLUE &
  34. // // feature X not enabled)
  35. // DoSomethingAwesome(EnableFeatureX(), // Turn feature X on
  36. // Color::RED); // And make it red.
  37. // DoSomethingAwesome(UnusedTrait(), // Compile time error.
  38. // Color::RED);
  39. //
  40. // DoSomethingAwesome might be defined as:
  41. //
  42. // template <class... ArgTypes,
  43. // class CheckArgumentsAreValid = std::enable_if_t<
  44. // trait_helpers::AreValidTraits<ValidTraits,
  45. // ArgTypes...>::value>>
  46. // constexpr void DoSomethingAwesome(ArgTypes... args)
  47. // : enable_feature_x(
  48. // trait_helpers::HasTrait<EnableFeatureX, ArgTypes...>()),
  49. // color(trait_helpers::GetEnum<Color, EnumTraitA::BLUE>(args...)) {}
  50. namespace base {
  51. namespace trait_helpers {
  52. // Represents a trait that has been removed by a predicate.
  53. struct EmptyTrait {};
  54. // Predicate used to remove any traits from the given list of types by
  55. // converting them to EmptyTrait. E.g.
  56. //
  57. // template <typename... Args>
  58. // void MyFunc(Args... args) {
  59. // DoSomethingWithTraits(
  60. // base::trait_helpers::Exclude<UnwantedTrait1,
  61. // UnwantedTrait2>::Filter(args)...);
  62. // }
  63. //
  64. // NB It's possible to actually remove the unwanted trait from the pack, but
  65. // that requires constructing a filtered tuple and applying it to the function,
  66. // which isn't worth the complexity over ignoring EmptyTrait.
  67. template <typename... TraitsToExclude>
  68. struct Exclude {
  69. template <typename T,
  70. std::enable_if_t<ParameterPack<
  71. TraitsToExclude...>::template HasType<T>::value>* = nullptr>
  72. static constexpr EmptyTrait Filter(T t) {
  73. return EmptyTrait();
  74. }
  75. template <typename T,
  76. std::enable_if_t<!ParameterPack<
  77. TraitsToExclude...>::template HasType<T>::value>* = nullptr>
  78. static constexpr T Filter(T t) {
  79. return t;
  80. }
  81. };
  82. // CallFirstTag is an argument tag that helps to avoid ambiguous overloaded
  83. // functions. When the following call is made:
  84. // func(CallFirstTag(), arg...);
  85. // the compiler will give precedence to an overload candidate that directly
  86. // takes CallFirstTag. Another overload that takes CallSecondTag will be
  87. // considered iff the preferred overload candidates were all invalids and
  88. // therefore discarded.
  89. struct CallSecondTag {};
  90. struct CallFirstTag : CallSecondTag {};
  91. // A trait filter class |TraitFilterType| implements the protocol to get a value
  92. // of type |ArgType| from an argument list and convert it to a value of type
  93. // |TraitType|. If the argument list contains an argument of type |ArgType|, the
  94. // filter class will be instantiated with that argument. If the argument list
  95. // contains no argument of type |ArgType|, the filter class will be instantiated
  96. // using the default constructor if available; a compile error is issued
  97. // otherwise. The filter class must have the conversion operator TraitType()
  98. // which returns a value of type TraitType.
  99. // |InvalidTrait| is used to return from GetTraitFromArg when the argument is
  100. // not compatible with the desired trait.
  101. struct InvalidTrait {};
  102. // Returns an object of type |TraitFilterType| constructed from |arg| if
  103. // compatible, or |InvalidTrait| otherwise.
  104. template <class TraitFilterType,
  105. class ArgType,
  106. class CheckArgumentIsCompatible = std::enable_if_t<
  107. std::is_constructible<TraitFilterType, ArgType>::value>>
  108. constexpr TraitFilterType GetTraitFromArg(CallFirstTag, ArgType arg) {
  109. return TraitFilterType(arg);
  110. }
  111. template <class TraitFilterType, class ArgType>
  112. constexpr InvalidTrait GetTraitFromArg(CallSecondTag, ArgType arg) {
  113. return InvalidTrait();
  114. }
  115. // Returns an object of type |TraitFilterType| constructed from a compatible
  116. // argument in |args...|, or default constructed if none of the arguments are
  117. // compatible. This is the implementation of GetTraitFromArgList() with a
  118. // disambiguation tag.
  119. template <class TraitFilterType,
  120. class... ArgTypes,
  121. class TestCompatibleArgument = std::enable_if_t<any_of(
  122. {std::is_constructible<TraitFilterType, ArgTypes>::value...})>>
  123. constexpr TraitFilterType GetTraitFromArgListImpl(CallFirstTag,
  124. ArgTypes... args) {
  125. return std::get<TraitFilterType>(std::make_tuple(
  126. GetTraitFromArg<TraitFilterType>(CallFirstTag(), args)...));
  127. }
  128. template <class TraitFilterType, class... ArgTypes>
  129. constexpr TraitFilterType GetTraitFromArgListImpl(CallSecondTag,
  130. ArgTypes... args) {
  131. static_assert(std::is_constructible<TraitFilterType>::value,
  132. "The traits bag is missing a required trait.");
  133. return TraitFilterType();
  134. }
  135. // Constructs an object of type |TraitFilterType| from a compatible argument in
  136. // |args...|, or using the default constructor, and returns its associated trait
  137. // value using conversion to |TraitFilterType::ValueType|. If there are more
  138. // than one compatible argument in |args|, generates a compile-time error.
  139. template <class TraitFilterType, class... ArgTypes>
  140. constexpr typename TraitFilterType::ValueType GetTraitFromArgList(
  141. ArgTypes... args) {
  142. static_assert(
  143. count({std::is_constructible<TraitFilterType, ArgTypes>::value...},
  144. true) <= 1,
  145. "The traits bag contains multiple traits of the same type.");
  146. return GetTraitFromArgListImpl<TraitFilterType>(CallFirstTag(), args...);
  147. }
  148. // Helper class to implemnent a |TraitFilterType|.
  149. template <typename T, typename _ValueType = T>
  150. struct BasicTraitFilter {
  151. using ValueType = _ValueType;
  152. constexpr BasicTraitFilter(ValueType v) : value(v) {}
  153. constexpr operator ValueType() const { return value; }
  154. ValueType value = {};
  155. };
  156. template <typename ArgType, ArgType DefaultValue>
  157. struct EnumTraitFilter : public BasicTraitFilter<ArgType> {
  158. constexpr EnumTraitFilter() : BasicTraitFilter<ArgType>(DefaultValue) {}
  159. constexpr EnumTraitFilter(ArgType arg) : BasicTraitFilter<ArgType>(arg) {}
  160. };
  161. template <typename ArgType>
  162. struct OptionalEnumTraitFilter
  163. : public BasicTraitFilter<ArgType, Optional<ArgType>> {
  164. constexpr OptionalEnumTraitFilter()
  165. : BasicTraitFilter<ArgType, Optional<ArgType>>(nullopt) {}
  166. constexpr OptionalEnumTraitFilter(ArgType arg)
  167. : BasicTraitFilter<ArgType, Optional<ArgType>>(arg) {}
  168. };
  169. // Tests whether multiple given argtument types are all valid traits according
  170. // to the provided ValidTraits. To use, define a ValidTraits
  171. template <typename ArgType>
  172. struct RequiredEnumTraitFilter : public BasicTraitFilter<ArgType> {
  173. constexpr RequiredEnumTraitFilter(ArgType arg)
  174. : BasicTraitFilter<ArgType>(arg) {}
  175. };
  176. // Note EmptyTrait is always regarded as valid to support filtering.
  177. template <class ValidTraits, class T>
  178. using IsValidTrait = disjunction<std::is_constructible<ValidTraits, T>,
  179. std::is_same<T, EmptyTrait>>;
  180. // Tests whether a given trait type is valid or invalid by testing whether it is
  181. // convertible to the provided ValidTraits type. To use, define a ValidTraits
  182. // type like this:
  183. //
  184. // struct ValidTraits {
  185. // ValidTraits(MyTrait);
  186. // ...
  187. // };
  188. //
  189. // You can 'inherit' valid traits like so:
  190. //
  191. // struct MoreValidTraits {
  192. // MoreValidTraits(ValidTraits); // Pull in traits from ValidTraits.
  193. // MoreValidTraits(MyOtherTrait);
  194. // ...
  195. // };
  196. template <class ValidTraits, class... ArgTypes>
  197. using AreValidTraits =
  198. bool_constant<all_of({IsValidTrait<ValidTraits, ArgTypes>::value...})>;
  199. // Helper to make getting an enum from a trait more readable.
  200. template <typename Enum, typename... Args>
  201. static constexpr Enum GetEnum(Args... args) {
  202. return GetTraitFromArgList<RequiredEnumTraitFilter<Enum>>(args...);
  203. }
  204. // Helper to make getting an enum from a trait with a default more readable.
  205. template <typename Enum, Enum DefaultValue, typename... Args>
  206. static constexpr Enum GetEnum(Args... args) {
  207. return GetTraitFromArgList<EnumTraitFilter<Enum, DefaultValue>>(args...);
  208. }
  209. // Helper to make getting an optional enum from a trait with a default more
  210. // readable.
  211. template <typename Enum, typename... Args>
  212. static constexpr Optional<Enum> GetOptionalEnum(Args... args) {
  213. return GetTraitFromArgList<OptionalEnumTraitFilter<Enum>>(args...);
  214. }
  215. // Helper to make checking for the presence of a trait more readable.
  216. template <typename Trait, typename... Args>
  217. struct HasTrait : ParameterPack<Args...>::template HasType<Trait> {
  218. static_assert(
  219. count({std::is_constructible<Trait, Args>::value...}, true) <= 1,
  220. "The traits bag contains multiple traits of the same type.");
  221. };
  222. // If you need a template vararg constructor to delegate to a private
  223. // constructor, you may need to add this to the private constructor to ensure
  224. // it's not matched by accident.
  225. struct NotATraitTag {};
  226. } // namespace trait_helpers
  227. } // namespace base
  228. #endif // BASE_TRAITS_BAG_H_