Commit 12d80f9e authored by vkuzkokov's avatar vkuzkokov Committed by Commit bot

[DevTools] DevToolsEmbedderMessageDispatcher: variadic implementation of RegisterHandler

BUG=

Review URL: https://codereview.chromium.org/887453002

Cr-Commit-Position: refs/heads/master@{#313750}
parent 5da90daf
...@@ -9,21 +9,21 @@ ...@@ -9,21 +9,21 @@
namespace { namespace {
bool GetValue(const base::ListValue& list, int pos, std::string& value) { bool GetValue(const base::Value* value, std::string* result) {
return list.GetString(pos, &value); return value->GetAsString(result);
} }
bool GetValue(const base::ListValue& list, int pos, int& value) { bool GetValue(const base::Value* value, int* result) {
return list.GetInteger(pos, &value); return value->GetAsInteger(result);
} }
bool GetValue(const base::ListValue& list, int pos, bool& value) { bool GetValue(const base::Value* value, bool* result) {
return list.GetBoolean(pos, &value); return value->GetAsBoolean(result);
} }
bool GetValue(const base::ListValue& list, int pos, gfx::Rect& rect) { bool GetValue(const base::Value* value, gfx::Rect* rect) {
const base::DictionaryValue* dict; const base::DictionaryValue* dict;
if (!list.GetDictionary(pos, &dict)) if (!value->GetAsDictionary(&dict))
return false; return false;
int x = 0; int x = 0;
int y = 0; int y = 0;
...@@ -34,108 +34,60 @@ bool GetValue(const base::ListValue& list, int pos, gfx::Rect& rect) { ...@@ -34,108 +34,60 @@ bool GetValue(const base::ListValue& list, int pos, gfx::Rect& rect) {
!dict->GetInteger("width", &width) || !dict->GetInteger("width", &width) ||
!dict->GetInteger("height", &height)) !dict->GetInteger("height", &height))
return false; return false;
rect.SetRect(x, y, width, height); rect->SetRect(x, y, width, height);
return true; return true;
} }
template <typename T> template <typename T>
struct StorageTraits { struct StorageTraits {
typedef T StorageType; using StorageType = T;
}; };
template <typename T> template <typename T>
struct StorageTraits<const T&> { struct StorageTraits<const T&> {
typedef T StorageType; using StorageType = T;
}; };
template <class A> template <typename... Ts>
class Argument { struct ParamTuple {
public: bool Parse(const base::ListValue& list,
typedef typename StorageTraits<A>::StorageType ValueType; const base::ListValue::const_iterator& it) {
return it == list.end();
Argument(const base::ListValue& list, int pos) {
valid_ = GetValue(list, pos, value_);
} }
ValueType value() const { return value_; } template <typename H, typename... As>
bool valid() const { return valid_; } void Apply(const H& handler, As... args) {
handler.Run(args...);
private: }
ValueType value_;
bool valid_;
}; };
bool ParseAndHandle0(const base::Callback<void(void)>& handler, template <typename T, typename... Ts>
const base::ListValue& list) { struct ParamTuple<T, Ts...> {
if (list.GetSize() != 0) bool Parse(const base::ListValue& list,
return false; const base::ListValue::const_iterator& it) {
handler.Run(); if (it == list.end())
return true;
}
template <class A1>
bool ParseAndHandle1(const base::Callback<void(A1)>& handler,
const base::ListValue& list) {
if (list.GetSize() != 1)
return false; return false;
Argument<A1> arg1(list, 0); if (!GetValue(*it, &head))
if (!arg1.valid())
return false; return false;
handler.Run(arg1.value()); return tail.Parse(list, it + 1);
return true; }
}
template <class A1, class A2> template <typename H, typename... As>
bool ParseAndHandle2(const base::Callback<void(A1, A2)>& handler, void Apply(const H& handler, As... args) {
const base::ListValue& list) { tail.template Apply<H, As..., T>(handler, args..., head);
if (list.GetSize() != 2) }
return false;
Argument<A1> arg1(list, 0);
if (!arg1.valid())
return false;
Argument<A2> arg2(list, 1);
if (!arg2.valid())
return false;
handler.Run(arg1.value(), arg2.value());
return true;
}
template <class A1, class A2, class A3> typename StorageTraits<T>::StorageType head;
bool ParseAndHandle3(const base::Callback<void(A1, A2, A3)>& handler, ParamTuple<Ts...> tail;
const base::ListValue& list) { };
if (list.GetSize() != 3)
return false;
Argument<A1> arg1(list, 0);
if (!arg1.valid())
return false;
Argument<A2> arg2(list, 1);
if (!arg2.valid())
return false;
Argument<A3> arg3(list, 2);
if (!arg3.valid())
return false;
handler.Run(arg1.value(), arg2.value(), arg3.value());
return true;
}
template <class A1, class A2, class A3, class A4> template<typename... As>
bool ParseAndHandle4(const base::Callback<void(A1, A2, A3, A4)>& handler, bool ParseAndHandle(const base::Callback<void(As...)>& handler,
const base::ListValue& list) { const base::ListValue& list) {
if (list.GetSize() != 4) ParamTuple<As...> tuple;
return false; if (!tuple.Parse(list, list.begin()))
Argument<A1> arg1(list, 0);
if (!arg1.valid())
return false;
Argument<A2> arg2(list, 1);
if (!arg2.valid())
return false; return false;
Argument<A3> arg3(list, 2); tuple.Apply(handler);
if (!arg3.valid())
return false;
Argument<A4> arg4(list, 3);
if (!arg4.valid())
return false;
handler.Run(arg1.value(), arg2.value(), arg3.value(), arg4.value());
return true; return true;
} }
...@@ -173,42 +125,10 @@ class DispatcherImpl : public DevToolsEmbedderMessageDispatcher { ...@@ -173,42 +125,10 @@ class DispatcherImpl : public DevToolsEmbedderMessageDispatcher {
handlers_[method] = handler; handlers_[method] = handler;
} }
template<class T> template<typename T, typename... As>
void RegisterHandler(const std::string& method,
void (T::*handler)(), T* delegate) {
handlers_[method] = base::Bind(&ParseAndHandle0,
base::Bind(handler,
base::Unretained(delegate)));
}
template<class T, class A1>
void RegisterHandler(const std::string& method,
void (T::*handler)(A1), T* delegate) {
handlers_[method] = base::Bind(ParseAndHandle1<A1>,
base::Bind(handler,
base::Unretained(delegate)));
}
template<class T, class A1, class A2>
void RegisterHandler(const std::string& method,
void (T::*handler)(A1, A2), T* delegate) {
handlers_[method] = base::Bind(ParseAndHandle2<A1, A2>,
base::Bind(handler,
base::Unretained(delegate)));
}
template<class T, class A1, class A2, class A3>
void RegisterHandler(const std::string& method,
void (T::*handler)(A1, A2, A3), T* delegate) {
handlers_[method] = base::Bind(ParseAndHandle3<A1, A2, A3>,
base::Bind(handler,
base::Unretained(delegate)));
}
template<class T, class A1, class A2, class A3, class A4>
void RegisterHandler(const std::string& method, void RegisterHandler(const std::string& method,
void (T::*handler)(A1, A2, A3, A4), T* delegate) { void (T::*handler)(As...), T* delegate) {
handlers_[method] = base::Bind(ParseAndHandle4<A1, A2, A3, A4>, handlers_[method] = base::Bind(&ParseAndHandle<As...>,
base::Bind(handler, base::Bind(handler,
base::Unretained(delegate))); base::Unretained(delegate)));
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment