yarp-devices
StateObserver.hpp
1 // -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
2 
3 #ifndef __STATE_OBSERVER_HPP__
4 #define __STATE_OBSERVER_HPP__
5 
6 #include <cstdint>
7 #include <cstdlib>
8 
9 #include <type_traits>
10 
11 namespace roboticslab
12 {
13 
29 {
30 public:
32  StateObserverBase(double timeout);
33 
35  virtual ~StateObserverBase() = 0;
36 
39 
42 
44  double getTimeout() const
45  { return timeout; }
46 
48  bool await(void * raw = nullptr);
49 
51  bool notify(const void * raw = nullptr, std::size_t len = 0);
52 
53 protected:
55  void * getRemoteStorage();
56 
58  const void * getRemoteStorage() const;
59 
61  virtual void setRemoteStorage(const void * ptr, std::size_t len);
62 
63 private:
64  double timeout;
65 
66  class Private;
67  Private * impl;
68 };
69 
74 class StateObserver final : private StateObserverBase
75 {
76 public:
78 
80  bool await()
81  { return StateObserverBase::await(); }
82 
84  bool notify()
85  { return StateObserverBase::notify(); }
86 };
87 
93 template<typename T, typename NonFundamentalType = void>
95 {
96 public:
98 
100  bool await(T & remote)
101  { return StateObserverBase::await(&remote); }
102 
104  bool notify(const T & remote)
105  { return StateObserverBase::notify(&remote); }
106 
107 protected:
108  void setRemoteStorage(const void * remote, std::size_t len) override
109  { *static_cast<T *>(getRemoteStorage()) = *static_cast<const T *>(remote); }
110 };
111 
117 template<typename T>
118 class TypedStateObserver<T, typename std::enable_if_t<std::is_arithmetic_v<T>>> final : private StateObserverBase
119 {
120 public:
122 
124  bool await(T * raw)
125  { return StateObserverBase::await(raw); }
126 
128  bool notify(T raw)
129  { return StateObserverBase::notify(&raw, sizeof(T)); }
130 
132  bool notify(const std::uint8_t * raw, std::size_t len)
133  { return len == sizeof(T) && StateObserverBase::notify(raw, len); }
134 };
135 
140 template<>
141 class TypedStateObserver<std::uint8_t[]> final : private StateObserverBase
142 {
143 public:
145 
147  bool await(std::uint8_t * raw)
148  { return StateObserverBase::await(raw); }
149 
151  bool notify(const std::uint8_t * raw, std::size_t len)
152  { return StateObserverBase::notify(raw, len); }
153 };
154 
155 } // namespace roboticslab
156 
157 #endif // __STATE_OBSERVER_HPP__
Base class for a state observer.
Definition: StateObserver.hpp:29
StateObserverBase & operator=(const StateObserverBase &)=delete
Deleted copy assignment operator.
bool notify(const void *raw=nullptr, std::size_t len=0)
Wake up a thread that waits on this object's monitor.
Definition: StateObserver.cpp:188
bool await(void *raw=nullptr)
Causes the current thread to wait until notify is invoked or the timeout elapses.
Definition: StateObserver.cpp:183
StateObserverBase(double timeout)
Constructor, configure with timeout in seconds.
Definition: StateObserver.cpp:159
virtual void setRemoteStorage(const void *ptr, std::size_t len)
Set data storage to notify observers with.
Definition: StateObserver.cpp:178
void * getRemoteStorage()
Retrieve modifiable handle to notified data storage.
Definition: StateObserver.cpp:168
double getTimeout() const
Retrieve configured timeout (in seconds).
Definition: StateObserver.hpp:44
virtual ~StateObserverBase()=0
Virtual destructor.
Definition: StateObserver.cpp:163
StateObserverBase(const StateObserverBase &)=delete
Deleted copy constructor.
Data-free state observer.
Definition: StateObserver.hpp:75
bool notify()
Wakes up a waiting thread.
Definition: StateObserver.hpp:84
bool await()
Wait with timeout until another thread invokes notify.
Definition: StateObserver.hpp:80
bool await(T *raw)
Wait with timeout until another thread invokes notify.
Definition: StateObserver.hpp:124
bool notify(T raw)
Wakes up a waiting thread.
Definition: StateObserver.hpp:128
bool notify(const std::uint8_t *raw, std::size_t len)
Wakes up a waiting thread with byte array.
Definition: StateObserver.hpp:132
bool await(std::uint8_t *raw)
Wait with timeout until another thread invokes notify.
Definition: StateObserver.hpp:147
bool notify(const std::uint8_t *raw, std::size_t len)
Wakes up a waiting thread.
Definition: StateObserver.hpp:151
Type state observer for non-arithmetic types.
Definition: StateObserver.hpp:95
bool await(T &remote)
Wait with timeout until another thread invokes notify.
Definition: StateObserver.hpp:100
void setRemoteStorage(const void *remote, std::size_t len) override
Set data storage to notify observers with.
Definition: StateObserver.hpp:108
bool notify(const T &remote)
Wakes up a waiting thread.
Definition: StateObserver.hpp:104
The main, catch-all namespace for Robotics Lab UC3M.
Definition: groups.dox:6