Reputation: 1
I want to be able to declare a type of a class based on usage. For example, take a 3 stage pipeline, which has 3 queues connecting them together. I want to make sure that I connect the stages together with the queues correctly (ex: the queues are going in the right direction, to the right units, etc..)
Here is an extremely simplified example of what I am trying to accomplish:
template<class in, class out>
class Fifo: public queue{};
class Stage1; class Stage2; class Stage3;
class Stage1{
Fifo<Stage1,Stage2>* m_fifoOut;
};
class Stage2{
Fifo<Stage1,Stage2>* m_fifoIn;
Fifo<Stage2,Stage3>* m_fifoOut;
};
class Stage3{
Fifo<Stage2,Stage3>* m_fifoIn;
};
Is there a more appropriate way to achieve this functionally? It is preferred to know at compile time that the queues have been incorrectly set up. However, I am worried that using a template generates unnecessary copies of classes.
Upvotes: 0
Views: 298
Reputation: 490108
I think I'd try to represent a pipeline a bit more directly. Specifically, I'd try to concentrate on the pipeline stages instead of the FIFO. If you have C++11 available, one possibility would be a variadic template where you specify the types of the pipeline stages, and it automatically generates a FIFO between each pipeline stage and the next.
Upvotes: 2
Reputation: 131789
I want to make sure that I connect the stages together with the queues correctly (ex: the queues are going in the right direction, to the right units, etc..)
Just have a meta function that generates the second type, and just specify the first type.
template<class In>
struct get_target;
class Stage1; class Stage2; class Stage3;
template<>
struct get_target<Stage1>{
typedef Stage2 type;
};
template<>
struct get_target<Stage2>{
typedef Stage3 type;
};
template<class In>
class Fifo : public queue{
typedef In in_type;
typedef typename get_target<In>::type out_type;
};
struct Stage1{
Fifo<Stage1> m_fifoOut;
};
struct Stage2{
Fifo<Stage1> m_fifoIn;
Fifo<Stage2> m_fifoOut;
};
struct Stage1{
Fifo<Stage2> m_fifoIn;
};
Upvotes: 1
Reputation: 9863
It looks entirely reasonable as far as it goes. I can assure you that at the company I work for, we regularly do much wilder things with templates than that (you wouldn't want to know), and have never had any issues related to duplicate object code (what I assume is what you are thinking of).
Upvotes: 0