Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(376)

Side by Side Diff: base/task_scheduler/task_traits_details.h

Issue 2829083002: Add constexpr TaskTraits constructor. (Closed)
Patch Set: CR-etipdoray-gab-36-38 Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 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
5 #ifndef BASE_TASK_SCHEDULER_TASK_TRAITS_DETAILS_H_
6 #define BASE_TASK_SCHEDULER_TASK_TRAITS_DETAILS_H_
7
8 #include <type_traits>
9 #include <utility>
10
11 namespace base {
12 namespace internal {
13
14 // HasArgOfType<CheckedType, ArgTypes...>::value is true iff a type in ArgTypes
15 // matches CheckedType.
16 template <class...>
17 struct HasArgOfType : std::false_type {};
18 template <class CheckedType, class FirstArgType, class... ArgTypes>
19 struct HasArgOfType<CheckedType, FirstArgType, ArgTypes...>
20 : std::conditional<std::is_same<CheckedType, FirstArgType>::value,
21 std::true_type,
22 HasArgOfType<ArgTypes...>>::type {};
23
24 // ArgsAreConvertible<CheckedType, ArgTypes...>::value is true iff all types in
25 // ArgTypes are convertible to CheckedType.
26 template <class...>
27 struct ArgsAreConvertible : std::true_type {};
28 template <class CheckedType, class FirstArgType, class... ArgTypes>
29 struct ArgsAreConvertible<CheckedType, FirstArgType, ArgTypes...>
30 : std::conditional<std::is_convertible<FirstArgType, CheckedType>::value,
31 ArgsAreConvertible<CheckedType, ArgTypes...>,
32 std::false_type>::type {};
33
34 // When the following call is made:
35 // GetValueFromArgList(CallFirstTag(), GetterType(), args...);
36 // The compiler prefers using the first overload of GetValueFromArgList(),
37 // because the type of the first argument matches exactly. This overload returns
38 // getter.GetValueFromArg(first_arg), where |first_arg| is the first element in
39 // |args|. If getter.GetValueFromArg(first_arg) can't be called, the compiler
40 // uses the second or the third overload instead (depending on whether |args| is
41 // empty). The second overload discards |first_arg| and makes a recursive call
42 // to GetValueFromArgList() with CallFirstTag() as first argument. The third
43 // overload returns getter.GetDefaultValue(). GetValueFromArgList() generates a
44 // compile-time error if more than one of its arguments match the type of the
45 // argument of getter.GetValueFromArg().
46 //
47 // |getter| must provide:
48 //
49 // ValueType:
50 // The return type of GetValueFromArgList().
51 //
52 // ArgType:
53 // The type of the argument from which GetValueFromArgList() derives its
54 // return value.
55 //
56 // ValueType GetValueFromArg(ArgType):
57 // Converts an argument of type ArgType into a value returned by
58 // GetValueFromArgList().
59 //
60 // ValueType GetDefaultValue():
61 // Returns the value returned by GetValueFromArgList() if none of its
62 // arguments is of type ArgType.
63
64 struct CallSecondTag {};
65 struct CallFirstTag : CallSecondTag {};
robliao 2017/04/26 19:45:54 To make sure I understand this correctly, these ar
fdoray 2017/04/26 20:25:14 Done.
66
67 // Overload 1: Get value from first argument. Check that no argument in |args|
68 // has the same type as |first_arg|.
69 template <class GetterType,
70 class FirstArgType,
71 class... ArgTypes,
72 class TestGetValueFromArgDefined =
73 decltype(std::declval<GetterType>().GetValueFromArg(
74 std::declval<FirstArgType>()))>
75 constexpr typename GetterType::ValueType GetValueFromArgList(
76 CallFirstTag,
77 GetterType getter,
78 const FirstArgType& first_arg,
79 const ArgTypes&... args) {
80 static_assert(!HasArgOfType<FirstArgType, ArgTypes...>::value,
81 "Multiple arguments of the same type were provided to the "
82 "constructor of TaskTraits.");
83 return getter.GetValueFromArg(first_arg);
84 }
85
86 // Overload 2: Discard first argument.
87 template <class GetterType, class FirstArgType, class... ArgTypes>
88 constexpr typename GetterType::ValueType GetValueFromArgList(
89 CallSecondTag,
90 GetterType getter,
91 const FirstArgType&,
92 const ArgTypes&... args) {
93 return GetValueFromArgList(CallFirstTag(), getter, args...);
94 }
95
96 // Overload 3: Default value.
97 template <class GetterType>
98 constexpr typename GetterType::ValueType GetValueFromArgList(
99 CallSecondTag,
100 GetterType getter) {
101 return getter.GetDefaultValue();
102 }
103
104 template <typename ArgType>
105 struct BooleanArgGetter {
106 using ValueType = bool;
107 constexpr ValueType GetValueFromArg(ArgType) const { return true; }
108 constexpr ValueType GetDefaultValue() const { return false; }
109 };
110
111 template <typename ArgType, ArgType DefaultValue>
112 struct EnumArgGetter {
113 using ValueType = ArgType;
114 constexpr ValueType GetValueFromArg(ArgType arg) const { return arg; }
115 constexpr ValueType GetDefaultValue() const { return DefaultValue; }
116 };
117
118 } // namespace internal
119 } // namespace base
120
121 #endif // BASE_TASK_SCHEDULER_TASK_TRAITS_DETAILS_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698