our_dick/include/egn/observable.hpp

74 lines
1.9 KiB
C++

/**
This file is a part of our_dick
Copyright (C) 2020 rexy712
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef OUR_DICK_ENGINE_OBSERVABLE_HPP
#define OUR_DICK_ENGINE_OBSERVABLE_HPP
#include <vector>
namespace egn{
template<typename Event>
class observable;
template<typename Event>
class observer
{
public:
using event_type = Event;
using event_ref = event_type&;
using const_event_ref = const event_type&;
using observed_type = observable<event_type>;
using observed_ref = observed_type&;
using const_observed_ref = const observed_type&;
using observed_pointer = observed_type*;
using const_observed_pointer = const observed_type*;
public:
virtual ~observer(void) = default;
virtual void on_notify(const_event_ref e) = 0;
};
template<typename Event>
class observable
{
public:
using event_type = Event;
using event_ref = event_type&;
using const_event_ref = const event_type&;
using observer_type = observer<event_type>;
using observer_ref = observer_type&;
using const_observer_ref = const observer_type&;
using observer_pointer = observer_type*;
using const_observer_pointer = const observer_type*;
private:
std::vector<observer_pointer> m_observers;
public:
void notify(const_event_ref e);
void add_observer(observer_ref o);
void remove_observer(observer_ref o);
};
}
#include "observable.tpp"
#endif