Observer.hpp 4.18 KB
Newer Older
1
2
3
4
5
6
7
#pragma once

#include <algorithm>
#include <list>
#include <memory>
#include <type_traits>

Praetorius, Simon's avatar
Praetorius, Simon committed
8
9
#include <dune/common/typeutilities.hh>

10
11
12
13
14
#include <amdis/common/ConceptsBase.hpp>
#include <amdis/Output.hpp>

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

Praetorius, Simon's avatar
Praetorius, Simon committed
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
    /** 
     * 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; };

    /** 
     * 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;
  };
Praetorius, Simon's avatar
Praetorius, Simon committed
43
44
45
46


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

  template <class Event>
Praetorius, Simon's avatar
Praetorius, Simon committed
65
  class Notifier<Event>
66
67
  {
  public:
Praetorius, Simon's avatar
Praetorius, Simon committed
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
    /// 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)
    {
      observers_.push_back(o);
    }

    /// Detaches the passed observer from the list, if stored.
    void detach(ObserverInterface<Event>* o)
    {
      auto it = std::find(observers_.begin(), observers_.end(), o);
      if (it != observers_.end())
        observers_.erase(it);
    }

  private:
    std::list<ObserverInterface<Event>*> observers_;
91
92
93
  };


Praetorius, Simon's avatar
Praetorius, Simon committed
94
95
  /// Implementation of the \ref ObserverInterface 
  template <class Event, class... Tags>
96
  class Observer
Praetorius, Simon's avatar
Praetorius, Simon committed
97
      : public ObserverInterface<Event>
98
99
  {
  public:
Praetorius, Simon's avatar
Praetorius, Simon committed
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
    template <class Notifier>
    Observer(Notifier const& notifier)
      : notifier_(const_cast<Notifier*>(&notifier))
    {
      notifier_->attach(this);
    }

    /// Destructor, detaches from the notifier
    virtual ~Observer()
    {
      assert(notifier_);
      notifier_->detach(this);
    }

    /// 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)
    {
      notifier_ = other.notifier_;
      notifier_->attach(this);
      return *this;
    }

    /// Move assignment operator, implemented in terms of the move
    /// assignment operator
    Observer(Observer&& other)
    {
      *this = std::move(other);
    }

    /// Move-assignment operator, copies the notifier and attaches this.
    Observer& operator=(Observer&& other)
    {
      notifier_ = other.notifier_;
      notifier_->attach(this);
      return *this;
    }

    /// 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{}...);
    }
150

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

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

} // end namespace AMDiS