I'm trying to implement an EventProvider/Handler system to facilitate setting up a small application with reactive GUI for a school project. This particular approach to the broader Observer pattern is not mandated, but it seems like the most robust and lightweight - provided it's actually possible.
#pragma once
#include <vector>;
#include <algorithm>;
/// Provides a lightweight interface for implementing event-based behaviour.
template <class ...arg> class EventProvider
{
public:
/// Defines a pointer to a function equipped to handle an event from this provider.
typedef void( *EventHandler )(arg... arguments);
EventProvider();
virtual ~EventProvider();
void Subscribe( EventHandler handler );
void Unsubscibe( EventHandler handler );
void Notify( arg... arguments );
protected:
vector<EventHandler> _handlers;
};
template<class ...arg>
EventProvider<...arg>::EventProvider()
{}
template<class ...arg>
EventProvider<...arg>::~EventProvider()
{}
template<class ...arg>
inline void EventProvider<...arg>::Notify( arg... arguments )
{
for ( auto handler : _handlers )
{
handler( arguments );
}
}
template<class ...arg>
inline void EventProvider<...arg>::Subscribe( EventHandler handler )
{
if ( find( _handlers.begin(), _handlers.end(), handler ) == _handlers.end() )
{
_handlers.push_back( handler );
}
}
template<class ...arg>
inline void EventProvider<...arg>::Unsubscibe( EventHandler handler )
{
_handlers.erase( remove( _handlers.begin(), _handlers.end(), handler ), _handlers.end() );
}
What I have so far builds fine on its own, but if I actually try and include this header and use the code in other places, I get dozens of errors, all of them duplicates of...
C2238 unexpected token(s) preceding ';'
C2143 syntax error: missing ';' before '<'
C4430 missing type specifier - int assumed. Note: C++ does not support default-int
On the line where I've declared my vector of EventHandlers.
C2509 syntax error: '...'
C3211 'EventProvider<>::FunctionName': explicit specialization is using partial specialization syntax, use template <> instead
On all of the function signatures in the implementation following the class template declaration.
Aucun commentaire:
Enregistrer un commentaire