Observer.hpp 5.06 KB
Newer Older
1
2
#pragma once

Praetorius, Simon's avatar
Praetorius, Simon committed
3
#include <set>
4
#include <utility>
Praetorius, Simon's avatar
Praetorius, Simon committed
5

6
#include <amdis/Output.hpp>
7
#include <amdis/common/ConceptsBase.hpp>
Praetorius, Simon's avatar
Praetorius, Simon committed
8
#include <amdis/common/Index.hpp>
9
#include <amdis/common/TypeTraits.hpp>
10
11
12

namespace AMDiS
{
Praetorius, Simon's avatar
Praetorius, Simon committed
13
  namespace event
14
  {
15
16
    /**
     * An event that is signaled before the actual adaption happens. Example: grid.preAdapt().
Praetorius, Simon's avatar
Praetorius, Simon committed
17
18
19
     * The \ref value might indicate whether any pre-processing is necessary.
     **/
    struct preAdapt { bool value = true; };
20

21
    /**
Praetorius, Simon's avatar
Praetorius, Simon committed
22
23
24
25
26
     * An event that is called directly of the adaption. Example: grid.adapt().
     * The \ref value indicates whether something is changed during adaption.
     **/
    struct adapt { bool value = true; };

27
    /**
Praetorius, Simon's avatar
Praetorius, Simon committed
28
29
30
31
32
33
34
35
36
37
38
39
     * An event that is called after adaption to indicate the start of a clean-up phase.
     **/
    struct postAdapt {};
  }


  template <class Event>
  class ObserverInterface
  {
  public:
    virtual ~ObserverInterface() = default;
    virtual void update(Event e) = 0;
Müller, Felix's avatar
Müller, Felix committed
40
    virtual void unset() = 0;
Praetorius, Simon's avatar
Praetorius, Simon committed
41
  };
Praetorius, Simon's avatar
Praetorius, Simon committed
42
43
44
45


  /// \brief Mixin for signaling of certain events.
  /**
46
47
48
49
50
51
52
53
   *  Derived classes T can emit a signal e by calling notify(e). This will send the signal to all
   *  classes U... deriving from Observer<S, Events...> if
   *  - the type of the event is included in Events,
   *  - T = S or S is an Observer of T (directly or indirectly via other Observers),
   *  - U called the Observer constructor with an instance of S that has direct or indirect access
   *      to the instance of T (see Observer)
   */
  template <class Event, class... Events>
Praetorius, Simon's avatar
Praetorius, Simon committed
54
55
56
  class Notifier
      : public Notifier<Event>
      , public Notifier<Events...>
57
58
  {
  public:
Praetorius, Simon's avatar
Praetorius, Simon committed
59
60
    using Notifier<Event>::notify;
    using Notifier<Events...>::notify;
61
62
63
  };

