Browse Source

Snippets for a message pump class

Carl Olsen 5 years ago
parent
commit
ac62334f57
5 changed files with 75 additions and 3 deletions
  1. 29 3
      c++-mode/msgPump
  2. 12 0
      c++-mode/msgPumpMsg
  3. 18 0
      c++-mode/msgPumpRun
  4. 7 0
      c++-mode/msgPumpStart
  5. 9 0
      c++-mode/msgPumpStop

+ 29 - 3
c++-mode/msgPump

@@ -6,6 +6,8 @@
 
 #include <functional>
 #include <thread>
+#include <memory>
+#include "Queue.hpp"
 
 /**
 * \class ${1:`(file-name-nondirectory (file-name-sans-extension (buffer-file-name)))`}
@@ -19,6 +21,17 @@
 class $1
 {
 public:
+   /**
+    * \name constructor
+    * \brief constructor for $1
+    */
+   $1(){}
+   /**
+    * \name destructor
+    * \brief destructor for $1
+    */
+   ~$1(){}
+
   /** 
    * Typedef for message pump function, these are to 
    * placed in the message queue
@@ -29,6 +42,11 @@ public:
    */
   typedef Util::Queue< FunT > FunctionMsgQueueT;
 
+  /**
+   * Typedef shared pointer type for threads
+   */
+  typedef std::shared_ptr< std::thread > ThreadPtrT;
+  
   /**
    * \brief $2 short description
    * 
@@ -43,15 +61,23 @@ public:
    */
   void ${3:stopSystem}();
 
+  /**
+   * \name $4
+   * \brief get method for the current thread
+   *
+   * Returns the current thread for the $1
+   * \return ThreadPtrT
+   */
+  ThreadPtrT ${4:getThread}() const;
 private:
   /**
    * \brief Thread runnable method
    * 
    * This message is where the thread is executing.
    */
-  void ${4:run}();
+  void ${5:run}();
   /** Current thread */
-  std::thread *${5:itsThread_};
+  ThreadPtrT ${6:itsThread_};
   /** Current message queue*/
-  FunctionMsgQueueT ${6:itsQueue_};
+  FunctionMsgQueueT ${7:itsQueue_};
 };

+ 12 - 0
c++-mode/msgPumpMsg

@@ -0,0 +1,12 @@
+# -*- mode: snippet -*-
+# name: Message Pump Msg
+# key: msgPumpMsg
+# group: col
+# --
+
+  auto fun${1:Msg} = [${2:this}${3:,}$4] () -> bool {
+    this->handle$1($4);
+    return true;
+  };
+
+  itsQueue_.push(fun$1);

+ 18 - 0
c++-mode/msgPumpRun

@@ -0,0 +1,18 @@
+# -*- mode: snippet -*-
+# name: msg run method
+# key: msgPumpRun
+# group: col
+# --
+
+while(true)
+{
+  
+  FunT fun;
+  ${1:itsQueue_.pop(fun)};
+  if( fun  && !fun() )
+    {
+      break;
+    }
+  
+ }
+      

+ 7 - 0
c++-mode/msgPumpStart

@@ -0,0 +1,7 @@
+# -*- mode: snippet -*-
+# name: msg start method
+# key: msgPumpStart
+# group: col
+# --
+
+  ${1:itsThread_}.reset(new std::thread(${2:&`(file-name-nondirectory (file-name-sans-extension (buffer-file-name)))`::run},this));

+ 9 - 0
c++-mode/msgPumpStop

@@ -0,0 +1,9 @@
+# -*- mode: snippet -*-
+# name: MsgPump stop system
+# key: msgPumpStop
+# group: col
+# --
+
+
+  auto killFun = [] () -> bool { return false; };
+  ${1:itsQueue_}.push(killFun);