David Reiss | 5ddabb8 | 2010-10-06 17:09:37 +0000 | [diff] [blame] | 1 | #ifndef _THRIFT_ASYNC_SIMPLECALLBACK_H_ |
| 2 | #define _THRIFT_ASYNC_SIMPLECALLBACK_H_ 1 |
| 3 | |
| 4 | #include <Thrift.h> |
| 5 | namespace apache { namespace thrift { |
| 6 | |
| 7 | /** |
| 8 | * A template class for forming simple method callbacks with either an empty |
| 9 | * argument list or one argument of known type. |
| 10 | * |
| 11 | * For more efficiency where tr1::function is overkill. |
| 12 | */ |
| 13 | |
| 14 | template<typename C, ///< class whose method we wish to wrap |
| 15 | typename A = void, ///< type of argument |
| 16 | typename R = void> ///< type of return value |
| 17 | class SimpleCallback { |
| 18 | typedef R (C::*cfptr_t)(A); ///< pointer-to-member-function type |
| 19 | cfptr_t fptr_; ///< the embedded function pointer |
| 20 | C* obj_; ///< object whose function we're wrapping |
| 21 | public: |
| 22 | /** |
| 23 | * Constructor for empty callback object. |
| 24 | */ |
| 25 | SimpleCallback() : |
| 26 | fptr_(NULL), obj_(NULL) {} |
| 27 | /** |
| 28 | * Construct callback wrapper for member function. |
| 29 | * |
| 30 | * @param fptr pointer-to-member-function |
| 31 | * @param "this" for object associated with callback |
| 32 | */ |
| 33 | SimpleCallback(cfptr_t fptr, const C* obj) : |
| 34 | fptr_(fptr), obj_(const_cast<C*>(obj)) |
| 35 | {} |
| 36 | |
| 37 | /** |
| 38 | * Make a call to the member function we've wrapped. |
| 39 | * |
| 40 | * @param i argument for the wrapped member function |
| 41 | * @return value from that function |
| 42 | */ |
| 43 | R operator()(A i) const { |
| 44 | (obj_->*fptr_)(i); |
| 45 | } |
| 46 | |
| 47 | operator bool() const { |
| 48 | return obj_ != NULL && fptr_ != NULL; |
| 49 | } |
| 50 | |
| 51 | ~SimpleCallback() {} |
| 52 | }; |
| 53 | |
| 54 | /** |
| 55 | * Specialization of SimpleCallback for empty argument list. |
| 56 | */ |
| 57 | template<typename C, ///< class whose method we wish to wrap |
| 58 | typename R> ///< type of return value |
| 59 | class SimpleCallback<C, void, R> { |
| 60 | typedef R (C::*cfptr_t)(); ///< pointer-to-member-function type |
| 61 | cfptr_t fptr_; ///< the embedded function pointer |
| 62 | C* obj_; ///< object whose function we're wrapping |
| 63 | public: |
| 64 | /** |
| 65 | * Constructor for empty callback object. |
| 66 | */ |
| 67 | SimpleCallback() : |
| 68 | fptr_(NULL), obj_(NULL) {} |
| 69 | |
| 70 | /** |
| 71 | * Construct callback wrapper for member function. |
| 72 | * |
| 73 | * @param fptr pointer-to-member-function |
| 74 | * @param obj "this" for object associated with callback |
| 75 | */ |
| 76 | SimpleCallback(cfptr_t fptr, const C* obj) : |
| 77 | fptr_(fptr), obj_(const_cast<C*>(obj)) |
| 78 | {} |
| 79 | |
| 80 | /** |
| 81 | * Make a call to the member function we've wrapped. |
| 82 | * |
| 83 | * @return value from that function |
| 84 | */ |
| 85 | R operator()() const { |
| 86 | (obj_->*fptr_)(); |
| 87 | } |
| 88 | |
| 89 | operator bool() const { |
| 90 | return obj_ != NULL && fptr_ != NULL; |
| 91 | } |
| 92 | |
| 93 | ~SimpleCallback() {} |
| 94 | }; |
| 95 | |
| 96 | }} // apache::thrift |
| 97 | |
| 98 | #endif /* !_THRIFT_ASYNC_SIMPLECALLBACK_H_ */ |