OLD | NEW |
1 // Copyright (C) 2013 Google Inc. | 1 // Copyright (C) 2013 Google Inc. |
2 // | 2 // |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
6 // | 6 // |
7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
8 // | 8 // |
9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
13 // limitations under the License. | 13 // limitations under the License. |
14 | 14 |
15 #ifndef I18N_ADDRESSINPUT_CALLBACK_H_ | 15 #ifndef I18N_ADDRESSINPUT_CALLBACK_H_ |
16 #define I18N_ADDRESSINPUT_CALLBACK_H_ | 16 #define I18N_ADDRESSINPUT_CALLBACK_H_ |
17 | 17 |
18 #include <libaddressinput/util/scoped_ptr.h> | 18 #include <libaddressinput/util/scoped_ptr.h> |
19 | 19 |
20 #include <cassert> | 20 #include <cassert> |
21 #include <cstddef> | 21 #include <cstddef> |
| 22 #include <string> |
22 | 23 |
23 namespace i18n { | 24 namespace i18n { |
24 namespace addressinput { | 25 namespace addressinput { |
25 | 26 |
26 // Stores a pointer to a method in an object. Sample usage: | 27 // Stores a pointer to a method in an object. Sample usage: |
27 // class MyClass { | 28 // class MyClass { |
28 // public: | 29 // public: |
29 // typedef Callback<MyKeyType, MyDataType> MyCallback; | 30 // typedef Callback<std::string, std::string> MyConstRefCallback; |
| 31 // typedef ScopdedPtrCallback<std::string, MyDataType> MyScopedPtrCallback; |
30 // | 32 // |
31 // void GetDataAsynchronously() { | 33 // void GetStringAsynchronously() { |
32 // scoped_ptr<MyCallback> callback(BuildCallback( | 34 // scoped_ptr<MyCallback> callback(BuildCallback( |
33 // this, &MyClass::OnDataReady)); | 35 // this, &MyClass::OnStringReady)); |
34 // bool success = ... | 36 // bool success = ... |
35 // MyKeyType key = ... | 37 // std::string key = ... |
36 // MyDataType data = ... | 38 // std::string data = ... |
37 // (*callback)(success, key, data); | 39 // (*callback)(success, key, data); |
38 // } | 40 // } |
39 // | 41 // |
| 42 // void GetDataAsynchronously() { |
| 43 // scoped_ptr<MyScopedPtrCallback> callback(BuildScopedPtrCallback( |
| 44 // this, &MyClass::OnDataReady)); |
| 45 // bool success = ... |
| 46 // std::string key = ... |
| 47 // scoped_ptr<MyDataType> data = ... |
| 48 // (*callback)(success, key, data.Pass()); |
| 49 // } |
| 50 // |
| 51 // void OnStringReady(bool success, |
| 52 // const std::string& key, |
| 53 // const std::string& data) { |
| 54 // ... |
| 55 // } |
| 56 // |
40 // void OnDataReady(bool success, | 57 // void OnDataReady(bool success, |
41 // const MyKeyType& key, | 58 // const std::string& key, |
42 // const MyDataType& data) { | 59 // scoped_ptr<MyDataType> data) { |
43 // ... | 60 // ... |
44 // } | 61 // } |
45 // }; | 62 // }; |
46 template <typename Param1, typename Param2> | 63 template <typename RequestType, typename ResponseType> |
47 class Callback { | 64 class Callback { |
48 public: | 65 public: |
49 virtual ~Callback() {} | 66 virtual ~Callback() {} |
50 | 67 |
51 virtual void operator()(bool success, | 68 virtual void operator()(bool success, |
52 const Param1& param1, | 69 const RequestType& request, |
53 const Param2& param2) const = 0; | 70 const ResponseType& response) const = 0; |
| 71 }; |
| 72 |
| 73 template <typename RequestType, typename ResponseType> |
| 74 class ScopedPtrCallback { |
| 75 public: |
| 76 virtual ~ScopedPtrCallback() {} |
| 77 |
| 78 virtual void operator()(bool success, |
| 79 const RequestType& request, |
| 80 scoped_ptr<ResponseType> response) const = 0; |
54 }; | 81 }; |
55 | 82 |
56 namespace { | 83 namespace { |
57 | 84 |
58 template <typename BaseType, typename Param1, typename Param2> | 85 template <typename BaseType, typename RequestType, typename ResponseType> |
59 class CallbackImpl : public Callback<Param1, Param2> { | 86 class CallbackImpl : public Callback<RequestType, ResponseType> { |
60 public: | 87 public: |
61 typedef void (BaseType::*Method)(bool, const Param1&, const Param2&); | 88 typedef void (BaseType::*Method)( |
| 89 bool, const RequestType&, const ResponseType&); |
62 | 90 |
63 CallbackImpl(BaseType* instance, Method method) | 91 CallbackImpl(BaseType* instance, Method method) |
64 : instance_(instance), | 92 : instance_(instance), |
65 method_(method) { | 93 method_(method) { |
66 assert(instance_ != NULL); | 94 assert(instance_ != NULL); |
67 assert(method_ != NULL); | 95 assert(method_ != NULL); |
68 } | 96 } |
69 | 97 |
70 virtual ~CallbackImpl() {} | 98 virtual ~CallbackImpl() {} |
71 | 99 |
| 100 // Callback implementation. |
72 virtual void operator()(bool success, | 101 virtual void operator()(bool success, |
73 const Param1& param1, | 102 const RequestType& request, |
74 const Param2& param2) const { | 103 const ResponseType& response) const { |
75 (instance_->*method_)(success, param1, param2); | 104 (instance_->*method_)(success, request, response); |
76 } | 105 } |
77 | 106 |
78 private: | 107 private: |
| 108 BaseType* instance_; |
| 109 Method method_; |
| 110 }; |
| 111 |
| 112 template <typename BaseType, typename RequestType, typename ResponseType> |
| 113 class ScopedPtrCallbackImpl : |
| 114 public ScopedPtrCallback<RequestType, ResponseType> { |
| 115 public: |
| 116 typedef void (BaseType::*Method)( |
| 117 bool, const RequestType&, scoped_ptr<ResponseType>); |
| 118 |
| 119 ScopedPtrCallbackImpl(BaseType* instance, Method method) |
| 120 : instance_(instance), |
| 121 method_(method) { |
| 122 assert(instance_ != NULL); |
| 123 assert(method_ != NULL); |
| 124 } |
| 125 |
| 126 virtual ~ScopedPtrCallbackImpl() {} |
| 127 |
| 128 // ScopedPtrCallback implementation. |
| 129 virtual void operator()(bool success, |
| 130 const RequestType& request, |
| 131 scoped_ptr<ResponseType> response) const { |
| 132 (instance_->*method_)(success, request, response.Pass()); |
| 133 } |
| 134 |
| 135 private: |
79 BaseType* instance_; | 136 BaseType* instance_; |
80 Method method_; | 137 Method method_; |
81 }; | 138 }; |
82 | 139 |
83 } // namespace | 140 } // namespace |
84 | 141 |
85 // Returns a callback to |instance->method|. | 142 // Returns a callback to |instance->method| with constant reference to data. |
86 template <typename BaseType, typename Param1, typename Param2> | 143 template <typename BaseType, typename RequestType, typename ResponseType> |
87 scoped_ptr<Callback<Param1, Param2> > BuildCallback( | 144 scoped_ptr<Callback<RequestType, ResponseType> > BuildCallback( |
88 BaseType* instance, | 145 BaseType* instance, |
89 void (BaseType::*method)(bool, const Param1&, const Param2&)) { | 146 void (BaseType::*method)(bool, const RequestType&, const ResponseType&)) { |
90 return scoped_ptr<Callback<Param1, Param2> >( | 147 return scoped_ptr<Callback<RequestType, ResponseType> >( |
91 new CallbackImpl<BaseType, Param1, Param2>(instance, method)); | 148 new CallbackImpl<BaseType, RequestType, ResponseType>(instance, method)); |
| 149 } |
| 150 |
| 151 // Returns a callback to |instance->method| with scoped pointer to data. |
| 152 template <typename BaseType, typename RequestType, typename ResponseType> |
| 153 scoped_ptr<ScopedPtrCallback<RequestType, ResponseType> > |
| 154 BuildScopedPtrCallback( |
| 155 BaseType* instance, |
| 156 void (BaseType::*method)( |
| 157 bool, const RequestType&, scoped_ptr<ResponseType>)) { |
| 158 return scoped_ptr<ScopedPtrCallback<RequestType, ResponseType> >( |
| 159 new ScopedPtrCallbackImpl<BaseType, RequestType, ResponseType>( |
| 160 instance, method)); |
92 } | 161 } |
93 | 162 |
94 } // namespace addressinput | 163 } // namespace addressinput |
95 } // namespace i18n | 164 } // namespace i18n |
96 | 165 |
97 #endif // I18N_ADDRESSINPUT_CALLBACK_H_ | 166 #endif // I18N_ADDRESSINPUT_CALLBACK_H_ |
OLD | NEW |