GCC Code Coverage Report


Directory: src/gate/
File: src/gate/asyncexecution.hpp
Date: 2026-08-06 22:46:24
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 using FutureBase::getResult;
119
120
121 4 value_t const* getData() const noexcept
122 {
123 4 return static_cast<value_t const*>(FutureBase::getData());
124 }
125 2 void set(result_t result, value_t const& value)
126 {
127 2 FutureBase::set(result, static_cast<void const*>(&value));
128 1 }
129
130 2 value_t const& value()
131 {
132 2 this->wait();
133 2 value_t const* ptrValue = this->getData();
134
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 GATE_DEBUG_ASSERT(ptrValue);
135 2 return *ptrValue;
136 }
137 };
138
139
140 /// @brief
141 class GATE_CORE_CPP_API Async : private NonCopyable
142 {
143 public:
144 static void start(Runnable const& runnable);
145
146 template<class T, class F>
147 class AsyncRunnable : public IRunnableBuilder
148 {
149 private:
150 Future<Expected<T, Exception> > futureStore;
151 F functor;
152 public:
153
154 AsyncRunnable(Future<Expected<T, Exception> > const& fut, F const& func)
155 : futureStore(fut), functor(func)
156 {
157 }
158
159 virtual void run()
160 {
161 try
162 {
163 T result;
164 this->functor(result);
165 Expected<T, Exception> exp(result);
166 this->futureStore.set(exp);
167 }
168 catch (Throwable const& th)
169 {
170 Expected<T, Exception> exp(Exception(th.getResult(), th.getMessage(), th.getSource(), th.getErrorCode()));
171 this->futureStore.set(exp);
172 }
173 catch (...)
174 {
175 Expected<T, Exception> exp(Exception(results::UnknownException, "Unknown exception", "AsyncRunnable::run"));
176 this->futureStore.set(exp);
177 }
178 }
179
180 };
181
182
183 template<class T, class F>
184 static Runnable bind(F const& functor, Future<Expected<T, Exception> >& future)
185 {
186 AsyncRunnable<T, F>* asyncrun = new AsyncRunnable<T, F>(future, functor);
187 Runnable runnable(asyncrun->c_impl());
188 return runnable;
189 }
190 };
191
192 } // namespace gate
193
194 #endif
195