📄 module.h
字号:
#ifndef MODULE_H
#define MODULE_H
#include <stddef.h>
#include <list>
#include <iostream>
#include <boost/thread/thread.hpp>
#include <boost/thread/recursive_mutex.hpp>
#include <boost/thread/condition.hpp>
#include <boost/bind.hpp>
using namespace std;
using namespace boost;
#include "common.h"
typedef enum{ SHUT_DOWN= 0, RUNNING, PAUSE,} State;
class EXPORT Module
{
friend class World;
public:
Module();
virtual ~Module();
public:
int load( Module* __module );
void unload( Module* __module );
virtual void operator () (void);
bool getThreaded(void)
{
recursive_mutex::scoped_lock lock( _mutex );
return _threaded;
}
void setThreaded( bool __threaded )
{
_threaded= __threaded;
}
World& getWorld(void)
{
recursive_mutex::scoped_lock lock( _mutex );
return *_world;
}
void setWorld( World* __world )
{
recursive_mutex::scoped_lock lock( _mutex );
_world= __world;
for( list<Module*>::iterator itor= _modules.begin(); itor!=_modules.end(); ++itor ){
Module& module= *(*itor);
try{ module.setWorld( __world ); }catch( exception& e ){ cerr << "Setting World error: " << __world << "\t" << e.what() << endl;
}catch( ... ){
cerr << "Setting World error: " << __world << "\t" << "Unknown exception" << endl;
}
}
for( list<Module*>::iterator itor= _threaded_modules.begin(); itor!=_threaded_modules.end(); ++itor ){
Module& module= *(*itor);
try{ module.setWorld( __world ); }catch( exception& e ){ cerr << "Setting World error: " << __world << "\t" << e.what() << endl;
}catch( ... ){
cerr << "Setting World error: " << __world << "\t" << "Unknown exception" << endl;
}
}
}
State getState(void)
{
recursive_mutex::scoped_lock lock( _mutex );
return _state;
}
void setState( const State __state )
{
recursive_mutex::scoped_lock lock( _mutex );
_state= __state;
for( list<Module*>::iterator itor= _modules.begin(); itor!=_modules.end(); ++itor ){
Module& module= *(*itor);
try{ module.setState( __state ); }catch( exception& e ){ cerr << "Setting State error: " << __state << "\t" << e.what() << endl;
}catch( ... ){
cerr << "Setting State error: " << __state << "\t" << "Unknown exception" << endl;
}
}
for( list<Module*>::iterator itor= _threaded_modules.begin(); itor!=_threaded_modules.end(); ++itor ){
Module& module= *(*itor);
try{ module.setState( __state ); }catch( exception& e ){ cerr << "Setting State error: " << __state << "\t" << e.what() << endl;
}catch( ... ){
cerr << "Setting State error: " << __state << "\t" << "Unknown exception" << endl;
}
}
}
protected:
void moduleBreathe(void);
void moduleShutdown(void);
virtual int init(void) =0;
virtual void breathe(void) =0;
virtual void shutdown(void) =0;
protected:
bool have_shutdown;
World* _world;
bool _running; State _state;
bool _threaded;
recursive_mutex _mutex;
condition _cond;
list<Module*> _modules;
list<Module*> _threaded_modules;
thread_group _threads;
};
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -