Horizon Official Technical Documentation
ObservableStatus.hpp
Go to the documentation of this file.
1/***************************************************
2 * _ _ _ *
3 * | | | | (_) *
4 * | |_| | ___ _ __ _ _______ _ __ *
5 * | _ |/ _ \| '__| |_ / _ \| '_ \ *
6 * | | | | (_) | | | |/ / (_) | | | | *
7 * \_| |_/\___/|_| |_/___\___/|_| |_| *
8 ***************************************************
9 * This file is part of Horizon (c).
10 *
11 * Copyright (c) 2019 Sagun K. (sagunxp@gmail.com).
12 * Copyright (c) 2019 Horizon Dev Team.
13 *
14 * Base Author - Sagun K. (sagunxp@gmail.com)
15 *
16 * This library is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation, either version 3 of the License, or
19 * (at your option) any later version.
20 *
21 * This library is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this library. If not, see <http://www.gnu.org/licenses/>.
28 **************************************************/
29
30#ifndef HORIZON_ZONE_GAME_TRAITS_OBSERVABLESTATUS_HPP
31#define HORIZON_ZONE_GAME_TRAITS_OBSERVABLESTATUS_HPP
32
33#include <type_traits>
34#include <tuple>
35template <typename OBSERVABLE, typename ... HAS_OBSERVERS>
37{
38public:
39 explicit ObservableStatus(HAS_OBSERVERS ... args)
40 : _observers(args...)
41 {
42 }
44
45 template<std::size_t I = 0, typename... Tp>
46 inline typename std::enable_if<I == sizeof...(Tp), void>::type
47 notify(std::tuple<Tp...>& /*t*/)
48 { }
49
50 template<std::size_t I = 0, typename... Tp>
51 inline typename std::enable_if<I < sizeof...(Tp), void>::type
52 notify(std::tuple<Tp...>& t)
53 {
54 if (std::get<I>(_observers) != nullptr)
55 std::get<I>(_observers)->on_observable_changed(_observable);
56 notify<I + 1, Tp...>(t);
57 }
58
59 inline void notify_observers() { notify<0, HAS_OBSERVERS...>(_observers); }
60
61 void register_observable(OBSERVABLE obs) { _observable = obs; }
62 void register_observers(HAS_OBSERVERS... obs) { _observers = std::make_tuple(obs...); }
63
64 OBSERVABLE _observable;
65 std::tuple<HAS_OBSERVERS...> _observers;
66};
67
68#endif /* HORIZON_ZONE_GAME_STATUS_OBSERVABLESTATUS_HPP */
Definition: ObservableStatus.hpp:37
void register_observable(OBSERVABLE obs)
Definition: ObservableStatus.hpp:61
ObservableStatus(HAS_OBSERVERS ... args)
Definition: ObservableStatus.hpp:39
std::enable_if< I==sizeof...(Tp), void >::type notify(std::tuple< Tp... > &)
Definition: ObservableStatus.hpp:47
void register_observers(HAS_OBSERVERS... obs)
Definition: ObservableStatus.hpp:62
void notify_observers()
Definition: ObservableStatus.hpp:59
std::tuple< HAS_OBSERVERS... > _observers
Definition: ObservableStatus.hpp:65
OBSERVABLE _observable
Definition: ObservableStatus.hpp:64
~ObservableStatus()
Definition: ObservableStatus.hpp:43