I’m using the Subject/Observer pattern in a way that I have a class observing 2 kinds of event:
class manager : public observer<eventA>, observer<eventB>
To be valid, the manager class must implement an Update function for each event type, but what could I do if those functions can basically be written by a template?
So, instead of:
class manager : public observer<eventA>, observer<eventB> {
void Update(const eventA& ) { /* do something */ }
void Update(const eventB& ) { /* do something */ }
}
I may be error prone to use a template like:
template<typename T> void Update(const T&) { /* do something */}
I implemented something based on:
- a template function named
update_internal
- and a macro to generate the various ‘real’ implementations for all the types.
It works (similar to the pseudo-code below), but I’m looking for a more elegant solution! Any idea?
class manager : public observer<eventA>, observer<eventB> {
template<typename T> void Update_Internal(const T&) { /* do something */}
#define GEN(TYPE) void Update(TYPE a) { Update_Internal(a); }
GEN(eventA);
GEN(eventB);
}
You need to sign in to view this answers
Leave feedback about this