CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 22 of 22
  1. #16
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    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.

  2. #17
    Join Date
    Nov 2006
    Posts
    1,611

    Re: Smart Pointer Map Lookup

    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....
    If my post was interesting or helpful, perhaps you would consider clicking the 'rate this post' to let me know (middle icon of the group in the upper right of the post).

  3. #18
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Smart Pointer Map Lookup

    Does my solution works ?
    Thanks for your help.

  4. #19
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    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.
    Last edited by Peter_APIIT; August 6th, 2009 at 03:57 AM.
    Thanks for your help.

  5. #20
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    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;
    }
    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.
    Last edited by Peter_APIIT; August 9th, 2009 at 02:58 AM.
    Thanks for your help.

  6. #21
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Smart Pointer Map Lookup

    Please help JVene.
    Thanks for your help.

  7. #22
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    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.
    Thanks for your help.

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured