Pārlūkot izejas kodu

thread: Allow arbitrary std::function (incl. lambdas) in GenericThread

rdb 4 gadi atpakaļ
vecāks
revīzija
b1b05c2be9

+ 3 - 20
panda/src/pipeline/genericThread.I

@@ -15,31 +15,14 @@
  * Replaces the function that is called when the thread runs.
  * Replaces the function that is called when the thread runs.
  */
  */
 INLINE void GenericThread::
 INLINE void GenericThread::
-set_function(GenericThread::ThreadFunc *function) {
-  _function = function;
+set_function(std::function<void()> function) {
+  _function = std::move(function);
 }
 }
 
 
 /**
 /**
  * Returns the function that is called when the thread runs.
  * Returns the function that is called when the thread runs.
  */
  */
-INLINE GenericThread::ThreadFunc *GenericThread::
+INLINE const std::function<void()> &GenericThread::
 get_function() const {
 get_function() const {
   return _function;
   return _function;
 }
 }
-
-/**
- * Replaces the void pointer that is passed to the thread function.  This is
- * any arbitrary pointer; the thread object does no processing on it.
- */
-INLINE void GenericThread::
-set_user_data(void *user_data) {
-  _user_data = user_data;
-}
-
-/**
- * Returns the void pointer that is passed to the thread function.
- */
-INLINE void *GenericThread::
-get_user_data() const {
-  return _user_data;
-}

+ 17 - 6
panda/src/pipeline/genericThread.cxx

@@ -14,6 +14,8 @@
 #include "genericThread.h"
 #include "genericThread.h"
 #include "pnotify.h"
 #include "pnotify.h"
 
 
+#ifndef CPPPARSER
+
 TypeHandle GenericThread::_type_handle;
 TypeHandle GenericThread::_type_handle;
 
 
 /**
 /**
@@ -23,8 +25,6 @@ GenericThread::
 GenericThread(const std::string &name, const std::string &sync_name) :
 GenericThread(const std::string &name, const std::string &sync_name) :
   Thread(name, sync_name)
   Thread(name, sync_name)
 {
 {
-  _function = nullptr;
-  _user_data = nullptr;
 }
 }
 
 
 /**
 /**
@@ -33,8 +33,17 @@ GenericThread(const std::string &name, const std::string &sync_name) :
 GenericThread::
 GenericThread::
 GenericThread(const std::string &name, const std::string &sync_name, GenericThread::ThreadFunc *function, void *user_data) :
 GenericThread(const std::string &name, const std::string &sync_name, GenericThread::ThreadFunc *function, void *user_data) :
   Thread(name, sync_name),
   Thread(name, sync_name),
-  _function(function),
-  _user_data(user_data)
+  _function(std::bind(function, user_data))
+{
+}
+
+/**
+ *
+ */
+GenericThread::
+GenericThread(const std::string &name, const std::string &sync_name, std::function<void()> function) :
+  Thread(name, sync_name),
+  _function(std::move(function))
 {
 {
 }
 }
 
 
@@ -43,6 +52,8 @@ GenericThread(const std::string &name, const std::string &sync_name, GenericThre
  */
  */
 void GenericThread::
 void GenericThread::
 thread_main() {
 thread_main() {
-  nassertv(_function != nullptr);
-  (*_function)(_user_data);
+  nassertv(_function);
+  _function();
 }
 }
+
+#endif

+ 10 - 8
panda/src/pipeline/genericThread.h

@@ -17,6 +17,9 @@
 #include "pandabase.h"
 #include "pandabase.h"
 #include "thread.h"
 #include "thread.h"
 
 
+#ifndef CPPPARSER
+#include <functional>
+
 /**
 /**
  * A generic thread type that allows calling a C-style thread function without
  * A generic thread type that allows calling a C-style thread function without
  * having to subclass.
  * having to subclass.
@@ -27,19 +30,16 @@ public:
 
 
   GenericThread(const std::string &name, const std::string &sync_name);
   GenericThread(const std::string &name, const std::string &sync_name);
   GenericThread(const std::string &name, const std::string &sync_name, ThreadFunc *function, void *user_data);
   GenericThread(const std::string &name, const std::string &sync_name, ThreadFunc *function, void *user_data);
+  GenericThread(const std::string &name, const std::string &sync_name, std::function<void()> function);
 
 
-  INLINE void set_function(ThreadFunc *function);
-  INLINE ThreadFunc *get_function() const;
-
-  INLINE void set_user_data(void *user_data);
-  INLINE void *get_user_data() const;
+  INLINE void set_function(std::function<void()> function);
+  INLINE const std::function<void()> &get_function() const;
 
 
 protected:
 protected:
   virtual void thread_main();
   virtual void thread_main();
 
 
 private:
 private:
-  ThreadFunc *_function;
-  void *_user_data;
+  std::function<void()> _function;
 
 
 public:
 public:
   static TypeHandle get_class_type() {
   static TypeHandle get_class_type() {
@@ -61,4 +61,6 @@ private:
 
 
 #include "genericThread.I"
 #include "genericThread.I"
 
 
-#endif
+#endif  // CPPPARSER
+
+#endif  // GENERICTHREAD_H