Monday, December 12, 2011

C++ and Boost thread.

This is a class I think someone may find very entertaining one day, it fits a modest need to do something asynchronously, why not take a look?

If you want to use it for something it will need to be adjusted slightly as it is taken from my project Xinmapper hosted on sourceforge.

[code]

#pragma once
#include "stdafx.h"
#include "Mapper.h"
#include "XInputTranslater.h"
#include "XInputBoostMouse.h"
#include "PlayerInfo.h"

class InputPoller
{
 typedef boost::mutex::scoped_lock lock;
public:
 const int THREAD_DELAY;
 boost::shared_ptr<boost::thread> thread;
protected:
 volatile bool isThreadRunning;
 volatile bool isStopRequested;
 XINPUT_STATE local_state;
 boost::mutex stateMutex;
 
 PlayerInfo *p;
 Mapper *m;
 XInputTranslater *t;
 XInputBoostMouse *mse;

protected://member functions
 void startThread()
 {
  if( ! this->isThreadRunning )
  {
   this->isStopRequested =(false);
   this->isThreadRunning =(true);
   this->thread = boost::shared_ptr<boost::thread>
    (new boost::thread(boost::bind(&InputPoller::workThread,this)));
  }
 }

 void requestStop(){
  if( this->thread != nullptr ){
   if(! this->isThreadRunning )
    return;
   this->isStopRequested =(true);
  }
 }

 void stopThread(){
  if(this->thread != nullptr){
   if(! this->isThreadRunning )
    return;
   this->isStopRequested =(true);
   this->thread->join();
   this->isThreadRunning =(false);
  }
 }
  
 virtual void workThread()
 {
  while( ! this->isStopRequested )
  {
   DWORD error = XInputGetState(p->player_id, &local_state);
   if( error != ERROR_SUCCESS )
    break;
   mse->ProcessState(local_state);
   m->ProcessActionDetails( t->ProcessState(local_state) );
   Sleep(THREAD_DELAY);
  }
  this->isThreadRunning = false;
 }

public://public member functions
 InputPoller(PlayerInfo *playerInfo, Mapper *mapper, XInputTranslater *trans, XInputBoostMouse *mouse)
  : THREAD_DELAY(10), p(playerInfo), m(mapper), t(trans), mse(mouse)
 {
  isThreadRunning = false;
  isStopRequested = false;
  lock(stateMutex);
  memset(&local_state,0,sizeof(XINPUT_STATE));
 }
  
 //This function does block when it is called by default.
 virtual ~InputPoller(){
  if( this->thread != nullptr ){
   this->isStopRequested = true;
   this->thread->join();
  }
 }

 bool Start()
 {
  this->startThread();
  return this->isThreadRunning;
 }

 bool Stop()
 {
  this->stopThread();
  return this->isThreadRunning;
 }
 bool IsRunning()
 {
  return this->isThreadRunning;
 }
 bool IsControllerConnected()
 {
  XINPUT_STATE ss = {0};
  return XInputGetState(p->player_id, &ss) == ERROR_SUCCESS;
 }
};
[/code]
Also you may notice I've made good use of protected members as well as virtual functions.  Happy coding!

5 comments:

  1. c++/c# arent my thing but gotta love playing around with threads!!!

    ReplyDelete
  2. really helpful! i'm also programming :)

    ReplyDelete
  3. @SlowCarMan,

    What else would you use in this day and age? I personally despise Java.

    ReplyDelete
  4. That is a lot of code there sir.

    https://twitter.com/#!/MemeDoctor
    http://memedoctor.blogspot.com/

    ReplyDelete