0

我已经在这里问了一个类似的问题,但是我并没有真正得到我想要的答案,因为我的问题表述得很糟糕,而且例子也很糟糕。所以我再试一次,希望有更好的解释和更好的代码。

下面的代码已经去掉了不必要的细节,但它可以工作。问题是如果可能的话,我想使用模板参数推导来简化模板函数调用。

我有一个创建命令的工厂。要创建一个命令,我使用这样的调用:

mCommandFactory.createCommand<
                        DoSomeStuff, 
                        ParameterType1, 
                        ParameterType2, 
                        ParameterType3, 
                        ParameterType4
                        >
                        (std:string("some description"), 
                         parameter1, 
                         parameter2, 
                         parameter3, 
                         parameter4);

您可能已经猜到,parameter1 的类型是 ParameterType1,依此类推...。

现在,如果我们看一下命令的定义 - DoSomeStuff- 本身:

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*/ 
    };

如您所见,ParameterTypeN 信息已经在 DoSomeStuff 声明中。

我想知道是否有可能以某种更简单的方式替换上面的 createCommand 调用:

mCommandFactory.createCommand<DoSomeStuff>
                        (std:string("some description"), 
                         parameter1, 
                         parameter2, 
                         parameter3, 
                         parameter4);

这是 CommandFactory 代码:

    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)();            
        }

基本上,重点是移动CommandFactory 内部的复杂性,以使“客户端代码”(对createCommand 的调用)尽可能简单和简短。

有任何想法吗 ?

4

4 回答 4

1

不确定我是否正确理解了这个问题,但这应该可以。

template<typename ParameterType1 , typename ParameterType2, typename ParameterType3 , typename ParameterType4>
class UndoableCommand
{
public:
    UndoableCommand(ParameterType1 p1, ParameterType2 p2, ParameterType3 p3, ParameterType4 p4)
    {}
};

class DoSomeStuff : public UndoableCommand< int ,double, std::string , int>
{
public:
    DoSomeStuff(int p1, double p2, std::string p3, int p4)
        :UndoableCommand(p1,p2,p3,p4)
    {}
};

class CommandFactory
{
public:
    CommandFactory()
    {}

    template <class CommandType, typename P1, typename P2, typename P3, typename P4> 
    void createCommand( std::string& description,P1 p1, P2 p2, P3 p3, P4 p4)
    {
        UndoableCommand<P1,P2,P3,P4> * cmdPtr = new CommandType(p1, p2, p3, p4);

    }
 };

这样使用

CommandFactory fact;
fact.createCommand<DoSomeStuff>(std::string("description"),1,2.0,std::string("3"),4);
于 2012-08-20T14:52:31.333 回答
1

你在这里不需要什么特别的东西,你的方法mCommandFactory.createCommand<DoSomeStuff>(...);会奏效。编译器会自动从函数调用表达式中扣除给定参数的类型并createCommand<>正确实例化模板。

于 2012-08-20T16:33:30.060 回答
0

是的,您可以用更简单的东西替换它。你考虑过std::bind吗?

一旦你使用 change it to use <function>,你的 DoSomeStuff 类将只是一个包含一对std::functions 的结构。

于 2012-08-15T16:08:34.467 回答
0

您可以将 Parameter 类型的 typedef 设置为 Commandtype 类,从而在 createCommand 函数中解析参数类型,而无需将它们显式添加为模板参数。为了实现这一点,您可以在该类中将模板参数的 typedef 传递给 UndoableCommand。

于 2012-08-18T19:21:31.240 回答