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

100.0% Lines (36/36) 100.0% Functions (8/8) 77.8% Branches (7/9)
libs/corosio/src/corosio/src/timer.cpp
Line Branch Hits 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 5184 timer::
29 5184 ~timer()
30 {
31
2/2
✓ Branch 0 taken 5180 times.
✓ Branch 1 taken 4 times.
5184 if (impl_)
32 5180 detail::timer_service_destroy(get());
33 5184 }
34
35 5182 timer::
36 5182 timer(capy::execution_context& ctx)
37 5182 : io_object(ctx)
38 {
39
1/1
✓ Branch 1 taken 5182 times.
5182 impl_ = detail::timer_service_create(ctx);
40 5182 }
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
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (this != &other)
55 {
56
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 if (ctx_ != other.ctx_)
57 2 detail::throw_logic_error(
58 "cannot move timer across execution contexts");
59
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
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 5171 timer::
90 expires_after(duration d)
91 {
92 5171 detail::timer_service_expires_after(get(), d);
93 5171 }
94
95 } // namespace boost::corosio
96