Re: Smart Pointer Map Lookup
Quote:
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.
Quote:
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.
Quote:
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.
Re: Smart Pointer Map Lookup
Quote:
1. What approach replaces function template specialization ?
Function template specialization is overloading.
Just overload.
Some small attention must be paid to conversions.
The compiler is supposed to favor non-template overloads before resorting to a template function instantiation.
More on the rest later....
Re: Smart Pointer Map Lookup
Re: Smart Pointer Map Lookup
I try to overload the type of shared_ptr in parameter.
Code:
// ============ General Command ============
template <typename T>
boost::shared_ptr<GenericCommander>
CommandFactory(boost::shared_ptr<T>& targetObject,
void (T::*MemberFuncPtr)() )
{
// To create one command only
boost::shared_ptr<GenericCommander> returnObj(targetObject);
return returnObj;
}
// =========== Generic Command List =========
template <typename T>
boost::shared_ptr<GenericCommander> CommandListFactory(boost::shared_ptr<T>& targetObject)
{
}
// =========== Administrator Command ========
template <typename T>
boost::shared_ptr<GenericCommander> CommandListFactory(boost::shared_ptr<Administrator>& targetObject) void (T::*MemberFuncPtr)() )
{
// Regiter Administrator MF
/* &Administrator::Add
&Administrator::Delete
&Administrator::Modified
*/
}
// ============= HR Command =================
template <typename T>
boost::shared_ptr<GenericCommander> CommandListFactory(boost::shared_ptr<HumanResource>& targetObject) void (T::*MemberFuncPtr)() )
{
// Regiter HR MF
}
// ============== Staff Command ============
template <typename T>
boost::shared_ptr<GenericCommander> CommandListFactory(boost::shared_ptr<Staff>& targetObject) void (T::*MemberFuncPtr)() )
{
// Register Staff MF
}
// =========================================
Question :
1. How to implement CommandFactory and CommandListFactory ?
2. How to code the usage code for CommandFactory
Code:
shared_ptr<GenericCommand> obk = CommandFactory(thePtr, &thePtr.get()::Add);
Thanks.
Re: Smart Pointer Map Lookup
Yes, i really facing some type mismatch where i don't know how to solve them.
Code:
abstractHumanPtr::HumanPtr thePtr = theUserCreator.createUser();
shared_ptr<GenericCommander> GCPtr = CommandFactory(thePtr, &Administrator::Add);
template <typename T>
boost::shared_ptr<GenericCommander>
CommandFactory(T* targetObject,
void (T::*MemberFuncPtr)() )
{
// To create one command only
boost::shared_ptr<Command<T> > returnObj(new Command<T>(targetObject, MemberFuncPtr));
return returnObj;
}
Quote:
error C2782: 'boost::shared_ptr<T> CommandFactory(boost::shared_ptr<T>,void (__thiscall T::* )(void))' : template parameter 'T' is ambiguous
I know the error saying that the T for thePtr is Human and T for pointer to MF is Administrator. Therefore, compiler complaints.
How to solve that ?
How about creating class template specialization for administrator, staff or HR ?
I hope you also can answers my post 19 question ?
Thanks.
Re: Smart Pointer Map Lookup
Re: Smart Pointer Map Lookup
I not intended to hijack this thread but i really need to finish this assignment.
My problem now is i don't know how to write the CommandFactory ?
I not asking you all to finish this task for me but at least tell me what wrong or give an example.
A billion thanks to those kind helper.