Toast Engine
Loading...
Searching...
No Matches
workspace_history.hpp
1#pragma once
2
3#include "../export.hpp"
5
6#include <functional>
7#include <memory>
8#include <toast/assets/prefab.hpp>
9#include <toast/export.hpp>
10
11namespace toast {
12
13class TOAST_API WorkspaceHistory {
14public:
15 using Snapshot = assets::Prefab;
16 using Capture = std::function<Snapshot()>;
17 using Restore = std::function<bool(const Snapshot&)>;
18
19 struct Context {
20 event::HistoryOperation operation = event::HistoryOperation::change_value;
21 UID node;
22 std::string node_name;
23 std::string subject;
24 std::string previous_value;
25 std::string current_value;
26 };
27
28 WorkspaceHistory(uint64_t handle, Capture capture, Restore restore, bool available, bool initially_saved);
29 ~WorkspaceHistory();
30
31 [[nodiscard]]
32 auto available() const noexcept -> bool {
33 return m_available;
34 }
35
36 [[nodiscard]]
37 auto transactionOpen() const noexcept -> bool {
38 return m_transaction.has_value();
39 }
40
41 [[nodiscard]]
42 auto restoring() const noexcept -> bool {
43 return m_restoring;
44 }
45
46 void begin(uint64_t transaction, Context context);
47 void commit(uint64_t transaction = 0);
48 void cancel(uint64_t transaction = 0);
49
50 auto beginAtomic(Context context) -> bool;
51 void finishAtomic(bool owned);
52
53 void apply(const event::WorkspaceApplyHistorySnapshot& request);
54 void prepareMerge(const event::WorkspacePrepareHistoryMerge& request);
55 void resolve(const event::WorkspaceResolveHistoryConflicts& resolutions);
56
57 void sendInitial() const;
58
59private:
60 struct Transaction {
61 uint64_t id = 0;
62 Context context;
63 std::shared_ptr<const Snapshot> before;
64 bool observed_mutation = false;
65 };
66
67 struct PendingMerge;
68
69 uint64_t m_handle = 0;
70 Capture m_capture;
71 Restore m_restore;
72 bool m_available = true;
73 bool m_initially_saved = false;
74 bool m_restoring = false;
75 std::optional<Transaction> m_transaction;
76 std::unique_ptr<PendingMerge> m_pending;
77
78 [[nodiscard]]
79 auto same(const Snapshot& a, const Snapshot& b) const -> bool;
80 void beginMerge(
81 uint64_t request, bool merge, std::shared_ptr<const Snapshot> base, std::shared_ptr<const Snapshot> current,
82 std::shared_ptr<const Snapshot> incoming
83 );
84};
85
86}
Definition prefab.hpp:54
Definition uid.hpp:20
Definition workspace_history.hpp:19