FOSSredder 0.5.0
Loading...
Searching...
No Matches
StorageManager.cpp
Go to the documentation of this file.
1
5
6#include "core/pch.h"
8
9#include <filesystem>
10#include <stdexcept>
11#include <utility>
12
13namespace core::storage {
14
16 std::shared_ptr<core::ports::storage::IRegistry> registry)
17 : registry_(std::move(registry)) {
18}
19
21 atomicSave_ = std::move(saveFn);
22}
23
25 atomicLoad_ = std::move(loadFn);
26}
27
29 onDeletionImpact_ = std::move(cb);
30}
31
32std::optional<std::string> StorageManager::loadLatestPath() const {
33 if (registry_) {
34 return registry_->getLatest();
35 }
36
37 return std::nullopt;
38}
39
40void StorageManager::rememberLatestPath(const std::string& filePath) {
41 if (registry_) {
42 registry_->setLatest(filePath);
43 }
44}
45
47 const std::string previousPath = currentPath_;
48 currentPath_ = filePath;
49
50 try {
51 if (!atomicLoad_) {
52 throw std::runtime_error("Atomic store load callback not configured");
53 }
54
55 const core::application::workspace::WorkspaceSessionState document = atomicLoad_(currentPath_);
56 rememberLatestPath(filePath);
57 return document;
58 } catch (...) {
59 currentPath_ = previousPath;
60 throw;
61 }
62}
63
65 if (currentPath_.empty()) {
66 throw std::runtime_error("No file opened");
67 }
68
69 saveAs(currentPath_, document);
70}
71
72void StorageManager::saveAs(const std::string& filePath, const core::application::workspace::WorkspaceSessionState& document) {
73 const std::string previousPath = currentPath_;
74 currentPath_ = filePath;
75
76 try {
77 if (!atomicSave_) {
78 throw std::runtime_error("Atomic store save callback not configured");
79 }
80
81 DeletionImpact impact = atomicSave_(currentPath_, document);
82 rememberLatestPath(filePath);
83 if (onDeletionImpact_ && !impact.empty()) {
84 onDeletionImpact_(impact);
85 }
86 } catch (...) {
87 currentPath_ = previousPath;
88 throw;
89 }
90} // namespace core::storage
91
92void StorageManager::createNew(const std::string& filePath) {
93 std::filesystem::path p(filePath);
94 auto dir = p.parent_path();
95 if (!dir.empty() && !std::filesystem::exists(dir)) {
96 std::filesystem::create_directories(dir);
97 }
98 currentPath_ = filePath;
99
100 if (atomicLoad_) {
101 (void)atomicLoad_(currentPath_);
102 }
103
104 rememberLatestPath(filePath);
105}
106
107}
Storage manager: repository factory, atomic save/load and registry handling.
void setAtomicStoreSave(AtomicStoreSave saveFn) override
Configure the atomic save callback.
core::ports::storage::IStorageManager::AtomicStoreLoad AtomicStoreLoad
void save(const core::application::workspace::WorkspaceSessionState &document) override
Save the current application state.
void setDeletionImpactCallback(DeletionImpactCallback cb) override
Configure the deletion impact callback.
void saveAs(const std::string &filePath, const core::application::workspace::WorkspaceSessionState &document) override
Save the current application state to a specific file path.
core::ports::storage::IStorageManager::DeletionImpactCallback DeletionImpactCallback
void createNew(const std::string &filePath) override
Create a new storage target.
std::optional< std::string > loadLatestPath() const override
Load the latest known path.
core::ports::storage::IStorageManager::AtomicStoreSave AtomicStoreSave
core::application::workspace::WorkspaceSessionState loadFrom(const std::string &filePath) override
Load an application state from a file path.
StorageManager(std::shared_ptr< core::ports::storage::IRegistry > registry=nullptr)
Creates a storage manager with an optional latest-path registry backend.
void setAtomicStoreLoad(AtomicStoreLoad loadFn) override
Configure the atomic load callback.
Collects common core includes and aliases for precompiled-header builds.
Lists entity identifiers removed by cascading workspace operations.
bool empty() const noexcept
Reports whether no identifiers were deleted.