/src/libphonenumber/cpp/src/phonenumbers/callback.h
Line | Count | Source |
1 | | // Copyright (C) 2012 The Libphonenumber Authors |
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 | | // Author: Philippe Liard. |
16 | | // |
17 | | // Light implementation emulating base/callback.h. This is an internal header, |
18 | | // users should not depend on it. |
19 | | // Note that this implementation is very limited for now and |
20 | | // libphonenumber-specific. |
21 | | |
22 | | #ifndef I18N_PHONENUMBERS_CALLBACK_H_ |
23 | | #define I18N_PHONENUMBERS_CALLBACK_H_ |
24 | | |
25 | | namespace i18n { |
26 | | namespace phonenumbers { |
27 | | |
28 | | template <typename R, typename A1, typename A2, typename A3, typename A4> |
29 | | class ResultCallback4 { |
30 | | public: |
31 | 11.5k | virtual ~ResultCallback4() {} |
32 | | virtual R Run(A1 a1, A2 a2, A3 a3, A4 a4) = 0; |
33 | | }; |
34 | | |
35 | | template <typename R, typename A1, typename A2, typename A3, typename A4> |
36 | | class FunctionCallback4 : public ResultCallback4<R, A1, A2, A3, A4> { |
37 | | public: |
38 | | typedef R (FunctionType)(A1, A2, A3, A4); |
39 | | |
40 | 5.05k | explicit FunctionCallback4(FunctionType* function) : function_(function) {} |
41 | | virtual ~FunctionCallback4() {} |
42 | | |
43 | 12.4k | virtual R Run(A1 a1, A2 a2, A3 a3, A4 a4) { |
44 | 12.4k | return function_(a1, a2, a3, a4); |
45 | 12.4k | } |
46 | | |
47 | | private: |
48 | | FunctionType* const function_; |
49 | | }; |
50 | | |
51 | | template <typename T, typename R, typename A1, typename A2, typename A3, |
52 | | typename A4> |
53 | | class ConstMethodCallback4 : public ResultCallback4<R, A1, A2, A3, A4> { |
54 | | public: |
55 | | typedef R (T::*MethodType)(A1, A2, A3, A4) const; |
56 | | |
57 | | ConstMethodCallback4(const T* instance, MethodType method) |
58 | 6.53k | : instance_(instance), |
59 | 6.53k | method_(method) {} |
60 | | virtual ~ConstMethodCallback4() {} |
61 | | |
62 | 11.8k | virtual R Run(A1 a1, A2 a2, A3 a3, A4 a4) { |
63 | 11.8k | return (instance_->*method_)(a1, a2, a3, a4); |
64 | 11.8k | } |
65 | | |
66 | | private: |
67 | | const T* const instance_; |
68 | | MethodType const method_; |
69 | | }; |
70 | | |
71 | | template <typename R, typename A1, typename A2, typename A3, typename A4> |
72 | | ResultCallback4<R, A1, A2, A3, A4>* NewPermanentCallback( |
73 | 5.05k | R (*function)(A1, A2, A3, A4)) { |
74 | 5.05k | return new FunctionCallback4<R, A1, A2, A3, A4>(function); |
75 | 5.05k | } |
76 | | |
77 | | template <typename T, typename R, typename A1, typename A2, typename A3, |
78 | | typename A4> |
79 | | ResultCallback4<R, A1, A2, A3, A4>* NewPermanentCallback( |
80 | | const T* instance, |
81 | 6.53k | R (T::*method)(A1, A2, A3, A4) const) { |
82 | 6.53k | return new ConstMethodCallback4<T, R, A1, A2, A3, A4>(instance, method); |
83 | 6.53k | } |
84 | | |
85 | | } // namespace phonenumbers |
86 | | } // namespace i18n |
87 | | |
88 | | #endif // I18N_PHONENUMBERS_CALLBACK_H_ |