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

Unified Diff: components/login/base_screen_handler_utils.h

Issue 1038003002: Use variadic template for screen handler's AddCallback. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 9 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 side-by-side diff with in-line comments
Download patch
Index: components/login/base_screen_handler_utils.h
diff --git a/components/login/base_screen_handler_utils.h b/components/login/base_screen_handler_utils.h
index c2b89719d6ddc50e792c5107edeb698a0e70b4c0..55497a0d74dce195fc2aef46e98862ef115c56a8 100644
--- a/components/login/base_screen_handler_utils.h
+++ b/components/login/base_screen_handler_utils.h
@@ -11,6 +11,7 @@
#include "base/callback.h"
#include "base/logging.h"
#include "base/strings/string16.h"
+#include "base/tuple.h"
#include "base/values.h"
#include "components/login/login_export.h"
@@ -59,87 +60,29 @@ inline const T& MakeValue(const T& v) {
return v;
}
-void LOGIN_EXPORT CallbackWrapper0(base::Callback<void()> callback,
- const base::ListValue* args);
-
-template <typename A1>
-void CallbackWrapper1(base::Callback<void(A1)> callback,
- const base::ListValue* args) {
- DCHECK(args);
- DCHECK_EQ(1u, args->GetSize());
- typename UnwrapConstRef<A1>::Type arg1;
- if (!GetArg(args, 0, &arg1)) {
- NOTREACHED();
- return;
- }
- callback.Run(arg1);
+template <typename Arg, size_t index>
+typename UnwrapConstRef<Arg>::Type ParseArg(const base::ListValue* args) {
+ typename UnwrapConstRef<Arg>::Type parsed;
+ CHECK(GetArg(args, index, &parsed));
+ return parsed;
}
-template <typename A1, typename A2>
-void CallbackWrapper2(base::Callback<void(A1, A2)> callback,
- const base::ListValue* args) {
+template <typename... Args, size_t... Ns>
+inline void DispatchToCallback(const base::Callback<void(Args...)>& callback,
+ const base::ListValue* args,
+ IndexSequence<Ns...> indexes) {
DCHECK(args);
- DCHECK_EQ(2u, args->GetSize());
- typename UnwrapConstRef<A1>::Type arg1;
- typename UnwrapConstRef<A2>::Type arg2;
- if (!GetArg(args, 0, &arg1) || !GetArg(args, 1, &arg2)) {
- NOTREACHED();
- return;
- }
- callback.Run(arg1, arg2);
-}
+ DCHECK_EQ(sizeof...(Args), args->GetSize());
-template <typename A1, typename A2, typename A3>
-void CallbackWrapper3(base::Callback<void(A1, A2, A3)> callback,
- const base::ListValue* args) {
- DCHECK(args);
- DCHECK_EQ(3u, args->GetSize());
- typename UnwrapConstRef<A1>::Type arg1;
- typename UnwrapConstRef<A2>::Type arg2;
- typename UnwrapConstRef<A3>::Type arg3;
- if (!GetArg(args, 0, &arg1) || !GetArg(args, 1, &arg2) ||
- !GetArg(args, 2, &arg3)) {
- NOTREACHED();
- return;
- }
- callback.Run(arg1, arg2, arg3);
+ callback.Run(ParseArg<Args, Ns>(args)...);
}
-template <typename A1, typename A2, typename A3, typename A4>
-void CallbackWrapper4(base::Callback<void(A1, A2, A3, A4)> callback,
- const base::ListValue* args) {
- DCHECK(args);
- DCHECK_EQ(4u, args->GetSize());
- typename UnwrapConstRef<A1>::Type arg1;
- typename UnwrapConstRef<A2>::Type arg2;
- typename UnwrapConstRef<A3>::Type arg3;
- typename UnwrapConstRef<A4>::Type arg4;
- if (!GetArg(args, 0, &arg1) || !GetArg(args, 1, &arg2) ||
- !GetArg(args, 2, &arg3) || !GetArg(args, 3, &arg4)) {
- NOTREACHED();
- return;
- }
- callback.Run(arg1, arg2, arg3, arg4);
+template <typename... Args>
+void CallbackWrapper(const base::Callback<void(Args...)>& callback,
+ const base::ListValue* args) {
+ DispatchToCallback(callback, args, MakeIndexSequence<sizeof...(Args)>());
}
-template <typename A1, typename A2, typename A3, typename A4, typename A5>
-void CallbackWrapper5(base::Callback<void(A1, A2, A3, A4, A5)> callback,
- const base::ListValue* args) {
- DCHECK(args);
- DCHECK_EQ(5u, args->GetSize());
- typename UnwrapConstRef<A1>::Type arg1;
- typename UnwrapConstRef<A2>::Type arg2;
- typename UnwrapConstRef<A3>::Type arg3;
- typename UnwrapConstRef<A4>::Type arg4;
- typename UnwrapConstRef<A5>::Type arg5;
- if (!GetArg(args, 0, &arg1) || !GetArg(args, 1, &arg2) ||
- !GetArg(args, 2, &arg3) || !GetArg(args, 3, &arg4) ||
- !GetArg(args, 4, &arg5)) {
- NOTREACHED();
- return;
- }
- callback.Run(arg1, arg2, arg3, arg4, arg5);
-}
} // namespace login
« no previous file with comments | « chrome/browser/ui/webui/chromeos/login/base_screen_handler.h ('k') | components/login/base_screen_handler_utils.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698