Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 #include "base/bind.h" | |
| 6 #include "base/callback.h" | |
| 7 #include "base/values.h" | |
| 8 | |
| 9 #ifndef CHROME_BROWSER_DEVTOOLS_LIST_VALUE_PARSER_H_ | |
| 10 #define CHROME_BROWSER_DEVTOOLS_LIST_VALUE_PARSER_H_ | |
| 11 | |
| 12 namespace internal { | |
|
pfeldman
2013/08/23 11:29:43
Use anonymous namespace
Vladislav Kaznacheev
2013/08/23 13:44:17
Done.
| |
| 13 | |
| 14 bool GetValue(const base::ListValue& list, int pos, std::string& value) { | |
| 15 return list.GetString(pos, &value); | |
| 16 } | |
| 17 | |
| 18 bool GetValue(const base::ListValue& list, int pos, int& value) { | |
| 19 return list.GetInteger(pos, &value); | |
| 20 } | |
| 21 | |
| 22 bool GetValue(const base::ListValue& list, int pos, bool& value) { | |
| 23 return list.GetBoolean(pos, &value); | |
| 24 } | |
| 25 | |
| 26 template <typename T> | |
| 27 struct StorageTraits { | |
| 28 typedef T StorageType; | |
| 29 }; | |
| 30 | |
| 31 template <typename T> | |
| 32 struct StorageTraits<const T&> { | |
| 33 typedef T StorageType; | |
| 34 }; | |
| 35 | |
| 36 template <class A> | |
| 37 class Argument { | |
| 38 public: | |
| 39 typedef typename StorageTraits<A>::StorageType ValueType; | |
| 40 | |
| 41 Argument(const base::ListValue& list, int pos) { | |
| 42 valid_ = GetValue(list, pos, value_); | |
| 43 } | |
| 44 | |
| 45 ValueType value() const { return value_; } | |
| 46 bool valid() const { return valid_; } | |
| 47 | |
| 48 private: | |
| 49 ValueType value_; | |
| 50 bool valid_; | |
| 51 }; | |
| 52 | |
| 53 bool ParseAndHandle0(const base::Callback<void(void)>& handler, | |
| 54 const base::ListValue& list) { | |
| 55 handler.Run(); | |
| 56 return true; | |
| 57 } | |
| 58 | |
| 59 template <class A1> | |
| 60 bool ParseAndHandle1(const base::Callback<void(A1)>& handler, | |
| 61 const base::ListValue& list) { | |
| 62 if (list.GetSize() != 1) | |
| 63 return false; | |
| 64 Argument<A1> arg1(list, 0); | |
| 65 if (!arg1.valid()) | |
| 66 return false; | |
| 67 handler.Run(arg1.value()); | |
| 68 return true; | |
| 69 } | |
| 70 | |
| 71 template <class A1, class A2> | |
| 72 bool ParseAndHandle2(const base::Callback<void(A1, A2)>& handler, | |
| 73 const base::ListValue& list) { | |
| 74 if (list.GetSize() != 2) | |
| 75 return false; | |
| 76 Argument<A1> arg1(list, 0); | |
| 77 if (!arg1.valid()) | |
| 78 return false; | |
| 79 Argument<A2> arg2(list, 1); | |
| 80 if (!arg2.valid()) | |
| 81 return false; | |
| 82 handler.Run(arg1.value(), arg2.value()); | |
| 83 return true; | |
| 84 } | |
| 85 | |
| 86 template <class A1, class A2, class A3> | |
| 87 bool ParseAndHandle3(const base::Callback<void(A1, A2, A3)>& handler, | |
| 88 const base::ListValue& list) { | |
| 89 if (list.GetSize() != 3) | |
| 90 return false; | |
| 91 Argument<A1> arg1(list, 0); | |
| 92 if (!arg1.valid()) | |
| 93 return false; | |
| 94 Argument<A2> arg2(list, 1); | |
| 95 if (!arg2.valid()) | |
| 96 return false; | |
| 97 Argument<A3> arg3(list, 2); | |
| 98 if (!arg3.valid()) | |
| 99 return false; | |
| 100 handler.Run(arg1.value(), arg2.value(), arg3.value()); | |
| 101 return true; | |
| 102 } | |
| 103 | |
| 104 } // namespace internal | |
| 105 | |
| 106 typedef base::Callback<bool(const base::ListValue&)> ListValueParser; | |
| 107 | |
| 108 ListValueParser BindToListParser(const base::Callback<void()>& handler) { | |
| 109 return base::Bind(&internal::ParseAndHandle0, handler); | |
| 110 } | |
| 111 | |
| 112 template <class A1> | |
| 113 ListValueParser BindToListParser(const base::Callback<void(A1)>& handler) { | |
| 114 return base::Bind(&internal::ParseAndHandle1<A1>, handler); | |
| 115 } | |
| 116 | |
| 117 template <class A1, class A2> | |
| 118 ListValueParser BindToListParser(const base::Callback<void(A1,A2)>& handler) { | |
| 119 return base::Bind(&internal::ParseAndHandle2<A1, A2>, handler); | |
| 120 } | |
| 121 | |
| 122 template <class A1, class A2, class A3> | |
| 123 ListValueParser BindToListParser( | |
| 124 const base::Callback<void(A1,A2,A3)>& handler) { | |
| 125 return base::Bind(&internal::ParseAndHandle3<A1, A2, A3>, handler); | |
| 126 } | |
| 127 | |
| 128 #endif // CHROME_BROWSER_DEVTOOLS_LIST_VALUE_PARSER_H_ | |
| OLD | NEW |