I am having a design problem here. Basically, I have streaming data coming from a remote server, and at the same time I need to analyze these data. The class dataReceiver is an api class provided by the remote server,
// in file dataReceiver.cpp
class dataReceiver{
public: // some data members
public:
// some other member functions
void dataUpdate(string time, string tag, double value){
// this member function is called by the remote server
// in a manner which can not be predetermined. I can not
// change the interface for this function, but I can do
// anything I want inside this function.
}
}
Then there are another type of classes dataAnalyzer doing analysis on these data, for example,
// in file dataAnalyzer_1.cpp
include "dataReceiver.cpp"
class dataAnalyzer_1{
private:
dataReceiver receiver;
vector<pair<string, double>>data_to_be_analyzed; // pair is <tag, value> pair
public:
dataAnalyzer_1():receiver(something special for dataAnalyzer_1){}
void getData(){
receiver.dataUpdate(time, tag, value);
}
void analyze(){
// do something about data_tags
}
}
I may have several different dataAnalyzers, like dataAnalyzer_2, dataAnalyzer_3, these classes maybe written by totally uncorrelated people, and are very different in their class structure, but all of them have kind of same data member like data_to_be_analyzed in dataAnalyzer_1, and their own cpp files (dataAnalyzer_2.cpp, dataAnalyzer_3.cpp) all need to include "dataReceiver.cpp". I want to compile dataReceiver.cpp and dataAnalyzer_#.cpp separately and link later.
My problem is that, in order for the data_to_be_analyzed in dataAnalyzer_#.cpp to get the latest tag, value from receiver.dataUpdate(time, tag, value);, I need to save tag,value into a data member of dataReceiver, and copy the data there into data_to_be_analyzed in dataAnalyzer_#.cpp, this copying is what I don't like, I want the latest tag, value from receiver.dataUpdate(time, tag, value); directly to go to data_to_be_analyzed in dataAnalyzer_#.cpp. But somehow I want a more general type of thing like template or pointer that to be implemented inside dataReceiver::dataUpdate, and e.g., when dataAnalyzer_2 is constructed, its data member receiver and receiver.dataUpdate get specialized for dataAnalyzer_2. Is this doable in C++? If so, how would you do this?
PS:
I find it's difficult to come up with a suitable title for my question, if you have a better one, I will considering updating the title.
Aucun commentaire:
Enregistrer un commentaire