Skip to content

Commit dd35631

Browse files
committed
minor cleanup
1 parent 0e817d4 commit dd35631

1 file changed

Lines changed: 16 additions & 10 deletions

File tree

libs/internal/include/launchdarkly/async/promise.hpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,12 @@ class PromiseInternal {
161161
Promise<R> newPromise;
162162
Future<R> newFuture = newPromise.GetFuture();
163163

164-
T already_resolved;
164+
std::optional<T> already_resolved;
165165
{
166166
std::lock_guard lock(mutex_);
167167

168168
if (result_.has_value()) {
169-
already_resolved = *result_;
169+
already_resolved = result_;
170170
} else {
171171
continuations_.push_back(
172172
[newPromise = std::move(newPromise),
@@ -189,7 +189,7 @@ class PromiseInternal {
189189
[newPromise = std::move(newPromise),
190190
continuation = std::move(continuation),
191191
result = std::move(already_resolved)]() mutable {
192-
newPromise.Resolve(continuation(result));
192+
newPromise.Resolve(continuation(*result));
193193
}));
194194
return newFuture;
195195
}
@@ -216,18 +216,18 @@ class PromiseInternal {
216216
Future<T2> innerFuture = continuation(val);
217217
innerFuture.Then(
218218
[outerPromise = std::move(outerPromise)](
219-
T2 const& inner_val) mutable -> T2 {
219+
T2 const& inner_val) mutable -> std::monostate {
220220
outerPromise.Resolve(inner_val);
221-
return inner_val;
221+
return {};
222222
},
223223
executor);
224224
};
225225

226-
T already_resolved;
226+
std::optional<T> already_resolved;
227227
{
228228
std::lock_guard lock(mutex_);
229229
if (result_.has_value()) {
230-
already_resolved = *result_;
230+
already_resolved = result_;
231231
} else {
232232
continuations_.push_back([do_work = std::move(do_work),
233233
executor](T const& result) mutable {
@@ -245,14 +245,14 @@ class PromiseInternal {
245245
executor(Continuation<void()>(
246246
[do_work = std::move(do_work),
247247
result = std::move(already_resolved)]() mutable {
248-
do_work(result);
248+
do_work(*result);
249249
}));
250250
return outerFuture;
251251
}
252252

253253
private:
254254
mutable std::mutex mutex_;
255-
std::optional<T> result_{std::nullopt};
255+
std::optional<T> result_{};
256256
std::vector<Continuation<void(T const&)>> continuations_;
257257
};
258258

@@ -284,7 +284,13 @@ class Promise {
284284
// Sets the result to the given value and schedules any continuations that
285285
// were registered via Future::Then. Returns true if the result was set, or
286286
// false if Resolve was already called.
287-
bool Resolve(T result) { return internal_->Resolve(result); }
287+
bool Resolve(T result) {
288+
if constexpr (std::is_move_constructible_v<T>) {
289+
return internal_->Resolve(std::move(result));
290+
} else {
291+
return internal_->Resolve(result);
292+
}
293+
}
288294

289295
// Returns a Future that will resolve when this Promise is resolved.
290296
// May be called multiple times; each call returns a Future referring to

0 commit comments

Comments
 (0)