Observer.hpp 4.82 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/common/ConceptsBase.hpp>
Praetorius, Simon's avatar
Praetorius, Simon committed
7
#include <amdis/common/Index.hpp>
8
#include <amdis/common/TypeTraits.hpp>
9
10
11

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

20
    /**
Praetorius, Simon's avatar
Praetorius, Simon committed
21
22
23
24
25
     * 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; };

26
    /**
Praetorius, Simon's avatar
Praetorius, Simon committed
27
28
29
30
31
32
33
34
35
36
37
38
     * 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
39
    virtual void unset() = 0;
Praetorius, Simon's avatar
Praetorius, Simon committed
40
  };
Praetorius, Simon's avatar
Praetorius, Simon committed
41
42
43
44


  /// \brief Mixin for signaling of certain events.
  /**
45
46
47
48
49
50
51
52
   *  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
53
54
55
  class Notifier
      : public Notifier<Event>
      , public Notifier<Events...>
56
57
  {
  public:
Praetorius, Simon's avatar
Praetorius, Simon committed
58
59
    using Notifier<Event>::notify;
    using Notifier<Events...>::notify;
60
61
62
  };

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

Praetorius, Simon's avatar
Praetorius, Simon committed
73
74
75
76
77
78
79
80
81
82
    /// 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
83
      observers_.insert(o);
Praetorius, Simon's avatar
Praetorius, Simon committed
84
85
86
87
88
    }

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

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


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

    /// Destructor, detaches from the notifier
    virtual ~Observer()
    {
Müller, Felix's avatar
Müller, Felix committed
113
114
      if (notifier_)
        notifier_->detach(this);
Praetorius, Simon's avatar
Praetorius, Simon committed
115
116
117
118
119
120
121
122
123
124
125
126
    }

    /// 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
127
128
129
130
131
      if (&other != this) {
        notifier_->detach(this);
        notifier_ = other.notifier_;
        notifier_->attach(this);
      }
Praetorius, Simon's avatar
Praetorius, Simon committed
132
133
134
      return *this;
    }

Müller, Felix's avatar
Müller, Felix committed
135
136
137
    /// Set the Notifier* to nullptr. Used by the Notifer to avoid segfaults when destruction occurs
    /// out of order.
    void unset() final
138
    {
Müller, Felix's avatar
Müller, Felix committed
139
      notifier_ = nullptr;
140
141
    }

Praetorius, Simon's avatar
Praetorius, Simon committed
142
143
144
145
146
147
    /// 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{}...);
    }
148

Praetorius, Simon's avatar
Praetorius, Simon committed
149
150
151
152
153
  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;
154

Praetorius, Simon's avatar
Praetorius, Simon committed
155
  private:
156
    Notifier<Event>* notifier_ = nullptr;
157
158
  };

Praetorius, Simon's avatar
Praetorius, Simon committed
159

160
  namespace Impl
Praetorius, Simon's avatar
Praetorius, Simon committed
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
  {
    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>>;

183
} // end namespace AMDiS