|
-
August 4th, 2009, 11:15 PM
#16
Re: Smart Pointer Map Lookup
shared_ptr<Human> sph( new Staff );
shared_ptr<CommandBase> cp = new Command<Human>(spf, ...);
I don't understand this.
This and the example just before it simply indicate that if you're going to prepare for a call to a function in the Staff class, the Command that's fashioned has to be a Command<Staff>, however (this snippet) points out that even if the object IS a Staff, if the target function is really in Human (it's base), the command can be fashioned Command<Human>, if it's convenient.
I got it. Thanks.
On the other hand, it is entirely reasonable to want a command system that allows you to select functions for any target object. This target object can be supplied at the time the call is required (this is more complex and flexible - I don't yet sense this is your interest).
This is my final choice in case my solution cannot solve the commandList interface problem.
I got another solution like this. CommandList composes Vector of command.
class GenericCommander;
class ComamndList : public GenericCommander {};
class Command : public GenericCommander {};
This approach works with single command and list of command.
Code:
class GenericCommand
{
public:
GenericCommand();
virtual ~GenericCommand();
virtual void ExecuteSignalCallBack(int) = 0;
};
template <typename T>
class Command : public GenericCommand
{
public:
typedef boost::shared_ptr<T> objectPtr;
private:
objectPtr targetObjectCommandCallerPtr;
void (T::*MemberFuncPtr)();
public:
Command();
explicit Command(objectPtr& );
~Command();
void ExecuteSignalCallBack(int);
};
template <typename T>
Command<T>::Command() : targetObjectCommandCallerPtr(objectPtr())
{
}
// =========================================
template <typename T>
Command<T>::Command(objectPtr& user)
: targetObjectCommandCallerPtr(objectPtr(user))
{
}
// =========================================
template <typename T>
Command<T>::~Command()
{
}
// =========================================
template <typename T>
void Command<T>::ExecuteSignalCallBack(int choice)
{
(*targetObjectCommandCallerPtr.*MemberFuncPtr)();
}
template <typename T>
class Command;
template <typename T>
class CommandList : public GenericCommand
{
public:
typedef boost::shared_ptr<T> objectPtr;
private:
Command<T> cont[15];
public:
CommandList();
explicit CommandList(objectPtr&);
~CommandList();
void ExecuteSignalCallBack(int);
// void CallCommand(int );
};
// =========================================
template <typename T>
CommandList<T>::CommandList()
: cont(Command<T>())
{
}
// =========================================
template <typename T>
CommandList<T>::CommandList(objectPtr& userTargetObjectPtr)
: cont(Command<T>(userTargetObjectPtr))
{
}
// =========================================
template <typename T>
CommandList<T>::~CommandList()
{
}
// =========================================
template <typename T>
void CommandList<T>::ExecuteSignalCallBack(int choice)
{
(cont[choice - 1].*MemberFuncPtr)();
}
// =========================================
*
Not recommended to specialize
Function Template
*/
// ============ General Command ============
template <typename T>
boost::shared_ptr<GenericCommander>
CommandFactory(boost::shared_ptr<T>& targetObject,
void (T::*MemberFuncPtr)() )
{
// To create one command only
}
// =========== Generic Command List =========
template <typename T>
boost::shared_ptr<GenericCommander> CommandListFactory(boost::shared_ptr<T>& targetObject)
{
}
// =========== Administrator Command ========
template <>
boost::shared_ptr<GenericCommander> CommandListFactory<Administrator>(boost::shared_ptr<Administrator>& targetObject) void (T::*MemberFuncPtr)() )
{
// Regiter Administrator MF
/*
&Administrator::Add
&Administrator::Delete
&Administrator::Modified
*/
}
// ============= HR Command =================
template <>
boost::shared_ptr<GenericCommander> CommandListFactory<HumanResource>(boost::shared_ptr<HumanResource>& targetObject) void (T::*MemberFuncPtr)() )
{
// Regiter HR MF
}
// ============== Staff Command ============
template <>
boost::shared_ptr<GenericCommander> CommandListFactory<Staff>(boost::shared_ptr<Staff>& targetObject) void (T::*MemberFuncPtr)() )
{
// Register Staff MF
}
// =========================================
1. What approach replaces function template specialization ?
Usage Code:
share_ptr<GenericCommander> GCPtr = CommandFactory(thePtr);
GCPtr->ExecuteSignalCallBack(int);
Thanks for your help.
Thanks for your help.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|