Lely core libraries  2.2.5
loop_driver.hpp
Go to the documentation of this file.
1 
22 #ifndef LELY_COAPP_LOOP_DRIVER_HPP_
23 #define LELY_COAPP_LOOP_DRIVER_HPP_
24 
25 #include <lely/coapp/driver.hpp>
26 #include <lely/ev/loop.hpp>
27 #include <lely/ev/strand.hpp>
28 
29 #include <memory>
30 #include <utility>
31 
32 namespace lely {
33 
34 namespace canopen {
35 
36 namespace detail {
37 
43  protected:
44  ev::Loop loop{};
45  ev::Strand strand{loop.get_executor()};
46 };
47 
48 } // namespace detail
49 
52  public:
62  explicit LoopDriver(AsyncMaster& master, uint8_t id);
63 
70  ~LoopDriver();
71 
73  ev::Loop&
74  GetLoop() noexcept {
75  return loop;
76  }
77 
80  GetStrand() const noexcept {
81  return strand;
82  }
83 
94  void Join();
95 
101 
108  template <class F, class... Args>
109  void
110  Defer(F&& f, Args&&... args) {
111  GetStrand().post(::std::forward<F>(f), ::std::forward<Args>(args)...);
112  }
113 
126  template <class T>
127  T
129  GetLoop().wait(f);
130  return f.get().value();
131  }
132 
145  template <class T>
146  typename ::std::enable_if<!::std::is_void<T>::value, T>::type
147  Wait(SdoFuture<T> f, ::std::error_code& ec) {
148  GetLoop().wait(f, ec);
149  try {
150  return f.get().value();
151  } catch (const ::std::system_error& e) {
152  ec = e.code();
153  } catch (const ev::future_not_ready& e) {
154  ec = ::std::make_error_code(::std::errc::operation_canceled);
155  }
156  return T{};
157  }
158 
170  void Wait(SdoFuture<void> f, ::std::error_code& ec);
171 
177  void USleep(uint_least64_t usec);
178 
184  void USleep(uint_least64_t usec, ::std::error_code& ec) noexcept;
185 
191  template <class T>
192  T
193  RunRead(uint16_t idx, uint8_t subidx) {
194  return Wait(AsyncRead<T>(idx, subidx));
195  }
196 
203  template <class T>
204  T
205  RunRead(uint16_t idx, uint8_t subidx, ::std::error_code& ec) {
206  return Wait(AsyncRead<T>(idx, subidx), ec);
207  }
208 
214  template <class T>
215  T
216  RunRead(uint16_t idx, uint8_t subidx,
217  const ::std::chrono::milliseconds& timeout) {
218  return Wait(AsyncRead<T>(idx, subidx, timeout));
219  }
220 
236  template <class T>
237  T
238  RunRead(uint16_t idx, uint8_t subidx,
239  const ::std::chrono::milliseconds& timeout, ::std::error_code& ec) {
240  return Wait(AsyncRead<T>(idx, subidx, timeout), ec);
241  }
242 
248  template <class T>
249  void
250  RunWrite(uint16_t idx, uint8_t subidx, T&& value) {
251  Wait(AsyncWrite(idx, subidx, ::std::forward<T>(value)));
252  }
253 
260  template <class T>
261  void
262  RunWrite(uint16_t idx, uint8_t subidx, T&& value, ::std::error_code& ec) {
263  Wait(AsyncWrite(idx, subidx, ::std::forward<T>(value)), ec);
264  }
265 
271  template <class T>
272  void
273  RunWrite(uint16_t idx, uint8_t subidx, T&& value,
274  const ::std::chrono::milliseconds& timeout) {
275  Wait(AsyncWrite(idx, subidx, ::std::forward<T>(value), timeout));
276  }
277 
292  template <class T>
293  void
294  RunWrite(uint16_t idx, uint8_t subidx, T&& value,
295  const ::std::chrono::milliseconds& timeout, ::std::error_code& ec) {
296  Wait(AsyncWrite(idx, subidx, ::std::forward<T>(value), timeout), ec);
297  }
298 
299  private:
300  struct Impl_;
301  ::std::unique_ptr<Impl_> impl_;
302 };
303 
304 } // namespace canopen
305 
306 } // namespace lely
307 
308 #endif // LELY_COAPP_LOOP_DRIVER_HPP_
lely::ev::Executor::post
void post(ev_task &task) noexcept
Definition: exec.hpp:75
lely::ev::Loop
A polling event loop.
Definition: loop.hpp:41
lely::canopen::BasicDriver::master
BasicMaster & master
A reference to the master with which this driver is registered.
Definition: driver.hpp:671
lely::canopen::BasicDriver
The base class for drivers for remote CANopen nodes.
Definition: driver.hpp:278
lely::canopen::LoopDriver::AsyncStoppped
ev::Future< void, void > AsyncStoppped() noexcept
Returns a future which becomes ready once the dedicated event loop of the driver is stopped and the t...
Definition: loop_driver.cpp:87
lely::canopen::AsyncMaster
An asynchronous CANopen master.
Definition: master.hpp:1296
loop.hpp
lely::canopen::LoopDriver::RunWrite
void RunWrite(uint16_t idx, uint8_t subidx, T &&value, const ::std::chrono::milliseconds &timeout, ::std::error_code &ec)
Queues an asynchronous write (SDO download) operation and runs the event loop until the operation is ...
Definition: loop_driver.hpp:294
lely::ev::Strand
A strand executor.
Definition: strand.hpp:37
lely::canopen::BasicDriver::AsyncWrite
SdoFuture< void > AsyncWrite(uint16_t idx, uint8_t subidx, T &&value)
Equivalent to AsyncWrite(uint16_t idx, uint8_t subidx, T&& value, const ::std::chrono::milliseconds& ...
Definition: driver.hpp:612
lely::ev::Loop::get_executor
Executor get_executor() const noexcept
Definition: loop.hpp:80
lely::canopen::LoopDriver
A CANopen driver running its own dedicated event loop in a separate thread.
Definition: loop_driver.hpp:51
lely::canopen::LoopDriver::RunWrite
void RunWrite(uint16_t idx, uint8_t subidx, T &&value, ::std::error_code &ec)
Equivalent to RunWrite(uint16_t idx, uint8_t subidx, T&& value, const ::std::chrono::milliseconds& ti...
Definition: loop_driver.hpp:262
lely::canopen::LoopDriver::RunRead
T RunRead(uint16_t idx, uint8_t subidx, const ::std::chrono::milliseconds &timeout, ::std::error_code &ec)
Queues an asynchronous read (SDO upload) operation and runs the event loop until the operation is com...
Definition: loop_driver.hpp:238
lely::canopen::LoopDriver::RunWrite
void RunWrite(uint16_t idx, uint8_t subidx, T &&value)
Equivalent to RunWrite(uint16_t idx, uint8_t subidx, T&& value, ::std::error_code& ec),...
Definition: loop_driver.hpp:250
lely::canopen::LoopDriver::Wait
typename ::std::enable_if<!::std::is_void< T >::value, T >::type Wait(SdoFuture< T > f, ::std::error_code &ec)
Waits for the specified future to become ready by running pending tasks on the dedicated event loop o...
Definition: loop_driver.hpp:147
lely::canopen::LoopDriver::RunRead
T RunRead(uint16_t idx, uint8_t subidx, const ::std::chrono::milliseconds &timeout)
Equivalent to RunRead(uint16_t idx, uint8_t subidx, const ::std::chrono::milliseconds& timeout,...
Definition: loop_driver.hpp:216
lely::canopen::LoopDriver::Wait
T Wait(SdoFuture< T > f)
Waits for the specified future to become ready by running pending tasks on the dedicated event loop o...
Definition: loop_driver.hpp:128
lely::ev::Executor
An abstract task executor. This class is a wrapper around #ev_exec_t*.
Definition: exec.hpp:38
lely::ev::Future
A future.
Definition: future.hpp:50
lely::canopen::LoopDriver::RunRead
T RunRead(uint16_t idx, uint8_t subidx, ::std::error_code &ec)
Equivalent to RunRead(uint16_t idx, uint8_t subidx, const ::std::chrono::milliseconds& timeout,...
Definition: loop_driver.hpp:205
lely::canopen::detail::LoopDriverBase
A base class for lely::canopen::LoopDriver, containing an event loop and the associated executor.
Definition: loop_driver.hpp:42
lely::canopen::LoopDriver::GetStrand
ev::Executor GetStrand() const noexcept
Returns the strand executor associated with the event loop of the driver.
Definition: loop_driver.hpp:80
lely::ev::Future::get
result_type & get()
Returns the result of a ready future.
Definition: future.hpp:490
driver.hpp
strand.hpp
lely::canopen::LoopDriver::RunWrite
void RunWrite(uint16_t idx, uint8_t subidx, T &&value, const ::std::chrono::milliseconds &timeout)
Equivalent to RunWrite(uint16_t idx, uint8_t subidx, T&& value, const ::std::chrono::milliseconds& ti...
Definition: loop_driver.hpp:273
lely::ev::future_not_ready
The exception thrown when retrieving the result of a future which is not ready or does not contain a ...
Definition: future.hpp:45
lely::canopen::LoopDriver::Defer
void Defer(F &&f, Args &&... args)
Schedules the specified Callable object for execution by the event loop for this driver.
Definition: loop_driver.hpp:110
lely::canopen::LoopDriver::USleep
void USleep(uint_least64_t usec)
Runs the event loop for usec microseconds.
Definition: loop_driver.cpp:104
lely::ev::Loop::wait
::std::size_t wait(ev_future_t *future)
Definition: loop.hpp:104
lely::canopen::LoopDriver::GetLoop
ev::Loop & GetLoop() noexcept
Returns a reference to the dedicated event loop of the driver.
Definition: loop_driver.hpp:74
lely::canopen::make_error_code
::std::error_code make_error_code(SdoErrc e) noexcept
Creates an error code corresponding to an SDO abort code.
Definition: sdo_error.cpp:170
lely::canopen::LoopDriver::~LoopDriver
~LoopDriver()
Stops the event loop and terminates the thread in which it was running before destroying the driver.
lely::canopen::LoopDriver::Join
void Join()
Stops the dedicated event loop of the driver and waits until the thread running the event loop finish...
Definition: loop_driver.cpp:82
lely::canopen::LoopDriver::RunRead
T RunRead(uint16_t idx, uint8_t subidx)
Equivalent to RunRead(uint16_t idx, uint8_t subidx, ::std::error_code& ec), except that it throws lel...
Definition: loop_driver.hpp:193