Gazebo Gazebo

API Reference

3.15.2
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  class IGNITION_GAZEBO_VISIBLE EventManager
51  {
53  public: EventManager();
54 
56  public: ~EventManager();
57 
63  public: template <typename E>
64  gz::common::ConnectionPtr
65  Connect(const typename E::CallbackT &_subscriber)
66  {
67  if (this->events.find(typeid(E)) == this->events.end()) {
68  this->events[typeid(E)] = std::make_unique<E>();
69  }
70 
71  E *eventPtr = dynamic_cast<E *>(this->events[typeid(E)].get());
72  // All values in the map should be derived from Event,
73  // so this shouldn't be an issue, but it doesn't hurt to check.
74  if (eventPtr != nullptr)
75  {
76  return eventPtr->Connect(_subscriber);
77  }
78  else
79  {
80  ignerr << "Failed to connect event: "
81  << typeid(E).name() << std::endl;
82  return nullptr;
83  }
84  }
85 
89  public: template <typename E, typename ... Args>
90  void Emit(Args && ... _args)
91  {
92  if (this->events.find(typeid(E)) == this->events.end())
93  {
94  // If there are no events of type E in the map, create it.
95  // But it also means there is nothing to signal.
96  //
97  // This is also needed to suppress unused function warnings
98  // for Events that are purely emitted, with no connections.
99  this->events[typeid(E)] = std::make_unique<E>();
100  return;
101  }
102 
103  E *eventPtr = dynamic_cast<E *>(this->events[typeid(E)].get());
104  // All values in the map should be derived from Event,
105  // so this shouldn't be an issue, but it doesn't hurt to check.
106  if (eventPtr != nullptr)
107  {
108  eventPtr->Signal(std::forward<Args>(_args) ...);
109  }
110  else
111  {
112  ignerr << "Failed to signal event: "
113  << typeid(E).name() << std::endl;
114  }
115  }
116 
117 
119  private: using TypeInfoRef = std::reference_wrapper<const std::type_info>;
120 
122  private: struct Hasher
123  {
124  std::size_t operator()(TypeInfoRef _code) const
125  {
126  return _code.get().hash_code();
127  }
128  };
129 
131  private: struct EqualTo
132  {
133  bool operator()(TypeInfoRef _lhs, TypeInfoRef _rhs) const
134  {
135  return _lhs.get() == _rhs.get();
136  }
137  };
138 
140  private: std::unordered_map<TypeInfoRef,
142  Hasher, EqualTo> events;
143  };
144  }
145  }
146 }
147 
148 #endif // GZ_GAZEBO_EVENTMANAGER_HH_
This library is part of the Ignition Robotics 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:50
#define ignerr
void Emit(Args &&... _args)
Emit an event signal to connected subscribers.
Definition: gz/sim/EventManager.hh:90
T endl(T... args)
gz::common::ConnectionPtr Connect(const typename E::CallbackT &_subscriber)
Add a connection to an event.
Definition: gz/sim/EventManager.hh:65