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

Side by Side Diff: chrome/browser/ui/webui/chromeos/login/base_screen_handler_utils.h

Issue 14208014: Simplify adding callbacks. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix, sync. Created 7 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2013 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 CHROME_BROWSER_UI_WEBUI_CHROMEOS_LOGIN_BASE_SCREEN_HANDLER_UTILS_H_
6 #define CHROME_BROWSER_UI_WEBUI_CHROMEOS_LOGIN_BASE_SCREEN_HANDLER_UTILS_H_
7
8 #include <cstddef>
9 #include <string>
10
11 #include "base/callback.h"
12 #include "base/logging.h"
13 #include "base/string16.h"
14 #include "base/values.h"
15
16 namespace chromeos {
17
18 template<typename T>
19 struct UnwrapConstRef {
20 typedef T Type;
21 };
22
23 template<typename T>
24 struct UnwrapConstRef<const T&> {
25 typedef T Type;
26 };
27
28 template<typename T>
29 inline bool GetArg(const base::ListValue* args, size_t index, T* out_value);
30
31 template<>
32 inline bool GetArg<bool>(const base::ListValue* args,
33 size_t index,
34 bool* out_value) {
35 return args->GetBoolean(index, out_value);
36 }
37
38 template<>
39 inline bool GetArg<int>(const base::ListValue* args,
40 size_t index,
41 int* out_value) {
42 return args->GetInteger(index, out_value);
43 }
44
45 template<>
46 inline bool GetArg<double>(const base::ListValue* args,
47 size_t index,
48 double* out_value) {
49 return args->GetDouble(index, out_value);
50 }
51
52 template<>
53 inline bool GetArg<std::string>(const base::ListValue* args,
54 size_t index,
55 std::string* out_value) {
56 return args->GetString(index, out_value);
57 }
58
59 template<>
60 inline bool GetArg<string16>(const base::ListValue* args,
61 size_t index,
62 string16* out_value) {
63 return args->GetString(index, out_value);
64 }
65
66 inline void CallbackWrapper0(base::Callback<void()> callback,
67 const base::ListValue* args) {
68 DCHECK(args);
69 DCHECK(args->empty());
70 callback.Run();
71 }
72
73 template<typename A1>
74 void CallbackWrapper1(base::Callback<void(A1)> callback,
75 const base::ListValue* args) {
76 DCHECK(args);
77 DCHECK_EQ(1u, args->GetSize());
78 typename UnwrapConstRef<A1>::Type arg1;
79 if (!GetArg(args, 0, &arg1)) {
80 NOTREACHED();
81 return;
82 }
83 callback.Run(arg1);
84 }
85
86 template<typename A1, typename A2>
87 void CallbackWrapper2(base::Callback<void(A1, A2)> callback,
88 const base::ListValue* args) {
89 DCHECK(args);
90 DCHECK_EQ(2u, args->GetSize());
91 typename UnwrapConstRef<A1>::Type arg1;
92 typename UnwrapConstRef<A2>::Type arg2;
93 if (!GetArg(args, 0, &arg1) || !GetArg(args, 1, &arg2)) {
94 NOTREACHED();
95 return;
96 }
97 callback.Run(arg1, arg2);
98 }
99
100 template<typename A1, typename A2, typename A3>
101 void CallbackWrapper3(base::Callback<void(A1, A2, A3)> callback,
102 const base::ListValue* args) {
103 DCHECK(args);
104 DCHECK_EQ(3u, args->GetSize());
105 typename UnwrapConstRef<A1>::Type arg1;
106 typename UnwrapConstRef<A2>::Type arg2;
107 typename UnwrapConstRef<A3>::Type arg3;
108 if (!GetArg(args, 0, &arg1) ||
109 !GetArg(args, 1, &arg2) ||
110 !GetArg(args, 2, &arg3)) {
111 NOTREACHED();
112 return;
113 }
114 callback.Run(arg1, arg2, arg3);
115 }
116
117 template<typename A1, typename A2, typename A3, typename A4>
118 void CallbackWrapper4(base::Callback<void(A1, A2, A3, A4)> callback,
119 const base::ListValue* args) {
120 DCHECK(args);
121 DCHECK_EQ(4u, args->GetSize());
122 typename UnwrapConstRef<A1>::Type arg1;
123 typename UnwrapConstRef<A2>::Type arg2;
124 typename UnwrapConstRef<A3>::Type arg3;
125 typename UnwrapConstRef<A4>::Type arg4;
126 if (!GetArg(args, 0, &arg1) ||
127 !GetArg(args, 1, &arg2) ||
128 !GetArg(args, 2, &arg3) ||
129 !GetArg(args, 3, &arg4)) {
130 NOTREACHED();
131 return;
132 }
133 callback.Run(arg1, arg2, arg3, arg4);
134 }
135
136 } // namespace chromeos
137
138 #endif // CHROME_BROWSER_UI_WEBUI_CHROMEOS_LOGIN_BASE_SCREEN_HANDLER_UTILS_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698