I already asked a similar question here, however I didn’t really get the answer I wanted because my question was poorly formulated and the examples were bad. So I give it another shot, with hopefully a better explanation and better code .
The code bellow has been stripped out of unnecessary details but it works .
The thing is I would like to use Template Argument Deduction, if possible, to simplify the templated function call .
I have a factory which creates commands. To create a command, I use a call like that one :
mCommandFactory.createCommand<
DoSomeStuff,
ParameterType1,
ParameterType2,
ParameterType3,
ParameterType4
>
(std:string("some description"),
parameter1,
parameter2,
parameter3,
parameter4);
As you probably have guessed, parameter1 type is ParameterType1, and so on … .
Now if we look at the definition of the command – DoSomeStuff- itself :
class DoSomeStuff : public UndoableCommand< ParameterType1 , ParameterType2, ParameterType3 , ParameterType4 >
{
public:
DoSomeStuff(... /* arguments which are needed for precessing the command and undoing it*/ );
~DoSomeStuff() throw();
void executeImpl();
void undoImpl();
protected:
... /* members which are needed for precessing the command and undoing it*/
};
As you can see, ParameterTypeN information is already inside DoSomeStuff declaration.
I was wondering if it was possible somehow to replace the createCommand call above by something simpler :
mCommandFactory.createCommand<DoSomeStuff>
(std:string("some description"),
parameter1,
parameter2,
parameter3,
parameter4);
Here is the CommandFactory code :
class CommandFactory
{
private:
// some stuff used to initialize objects created by this factory
public:
CommandFactory(...) : ... /* members initialization */
{
}
template <class CommandType, typename P1, typename P2, typename P3, typename P4>
void createCommand(juce::String& description,P1 p1, P2 p2, P3 p3, P4 p4)
{
Undoable* cmdPtr = new CommandType(p1, p2, p3, p4);
...
// init cmdPtr
(*cmdPtr)();
}
Basically the point would be to move the complexity inside CommandFactory, to keep the “client code” (the call to createCommand) as simple and short as possible.
Any ideas ?
Not sure I understand the question correctly, but this should work.
Used as such