I have defined a 'vertex' class which, when combined, form a graph. these vertices receive input on one end and produce output on the other (both of these sides can have 0, 1, or more 'subscriptions' of other instances of this vertex class) The types for both the incoming and outgoing data are independent of each other and are specified as template parameters as these vertices must operate on all kinds of in/output types.
So in code this becomes:
template<class incomingDataType, class OutgoingDataType>
class Vertex {...}
Before sending out the received data, a vertex can apply a filter function to transform/filter the data before sending it back out again to its list subscribers. This function is declared as a pure virtual function as t开发者_如何学Gohe functionality of it depends on the kind of specialization class inheriting from this virtual vertex base class.
This means the filter is declared as:
OutgoingDataType filter(incomingDataType data) = 0;
Of course I want to link together multiple vertices to each other. Consider this linking in its most simple form where each vertex's input and output is connected to exactly one other vertex's input/output:
(ClassX)vertex1(int) --> (int)vertex2(std::string) --> (std::string)vertex3(OtherClassOrType) -->...
With this in mind, the problem statement is as follows for the given example:
vertex2 must list vertex1 as a subscriber and subscribe itself to vertex3. In order to keep track of all these subscriptions, a vertex keeps a set of pointers to all its publishers and subscribers:template<class incomingDataType, class OutgoingDataType>
class Vertex {
....
bool subscribe(Vertex<OutgoingDataType, __DontCareType__>* subscriber);
OutgoingDataType filter(incomingDataType data) = 0;
....
//set of 'outgoing vertices' - whom we provide with data
std::set< Vertex<OutgoingDataType, __DontCareType__>* >subscribers_;
//set of 'incomming vertices' - providing us with data
std::set< Vertex<__DontCareType__, incomingDataType>* >publishers_;
}
A vertex subscribing to us needs to match its incoming data type with our outgoing data type but we don't care about the subscriber's outgoing data type as it does not concern us. The same applies for the incoming data type of our publishers.
Unfortunately I can't seem to figure out how I can specify this. I've already fiddled with boost::any (won't compile), void pointers (won't compile), using function templates instead of class templates (won't work due to virtual filter function)... but nothing I can think of works.
So in short, I'm looking for a way to have a 'DontCareType' type as a template parameter, if that is possible at all, which is basically a way of saying "the template parameter I mark as 'I don't care' can be of any type, because I don't use its data anyway"
As a last resort to get a working solution, I'm also considering the option of using a kind of "VertexData" type, which must then be the base class of any incoming or outgoing data types used in the vertices. Would this be of any help?
Any other suggestions for making this feasible are of course also welcome.
You want to use run-time inheritance for this.
template<typename T> struct InputVertex { ... };
template<typename T> struct OutputVertex { ... };
template<typename Incoming, typename Outgoing> struct Vertex
: public InputVertex<Incoming>, public OutputVertex<Outgoing> {
std::set<InputVertex<Outgoing>*> outgoings;
public:
void RegisterFilter(InputVertex<Outgoing>* ptr) {
outgoings.insert(ptr);
}
void RemoveFilter(InputVertex<Outgoing>* ptr) {
outgoings.erase(ptr);
}
void Process(const std::vector<Incoming>& ref) {
std::vector<Outgoing> output;
// Transform
for(auto ref : outgoings) {
outgoings->Process(output);
}
}
};
In this case, the interface for incoming and outgoing is clearly separated, making it much easier to deal with the two independently. Excuse my use of C++0x's range-based for.
精彩评论