THRIFT-1315. cpp: generate server interface factory classes

Patch: Adam Simpkins

git-svn-id: https://svn.apache.org/repos/asf/thrift/trunk@1164187 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/cpp/src/TProcessor.h b/lib/cpp/src/TProcessor.h
index cf4f3b8..a593776 100644
--- a/lib/cpp/src/TProcessor.h
+++ b/lib/cpp/src/TProcessor.h
@@ -161,6 +161,34 @@
   boost::shared_ptr<TProcessorEventHandler> eventHandler_;
 };
 
+/**
+ * This is a helper class to allow boost::shared_ptr to be used with handler
+ * pointers returned by the generated handler factories.
+ *
+ * The handler factory classes generated by the thrift compiler return raw
+ * pointers, and factory->releaseHandler() must be called when the handler is
+ * no longer needed.
+ *
+ * A ReleaseHandler object can be instantiated and passed as the second
+ * parameter to a shared_ptr, so that factory->releaseHandler() will be called
+ * when the object is no longer needed, instead of deleting the pointer.
+ */
+template<typename HandlerFactory_>
+class ReleaseHandler {
+ public:
+   ReleaseHandler(const boost::shared_ptr<HandlerFactory_>& handlerFactory) :
+       handlerFactory_(handlerFactory) {}
+
+   void operator()(typename HandlerFactory_::Handler* handler) {
+     if (handler) {
+       handlerFactory_->releaseHandler(handler);
+     }
+   }
+
+ private:
+   boost::shared_ptr<HandlerFactory_> handlerFactory_;
+};
+
 }} // apache::thrift
 
 #endif // #ifndef _THRIFT_TPROCESSOR_H_