Thrift-1444:FunctionRunner - add syntactic sugar to create shared_ptrs
Client: cpp
Patch: Dave Watson

Simplify FunctionRunner addTask calls.


git-svn-id: https://svn.apache.org/repos/asf/thrift/trunk@1210741 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/cpp/src/concurrency/FunctionRunner.h b/lib/cpp/src/concurrency/FunctionRunner.h
index 5a8cd3d..f2162d6 100644
--- a/lib/cpp/src/concurrency/FunctionRunner.h
+++ b/lib/cpp/src/concurrency/FunctionRunner.h
@@ -34,16 +34,15 @@
  *  void* my_thread_main(void* arg);
  *  shared_ptr<ThreadFactory> factory = ...;
  *  // To create a thread that executes my_thread_main once:
- *  shared_ptr<Thread> thread =
- *    factory->newThread(shared_ptr<FunctionRunner>(
- *      new FunctionRunner(my_thread_main, some_argument)));
+ *  shared_ptr<Thread> thread = factory->newThread(
+ *    FunctionRunner::create(my_thread_main, some_argument));
  *  thread->start();
  *
  *  bool A::foo();
  *  A* a = new A();
  *  // To create a thread that executes a.foo() every 100 milliseconds:
- *  factory->newThread(shared_ptr<FunctionRunner>(
- *    new FunctionRunner(std::tr1::bind(&A::foo, a), 100)))->start();
+ *  factory->newThread(FunctionRunner::create(
+ *    std::tr1::bind(&A::foo, a), 100))->start();
  *
  */
 
@@ -57,6 +56,20 @@
   typedef std::tr1::function<bool()> BoolFunc;
 
   /**
+   * Syntactic sugar to make it easier to create new FunctionRunner
+   * objects wrapped in shared_ptr.
+   */
+  static boost::shared_ptr<FunctionRunner> create(const VoidFunc& cob) {
+    return boost::shared_ptr<FunctionRunner>(new FunctionRunner(cob));
+  }
+
+  static boost::shared_ptr<FunctionRunner> create(PthreadFuncPtr func,
+                                                  void* arg) {
+    return boost::shared_ptr<FunctionRunner>(new FunctionRunner(func, arg));
+  }
+
+
+  /**
    * Given a 'pthread_create' style callback, this FunctionRunner will
    * execute the given callback.  Note that the 'void*' return value is ignored.
    */