chxavcommand.h
上传用户:zhongxx05
上传日期:2007-06-06
资源大小:33641k
文件大小:7k
- /************************************************************************
- * chxavcommand.h
- * --------------
- *
- * Synopsis:
- * abstract interface for command objects, CHXAvCommand
- * concrete wrappers for calls to class member function:
- * no-arg class member function object
- * one-arg class member function object
- *
- * Example:
- *
- * class Music
- * {
- * public:
- * void SetEventAction(EventType type, const CHXAvCommand& cmd);
- * ...
- * };
- *
- * class Dancer
- * { ... };
- *
- * void DoBreakDance() {...} // no args
- * void Bow(TInt count) {...} // one arg
- *
- * void Dancer::Init(Music* pMusic)
- * {
- * //
- * // start break dancing when music begins,
- * // bow three times when music ends
- * //
- * const CHXAvCommand& cmdStart = MakeCommand(this, &Dancer::DoBreakDance);
- * const CHXAvCommand& cmdEnd = MakeCommand(this, &Dancer::Bow, 3);
- * pMusic->SetEventAction(Music::OnBegin, cmdStart);
- * pMusic->SetEventAction(Music::OnEnd, cmdEnd);
- * }
- *
- *
- * Target:
- * Symbian OS
- *
- *
- * (c) 1995-2003 RealNetworks, Inc. Patents pending. All rights reserved.
- *
- ************************************************************************/
- #ifndef _chxavcommand_h_
- #define _chxavcommand_h_
- // Includes from this project...
- #include "chxavrefptr.h"
- //
- // class CHXAvCommand_
- //
- // command base class
- template<typename ReturnType>
- class CHXAvCommand_
- {
- public:
- virtual ReturnType Execute() = 0;
- virtual CHXAvCommand_* CloneL() const = 0;
- virtual ~CHXAvCommand_() {};
- };
- // class CHXAvCommand
- typedef CHXAvCommand_<void> CHXAvCommand;
- typedef refptr<CHXAvCommand> CHXAvCommandPtr;
- // class CHXAvBoolCommand
- typedef CHXAvCommand_<bool> CHXAvBoolCommand;
- typedef refptr<CHXAvBoolCommand> CHXAvBoolCommandPtr;
- //
- // class CHXAvCommand0_
- //
- // call to member func that takes no args, has a return val
- template< typename ClassType, typename ReturnType >
- class CHXAvCommand0_ : public CHXAvCommand_<ReturnType>
- {
- public:
- typedef ReturnType (ClassType::*PCMFN)();
- CHXAvCommand0_(ClassType* pObject, PCMFN pMethod) : m_pObject(pObject), m_pMethod(pMethod) {}
- ReturnType Execute()
- {
- return (m_pObject->*m_pMethod)();
- }
- CHXAvCommand_<ReturnType>* CloneL() const
- {
- return new (ELeave) CHXAvCommand0_(*this);
- }
- private:
- ClassType* m_pObject;
- PCMFN m_pMethod;
- };
- //
- // class CHXAvCommand1_
- //
- // call to member func that takes one arg, has a return val
- template< typename ClassType, typename ReturnType, typename ArgType >
- class CHXAvCommand1_ : public CHXAvCommand_<ReturnType>
- {
- public:
- typedef ReturnType (ClassType::*PCMFN)(ArgType arg);
- CHXAvCommand1_(ClassType* pObject, PCMFN pMethod, ArgType arg)
- : m_pObject(pObject), m_pMethod(pMethod), m_arg(arg) {}
- ReturnType Execute()
- {
- return (m_pObject->*m_pMethod)(m_arg);
- }
- CHXAvCommand_<ReturnType>* CloneL() const
- {
- return new (ELeave) CHXAvCommand1_(*this);
- }
- private:
- ClassType* m_pObject;
- PCMFN m_pMethod;
- ArgType m_arg;
- };
- //
- // class CHXAvCommand0
- //
- // call to member func that takes no args, no return val
- template< typename ClassType>
- class CHXAvCommand0 : public CHXAvCommand
- {
- public:
- typedef void (ClassType::*PCMFN)();
- CHXAvCommand0(ClassType* pObject, PCMFN pMethod)
- : m_pObject(pObject), m_pMethod(pMethod) {}
- void Execute()
- {
- (m_pObject->*m_pMethod)();
- }
- CHXAvCommand* CloneL() const
- {
- return new (ELeave) CHXAvCommand0(*this);
- }
- private:
- ClassType* m_pObject;
- PCMFN m_pMethod;
- };
- //
- // class CHXAvCommand1
- //
- // call to member func that takes one arg, no return val
- template< typename ClassType, typename ArgType >
- class CHXAvCommand1 : public CHXAvCommand
- {
- public:
- typedef void (ClassType::*PCMFN)(ArgType arg);
- CHXAvCommand1(ClassType* pObject, PCMFN pMethod, ArgType arg)
- : m_pObject(pObject), m_pMethod(pMethod), m_arg(arg) {}
- void Execute()
- {
- (m_pObject->*m_pMethod)(m_arg);
- }
- CHXAvCommand* CloneL() const
- {
- return new (ELeave) CHXAvCommand1(*this);
- }
- private:
- ClassType* m_pObject;
- PCMFN m_pMethod;
- ArgType m_arg;
- };
- //
- // convenience helpers
- //
- // these save a few keystrokes (so you don't have to explicity
- // type out template args) and enhance readability
- //
- // MakeCommand(pMyClass, &MyClass::Dance, value)
- //
- // vs.
- //
- // CHXAvCommand0<MyClass, int>(pMyClass, &MyClass::Dance, value);
- //
- // *** note use of long-hand for handling void return; we assume recent C++
- // features (partial specializations, member fun templates, return void)
- // not likely to be supported
- //
- // *** with return value ***
- //
- template< typename ClassType, typename ReturnType >
- inline
- CHXAvCommand0_ <ClassType, ReturnType>
- MakeCommand_( ClassType* pClass, ReturnType (ClassType::*pcmfn)() )
- {
- return CHXAvCommand0_<ClassType, ReturnType>(pClass, pcmfn);
- }
- template< typename ClassType, typename ReturnType, typename ArgType>
- inline
- CHXAvCommand1_<ClassType, ReturnType, ArgType>
- MakeCommand_( ClassType* pClass, ReturnType (ClassType::*pcmfn)(ArgType), ArgType arg )
- {
- return CHXAvCommand1_<ClassType, ReturnType, ArgType>(pClass, pcmfn, arg);
- }
- template< typename ClassType, typename ReturnType >
- inline
- CHXAvCommand0_ <ClassType, ReturnType> *
- AllocCommandL_( ClassType* pClass, ReturnType (ClassType::*pcmfn)() )
- {
- return new CHXAvCommand0_<ClassType, ReturnType>(pClass, pcmfn);
- }
- template< typename ClassType, typename ReturnType, typename ArgType>
- inline
- CHXAvCommand1_<ClassType, ReturnType, ArgType> *
- AllocCommandL_( ClassType* pClass, ReturnType (ClassType::*pcmfn)(ArgType), ArgType arg )
- {
- return new CHXAvCommand1_<ClassType, ReturnType, ArgType>(pClass, pcmfn, arg);
- }
- //
- // *** no return (void) ***
- //
- template< typename ClassType>
- inline
- CHXAvCommand0 <ClassType>
- MakeCommand( ClassType* pClass, void (ClassType::*pcmfn)() )
- {
- return CHXAvCommand0<ClassType>(pClass, pcmfn);
- }
- template< typename ClassType, typename ArgType>
- inline
- CHXAvCommand1<ClassType, ArgType>
- MakeCommand( ClassType* pClass, void (ClassType::*pcmfn)(ArgType), ArgType arg )
- {
- return CHXAvCommand1<ClassType, ArgType>(pClass, pcmfn, arg);
- }
- template< typename ClassType>
- inline
- CHXAvCommand0 <ClassType> *
- AllocCommandL( ClassType* pClass, void (ClassType::*pcmfn)() )
- {
- return new (ELeave) CHXAvCommand0<ClassType>(pClass, pcmfn);
- }
- template< typename ClassType, typename ArgType>
- inline
- CHXAvCommand1<ClassType, ArgType> *
- AllocCommandL( ClassType* pClass, void (ClassType::*pcmfn)(ArgType), ArgType arg )
- {
- return new (ELeave) CHXAvCommand1<ClassType, ArgType>(pClass, pcmfn, arg);
- }
- #endif // _chxavcommand_h_