`
- 浏览:
49018 次
- 来自:
...
-
- #pragma once
- #include <boost/shared_ptr.hpp>
- #include <boost/any.hpp>
- #include "controlled_module.hpp"
-
- struct _command
- {
- typedef boost::shared_ptr<_command> CCmdPtr;
- unsigned int nCmd;
- boost::any anyParam;
- };
- struct _wait_command
- {
- boost::any par;
- unsigned int command;
- void * event;
- boost::shared_ptr<boost::any> resp;
- };
- class controlled_module_ex;
- struct _notify
- {
- controlled_module_ex * sender;
- int id;
- boost::any par;
- };
- #define BM_RESERVE 1000
- #define BM_RING_START BM_RESERVE+1
- #define BM_RING_STOP BM_RESERVE+2
- #define BM_RING_SETTIME BM_RESERVE+3
- #define BM_RING_SETPARENT BM_RESERVE+4
- #define BM_RING_CYCLE BM_RESERVE+5
- #define BM_RING_PROCESS BM_RESERVE+6
- #define BM_RING_PROCESSEND BM_RESERVE+7
- #define BM_RING_PROCESSFAIL BM_RESERVE+8
- #define BM_TIMER BM_RESERVE+9
- #define BM_COMMAND BM_RESERVE+10
- #define BM_NOTIFY BM_RESERVE+11
-
- #define BM_USER 9000
- class controlled_timer;
- class controlled_module_ex: public controlled_module
- {
- public:
- controlled_module_ex()
- {
- m_safe = false;
- }
- ~controlled_module_ex()
- {
- safestop();
- }
- public:
- template<typename T>
- bool postmessage(unsigned int nCmd, const boost::shared_ptr<T>& p)
- {
- if(this==0||!m_safe)return false;
- boost::mutex::scoped_lock lock(m_mutex_command);
- _command::CCmdPtr cmd(new _command);
- cmd->nCmd = nCmd;
- cmd->anyParam = p;
- m_list_command.push_back(cmd);
- return true;
- }
- boost::any execute(unsigned int command,boost::any par,int timeout=-1)
- {
- boost::shared_ptr<_wait_command> shared(new _wait_command);
- _wait_command & cmd = *shared;
- cmd.command = command;
- cmd.event = (void *)CreateEvent(0,FALSE,FALSE,0);
- cmd.par = par;
- cmd.resp = boost::shared_ptr<boost::any>(new boost::any);
- if(this->postmessage(BM_COMMAND,shared))
- {
- DWORD dw = WaitForSingleObject(cmd.event,timeout);
- CloseHandle(cmd.event);
- if(dw!=WAIT_OBJECT_0)
- return boost::any();
- else
- return *cmd.resp;
- }
- else
- {
- CloseHandle(cmd.event);
- return boost::any();
- }
- }
- void notify(_notify p)
- {
- this->postmessage(BM_NOTIFY,p);
- }
- bool postmessage(unsigned int nCmd,boost::any p)
- {
- if(this==0||!m_safe)
- return false;
- boost::mutex::scoped_lock lock(m_mutex_command);
- _command::CCmdPtr cmd(new _command);
- cmd->nCmd = nCmd;
- cmd->anyParam = p;
- m_list_command.push_back(cmd);
- return true;
- }
- bool postmessage(unsigned int nCmd)
- {
- if(this==0||!m_safe)
- return false;
- boost::mutex::scoped_lock lock(m_mutex_command);
- _command::CCmdPtr cmd(new _command);
- cmd->nCmd = nCmd;
- cmd->anyParam = 0;
- m_list_command.push_back(cmd);
- return true;
- }
- virtual bool work()
- {
- if(!getmessage())
- return false;
- else
- {
- Sleep(this->m_sleeptime);
- return true;
- }
- }
- virtual void message(const _command & cmd)
- {
- if(cmd.nCmd==BM_RING_START)
- {
- this->on_safestart();
- }
- else if(cmd.nCmd==BM_RING_STOP)
- {
- this->on_safestop();
- }
- else if(cmd.nCmd==BM_TIMER)
- {
- this->on_timer(boost::any_cast<controlled_timer*>(cmd.anyParam));
- }
- else if(cmd.nCmd==BM_COMMAND)
- {
- boost::shared_ptr<_wait_command> shared =
- boost::any_cast< boost::shared_ptr<_wait_command> >(cmd.anyParam);
- _wait_command & cmd = *shared;
- *cmd.resp = this->on_command(cmd.command,cmd.par);
- SetEvent((HANDLE)cmd.event);
- }
- else if(cmd.nCmd==BM_NOTIFY)
- {
- try
- {
- _notify par = boost::any_cast<_notify>(cmd.anyParam);
- this->on_notify(par);
- }
- catch(boost::bad_any_cast)
- {
- }
- }
- }
- virtual void release()
- {
- boost::mutex::scoped_lock lock(m_mutex_command);
- m_list_command.clear();
- }
- void safestart()
- {
- if(!islive())
- start();
- m_safe = true;
- m_safestart_event = (void*)CreateEvent(NULL,FALSE,FALSE,0);
- postmessage(BM_RING_START);
- ::WaitForSingleObject((HANDLE)m_safestart_event,INFINITE);
- CloseHandle(m_safestart_event);
- }
- void safestop()
- {
- if(this->islive())
- {
- m_safe = false;
- m_safestop_event = (void*)CreateEvent(NULL,FALSE,FALSE,0);
- {
- boost::mutex::scoped_lock lock(m_mutex_command);
- _command::CCmdPtr cmd(new _command);
- cmd->nCmd = BM_RING_STOP;
- cmd->anyParam = 0;
- m_list_command.push_back(cmd);
- }
- DWORD dw = ::WaitForSingleObject((HANDLE)m_safestop_event,3*1000);
- if(WAIT_OBJECT_0!=dw)
- {
- }
- CloseHandle(m_safestop_event);
- stop();
- }
- }
- virtual void on_timer(const controlled_timer * p){}
- virtual void on_safestart()
- {
- SetEvent(m_safestart_event);
- }
- virtual void on_safestop()
- {
- SetEvent(m_safestop_event);
- }
- virtual void on_notify(const _notify & p)
- {
- }
- protected:
- virtual boost::any on_command(const unsigned int command,const boost::any par)
- {
- return boost::any();
- }
- bool getmessage()
- {
- std::list<_command::CCmdPtr> cache;
- {
- boost::mutex::scoped_lock lock(m_mutex_command);
- while(!m_list_command.empty())
- {
- _command::CCmdPtr p = m_list_command.front();
- m_list_command.pop_front();
- cache.push_back(p);
- }
- }
- _command::CCmdPtr stop_command;
- std::list<_command::CCmdPtr>::iterator item;
- for(item = cache.begin();item!=cache.end();item++)
- {
- if((*(*item)).nCmd==BM_RING_STOP)
- {
- stop_command = *item;
- break;
- }
- }
- if(stop_command.get()==0)
- {
- while(!cache.empty())
- {
- _command::CCmdPtr p = cache.front();
- cache.pop_front();
- try
- {
- if((*p).nCmd!=BM_RING_START)
- {
- if(!this->m_safe)
- continue;
- }
- this->message(*p);
- }
- catch(boost::bad_any_cast &)
- {
- }
- }
- return true;
- }
- else
- {
- cache.clear();
- this->message(*stop_command);
- return false;
- }
- }
- private:
- void* m_safestart_event;
- void* m_safestop_event;
- bool m_safe;
-
- boost::mutex m_mutex_command;
- std::list<_command::CCmdPtr> m_list_command;
- };
- class controlled_timer: public controlled_module_ex
- {
- public:
- controlled_timer()
- {
- this->m_time = 0;
- this->m_parent = 0;
- this->m_step = 0;
- }
- ~controlled_timer(){
- }
- protected:
- controlled_module_ex* m_parent;
- int m_time;
- int m_step;
- public:
- void starttimer(int time,controlled_module_ex* parent)
- {
- this->safestart();
- this->postmessage(BM_RING_SETPARENT,parent);
- this->postmessage(BM_RING_SETTIME,time);
- }
- void stoptimer()
- {
- this->safestop();
- }
- public:
- virtual void on_safestop()
- {
- m_time = 0;
- controlled_module_ex::on_safestop();
- }
- virtual void message(const _command & cmd)
- {
- controlled_module_ex::message(cmd);
- if(cmd.nCmd==BM_RING_SETTIME)
- {
- int time = boost::any_cast<int>(cmd.anyParam);
- this->m_time = time/this->m_sleeptime;
- this->postmessage(BM_RING_CYCLE);
- }
- else if(cmd.nCmd==BM_RING_SETPARENT)
- {
- this->m_parent = boost::any_cast<controlled_module_ex*>(cmd.anyParam);
- }
- else if(cmd.nCmd==BM_RING_CYCLE)
- {
- if(m_time>0)
- {
- if(m_step>m_time)
- {
- m_parent->postmessage(BM_TIMER,this);
- m_step=0;
- }
- m_step++;
- }
- this->postmessage(BM_RING_CYCLE);
- }
- }
- };
分享到:
Global site tag (gtag.js) - Google Analytics
相关推荐
450._Delete_Node_in_a_BST【LeetCode单题讲解系列】
BST24108 PCI总线,8通道CAN总线通讯卡,光隔离,支持定时发送 BST24208-01 CPCI(3U前出线)总线,8通道CAN总线通讯卡,磁隔离,支持定时发送 BST24212 CPCI(3U前出线)总线,20通道CAN总线通讯卡,磁隔离,支持...
将bst文件放到MiKTeX的bst文件夹下自己新建的gbt7714-2005文件下,1)作者名称如 KERNER B S 的样式 GBT7714-2005NLang.bst 2)作者名称如 KERNER B S 的样式 GBT7714-2005NLang_UP.bst 3)作者名称如 Kerner B S 的...
BST纠偏调试步骤
BST纠偏系统调试手册.pdf
该资源为国标参考文献的bst样式文件,GBT7714-2005NLang.bst样式文件。可以在论文参考文献排版时使用
1. BST 物料纠偏系统概述 BST 物料纠偏系统是一种智能物流系统,旨在提高物流效率和准确性。该系统由 ekrPro com40 控制器和 CCD Pro 传感器组成,能够自动检测和纠偏物料。 2. ekrPro com40 控制器 ekrPro ...
BST自动纠偏系统
代码支持《数据结构与算法分析》中的BST数据结构C++代码
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node's key. The right ...
解决springer论文参考文献引用乱序问题
舵机供电模块超声波模块供电口舵机模块电机模块红外检测模块检测提示模块电源提示灯亚博 BST-V51 智能小车底板电路原理图。
BST-物料纠偏系统ERK 1000H使用手册pdf,BST-物料纠偏系统ERK 1000H使用手册
Latex修改参考文献展示方式:修改apsrev4-1.bst文件
BST纠偏操作英文说明pdf,BST纠偏操作英文说明
解决IEE模板同名作者不显示问题,放入到latex目录即可
包含了IEEEabrv.bib和IEEEtran.bst文件,使用bib文件管理参考文献时使用
BST-BMP180-DS000-07.pdf英文资料
此代码为原创代码 主要描述排序二叉树即BST的创建,以及二叉树的中序遍历,BST的广度遍历下的层序遍历。
BST的定义,非常标准,非常值得参考,,对于入门的同学很有用