This is a bit convoluted...
There is existing code for an observer pattern. There is a data repository class. There is a data observer super class with an update() function. Usually I derive from the data observer and call attach on the data I want to observe. When the data repository updates the data it will call the update() function on my observer class. The code sorta looks like this.
to use that code I do the following:Code:class DataObserver { public: virtual void Update()=0; } class DataClass { public: void Attach(DataObserver * obs); } class DataRepository { DataClass data[somelength]; }
So that all works fine. My only issue is that I got to create a lot of "MyObserver" classes each time I want to observe a specific data field. And I will observe the same field in multiple address spaces. So I'd like to make MyObserver a template. I am not allowed to change the old observer pattern code :/Code:class MyObserver : public DataObserver { public: MyAlgoClass* algoClassPtr; MyObserver (MyAlgoClass* myClass); virtual void Update(); } void MyObserver ::Update(void) { DataType data; data = static_cast<DataType>(dataPtr->GetData()); algoClassPtr->SomeRandomFunction(data); }
So here is my templete so far:
So the porblem lies in MyAlgoClass* algoClassPtr; That class can be different for every observer class I might have. So I believe i need something like a callback pattern. Which I could probably figure out, but having a template is making it hard to figure out the best way to do it. So what is the best way to have my observer template have the ability to call any random function from any class?Code:template <typename DataType, fieldId FieldId> class GenericObserver : public DataObserver { public: MyAlgoClass* algoClassPtr; MyObserver (MyAlgoClass* myClass); virtual void Update(); } template <typename DataType, fieldId FieldId> void GenericObserver<FieldType, FieldId> ::Update(void) { DataType data; data= static_cast<DataType >(dataPtr->GetData()); algoClassPtr->SomeRandomFunction(data); }
Let me know if something isn't clear as I skipped around trying to make this short.




Reply With Quote