rmw_fastrtps_shared_cpp  master
Code shared on static and dynamic type support of rmw_fastrtps_cpp.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
custom_event_info.hpp
Go to the documentation of this file.
1 // Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef RMW_FASTRTPS_SHARED_CPP__CUSTOM_EVENT_INFO_HPP_
16 #define RMW_FASTRTPS_SHARED_CPP__CUSTOM_EVENT_INFO_HPP_
17 
18 #include <atomic>
19 #include <condition_variable>
20 #include <list>
21 #include <memory>
22 #include <mutex>
23 #include <utility>
24 
25 #include "fastcdr/FastBuffer.h"
26 
27 #include "rmw/event.h"
28 
30 
31 
33 {
34 protected:
36 
37 public:
39  virtual void attachCondition(
40  std::mutex * conditionMutex,
41  std::condition_variable * conditionVariable) = 0;
42 
44  virtual void detachCondition() = 0;
45 
47 
51  virtual bool hasEvent(rmw_event_type_t event_type) const = 0;
52 
54 
60  virtual bool takeNextEvent(rmw_event_type_t event_type, void * event_info) = 0;
61 };
62 
64 {
65 public:
67  std::mutex * mutex,
68  std::condition_variable * condition_variable = nullptr)
69  : mutex_(mutex), cv_(condition_variable)
70  {
71  if (nullptr != mutex_) {
72  mutex_->lock();
73  }
74  }
75 
77  {
78  if (nullptr != mutex_) {
79  mutex_->unlock();
80  if (nullptr != cv_) {
81  cv_->notify_all();
82  }
83  }
84  }
85 
86 private:
87  std::mutex * mutex_;
89 };
90 
92 {
93  virtual EventListenerInterface * getListener() const = 0;
94 };
95 
96 #endif // RMW_FASTRTPS_SHARED_CPP__CUSTOM_EVENT_INFO_HPP_
std::mutex::lock
T lock(T... args)
event.h
EventListenerInterface::ConditionalScopedLock::ConditionalScopedLock
ConditionalScopedLock(std::mutex *mutex, std::condition_variable *condition_variable=nullptr)
Definition: custom_event_info.hpp:66
rmw_event_type_t
rmw_event_type_t
TypeSupport.hpp
EventListenerInterface::attachCondition
virtual void attachCondition(std::mutex *conditionMutex, std::condition_variable *conditionVariable)=0
Connect a condition variable so a waiter can be notified of new data.
std::mutex::unlock
T unlock(T... args)
EventListenerInterface::ConditionalScopedLock::~ConditionalScopedLock
~ConditionalScopedLock()
Definition: custom_event_info.hpp:76
EventListenerInterface::takeNextEvent
virtual bool takeNextEvent(rmw_event_type_t event_type, void *event_info)=0
Take ready data for an event type.
EventListenerInterface::ConditionalScopedLock
Definition: custom_event_info.hpp:63
std::condition_variable
std::mutex
EventListenerInterface::detachCondition
virtual void detachCondition()=0
Unset the information from attachCondition.
EventListenerInterface::hasEvent
virtual bool hasEvent(rmw_event_type_t event_type) const =0
Check if there is new data available for a specific event type.
CustomEventInfo::getListener
virtual EventListenerInterface * getListener() const =0
std::condition_variable::notify_all
T notify_all(T... args)
CustomEventInfo
Definition: custom_event_info.hpp:91
EventListenerInterface
Definition: custom_event_info.hpp:32