SSP21-CPP
BasicExecutor.h
1 /*
2  * Copyright (c) 2018, Automatak LLC
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
6  * following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
9  * disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
12  * disclaimer in the documentation and/or other materials provided with the distribution.
13  *
14  * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
15  * products derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
18  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
23  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 #ifndef EXE4CPP_ASIO_BASICEXECUTOR_H
26 #define EXE4CPP_ASIO_BASICEXECUTOR_H
27 
28 #include "exe4cpp/IExecutor.h"
29 #include "exe4cpp/asio/AsioTimer.h"
30 
31 #include "asio.hpp"
32 
33 namespace exe4cpp
34 {
35 
36 /**
37 *
38 * Simple implementation of openpal::IExecutor that directly uses asio::io_context
39 *
40 * Should only be used when asio::io_context::run() is called from a single thread
41 *
42 */
43 class BasicExecutor final :
44  public exe4cpp::IExecutor,
45  public std::enable_shared_from_this<BasicExecutor>
46 {
47 public:
48  BasicExecutor(const std::shared_ptr<asio::io_service>& io_service) : io_service{io_service}
49  {}
50 
51  // Uncopyable
52  BasicExecutor(const BasicExecutor&) = delete;
53  BasicExecutor& operator=(const BasicExecutor&) = delete;
54 
55  static std::shared_ptr<BasicExecutor> create(const std::shared_ptr<asio::io_service>& io_service)
56  {
57  return std::make_shared<BasicExecutor>(io_service);
58  }
59 
60  // ---- Implement IExecutor -----
61 
62  virtual Timer start(const duration_t& duration, const action_t& action) override
63  {
64  return this->start(this->get_time() + duration, action);
65  }
66 
67  virtual Timer start(const steady_time_t& expiration, const action_t& action) override
68  {
69  const auto timer = AsioTimer::create(this->io_service);
70 
71  timer->impl.expires_at(expiration);
72 
73  // neither the executor nor the timer can be deleted while the timer is still active
74  auto callback = [timer, action, self = shared_from_this()](const std::error_code & ec)
75  {
76  if (!ec) // an error indicate timer was canceled
77  {
78  action();
79  }
80  };
81 
82  timer->impl.async_wait(callback);
83 
84  return Timer(timer);
85  }
86 
87  virtual void post(const action_t& action) override
88  {
89  this->io_service->post(action);
90  }
91 
92  virtual steady_time_t get_time() override
93  {
94  return std::chrono::steady_clock::now();
95  }
96 
97  // lots of ASIO components must be initialized with a reference to the io_service
98  inline std::shared_ptr<asio::io_service> get_service()
99  {
100  return io_service;
101  }
102 
103 private:
104  // we hold a shared_ptr to the io_service so that it cannot dissapear while the executor is still around
105  const std::shared_ptr<asio::io_service> io_service;
106 };
107 
108 }
109 
110 #endif
exe4cpp header-only library namespace
Definition: AsioTimer.h:29
virtual void post(const action_t &action) override
Definition: BasicExecutor.h:87
virtual Timer start(const duration_t &duration, const action_t &action) override
Definition: BasicExecutor.h:62
virtual steady_time_t get_time() override
Definition: BasicExecutor.h:92
virtual Timer start(const steady_time_t &expiration, const action_t &action) override
Definition: BasicExecutor.h:67