SSP21-CPP
AsioTimer.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 #include "exe4cpp/ITimer.h"
26 
27 #include "asio.hpp"
28 
29 namespace exe4cpp
30 {
31 
32 class AsioTimer final : public exe4cpp::ITimer
33 {
34  friend class BasicExecutor;
35  friend class StrandExecutor;
36 
37 public:
38  AsioTimer(const std::shared_ptr<asio::io_service>& io_service) :
39  io_service{io_service},
40  impl{*io_service}
41  {}
42 
43  // Uncopyable
44  AsioTimer(const AsioTimer&) = delete;
45  AsioTimer& operator=(const AsioTimer&) = delete;
46 
47  static std::shared_ptr<AsioTimer> create(const std::shared_ptr<asio::io_service>& io_service)
48  {
49  return std::make_shared<AsioTimer>(io_service);
50  }
51 
52  virtual void cancel() override
53  {
54  std::error_code ec;
55  impl.cancel(ec);
56  }
57 
58  virtual steady_time_t expires_at() override
59  {
60  return impl.expires_at();
61  }
62 
63 private:
64  const std::shared_ptr<asio::io_service> io_service;
65  asio::basic_waitable_timer<std::chrono::steady_clock> impl;
66 };
67 
68 }
exe4cpp header-only library namespace
Definition: AsioTimer.h:29