Line data Source code
1 : //
2 : // Copyright (c) 2025 Vinnie Falco (vinnie dot falco at gmail dot com)
3 : //
4 : // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 : // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 : //
7 : // Official repository: https://github.com/cppalliance/corosio
8 : //
9 :
10 : #include <boost/corosio/timer.hpp>
11 :
12 : #include <boost/corosio/detail/except.hpp>
13 :
14 : namespace boost::corosio {
15 :
16 : namespace detail {
17 :
18 : // Defined in timer_service.cpp
19 : extern timer::timer_impl* timer_service_create(capy::execution_context&);
20 : extern void timer_service_destroy(timer::timer_impl&) noexcept;
21 : extern timer::time_point timer_service_expiry(timer::timer_impl&) noexcept;
22 : extern void timer_service_expires_at(timer::timer_impl&, timer::time_point);
23 : extern void timer_service_expires_after(timer::timer_impl&, timer::duration);
24 : extern void timer_service_cancel(timer::timer_impl&) noexcept;
25 :
26 : } // namespace detail
27 :
28 5212 : timer::
29 5212 : ~timer()
30 : {
31 5212 : if (impl_)
32 5208 : detail::timer_service_destroy(get());
33 5212 : }
34 :
35 5210 : timer::
36 5210 : timer(capy::execution_context& ctx)
37 5210 : : io_object(ctx)
38 : {
39 5210 : impl_ = detail::timer_service_create(ctx);
40 5210 : }
41 :
42 2 : timer::
43 2 : timer(timer&& other) noexcept
44 2 : : io_object(other.context())
45 : {
46 2 : impl_ = other.impl_;
47 2 : other.impl_ = nullptr;
48 2 : }
49 :
50 : timer&
51 4 : timer::
52 : operator=(timer&& other)
53 : {
54 4 : if (this != &other)
55 : {
56 4 : if (ctx_ != other.ctx_)
57 2 : detail::throw_logic_error(
58 : "cannot move timer across execution contexts");
59 2 : if (impl_)
60 2 : detail::timer_service_destroy(get());
61 2 : impl_ = other.impl_;
62 2 : other.impl_ = nullptr;
63 : }
64 2 : return *this;
65 : }
66 :
67 : void
68 14 : timer::
69 : cancel()
70 : {
71 14 : detail::timer_service_cancel(get());
72 14 : }
73 :
74 : timer::time_point
75 28 : timer::
76 : expiry() const
77 : {
78 28 : return detail::timer_service_expiry(get());
79 : }
80 :
81 : void
82 14 : timer::
83 : expires_at(time_point t)
84 : {
85 14 : detail::timer_service_expires_at(get(), t);
86 14 : }
87 :
88 : void
89 5199 : timer::
90 : expires_after(duration d)
91 : {
92 5199 : detail::timer_service_expires_after(get(), d);
93 5199 : }
94 :
95 : } // namespace boost::corosio
|