OLD | NEW |
(Empty) | |
| 1 // Copyright (C) 2013 Google Inc. |
| 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (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 |
| 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. |
| 14 // |
| 15 // An object to store a pointer to a method in an object with the following |
| 16 // signature: |
| 17 // |
| 18 // void Observer::ObserveEvent(bool success, |
| 19 // const Key& key, |
| 20 // const Data& data); |
| 21 |
| 22 #ifndef I18N_ADDRESSINPUT_CALLBACK_H_ |
| 23 #define I18N_ADDRESSINPUT_CALLBACK_H_ |
| 24 |
| 25 #include <cassert> |
| 26 #include <cstddef> |
| 27 |
| 28 namespace i18n { |
| 29 namespace addressinput { |
| 30 |
| 31 // Stores a pointer to a method in an object. Sample usage: |
| 32 // class MyClass { |
| 33 // public: |
| 34 // typedef Callback<MyType, MyDataType> MyCallback; |
| 35 // |
| 36 // void GetDataAsynchronously() { |
| 37 // scoped_ptr<MyCallback> callback(BuildCallback( |
| 38 // this, &MyClass::OnDataReady)); |
| 39 // bool success = ... |
| 40 // MyKeyType key = ... |
| 41 // MyDataType data = ... |
| 42 // (*callback)(success, key, data); |
| 43 // } |
| 44 // |
| 45 // void OnDataReady(bool success, |
| 46 // const MyKeyType& key, |
| 47 // const MyDataType& data) { |
| 48 // ... |
| 49 // } |
| 50 // }; |
| 51 template <typename Key, typename Data> |
| 52 class Callback { |
| 53 public: |
| 54 virtual ~Callback() {} |
| 55 |
| 56 virtual void operator()(bool success, |
| 57 const Key& key, |
| 58 const Data& data) const = 0; |
| 59 }; |
| 60 |
| 61 namespace { |
| 62 |
| 63 template <typename Observer, typename Key, typename Data> |
| 64 class CallbackImpl : public Callback<Key, Data> { |
| 65 public: |
| 66 typedef void (Observer::*ObserveEvent)(bool, const Key&, const Data&); |
| 67 |
| 68 CallbackImpl(Observer* observer, ObserveEvent observe_event) |
| 69 : observer_(observer), |
| 70 observe_event_(observe_event) { |
| 71 assert(observer_ != NULL); |
| 72 assert(observe_event_ != NULL); |
| 73 } |
| 74 |
| 75 virtual ~CallbackImpl() {} |
| 76 |
| 77 virtual void operator()(bool success, |
| 78 const Key& key, |
| 79 const Data& data) const { |
| 80 (observer_->*observe_event_)(success, key, data); |
| 81 } |
| 82 |
| 83 private: |
| 84 Observer* observer_; |
| 85 ObserveEvent observe_event_; |
| 86 }; |
| 87 |
| 88 } // namespace |
| 89 |
| 90 // Returns a callback to |observer->observe_event| method. The caller owns the |
| 91 // result. |
| 92 template <typename Observer, typename Key, typename Data> |
| 93 Callback<Key, Data>* BuildCallback( |
| 94 Observer* observer, |
| 95 void (Observer::*observe_event)(bool, const Key&, const Data&)) { |
| 96 return new CallbackImpl<Observer, Key, Data>(observer, observe_event); |
| 97 } |
| 98 |
| 99 } // namespace addressinput |
| 100 } // namespace i18n |
| 101 |
| 102 #endif // I18N_ADDRESSINPUT_CALLBACK_H_ |
OLD | NEW |