std :: functions的复合模式

时间:2017-09-23 09:05:37

标签: c++ c++11 templates variadic-templates composite

我正在尝试使用模板类为std::functions实现复合模式,其中每个复合类处理其子项的返回值。
所以模式类可能看起来像这样:

class AbstractClass {
  public:
     virtual void process() = 0;
};

template<typename ReturnType>
class PrimitiveClass : public AbstractClass {
  public: 
    ReturnType process() {
       // please note, that the result is not returned by the return statement
       return this->func();  //this is just for simplicity
    }

  private:
    std::function<ReturnType()> func;
}

template<typename ReturnType, typename ...Args>
class CompositeClass : public AbstractClass {
  public:
    ReturnType process() {
      // --> This is where I want to process all children first and then pass their return values to this->func
      // the following code is kind of a pseudo code:
      for(auto it = vector.begin(); it != vector.end(); ++it {
          results.add((**it).process())
      }
      return this->func(results)
    }

  private:
    std::function<ReturnType(Args...)> func;
    std::vector<std::shared_ptr<AbstractClass>> children;
};

例如,我有CompositeClass std::function<int(int, double, bool),该函数的参数类型也是其子项的ReturnType。我想将孩子们的返回值传递给上述std::function 任何人都可以想到一种方法,我怎么能做到这一点?

1 个答案:

答案 0 :(得分:2)

如果我理解你想要的东西(如果我没错的话)......

(1)解决来自process()的无协变返回值的问题(参见Igor Tandetnik的评论)你需要一个模板抽象类来表达正确的返回值;通过例子

template <typename T>
struct abstClass 
 { virtual T process() const = 0; };

(2)因此,CompositeClass(在以下示例中重命名为nodeClass)继承自abstClass<ReturnType>

(3)你的PrimitiveClass没用,因为你可以管理案例(引用没有参数的函数)作为CompositeClass,其中Args

(4)您需要leafClass来处理基本值

(5)在CompositeClassnodeClass),children,而不是std::vector shared_ptr<AbstractClass>(不能做什么)你想要的,可以是

std::tuple<std::shared_ptr<abstClass<Args>>...>  children;

鉴于这些要点,我提出了以下解决方案(不幸的是,这是C ++ 14,因为使用从{+ C ++ 14开始可用的std::index_sequencestd::make_index_sequence;但如果你需要C ++ 11解决方案,难以为它们编写替代品)

#include <tuple>
#include <memory>
#include <iostream>
#include <functional>

template <typename T>
struct abstClass 
 { virtual T process() const = 0; };

template <typename T>
class leafClass : public abstClass<T>
 {
   private:
      T  value;

   public:
      leafClass (T && v0) : value { std::forward<T>(v0) }
       { }

      T process () const
       { return value; };
 };

template <typename RetT, typename ... ArgTs>
class nodeClass : public abstClass<RetT>
 {
   private:
      using funcT = std::function<RetT(ArgTs...)>;

      template <typename T>
      using shrPAC = std::shared_ptr<abstClass<T>>;

      funcT                         func;
      std::tuple<shrPAC<ArgTs>...>  childrens;

      template <std::size_t ... Is>
      RetT processH (std::index_sequence<Is...> const &) const
       { return func(std::get<Is>(childrens)->process()...); }

   public:
      nodeClass (funcT && f0, shrPAC<ArgTs> && ... as)
         : func { std::forward<funcT>(f0) },
           childrens { std::forward<shrPAC<ArgTs>>(as)... }
       { }

      RetT process () const
       { return processH(std::make_index_sequence<sizeof...(ArgTs)>{}); }
 };

int main ()
 {
   auto func0 = [](int i, double d, bool b) { return int( b ? i+d : i-d ); };

   auto shpLci = std::make_shared<leafClass<int>>(1);
   auto shpLcd = std::make_shared<leafClass<double>>(2.2);

   auto shpNb  = std::make_shared<nodeClass<bool>>([](){ return true; });

   auto shpNc0 = std::make_shared<nodeClass<int, int, double, bool>>
      (func0, shpLci, shpLcd, shpNb);
   auto shpNc1 = std::make_shared<nodeClass<int, int, double, bool>>
      (func0, shpNc0, shpLcd, shpNb);
   auto shpNc2 = std::make_shared<nodeClass<int, int, double, bool>>
      (func0, shpNc1, shpLcd, shpNb);

   std::cout << shpNc0->process() << std::endl; // print 3
   std::cout << shpNc1->process() << std::endl; // print 5
   std::cout << shpNc2->process() << std::endl; // print 7
 }