ICommand.h
上传用户:dzyhzl
上传日期:2019-04-29
资源大小:56270k
文件大小:2k
源码类别:

模拟服务器

开发平台:

C/C++

  1. /********************************************************************
  2. created: 2003/04/08
  3. file base: ICommand
  4. file ext: h
  5. author: liupeng
  6. purpose:
  7. *********************************************************************/
  8. #ifndef __INCLUDE_ICOMMAND_H__
  9. #define __INCLUDE_ICOMMAND_H__
  10. #include "Macro.h"
  11. /*
  12.  * Interface class for execute command
  13.  */
  14. class ICommand
  15. {
  16. public:
  17. virtual bool Execute() = 0;
  18. virtual unsigned long GetFaildValue() = 0;
  19. virtual ~ICommand(){};
  20. };
  21. /*
  22.  * template classs
  23.  */
  24. template <class Receive>
  25. class CTaskCommand : public ICommand
  26. {
  27. public:
  28. typedef bool ( Receive::* Action )();
  29. explicit CTaskCommand( Receive *pReceiver, Action pfunAction, unsigned long dwFaildValue );
  30. virtual ~CTaskCommand();
  31. virtual bool Execute();
  32. virtual unsigned long GetFaildValue() { return m_dwFaildValue; };
  33. protected:
  34. CTaskCommand();
  35. /*
  36.  * This function pointer is used to execute command
  37.  */
  38. Action  m_pfunAction;
  39. /*
  40.  * The receiver class
  41.  */
  42. Receive *m_pReceiver;
  43. /*
  44.  * Store a faild value for return it to user when this class was found error
  45.  */
  46. unsigned long m_dwFaildValue;
  47. };
  48. template <class Receive>
  49. CTaskCommand<Receive>::CTaskCommand( Receive *pReceiver, Action pfunAction, unsigned long dwFaildValue )
  50. : m_pReceiver( pReceiver ), 
  51. m_pfunAction( pfunAction ),
  52. m_dwFaildValue( dwFaildValue )
  53. {
  54. }
  55. template <class Receive>
  56. CTaskCommand<Receive>::CTaskCommand()
  57. {
  58. ASSERT( FALSE );
  59. }
  60. template <class Receive>
  61. CTaskCommand<Receive>::~CTaskCommand()
  62. {
  63. }
  64. template <class Receive>
  65. bool CTaskCommand<Receive>::Execute()
  66. {
  67. return ( m_pReceiver->*m_pfunAction )();
  68. }
  69. #endif // __INCLUDE_ICOMMAND_H__