1

假设最初我使用 CRTP 进行了以下设计:

template<class Outputter> class Generator {
protected:
    vector<int> v;
private:
    void work(ostream& out) {
        // perform first part of some complex operations on v
        out << *static_cast<Outputter *>(this);
        // perform second part of some complex operations on v
        out << *static_cast<Outputter *>(this);
        // many more ....
        // perform some final actions
    }
public:
    Generator(unsigned length): v(length) {}
    friend ostream& operator<<(ostream& out, Outputter&& generator) {
        // perform some preparation work
        work(out);
        // perform some final actions
        return out;
    }
};

class SimpleDumpOutputter : public Generator<SimpleDumpOutputter> {
private:
    unsigned count;
public:
    SimpleDumpOutputter(unsigned length): Generator(length), count() {}
    friend ostream& operator<<(ostream& out, SimpleDumpOutputter& outputter) {
        out << "Step " << ++count << " of calculation: "
        copy(outputter.v.begin(), outputter.v.end(), ostream_iterator<int>(out, " "));
        out << endl;
        return out;
    }
};

class FancyOutputter : public Generator<FancyOutputter> { // create a graph using graphviz's dot language to visualise v
private:
    // abbreviated
public:
    FancyOutputter(unsigned length): Generator(length) {}
    friend ostream& operator<<(ostream& out, FancyOutputter& outputter) {
        // write statements to out
        return out;
    }
};

// some more different Outputters, for example an Outputter that creates a pretty LaTeX document

在这个设计中,有一个GeneratorCRTP 类模板,它使用其派生类的 befriended 对 执行复杂计算并在计算的vector<int> v每个步骤/部分打印结果operator<<

这是我想要实现的一个有趣的概念:我希望在一次执行中以多种格式输出。具体来说,我认为我可以这样做:

template<class Outputters> class AggregateOutputter : public Generator<AggregateOutputter<Outputters...> > {
private:
    static const unsigned outputter_count = sizeof...(Outputters);
    typedef array<ostream *, outputter_count> DestArr;
    DestArr destinations;
public:
    AggregateOutputter(unsigned v_length, DestArr destinations): IsomerGenerator<AggregateOutputter<Outputters...> >(length), destinations(destinations) {}
    friend ostream& operator<<(ostream&, AggregateOutputter& outputter); // first argument is dummy, because we would use the ostreams in destinations
}

这个想法是用户会使用,比如说,并用两个sAggregateOutputter<SimpleDumpOutputter, FancyOutputter构造对象。每当调用输出器类时,都会遍历s in和类型 in并调用类似.arrayostreamGeneratoroperator<<AggregateOutputterostreamdestinationsOutputters*dest_iter << *static_cast<Outputter_Iter>(this);

我不确定这将如何工作。我不确定是否可以以这种方式使用多重继承,是否可以在一个array和一组参数化类型之间“压缩”。有没有人了解这种情况?

4

2 回答 2

3

我修改了你原来的设计。我认为在调用输出运算符时生成器进行大量计算至少可以说是令人惊讶的。此外,您的 AggregateOutputter 输出忽略 << 的 ostream 参数也令人惊讶。此外,Outputter 与 Generator 没有 is-a 关系。

我试图分离出这些问题,最终没有使用 CRTP,而是使用了可变参数模板,但我认为它可以满足您的需求。

http://ideone.com/xQrnW4

#include <vector>
#include <iostream>
#include <iterator>
#include <array>
using namespace std;

class Generator {
protected:
    vector<int> v;
public:
    Generator(unsigned length): v(length) {}

    template<class Outputter>
    void do_calculations_with_output(Outputter& out){
        // perform first part of some complex operations on v
        out.output(v);
        // perform second part of some complex operations on v
        out.output(v);
        // perform some final actions
    }

};

class SimpleDumpOutputter {
private:

    ostream* out;
    unsigned count;
public:
    SimpleDumpOutputter(ostream& os): out(&os), count() {}
    template<class C>
    void output(const C& c) {
        *out << "Step " << ++count << " of calculation: ";
        copy(c.begin(),c.end(), ostream_iterator<int>(*out, " "));
        *out << endl;
    }
};

