libs/corosio/src/corosio/src/timer.cpp

100.0% Lines (40/40) 100.0% Functions (10/10) 80.0% Branches (8/10)
libs/corosio/src/corosio/src/timer.cpp
Line Branch Hits Source Code
1 //
2 // Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com)
3 // Copyright (c) 2026 Steve Gerbino
4 //
5 // Distributed under the Boost Software License, Version 1.0. (See accompanying
6 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 //
8 // Official repository: https://github.com/cppalliance/corosio
9 //
10
11 #include <boost/corosio/timer.hpp>
12
13 #include <boost/corosio/detail/except.hpp>
14
15 namespace boost::corosio {
16
17 namespace detail {
18
19 // Defined in timer_service.cpp
20 extern timer::timer_impl* timer_service_create(capy::execution_context&);
21 extern void timer_service_destroy(timer::timer_impl&) noexcept;
22 extern timer::time_point timer_service_expiry(timer::timer_impl&) noexcept;
23 extern std::size_t timer_service_expires_at(timer::timer_impl&, timer::time_point);
24 extern std::size_t timer_service_expires_after(timer::timer_impl&, timer::duration);
25 extern std::size_t timer_service_cancel(timer::timer_impl&) noexcept;
26 extern std::size_t timer_service_cancel_one(timer::timer_impl&) noexcept;
27
28 } // namespace detail
29
30 8784 timer::
31 8782 ~timer()
32 {
33
2/2
✓ Branch 0 taken 8778 times.
✓ Branch 1 taken 4 times.
8782 if (impl_)
34 8778 detail::timer_service_destroy(get());
35 8784 }
36
37 8780 timer::
38 8780 timer(capy::execution_context& ctx)
39 8780 : io_object(ctx)
40 {
41
1/1
✓ Branch 1 taken 8780 times.
8780 impl_ = detail::timer_service_create(ctx);
42 8780 }
43
44 2 timer::
45 2 timer(capy::execution_context& ctx, time_point t)
46 2 : timer(ctx)
47 {
48
1/1
✓ Branch 1 taken 2 times.
2 expires_at(t);
49 2 }
50
51 2 timer::
52 2 timer(timer&& other) noexcept
53 2 : io_object(other.context())
54 {
55 2 impl_ = other.impl_;
56 2 other.impl_ = nullptr;
57 2 }
58
59 timer&
60 4 timer::
61 operator=(timer&& other)
62 {
63
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (this != &other)
64 {
65
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 if (ctx_ != other.ctx_)
66 2 detail::throw_logic_error(
67 "cannot move timer across execution contexts");
68
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (impl_)
69 2 detail::timer_service_destroy(get());
70 2 impl_ = other.impl_;
71 2 other.impl_ = nullptr;
72 }
73 2 return *this;
74 }
75
76 std::size_t
77 20 timer::
78 cancel()
79 {
80 20 return detail::timer_service_cancel(get());
81 }
82
83 std::size_t
84 4 timer::
85 cancel_one()
86 {
87 4 return detail::timer_service_cancel_one(get());
88 }
89
90 timer::time_point
91 34 timer::
92 expiry() const
93 {
94 34 return detail::timer_service_expiry(get());
95 }
96
97 std::size_t
98 18 timer::
99 expires_at(time_point t)
100 {
101 18 return detail::timer_service_expires_at(get(), t);
102 }
103
104 std::size_t
105 8769 timer::
106 expires_after(duration d)
107 {
108 8769 return detail::timer_service_expires_after(get(), d);
109 }
110
111 } // namespace boost::corosio
112