Gazebo Gazebo

API Reference

6.16.0
gz/sim/EventManager.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2018 Open Source Robotics Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */
17 #ifndef GZ_GAZEBO_EVENTMANAGER_HH_
18 #define GZ_GAZEBO_EVENTMANAGER_HH_
19 
20 #include <functional>
21 #include <memory>
22 #include <typeinfo>
23 #include <unordered_map>
24 #include <utility>
25 
26 #include <gz/common/Console.hh>
27 #include <gz/common/Event.hh>
28 
29 #include <gz/sim/config.hh>
30 #include <gz/sim/Export.hh>
31 #include <gz/sim/Types.hh>
32 
33 namespace ignition
34 {
35  namespace gazebo
36  {
37  // Inline bracket to help doxygen filtering.
38  inline namespace IGNITION_GAZEBO_VERSION_NAMESPACE {
39  // Forward declarations.
40  class IGNITION_GAZEBO_HIDDEN EventManagerPrivate;
41 
50 
55  {
57  public: EventManager() = default;
58 
60  public: ~EventManager() = default;
61 
67  public: template <typename E>
68  gz::common::ConnectionPtr
69  Connect(const typename E::CallbackT &_subscriber)
70  {
71  if (this->events.find(typeid(E)) == this->events.end()) {
72  this->events[typeid(E)] = std::make_unique<E>();
73  }
74 
75  E *eventPtr = dynamic_cast<E *>(this->events[typeid(E)].get());
76  // All values in the map should be derived from Event,
77  // so this shouldn't be an issue, but it doesn't hurt to check.
78  if (eventPtr != nullptr)
79  {
80  return eventPtr->Connect(_subscriber);
81  }
82  else
83  {
84  ignerr << "Failed to connect event: "
85  << typeid(E).name() << std::endl;
86  return nullptr;
87  }
88  }
89 
93  public: template <typename E, typename ... Args>
94  void Emit(Args && ... _args)
95  {
96  if (this->events.find(typeid(E)) == this->events.end())
97  {
98  // If there are no events of type E in the map, create it.
99  // But it also means there is nothing to signal.
100  //
101  // This is also needed to suppress unused function warnings
102  // for Events that are purely emitted, with no connections.
103  this->events[typeid(E)] = std::make_unique<E>();
104  return;
105  }
106 
107  E *eventPtr = dynamic_cast<E *>(this->events[typeid(E)].get());
108  // All values in the map should be derived from Event,
109  // so this shouldn't be an issue, but it doesn't hurt to check.
110  if (eventPtr != nullptr)
111  {
112  eventPtr->Signal(std::forward<Args>(_args) ...);
113  }
114  else
115  {
116  ignerr << "Failed to signal event: "
117  << typeid(E).name() << std::endl;
118  }
119  }
120 
123  public: template <typename E>
124  unsigned int
126  {
127  if (this->events.find(typeid(E)) == this->events.end())
128  {
129  return 0u;
130  }
131 
132  E *eventPtr = dynamic_cast<E *>(this->events[typeid(E)].get());
133  // All values in the map should be derived from Event,
134  // so this shouldn't be an issue, but it doesn't hurt to check.
135  if (eventPtr != nullptr)
136  {
137  return eventPtr->ConnectionCount();
138  }
139  else
140  {
141  ignerr << "Failed to get connection count for event: "
142  << typeid(E).name() << std::endl;
143  return 0u;
144  }
145  }
146 
148  private: using TypeInfoRef = std::reference_wrapper<const std::type_info>;
149 
151  private: struct Hasher
152  {
153  std::size_t operator()(TypeInfoRef _code) const
154  {
155  return _code.get().hash_code();
156  }
157  };
158 
160  private: struct EqualTo
161  {
162  bool operator()(TypeInfoRef _lhs, TypeInfoRef _rhs) const
163  {
164  return _lhs.get() == _rhs.get();
165  }
166  };
167 
169  private: std::unordered_map<TypeInfoRef,
171  Hasher, EqualTo> events;
172  };
173  }
174  }
175 }
176 
177 #endif // GZ_GAZEBO_EVENTMANAGER_HH_
~EventManager()=default
Destructor.
This library is part of the Gazebo project.
class IGNITION_GAZEBO_HIDDEN EventManagerPrivate
Definition: gz/sim/EventManager.hh:40
The EventManager is used to send/receive notifications of simulator events.
Definition: gz/sim/EventManager.hh:54
#define ignerr
void Emit(Args &&... _args)
Emit an event signal to connected subscribers.
Definition: gz/sim/EventManager.hh:94
T endl(T... args)
unsigned int ConnectionCount()
Get connection count for a particular event Connection count for the event.
Definition: gz/sim/EventManager.hh:125
gz::common::ConnectionPtr Connect(const typename E::CallbackT &_subscriber)
Add a connection to an event.
Definition: gz/sim/EventManager.hh:69
EventManager()=default
Constructor.