class FancyOutputter {
    ostream* out;
    int count;
public:
    FancyOutputter(ostream& os): out(&os),count() {}
    template<class C>
    void output(const C& c) {
        // create a graph using graphviz's dot language to ease visualisation of v
        *out << "Step " << ++count << " of calculation: ";
       *out << "Graphviz output\n";
    }
};

template<class... Outputters> class AggregateOutputter : private Outputters... {
private:
   template<class First, class... Rest>
   struct output_helper{
      template<class C>
      static void do_output(AggregateOutputter* pthis,const C& c){
          static_cast<First*>(pthis)->output(c);
          output_helper<Rest...>::do_output(pthis,c);
      }

   };

   template<class First>
   struct output_helper<First>{
      template<class C>
      static void do_output(AggregateOutputter* pthis,const C& c){
          static_cast<First*>(pthis)->output(c);
      }

   };
public:
   template<class... Out>
    AggregateOutputter( Out&... out): Outputters(out)...{}
    template<class C>
    void output(const C& c) {
        output_helper<Outputters...>::do_output(this,c);
    }

};
int main(){

    AggregateOutputter<FancyOutputter,SimpleDumpOutputter> out(cout,cout);

    Generator g(10);

    g.do_calculations_with_output(out);

}
于 2013-04-06T21:00:50.033 回答
0

好的,这是我在受到 John Bandela 的解决方案启发后提出的解决方案。(请参阅我对答案的评论,了解为什么我认为他的方法不符合我的需求)

template<class... Outputters> class AggregateOutputter : public Generator<AggregateOutputter<Outputters...> > {
private:
    typedef array<ostream *, sizeof...(Outputters)> DestArr;
    DestArr destinations;
    typedef typename DestArr::iterator DestArrIter;
    struct OutputterHolder : public Outputters... {
        OutputterHolder(vector<int>& v): Outputters(v)... {}
    } outputter_holder;
    template<class First, class... Rest> struct OutputHelper {
        static void do_output(OutputterHolder *pthis, DestArrIter dest) {
            **dest << *static_cast<First *>(pthis);
            OutputHelper<Rest...>::do_output(pthis, ++dest);
        }
    };
    template<class First> struct OutputHelper<First> {
        static void do_output(OutputterHolder *pthis, DestArrIter dest) {
            **dest << *static_cast<First *>(pthis);
        }
    };
public:
    template<typename... OstreamStar> AggregateOutputter(unsigned length, OstreamStar... ostreams): Generator<AggregateOutputter<Outputters...> >(length), destinations{{ostreams...}}, outputter_holder(this->v) {
        static_assert(sizeof...(OstreamStar) == sizeof...(Outputters), "number of outputters and destinations do not match");
    }
    friend ostream& operator<<(ostream& dummy_out, AggregateOutputter& outputter) {
        OutputHelper<Outputters...>::do_output(&outputter.outputter_holder, outputter.destinations.begin());
        // possibly write some logging info to dummy_out
        return dummy_out;
    }
};

// to use this:
ofstream fout("gv.gv");
cout << AggregateOutputter<FancyOutputter, SimpleDumpOutputter>(length, &fout, &cout);

这个想法是除了output_helper在约翰的答案(我已重命名为OutputHelper)之外,还有另一个struct名为的辅助OutputterHolder,它继承自所有Outputters. 我还使用了arrayofostream *来存储输出的目的地,并修改do_output为也采用 aniterator以便ostream可以匹配正确的。

重要的是,为了配合更改,我将受保护的成员vector<int> vinGenerator更改为引用,即vector<int>& v,以便outputter_holder可以使 in 的数据结构引用 in 的结构AggregateOutputter。这还需要在所有采用vector<int>&. 长度为 的原始构造函数v现在将使用new.

我不确定我想出的这个解决方案是最好/最优雅的解决方案。

于 2013-04-07T05:13:03.760 回答