THRIFT-923. cpp: Implement a fully nonblocking server and client
There are three major parts of this:
1/ New callback-style interfaces for for a few key Thrift components:
TAsyncProcessor for servers and TAsyncChannel for clients.
2/ Concrete implementations of TAsyncChannel and a server for
TAsyncProcessor based on evhttp.
3/ Async-style code generation for C++
git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@1005127 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/cpp/src/async/SimpleCallback.h b/lib/cpp/src/async/SimpleCallback.h
new file mode 100644
index 0000000..4218328
--- /dev/null
+++ b/lib/cpp/src/async/SimpleCallback.h
@@ -0,0 +1,98 @@
+#ifndef _THRIFT_ASYNC_SIMPLECALLBACK_H_
+#define _THRIFT_ASYNC_SIMPLECALLBACK_H_ 1
+
+#include <Thrift.h>
+namespace apache { namespace thrift {
+
+/**
+ * A template class for forming simple method callbacks with either an empty
+ * argument list or one argument of known type.
+ *
+ * For more efficiency where tr1::function is overkill.
+ */
+
+template<typename C, ///< class whose method we wish to wrap
+ typename A = void, ///< type of argument
+ typename R = void> ///< type of return value
+class SimpleCallback {
+ typedef R (C::*cfptr_t)(A); ///< pointer-to-member-function type
+ cfptr_t fptr_; ///< the embedded function pointer
+ C* obj_; ///< object whose function we're wrapping
+ public:
+ /**
+ * Constructor for empty callback object.
+ */
+ SimpleCallback() :
+ fptr_(NULL), obj_(NULL) {}
+ /**
+ * Construct callback wrapper for member function.
+ *
+ * @param fptr pointer-to-member-function
+ * @param "this" for object associated with callback
+ */
+ SimpleCallback(cfptr_t fptr, const C* obj) :
+ fptr_(fptr), obj_(const_cast<C*>(obj))
+ {}
+
+ /**
+ * Make a call to the member function we've wrapped.
+ *
+ * @param i argument for the wrapped member function
+ * @return value from that function
+ */
+ R operator()(A i) const {
+ (obj_->*fptr_)(i);
+ }
+
+ operator bool() const {
+ return obj_ != NULL && fptr_ != NULL;
+ }
+
+ ~SimpleCallback() {}
+};
+
+/**
+ * Specialization of SimpleCallback for empty argument list.
+ */
+template<typename C, ///< class whose method we wish to wrap
+ typename R> ///< type of return value
+class SimpleCallback<C, void, R> {
+ typedef R (C::*cfptr_t)(); ///< pointer-to-member-function type
+ cfptr_t fptr_; ///< the embedded function pointer
+ C* obj_; ///< object whose function we're wrapping
+ public:
+ /**
+ * Constructor for empty callback object.
+ */
+ SimpleCallback() :
+ fptr_(NULL), obj_(NULL) {}
+
+ /**
+ * Construct callback wrapper for member function.
+ *
+ * @param fptr pointer-to-member-function
+ * @param obj "this" for object associated with callback
+ */
+ SimpleCallback(cfptr_t fptr, const C* obj) :
+ fptr_(fptr), obj_(const_cast<C*>(obj))
+ {}
+
+ /**
+ * Make a call to the member function we've wrapped.
+ *
+ * @return value from that function
+ */
+ R operator()() const {
+ (obj_->*fptr_)();
+ }
+
+ operator bool() const {
+ return obj_ != NULL && fptr_ != NULL;
+ }
+
+ ~SimpleCallback() {}
+};
+
+}} // apache::thrift
+
+#endif /* !_THRIFT_ASYNC_SIMPLECALLBACK_H_ */