  template <class Event>
Praetorius, Simon's avatar
Praetorius, Simon committed
64
  class Notifier<Event>
65
66
  {
  public:
67
68
    virtual ~Notifier()
    {
Müller, Felix's avatar
Müller, Felix committed
69
      // Remove remaining pointers to this to avoid segfaults
70
      for (ObserverInterface<Event>* o : observers_)
Müller, Felix's avatar
Müller, Felix committed
71
        o->unset();
72
73
    }

Praetorius, Simon's avatar
Praetorius, Simon committed
74
75
76
77
78
79
80
81
82
83
    /// Call the \ref update method on all attached observers.
    void notify(Event const& e)
    {
      for (ObserverInterface<Event>* o : observers_)
        o->update(e);
    }

    /// Attach a new observer that gets called on \ref notify
    void attach(ObserverInterface<Event>* o)
    {
Praetorius, Simon's avatar
Praetorius, Simon committed
84
      observers_.insert(o);
Praetorius, Simon's avatar
Praetorius, Simon committed
85
86
87
88
89
    }

    /// Detaches the passed observer from the list, if stored.
    void detach(ObserverInterface<Event>* o)
    {
Praetorius, Simon's avatar
Praetorius, Simon committed
90
      observers_.erase(o);
Praetorius, Simon's avatar
Praetorius, Simon committed
91
92
93
    }

  private:
Praetorius, Simon's avatar
Praetorius, Simon committed
94
    std::set<ObserverInterface<Event>*> observers_;
95
96
97
  };


98
  /// Implementation of the \ref ObserverInterface
Praetorius, Simon's avatar
Praetorius, Simon committed
99
  template <class Event, class... Tags>
100
  class Observer
Praetorius, Simon's avatar
Praetorius, Simon committed
101
      : public ObserverInterface<Event>
102
103
  {
  public:
104
105
106
107
    template <class N,
      REQUIRES(std::is_base_of_v<Notifier<Event>,N>)>
    Observer(N const& notifier)
      : notifier_(const_cast<N*>(&notifier))
Praetorius, Simon's avatar
Praetorius, Simon committed
108
109
110
111
    {
      notifier_->attach(this);
    }

112
113
114
115
116
117
118
119
    template <class N,
      REQUIRES(not std::is_base_of_v<Notifier<Event>,N>)>
    Observer(N const& notifier)
    {
      warning("Ignoring Notifier. Use AdaptiveGrid wrapper.");
    }


Praetorius, Simon's avatar
Praetorius, Simon committed
120
121
122
    /// Destructor, detaches from the notifier
    virtual ~Observer()
    {
Müller, Felix's avatar
Müller, Felix committed
123
124
      if (notifier_)
        notifier_->detach(this);
Praetorius, Simon's avatar
Praetorius, Simon committed
125
126
127
128
129
130
131
132
133
134
135
136
    }

    /// Copy constructor. Attaches this to the copied notifier
    Observer(Observer const& other)
      : notifier_(other.notifier_)
    {
      notifier_->attach(this);
    }

    /// Copy-assignment operator, copies the notifier and attaches this.
    Observer& operator=(Observer const& other)
    {
Praetorius, Simon's avatar
Praetorius, Simon committed
137
138
139
140
141
      if (&other != this) {
        notifier_->detach(this);
        notifier_ = other.notifier_;
        notifier_->attach(this);
      }
Praetorius, Simon's avatar
Praetorius, Simon committed
142
143
144
      return *this;
    }

145
    /// Set the Notifier* to nullptr. Used by the Notifier to avoid segfaults when destruction occurs
Müller, Felix's avatar
Müller, Felix committed
146
147
    /// out of order.
    void unset() final
148
    {
Müller, Felix's avatar
Müller, Felix committed
149
      notifier_ = nullptr;
150
151
    }

Praetorius, Simon's avatar
Praetorius, Simon committed
152
153
154
155
156
157
    /// Implementation of the interface method \ref ObserverInterface::update.
    /// Redirects to the \ref updateImpl method with additional \ref Tags parameters
    void update(Event e) final
    {
      updateImpl(e, Tags{}...);
    }
158

Praetorius, Simon's avatar
Praetorius, Simon committed
159
160
161
162
163
  protected:
    /// \brief Implementation of the update method in derived class
    // NOTE: The additional `Tags...` arguments can be used to distinguish
    //       between multiple observers of the same event.
    virtual void updateImpl(Event e, Tags...) = 0;
164

Praetorius, Simon's avatar
Praetorius, Simon committed
165
  private:
166
    Notifier<Event>* notifier_ = nullptr;
167
168
  };

Praetorius, Simon's avatar
Praetorius, Simon committed
169

170
  namespace Impl
Praetorius, Simon's avatar
Praetorius, Simon committed
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
  {
    template <class Event, class Tags>
    class ObserverSequenceImpl;

    /// Combination of multiple observers of the same event but with different tags
    template <class Event, std::size_t... Is>
    class ObserverSequenceImpl<Event, std::index_sequence<Is...>>
        : private Observer<Event,index_t<Is>>...
    {
    public:
      template <class... Notifiers,
        REQUIRES(sizeof...(Notifiers) == sizeof...(Is))>
      ObserverSequenceImpl(Notifiers&&... notifiers)
        : Observer<Event,index_t<Is>>(FWD(notifiers))...
      {}
    };

  } // end namespace Impl

  template <class Event, std::size_t N>
  using ObserverSequence = Impl::ObserverSequenceImpl<Event, std::make_index_sequence<N>>;

193
} // end namespace AMDiS