GCC Code Coverage Report


Directory: src/gate/
File: src/gate/asyncexecution.hpp
Date: 2026-06-21 00:38:37
Exec Total Coverage
Lines: 25 25 100.0%
Functions: 8 8 100.0%
Branches: 4 8 50.0%

Line Branch Exec Source
1 /* GATE PROJECT LICENSE:
2 +----------------------------------------------------------------------------+
3 | Copyright (c) 2018-2026, Stefan Meislinger <sm@opengate.at> |
4 | All rights reserved. |
5 | |
6 | Redistribution and use in source and binary forms, with or without |
7 | modification, are permitted provided that the following conditions are met:|
8 | |
9 | 1. Redistributions of source code must retain the above copyright notice, |
10 | this list of conditions and the following disclaimer. |
11 | 2. Redistributions in binary form must reproduce the above copyright |
12 | notice, this list of conditions and the following disclaimer in the |
13 | documentation and/or other materials provided with the distribution. |
14 | |
15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
16 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
17 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
18 | ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
19 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
20 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
21 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
22 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
23 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
24 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
25 | THE POSSIBILITY OF SUCH DAMAGE. |
26 +----------------------------------------------------------------------------+
27 */
28
29 /** @file
30 * @brief Asynchronous code execution
31 * @ingroup gatecore_cpp
32 */
33
34 #ifndef GATE_ASYNCEXECUTION_HPP_INCLUDED
35 #define GATE_ASYNCEXECUTION_HPP_INCLUDED
36
37 #include "gate/gate_core_api.hpp"
38 #include "gate/gatetypes.hpp"
39 #include "gate/runnables.hpp"
40 #include "gate/wrappers.hpp"
41
42 #include "gate/asyncexecution.h"
43
44 namespace gate
45 {
46
47
48 /// @brief
49 class GATE_CORE_CPP_API FutureBase : public SafeBoolBase<FutureBase>
50 {
51 public:
52 FutureBase(size_t typeSize, gate_mem_copyctor_t cctor = NULL, gate_mem_dtor_t dtor = NULL);
53 FutureBase(FutureBase const& src);
54 FutureBase& operator=(FutureBase const& src);
55 ~FutureBase() noexcept;
56
57 bool hasValue() const noexcept;
58 result_t getResult() const noexcept;
59 void const* getData() const noexcept;
60 void set(result_t result, void const* data);
61 void wait();
62 bool waitFor(uint32_t timeoutMs);
63
64 template<class DURATION>
65 1 bool waitFor(DURATION const& duration)
66 {
67
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 time::Milliseconds ms = time::duration_cast<time::Milliseconds>(duration);
68
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
2 return this->waitFor(static_cast<uint32_t>(ms.value()));
69 }
70
71
72 bool empty() const noexcept;
73 bool operator!() const noexcept;
74
75 gate_future_t* c_impl();
76 gate_future_t const* c_impl() const;
77
78 private:
79 gate_future_t impl;
80 };
81
82
83 /// @brief
84 /// @tparam T
85 template<class T>
86 class GATE_CORE_CPP_TEMPLATE_API Future : private FutureBase
87 {
88 public:
89 typedef T value_t;
90 typedef Future<T> self_t;
91
92 2 Future()
93 2 : FutureBase(sizeof(value_t), &TypeFunctions<value_t>::copyConstruct, &TypeFunctions<value_t>::destruct)
94 {
95 2 }
96 1 Future(self_t const& src)
97 1 : FutureBase(src)
98 {
99 1 }
100 1 self_t& operator=(self_t const& src)
101 {
102
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (this != &src)
103 {
104 1 FutureBase::operator=(src);
105 }
106 1 return *this;
107 }
108 3 ~Future() noexcept
109 {
110 3 }
111
112 using FutureBase::hasValue;
113 using FutureBase::empty;
114 using FutureBase::operator!;
115 using FutureBase::wait;
116 using FutureBase::waitFor;
117 using FutureBase::c_impl;
118
119
120 4 value_t const* getData() const noexcept
121 {
122 4 return static_cast<value_t const*>(FutureBase::getData());
123 }
124 2 void set(result_t result, value_t const& value)
125 {
126 2 FutureBase::set(result, static_cast<void const*>(&value));
127 1 }
128
129 2 value_t const& value()
130 {
131 2 this->wait();
132 2 value_t const* ptrValue = this->getData();
133
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 GATE_DEBUG_ASSERT(ptrValue);
134 2 return *ptrValue;
135 }
136 };
137
138
139 /// @brief
140 class GATE_CORE_CPP_API Async : private NonCopyable
141 {
142 public:
143 static void start(Runnable const& runnable);
144
145 template<class T, class F>
146 class AsyncRunnable : public IRunnableBuilder
147 {
148 private:
149 Future<Expected<T, Exception> > futureStore;
150 F functor;
151 public:
152
153 AsyncRunnable(Future<Expected<T, Exception> > const& fut, F const& func)
154 : futureStore(fut), functor(func)
155 {
156 }
157
158 virtual void run()
159 {
160 try
161 {
162 T result;
163 this->functor(result);
164 Expected<T, Exception> exp(result);
165 this->futureStore.set(exp);
166 }
167 catch (Throwable const& th)
168 {
169 Expected<T, Exception> exp(Exception(th.getResult(), th.getMessage(), th.getSource(), th.getErrorCode()));
170 this->futureStore.set(exp);
171 }
172 catch (...)
173 {
174 Expected<T, Exception> exp(Exception(results::UnknownException, "Unknown exception", "AsyncRunnable::run"));
175 this->futureStore.set(exp);
176 }
177 }
178
179 };
180
181
182 template<class T, class F>
183 static Runnable bind(F const& functor, Future<Expected<T, Exception> >& future)
184 {
185 AsyncRunnable<T, F>* asyncrun = new AsyncRunnable<T, F>(future, functor);
186 Runnable runnable(asyncrun->c_impl());
187 return runnable;
188 }
189 };
190
191 } // namespace gate
192
193 #endif
194