Bertini 2 - C++ Core
Software for Numerical Algebraic Geometry
Loading...
Searching...
No Matches
observable.hpp
Go to the documentation of this file.
1//This file is part of Bertini 2.
2//
3//bertini2/detail/observable.hpp is free software: you can redistribute it and/or modify
4//it under the terms of the GNU General Public License as published by
5//the Free Software Foundation, either version 3 of the License, or
6//(at your option) any later version.
7//
8//bertini2/detail/observable.hpp is distributed in the hope that it will be useful,
9//but WITHOUT ANY WARRANTY; without even the implied warranty of
10//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11//GNU General Public License for more details.
12//
13//You should have received a copy of the GNU General Public License
14//along with bertini2/detail/observable.hpp. If not, see <http://www.gnu.org/licenses/>.
15//
16// Copyright(C) 2015 - 2021 by Bertini2 Development Team
17//
18// See <http://www.gnu.org/licenses/> for a copy of the license,
19// as well as COPYING. Bertini2 is provided with permitted
20// additional terms in the b2/licenses/ directory.
21
22// individual authors of this file include:
23// silviana amethyst, university of wisconsin-eau claire
24//
25
33
34#ifndef BERTINI_DETAIL_OBSERVABLE_HPP
35#define BERTINI_DETAIL_OBSERVABLE_HPP
36
39
40namespace bertini{
41
48 {
49 public:
50
51 virtual ~Observable() = default;
52
53
57 void AddObserver(AnyObserver& new_observer) const
58 {
59 if (find_if(begin(current_watchers_), end(current_watchers_), [&](const auto& held_obs)
60 { return &held_obs.get() == &new_observer; })==end(current_watchers_))
61
62 current_watchers_.push_back(std::ref(new_observer));
63 }
64
68 void RemoveObserver(AnyObserver& observer) const
69 {
70
71 auto new_end = std::remove_if(current_watchers_.begin(), current_watchers_.end(),
72 [&](const auto& held_obs)
73 { return &held_obs.get() == &observer; });
74
75 current_watchers_.erase(new_end, current_watchers_.end());
76
77
78 // current_watchers_.erase(std::remove(current_watchers_.begin(), current_watchers_.end(), std::ref(observer)), current_watchers_.end());
79 }
80
81 protected:
82
90 void NotifyObservers(AnyEvent const& e) const
91 {
92
93 for (auto& obs : current_watchers_)
94 obs.get().Observe(e);
95
96 }
97
98 void NotifyObservers(AnyEvent & e) const
99 {
100
101 for (auto& obs : current_watchers_)
102 obs.get().Observe(e);
103 }
104
105
106 private:
107
108 using ObserverContainer = std::vector<std::reference_wrapper<AnyObserver>>;
109
110 mutable ObserverContainer current_watchers_;
111 };
112
113} // namespace bertini
114
115
116#endif
117
Strawman Event type, enabling polymorphism.
Definition detail/events.hpp:47
Strawman base class for Observer objects.
Definition observer.hpp:57
An abstract observable type, maintaining a list of observers, who can be notified in case of Events.
Definition observable.hpp:48
void RemoveObserver(AnyObserver &observer) const
Remove an observer from this observable.
Definition observable.hpp:68
void AddObserver(AnyObserver &new_observer) const
Add an observer, to observe this observable.
Definition observable.hpp:57
void NotifyObservers(AnyEvent const &e) const
Sends an Event (more particularly, AnyEvent) to all watching observers of this object.
Definition observable.hpp:90
Contains the detail/events base types.
Contains the observer base types.