Ignition Gazebo

API Reference

5.1.0
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 IGNITION_GAZEBO_EVENTMANAGER_HH_
18 #define IGNITION_GAZEBO_EVENTMANAGER_HH_
19 
20 #include <functional>
21 #include <memory>
22 #include <typeinfo>
23 #include <unordered_map>
24 #include <utility>
25 
27 #include <ignition/common/Event.hh>
28 
29 #include <ignition/gazebo/config.hh>
30 #include <ignition/gazebo/Export.hh>
31 #include <ignition/gazebo/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>
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 
121 
124 
126  private: struct Hasher
127  {
128  std::size_t operator()(TypeInfoRef _code) const
129  {
130  return _code.get().hash_code();
131  }
132  };
133 
135  private: struct EqualTo
136  {
137  bool operator()(TypeInfoRef _lhs, TypeInfoRef _rhs) const
138  {
139  return _lhs.get() == _rhs.get();
140  }
141  };
142 
146  Hasher, EqualTo> events;
147  };
148  }
149  }
150 }
151 
152 #endif // IGNITION_GAZEBO_EVENTMANAGER_HH_
void Emit(Args &&... _args)
Emit an event signal to connected subscribers.
Definition: EventManager.hh:94
T endl(T... args)
ignition::common::ConnectionPtr Connect(const typename E::CallbackT &_subscriber)
Add a connection to an event.
Definition: EventManager.hh:69
The EventManager is used to send/receive notifications of simulator events.
Definition: EventManager.hh:54
#define ignerr
This library is part of the Ignition Robotics project.
class IGNITION_GAZEBO_HIDDEN EventManagerPrivate
Definition: EventManager.hh:40