VPTissue Reference Manual
Subject.h
Go to the documentation of this file.
1 #ifndef SUBJECT_H_INCLUDED
2 #define SUBJECT_H_INCLUDED
3 /*
4  * Copyright 2011-2016 Universiteit Antwerpen
5  *
6  * Licensed under the EUPL, Version 1.1 or as soon they will be approved by
7  * the European Commission - subsequent versions of the EUPL (the "Licence");
8  * You may not use this work except in compliance with the Licence.
9  * You may obtain a copy of the Licence at: http://ec.europa.eu/idabc/eupl5
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the Licence is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the Licence for the specific language governing
15  * permissions and limitations under the Licence.
16  */
22 #include <functional>
23 #include <map>
24 #include <memory>
25 #include <string>
26 
27 namespace SimPT_Sim {
28 namespace Util {
29 
33 template <typename E, typename K = void>
34 class Subject {
35 public:
36  typedef E EventType;
37  typedef const K* KeyType;
38  typedef std::function<void(const EventType&)> CallbackType;
39 
40  virtual ~Subject()
41  {
42  UnregisterAll();
43  }
44 
45  template<typename U>
46  void Register(const U*, CallbackType);
47 
48  template <typename U>
49  void Unregister(const U*);
50 
51  void UnregisterAll();
52 
53  void Notify(const EventType&);
54 
55 private:
56  typedef std::less<KeyType> Compare;
57 
58  std::map<KeyType, CallbackType, Compare> m_observers;
59 };
60 
61 
62 //template<typename E, typename K>
63 //template<typename U>
64 //void Subject<E, K>::Register(const U* u, CallbackType f)
65 //{
66 // m_observers.insert(make_pair(static_cast<KeyType>(u), f));
67 //}
68 //
69 //template <typename E, typename K>
70 //template <typename U>
71 //void Subject<E, K>::Unregister(const U* u)
72 //{
73 // m_observers.erase(static_cast<KeyType>(u));
74 //}
75 //
76 //template<typename E, typename K>
77 //void Subject<E, K>::UnregisterAll()
78 //{
79 // m_observers.clear();
80 //}
81 //
82 //template<typename E, typename K>
83 //void Subject<E, K>::Notify(const EventType& e)
84 //{
85 // for (auto const& o : m_observers) {
86 // (o.second)(e);
87 // }
88 //}
89 
90 
94 template <typename E>
95 class Subject<E, std::weak_ptr<const void>>
96 {
97 public:
98  typedef E EventType;
99  typedef std::weak_ptr<const void> KeyType;
100  typedef std::function<void(const EventType&)> CallbackType;
101 
102 
103  virtual ~Subject()
104  {
105  UnregisterAll();
106  }
107 
108  template <typename U>
109  void Register(const std::shared_ptr<U>&, CallbackType);
110 
111  template <typename U>
112  void Unregister(const std::shared_ptr<U>&);
113 
114  void UnregisterAll();
115 
116  void Notify(const EventType&);
117 
118 private:
119  typedef std::owner_less<KeyType> Compare;
120 
121  std::map<KeyType, CallbackType, Compare> m_observers;
122 };
123 
124 
125 template<typename E>
126 template<typename U>
127 void Subject<E, std::weak_ptr<const void>>::Register(const std::shared_ptr<U>& u, CallbackType f)
128 {
129  m_observers.insert(make_pair(std::static_pointer_cast<const void>(u), f));
130 }
131 
132 template <typename E>
133 template <typename U>
134 void Subject<E, std::weak_ptr<const void>>::Unregister(const std::shared_ptr<U>& u)
135 {
136  m_observers.erase(std::static_pointer_cast<const void>(u));
137 }
138 
139 template<typename E>
140 void Subject<E, std::weak_ptr<const void>>::UnregisterAll()
141 {
142  m_observers.clear();
143 }
144 
145 template<typename E>
146 void Subject<E, std::weak_ptr<const void>>::Notify(const EventType& e)
147 {
148  for (auto const& o : m_observers) {
149  auto spt = o.first.lock();
150  if (spt) {
151  (o.second)(e);
152  } else {
153  // JB the statement below causes frequent crashes on Mac OS X
154  // As it's just a clean up, I've commented it out
155  //auto it = m_observers.erase(o.first);
156  }
157  }
158 }
159 
160 } // namespace
161 } // namespace
162 
163 #endif
STL namespace.
Namespace for the core simulator.
Subject in Observer pattern.
Definition: Subject.h:34