Aurore
A Switch-like interface for your app, on every screen
Loading...
Searching...
No Matches
guarded.hpp
Go to the documentation of this file.
1// guarded — a callback that does nothing once its screen is gone.
2//
3// A screen (a tab, an activity) owns an `alive` token; what finishes later
4// (aurore::sync after a background task, a flow that waits for the network or
5// a dialog) takes guarded(alive, fn) instead of a bare [this]: fn runs only
6// while the token is held and true. Checked where the callback is called,
7// which is the UI thread: a worker thread hands it to aurore::sync, never calls
8// it itself. No Aurore: host-tested (tests/host/guarded_tests.cpp).
9// Copyright (C) 2026 JigSawFr. GPLv3-or-later (see LICENSE.GPL).
10#pragma once
11
12#include <memory>
13#include <utility>
14
15namespace ui
16{
17
20using Alive = std::shared_ptr<bool>;
23using Weak = std::weak_ptr<bool>;
24inline Alive make_alive() { return std::make_shared<bool>(true); }
25
28template <typename F>
29auto guarded(Weak alive, F fn)
30{
31 return [weak = std::move(alive), fn = std::move(fn)](auto&&... args)
32 {
33 const Alive held = weak.lock();
34 if (!held || !*held)
35 return;
36 fn(std::forward<decltype(args)>(args)...);
37 };
38}
39template <typename F>
40auto guarded(const Alive& alive, F fn)
41{
42 return guarded(Weak(alive), std::move(fn));
43}
44
45} // namespace ui
Definition action_sheet.hpp:11
std::weak_ptr< bool > Weak
Definition guarded.hpp:23
std::shared_ptr< bool > Alive
Definition guarded.hpp:20
auto guarded(Weak alive, F fn)
Definition guarded.hpp:29
Alive make_alive()
Definition guarded.hpp:24