Fray Engine
Loading...
Searching...
No Matches
fray::ProjectManager Class Reference

Owns a project’s files, state stores, watcher and registered threads. Public operations synchronize their own mutable state unless they return a reference or expose queue-owned pendingStep. Opening, closing and file-change handling are intended for the project’s own thread; callers must keep borrowed references valid and coordinate their use. More...

#include <project_manager.hpp>

Classes

struct  CloseStatus
 The latest queued close and its result, once finished. More...
struct  RenameStatus
 The latest queued rename and its result, once finished. More...
struct  RenameRequest
 Rename arguments captured before the log worker suspends its session. More...
struct  PendingStep
 Undo/redo request waiting asynchronously for renderer-supplied revisions; used only on the project queue. More...

Public Member Functions

 ProjectManager ()=default
 Constructs an initially closed project manager.
 ProjectManager (const ProjectManager &)=delete
 Project managers cannot be copied.
ProjectManager & operator= (const ProjectManager &)=delete
 Project managers cannot be copy-assigned.
 ~ProjectManager ()
 Closes an open project or cancels a pending database choice, logging close failures.
F_err open (const std::string &path, const std::string &applicationId, files::ConflictChoice choice=files::ConflictChoice::Ask, threads::ThreadManager *threads=nullptr, std::shared_ptr< threads::TaskQueue > queue=nullptr, files::OpenMode mode=files::OpenMode::OpenOrCreate, std::optional< bool > newProjectPackageDefault=std::nullopt)
 Opens or creates a project and its Fray state on the calling thread. Intended for the project thread; takes _mutex, acquires project files and may start watching. A database conflict leaves an open attempt pending for resolveDatabase or cancelOpen.
std::vector< std::string > databaseCandidates () const
 Returns a locked copy of database stems offered after DATABASE_CONFLICT.
F_err resolveDatabase (const DatabaseChoice &choice)
 Resolves a pending database conflict and finishes opening under _mutex.
F_err cancelOpen ()
 Abandons a pending database choice under _mutex, removing an extracted folder but leaving the archive untouched.
void beginOpening ()
 Marks the project as opening using an atomic flag.
bool isOpening () const
 Returns whether the atomic opening flag is set.
threads::ThreadManager::Id registerThread (const std::string &name, std::function< void(threads::StopToken)> body)
 Registers and starts a project-owned thread under _threadMutex. The thread is tagged with the project ID for logging; the project thread remains alive while registered threads run.
void stopThread (threads::ThreadManager::Id id)
 Removes a thread from registration, requests its stop and joins it. Releases _threadMutex before joining; do not call from the thread being joined or while holding a lock that thread needs to exit.
void requestStopThreads ()
 Requests cooperative termination of registered project threads without joining them.
bool hasThreads () const
 Returns whether any registered project thread is running, under _threadMutex.
void setRenameCallback (std::function< void(const std::filesystem::path &)> callback)
 Replaces the callback invoked with the new working path after a rename. Takes _mutex; the callback runs synchronously under _mutex and must not wait for work requiring it.
void setClosingCallback (std::function< void()> callback)
 Replaces the callback invoked at close start before packaging or deletion. Takes _mutex; the callback runs synchronously under _mutex and must not wait for work requiring it.
void setCloseDispatcher (std::function< F_err(bool)> dispatcher)
 Installs the owner-provided asynchronous close dispatcher.
void setLogCloseBarrier (std::function< bool()> callback)
 Installs the owner-provided project-log close barrier.
watch::FileWatcher & watcher ()
 Returns a borrowed watcher reference; its lifetime is bounded by this manager.
void restoreLock ()
 Recreates a renamed or deleted project lock while open, under _mutex.
void pump ()
 Dispatches queued watcher events on the calling thread while open, under _mutex.
F_err refreshFiles ()
 Polls the watcher and dispatches changes on the calling thread.
F_err rename (const std::string &newName)
 Requests a project rename; managed projects complete it on their queues.
std::optional< RenameStatus > renameStatus () const
 Reads the most recent rename state without waiting for its work.
bool isOpen () const
 Returns whether the atomic open flag is set.
std::string name () const
 Returns a copy of the current project name under _mutex.
std::string id () const
 Returns a copy of the persistent project ID under _mutex.
std::string applicationId () const
 Returns a copy of the application identifier under _mutex.
files::FileManager & fileManager ()
 Returns a borrowed file manager reference; callers must coordinate access and lifetime.
F_err openState (const std::string &name)
 Opens a named state store if not already open, under _mutex.
state::StateManager & state (const std::string &name)
 Returns a borrowed reference to an open named state store under _mutex. The reference is invalidated when its store is replaced or the project closes; coordinate its lifetime with project operations.
history::HistoryManager history ()
 Returns a history manager borrowing the open Fray state store.
template<typename T>
F_err set (const std::string &key, const T &value)
 Writes Fray state under _mutex; project.info.name instead requests a project rename.
template<typename T>
F_err get (const std::string &key, T &value)
 Reads Fray state under _mutex.
ProjectMessageResult handleProjectMessage (const message::InData &incoming)
 Handles a close, layout-change or history-step message on the selected project queue. May mutate state or return an asynchronous history request rather than waiting for the renderer.
F_err close (bool forcePackage=false)
 Requests a project close without waiting for it to finish. Keep a standalone project alive until closeStatus() reports completion.
std::optional< CloseStatus > closeStatus () const
 Reads the most recent direct or managed close state.

Static Public Member Functions

static bool validProjectName (const std::string &name)
 Checks whether a project name is a safe single relative component without a .fry suffix.

Public Attributes

std::optional< PendingStep > pendingStep
 Pending asynchronous history step, accessed only on the project queue.

Private Member Functions

std::string locateDatabase (const std::filesystem::path &folder) const
 Finds the database by its recorded file identity, including after a rename. Caller must hold _mutex.
F_err prepareDatabase ()
 Selects, snapshots or adopts a project database before opening state. Caller must hold _mutex; multiple valid candidates leave a choice pending.
void abandon (bool removeExtracted)
 Rolls back an unsuccessful open, releases files and clears pending state. Caller must hold _mutex; cleanup failures are logged.
F_err reassignProjectId (const std::string &id)
 Persists a replacement identity for an open copied project.
F_err applyDefaults (state::StateManager &state)
 Adds missing application and project defaults without replacing existing settings. Caller must hold _mutex.
F_err finishOpen ()
 Opens Fray state, applies defaults, commits conflict resolution and starts watching. Caller must hold _mutex; failure abandons the open attempt.
void startWatching ()
 Watches the project folder and, when configured, starts a watcher thread that posts to the project queue. Caller must hold _mutex; watch failures are logged.
void stopWatching ()
 Stops and joins the watcher thread, removes its subscription and unwatches the folder. Called during close under _mutex; joining waits for the watcher thread to exit.
F_err startRename (RenameRequest request)
 Starts a rename while holding _mutex, or applies it directly for a standalone project.
void finishRename (F_err result)
 Records the result of a queued rename.
void replayRenameChanges ()
 Replays watcher changes received while a rename was in progress.
RenameRequest refreshRenameRequest (RenameRequest request)
 Updates a queued rename for folder moves observed while its log was suspended. Caller holds _mutex.
void onChange (const watch::Change &change)
 Handles project folder and database rename events on the dispatching thread. Takes _mutex and may rename project resources.
F_err applyName (const std::string &newName, const std::filesystem::path &workspace, bool renameFolder, std::string databaseStem)
 Coordinates project folder, database, lock, archive and stored-name changes. Caller must hold _mutex; locks each state store, closes and reopens connections, holds the watcher, and invokes the rename callback under _mutex. File failures trigger rollback attempts.
F_err closeNow (bool forcePackage=false, bool *packaged=nullptr, bool leaveWorkingFolder=false)
 Stops watching, closes stores and releases or packages the project under _mutex. Intended for the project thread; invokes the closing callback before packaging or deletion. Failed closes can be retried.

Static Private Member Functions

static bool looksLikeFrayDatabase (const std::filesystem::path &path)
 Checks read-only for the current Fray state table and its key, type and value columns.
static std::vector< std::string > databaseFiles (const std::filesystem::path &folder)
 Lists sorted stems of nonsymlink regular .sqlite files in a folder.
template<typename T>
static F_err setDefault (state::StateManager &state, const std::string &key, const T &value)
 Writes a default only if the state key is absent. Runs on the opening thread through the state manager.
static std::string newProjectId ()
 Generates a hexadecimal project identifier from random-device values.

Private Attributes

std::recursive_mutex _mutex
 Protects project lifecycle, state map, file operations and callbacks; recursive for nested operations.
files::FileManager _files
 Owns project paths, lock and archive operations, normally guarded by _mutex.
std::unordered_map< std::string, std::unique_ptr< state::StateManager > > _states
 Owns named state stores; map and lifetimes are guarded by _mutex.
std::string _applicationId
 Application identifier used when opening state stores, guarded by _mutex.
std::string _projectId
 Persistent project identifier used for logging and identity, guarded by _mutex.
std::atomic< bool > _open {false}
 Whether a project is open, readable without _mutex.
std::atomic< bool > _opening {false}
 Whether an opening attempt is in progress, readable without _mutex.
std::atomic< bool > _closePending {false}
 Whether a managed close has been queued, readable without _mutex.
bool _closing = false
 Whether close has begun and can be retried, guarded by _mutex.
bool _packageOnClose = false
 Whether the current close attempt packages the project, guarded by _mutex.
std::optional< bool > _newProjectPackageDefault
 Packaging default captured for a newly created project.
bool _archiveComplete = false
 Whether packaging already succeeded during a retriable close, guarded by _mutex.
bool _pendingDatabase = false
 Whether opening awaits a database choice, guarded by _mutex.
std::vector< std::string > _candidates
 Candidate database stems for a pending choice, guarded by _mutex.
watch::FileWatcher _watcher
 Owns project folder watching and dispatch.
watch::FileId _databaseId
 File identity used to recognize the project database across renames, guarded by _mutex.
watch::FileWatcher::Subscription _subscription = 0
 Watcher subscription for project changes, or zero when absent.
threads::ThreadManager * _threads = nullptr
 Non-owning thread manager supplied at open; it must outlive registered threads.
std::shared_ptr< threads::TaskQueue > _queue
 Shared project queue used to post watcher work.
std::mutex _threadMutex
 Protects registered-thread bookkeeping and thread-manager access in thread methods.
std::vector< threads::ThreadManager::Id > _registered
 IDs registered for this project, guarded by _threadMutex.
threads::ThreadManager::Id _watcherThread = 0
 Registered watcher thread ID, or zero when none exists.
std::function< void(const std::filesystem::path &)> _onRenamed
 Owned callback invoked with the new working path while _mutex is held.
std::function< void()> _onClosing
 Owned callback invoked when close starts while _mutex is held.
std::function< bool()> _prepareLogClose
 Coordinates the owned log session before project files are closed or packaged.
std::function< bool(RenameRequest)> _renameDispatcher
 Dispatches a managed rename to its log and project queues.
std::mutex _renameStatusMutex
 Protects the observable rename status.
std::optional< RenameStatus > _renameStatus
 Latest managed or standalone rename state, guarded by _renameStatusMutex.
std::atomic< bool > _renamePending {false}
 True while a managed rename is in progress.
std::vector< watch::Change > _renameChanges
 Watcher changes received while a rename is pending, guarded by _mutex.
std::mutex _closeDispatchMutex
 Protects the close dispatcher and the standalone close worker.
std::function< F_err(bool)> _closeDispatcher
 Manager-provided dispatcher for closing an owned project.
std::optional< CloseStatus > _closeStatus
 Latest close state, guarded by _closeDispatchMutex.
std::shared_ptr< threads::TaskQueue > _standaloneCloseQueue
 Queue used for asynchronous closes of standalone projects.
std::thread _standaloneCloseThread
 Worker draining the standalone close queue.

Friends

class FrayManager

Detailed Description

Owns a project’s files, state stores, watcher and registered threads. Public operations synchronize their own mutable state unless they return a reference or expose queue-owned pendingStep. Opening, closing and file-change handling are intended for the project’s own thread; callers must keep borrowed references valid and coordinate their use.

Definition at line 66 of file project_manager.hpp.

Constructor & Destructor Documentation

◆ ProjectManager() [1/2]

fray::ProjectManager::ProjectManager ( )
default

Constructs an initially closed project manager.

◆ ProjectManager() [2/2]

fray::ProjectManager::ProjectManager ( const ProjectManager & )
delete

Project managers cannot be copied.

◆ ~ProjectManager()

fray::ProjectManager::~ProjectManager ( )
inline

Closes an open project or cancels a pending database choice, logging close failures.

Definition at line 820 of file project_manager.hpp.

Member Function Documentation

◆ abandon()

void fray::ProjectManager::abandon ( bool removeExtracted)
inlineprivate

Rolls back an unsuccessful open, releases files and clears pending state. Caller must hold _mutex; cleanup failures are logged.

Parameters
removeExtractedWhether to remove an extracted working folder.

Definition at line 324 of file project_manager.hpp.

◆ applicationId()

std::string fray::ProjectManager::applicationId ( ) const
inline

Returns a copy of the application identifier under _mutex.

Definition at line 1286 of file project_manager.hpp.

◆ applyDefaults()

F_err fray::ProjectManager::applyDefaults ( state::StateManager & state)
inlineprivate

Adds missing application and project defaults without replacing existing settings. Caller must hold _mutex.

Parameters
stateOpen Fray state store.
Returns
errors::NONE on success, otherwise the first state error.

Definition at line 401 of file project_manager.hpp.

◆ applyName()

F_err fray::ProjectManager::applyName ( const std::string & newName,
const std::filesystem::path & workspace,
bool renameFolder,
std::string databaseStem )
inlineprivate

Coordinates project folder, database, lock, archive and stored-name changes. Caller must hold _mutex; locks each state store, closes and reopens connections, holds the watcher, and invokes the rename callback under _mutex. File failures trigger rollback attempts.

Parameters
newNameDesired project name.
workspaceCurrent folder path, including an externally renamed folder.
renameFolderTrue to rename the folder; false if it was already renamed externally.
databaseStemCurrent database stem, or empty to locate it by identity.
Returns
errors::NONE on success; errors::FAILED_RENAME_PROJECT on validation, file or reopen failure, or a state write error.

Definition at line 694 of file project_manager.hpp.

◆ beginOpening()

void fray::ProjectManager::beginOpening ( )
inline

Marks the project as opening using an atomic flag.

Definition at line 1044 of file project_manager.hpp.

◆ cancelOpen()

F_err fray::ProjectManager::cancelOpen ( )
inline

Abandons a pending database choice under _mutex, removing an extracted folder but leaving the archive untouched.

Returns
errors::NONE if cancelled; errors::FAILED_OPEN_PROJECT if no choice is pending.

Definition at line 1029 of file project_manager.hpp.

◆ close()

F_err fray::ProjectManager::close ( bool forcePackage = false)
inline

Requests a project close without waiting for it to finish. Keep a standalone project alive until closeStatus() reports completion.

Parameters
forcePackageForce packaging regardless of the stored preference.
Returns
NONE when queued, or FAILED_CLOSE_PROJECT if dispatch is rejected.

Definition at line 1414 of file project_manager.hpp.

◆ closeNow()

F_err fray::ProjectManager::closeNow ( bool forcePackage = false,
bool * packaged = nullptr,
bool leaveWorkingFolder = false )
inlineprivate

Stops watching, closes stores and releases or packages the project under _mutex. Intended for the project thread; invokes the closing callback before packaging or deletion. Failed closes can be retried.

Parameters
forcePackageForce packaging regardless of the stored preference.
packagedOptional output set on success to whether packaging occurred.
leaveWorkingFolderIgnore the stored packaging preference when rejecting a restored identity.
Returns
errors::NONE on success; errors::FAILED_SAVE_PROJECT on archive failure; errors::FAILED_CLOSE_PROJECT for other close failures or a closed project.

Definition at line 1480 of file project_manager.hpp.

◆ closeStatus()

std::optional< CloseStatus > fray::ProjectManager::closeStatus ( ) const
inline

Reads the most recent direct or managed close state.

Returns
The close state, or nullopt if no close has been queued.

Definition at line 1465 of file project_manager.hpp.

◆ databaseCandidates()

std::vector< std::string > fray::ProjectManager::databaseCandidates ( ) const
inline

Returns a locked copy of database stems offered after DATABASE_CONFLICT.

Definition at line 988 of file project_manager.hpp.

◆ databaseFiles()

std::vector< std::string > fray::ProjectManager::databaseFiles ( const std::filesystem::path & folder)
inlinestaticprivate

Lists sorted stems of nonsymlink regular .sqlite files in a folder.

Parameters
folderDirectory to inspect.
Returns
Sorted stems collected before any directory iteration error.

Definition at line 252 of file project_manager.hpp.

◆ fileManager()

files::FileManager & fray::ProjectManager::fileManager ( )
inline

Returns a borrowed file manager reference; callers must coordinate access and lifetime.

Definition at line 1294 of file project_manager.hpp.

◆ finishOpen()

F_err fray::ProjectManager::finishOpen ( )
inlineprivate

Opens Fray state, applies defaults, commits conflict resolution and starts watching. Caller must hold _mutex; failure abandons the open attempt.

Returns
errors::NONE on success, errors::FAILED_OPEN_PROJECT on conflict commit failure, or a state error.

Definition at line 435 of file project_manager.hpp.

◆ finishRename()

void fray::ProjectManager::finishRename ( F_err result)
inlineprivate

Records the result of a queued rename.

Parameters
resultRename or dispatch result.

Definition at line 580 of file project_manager.hpp.

◆ get()

template<typename T>
F_err fray::ProjectManager::get ( const std::string & key,
T & value )
inline

Reads Fray state under _mutex.

Template Parameters
TExpected value type.
Parameters
keyState key to read.
valueOutput receiving the decoded value.
Returns
errors::NONE on success; errors::FAILED_LOAD_STATE if closed or on other read failures; errors::INVALID_STATE_KEY, errors::STATE_NOT_FOUND, errors::STATE_TYPE_MISMATCH or errors::STATE_VALUE_OUT_OF_RANGE for invalid keys, absent data, type mismatches or out-of-range values.

Definition at line 1385 of file project_manager.hpp.

◆ handleProjectMessage()

ProjectMessageResult fray::ProjectManager::handleProjectMessage ( const message::InData & incoming)
inline

Handles a close, layout-change or history-step message on the selected project queue. May mutate state or return an asynchronous history request rather than waiting for the renderer.

Handles a close, layout change, or history step on the project queue. May close the project, record layout history, or resume a pending step; rejects protocol notifications.

Parameters
incomingIncoming framed request and payload.
Returns
Status and optional response frame; invalid or unsupported requests report errors::INVALID_MESSAGE, and history failures report errors::FAILED_HISTORY.

Definition at line 218 of file project_message_handler.hpp.

◆ hasThreads()

bool fray::ProjectManager::hasThreads ( ) const
inline

Returns whether any registered project thread is running, under _threadMutex.

Definition at line 1106 of file project_manager.hpp.

◆ history()

history::HistoryManager fray::ProjectManager::history ( )
inline

Returns a history manager borrowing the open Fray state store.

Exceptions
std::out_of_rangeIf Fray state is not open; the returned manager must not outlive that store.

Definition at line 1342 of file project_manager.hpp.

◆ id()

std::string fray::ProjectManager::id ( ) const
inline

Returns a copy of the persistent project ID under _mutex.

Definition at line 1252 of file project_manager.hpp.

◆ isOpen()

bool fray::ProjectManager::isOpen ( ) const
inline

Returns whether the atomic open flag is set.

Definition at line 1240 of file project_manager.hpp.

◆ isOpening()

bool fray::ProjectManager::isOpening ( ) const
inline

Returns whether the atomic opening flag is set.

Definition at line 1048 of file project_manager.hpp.

◆ locateDatabase()

std::string fray::ProjectManager::locateDatabase ( const std::filesystem::path & folder) const
inlineprivate

Finds the database by its recorded file identity, including after a rename. Caller must hold _mutex.

Parameters
folderCurrent project working folder.
Returns
Database file stem, or an empty string if no match exists.

Definition at line 277 of file project_manager.hpp.

◆ looksLikeFrayDatabase()

bool fray::ProjectManager::looksLikeFrayDatabase ( const std::filesystem::path & path)
inlinestaticprivate

Checks read-only for the current Fray state table and its key, type and value columns.

Parameters
pathDatabase file to inspect.
Returns
True if the query prepares successfully; false if opening or validation fails.

Definition at line 228 of file project_manager.hpp.

◆ name()

std::string fray::ProjectManager::name ( ) const
inline

Returns a copy of the current project name under _mutex.

Definition at line 1244 of file project_manager.hpp.

◆ newProjectId()

std::string fray::ProjectManager::newProjectId ( )
inlinestaticprivate

Generates a hexadecimal project identifier from random-device values.

Returns
Generated identifier string.

Definition at line 365 of file project_manager.hpp.

◆ onChange()

void fray::ProjectManager::onChange ( const watch::Change & change)
inlineprivate

Handles project folder and database rename events on the dispatching thread. Takes _mutex and may rename project resources.

Parameters
changeWatcher event to inspect.

Definition at line 640 of file project_manager.hpp.

◆ open()

F_err fray::ProjectManager::open ( const std::string & path,
const std::string & applicationId,
files::ConflictChoice choice = files::ConflictChoice::Ask,
threads::ThreadManager * threads = nullptr,
std::shared_ptr< threads::TaskQueue > queue = nullptr,
files::OpenMode mode = files::OpenMode::OpenOrCreate,
std::optional< bool > newProjectPackageDefault = std::nullopt )
inline

Opens or creates a project and its Fray state on the calling thread. Intended for the project thread; takes _mutex, acquires project files and may start watching. A database conflict leaves an open attempt pending for resolveDatabase or cancelOpen.

Parameters
pathProject path to open or create.
applicationIdApplication identifier stored with state.
choiceHow to resolve a folder/archive conflict.
threadsOptional borrowed thread manager, which must outlive its registered threads.
queueOptional shared project queue for watcher notifications.
modeWhether and how to open or create the project.
newProjectPackageDefaultOptional application default used only for a newly created project.
Returns
errors::NONE on success; errors::DATABASE_CONFLICT if a database choice is needed; otherwise a file, validation or state error, including INVALID_PROJECT_DATABASE, PROJECT_DATABASE_OCCUPIED and PROJECT_FOLDER_EXISTS.

Definition at line 844 of file project_manager.hpp.

◆ openState()

F_err fray::ProjectManager::openState ( const std::string & name)
inline

Opens a named state store if not already open, under _mutex.

Parameters
nameState namespace to open.
Returns
errors::NONE on success or if already open; errors::FAILED_OPEN_STORE if closed or closing, otherwise the state open error.

Definition at line 1301 of file project_manager.hpp.

◆ operator=()

ProjectManager & fray::ProjectManager::operator= ( const ProjectManager & )
delete

Project managers cannot be copy-assigned.

◆ prepareDatabase()

F_err fray::ProjectManager::prepareDatabase ( )
inlineprivate

Selects, snapshots or adopts a project database before opening state. Caller must hold _mutex; multiple valid candidates leave a choice pending.

Returns
errors::NONE if ready; errors::DATABASE_CONFLICT for multiple candidates; errors::FAILED_OPEN_PROJECT on file failure.

Definition at line 295 of file project_manager.hpp.

◆ pump()

void fray::ProjectManager::pump ( )
inline

Dispatches queued watcher events on the calling thread while open, under _mutex.

Definition at line 1181 of file project_manager.hpp.

◆ reassignProjectId()

F_err fray::ProjectManager::reassignProjectId ( const std::string & id)
inlineprivate

Persists a replacement identity for an open copied project.

Parameters
idNew reserved project identity.
Returns
NONE on success, otherwise a state error.

Definition at line 380 of file project_manager.hpp.

◆ refreshFiles()

F_err fray::ProjectManager::refreshFiles ( )
inline

Polls the watcher and dispatches changes on the calling thread.

Returns
errors::NONE; watcher handling logs errors independently.

Definition at line 1192 of file project_manager.hpp.

◆ refreshRenameRequest()

RenameRequest fray::ProjectManager::refreshRenameRequest ( RenameRequest request)
inlineprivate

Updates a queued rename for folder moves observed while its log was suspended. Caller holds _mutex.

Parameters
requestOriginal rename request.
Returns
Request targeting the latest observed location.

Definition at line 604 of file project_manager.hpp.

◆ registerThread()

threads::ThreadManager::Id fray::ProjectManager::registerThread ( const std::string & name,
std::function< void(threads::StopToken)> body )
inline

Registers and starts a project-owned thread under _threadMutex. The thread is tagged with the project ID for logging; the project thread remains alive while registered threads run.

Parameters
nameThread name suffix, prefixed with the project name.
bodyOwned thread function receiving its stop token.
Returns
Registered thread ID, or 0 if no thread manager was supplied.

Definition at line 1057 of file project_manager.hpp.

◆ rename()

F_err fray::ProjectManager::rename ( const std::string & newName)
inline

Requests a project rename; managed projects complete it on their queues.

Parameters
newNameDesired project name.
Returns
NONE when queued or unchanged; otherwise a validation, dispatch, or standalone rename error.

Definition at line 1204 of file project_manager.hpp.

◆ renameStatus()

std::optional< RenameStatus > fray::ProjectManager::renameStatus ( ) const
inline

Reads the most recent rename state without waiting for its work.

Returns
Rename state, or nullopt if no rename has been started.

Definition at line 1231 of file project_manager.hpp.

◆ replayRenameChanges()

void fray::ProjectManager::replayRenameChanges ( )
inlineprivate

Replays watcher changes received while a rename was in progress.

Definition at line 589 of file project_manager.hpp.

◆ requestStopThreads()

void fray::ProjectManager::requestStopThreads ( )
inline

Requests cooperative termination of registered project threads without joining them.

Definition at line 1095 of file project_manager.hpp.

◆ resolveDatabase()

F_err fray::ProjectManager::resolveDatabase ( const DatabaseChoice & choice)
inline

Resolves a pending database conflict and finishes opening under _mutex.

Parameters
choiceCandidate to adopt or instruction to create a new database.
Returns
errors::NONE on success; errors::FAILED_OPEN_PROJECT for no pending choice, invalid candidate or file failure; otherwise a finish-open state error.

Definition at line 999 of file project_manager.hpp.

◆ restoreLock()

void fray::ProjectManager::restoreLock ( )
inline

Recreates a renamed or deleted project lock while open, under _mutex.

Definition at line 1171 of file project_manager.hpp.

◆ set()

template<typename T>
F_err fray::ProjectManager::set ( const std::string & key,
const T & value )
inline

Writes Fray state under _mutex; project.info.name instead requests a project rename.

Template Parameters
TValue type supported by the state store.
Parameters
keyState key to write.
valueValue to persist; project.info.name requires a string-convertible value.
Returns
errors::NONE on success or rename dispatch; errors::FAILED_SAVE_STATE if closed or the name is not string-convertible; otherwise a rename or state write error such as errors::INVALID_STATE_KEY or errors::STATE_VALUE_OUT_OF_RANGE.

Definition at line 1355 of file project_manager.hpp.

◆ setCloseDispatcher()

void fray::ProjectManager::setCloseDispatcher ( std::function< F_err(bool)> dispatcher)
inline

Installs the owner-provided asynchronous close dispatcher.

Parameters
dispatcherFunction accepting the forced-package flag and returning the dispatch result.

Definition at line 1145 of file project_manager.hpp.

◆ setClosingCallback()

void fray::ProjectManager::setClosingCallback ( std::function< void()> callback)
inline

Replaces the callback invoked at close start before packaging or deletion. Takes _mutex; the callback runs synchronously under _mutex and must not wait for work requiring it.

Parameters
callbackOwned callback, or empty to disable notification.

Definition at line 1134 of file project_manager.hpp.

◆ setDefault()

template<typename T>
F_err fray::ProjectManager::setDefault ( state::StateManager & state,
const std::string & key,
const T & value )
inlinestaticprivate

Writes a default only if the state key is absent. Runs on the opening thread through the state manager.

Template Parameters
TType of the default value.
Parameters
stateOpen state store.
keySetting to check.
valueDefault to write if absent.
Returns
errors::NONE on success or an existing key; otherwise the state lookup or write error.

Definition at line 349 of file project_manager.hpp.

◆ setLogCloseBarrier()

void fray::ProjectManager::setLogCloseBarrier ( std::function< bool()> callback)
inline

Installs the owner-provided project-log close barrier.

Parameters
callbackFunction returning true only when the log session is safely closed.

Definition at line 1156 of file project_manager.hpp.

◆ setRenameCallback()

void fray::ProjectManager::setRenameCallback ( std::function< void(const std::filesystem::path &)> callback)
inline

Replaces the callback invoked with the new working path after a rename. Takes _mutex; the callback runs synchronously under _mutex and must not wait for work requiring it.

Parameters
callbackOwned callback, or empty to disable notification.

Definition at line 1122 of file project_manager.hpp.

◆ startRename()

F_err fray::ProjectManager::startRename ( RenameRequest request)
inlineprivate

Starts a rename while holding _mutex, or applies it directly for a standalone project.

Parameters
requestCaptured project and filesystem rename details.
Returns
Dispatch result for managed projects, or the standalone rename result.

Definition at line 535 of file project_manager.hpp.

◆ startWatching()

void fray::ProjectManager::startWatching ( )
inlineprivate

Watches the project folder and, when configured, starts a watcher thread that posts to the project queue. Caller must hold _mutex; watch failures are logged.

Definition at line 484 of file project_manager.hpp.

◆ state()

state::StateManager & fray::ProjectManager::state ( const std::string & name)
inline

Returns a borrowed reference to an open named state store under _mutex. The reference is invalidated when its store is replaced or the project closes; coordinate its lifetime with project operations.

Parameters
nameState namespace to look up.
Returns
Open state manager reference.
Exceptions
std::out_of_rangeIf the project or named store is not open.

Definition at line 1328 of file project_manager.hpp.

◆ stopThread()

void fray::ProjectManager::stopThread ( threads::ThreadManager::Id id)
inline

Removes a thread from registration, requests its stop and joins it. Releases _threadMutex before joining; do not call from the thread being joined or while holding a lock that thread needs to exit.

Parameters
idRegistered thread ID to stop and join.

Definition at line 1079 of file project_manager.hpp.

◆ stopWatching()

void fray::ProjectManager::stopWatching ( )
inlineprivate

Stops and joins the watcher thread, removes its subscription and unwatches the folder. Called during close under _mutex; joining waits for the watcher thread to exit.

Definition at line 514 of file project_manager.hpp.

◆ validProjectName()

bool fray::ProjectManager::validProjectName ( const std::string & name)
inlinestatic

Checks whether a project name is a safe single relative component without a .fry suffix.

Parameters
nameProposed project name.
Returns
True if valid; false otherwise.

Definition at line 90 of file project_manager.hpp.

◆ watcher()

watch::FileWatcher & fray::ProjectManager::watcher ( )
inline

Returns a borrowed watcher reference; its lifetime is bounded by this manager.

Definition at line 1166 of file project_manager.hpp.

◆ FrayManager

friend class FrayManager
friend

Definition at line 68 of file project_manager.hpp.

Member Data Documentation

◆ _applicationId

std::string fray::ProjectManager::_applicationId
private

Application identifier used when opening state stores, guarded by _mutex.

Definition at line 114 of file project_manager.hpp.

◆ _archiveComplete

bool fray::ProjectManager::_archiveComplete = false
private

Whether packaging already succeeded during a retriable close, guarded by _mutex.

Definition at line 142 of file project_manager.hpp.

◆ _candidates

std::vector<std::string> fray::ProjectManager::_candidates
private

Candidate database stems for a pending choice, guarded by _mutex.

Definition at line 150 of file project_manager.hpp.

◆ _closeDispatcher

std::function<F_err(bool)> fray::ProjectManager::_closeDispatcher
private

Manager-provided dispatcher for closing an owned project.

Definition at line 215 of file project_manager.hpp.

◆ _closeDispatchMutex

std::mutex fray::ProjectManager::_closeDispatchMutex
mutableprivate

Protects the close dispatcher and the standalone close worker.

Definition at line 213 of file project_manager.hpp.

◆ _closePending

std::atomic<bool> fray::ProjectManager::_closePending {false}
private

Whether a managed close has been queued, readable without _mutex.

Definition at line 128 of file project_manager.hpp.

◆ _closeStatus

std::optional<CloseStatus> fray::ProjectManager::_closeStatus
private

Latest close state, guarded by _closeDispatchMutex.

Definition at line 217 of file project_manager.hpp.

◆ _closing

bool fray::ProjectManager::_closing = false
private

Whether close has begun and can be retried, guarded by _mutex.

Definition at line 132 of file project_manager.hpp.

◆ _databaseId

watch::FileId fray::ProjectManager::_databaseId
private

File identity used to recognize the project database across renames, guarded by _mutex.

Definition at line 159 of file project_manager.hpp.

◆ _files

files::FileManager fray::ProjectManager::_files
private

Owns project paths, lock and archive operations, normally guarded by _mutex.

Definition at line 106 of file project_manager.hpp.

◆ _mutex

std::recursive_mutex fray::ProjectManager::_mutex
mutableprivate

Protects project lifecycle, state map, file operations and callbacks; recursive for nested operations.

Definition at line 102 of file project_manager.hpp.

◆ _newProjectPackageDefault

std::optional<bool> fray::ProjectManager::_newProjectPackageDefault
private

Packaging default captured for a newly created project.

Definition at line 138 of file project_manager.hpp.

◆ _onClosing

std::function<void()> fray::ProjectManager::_onClosing
private

Owned callback invoked when close starts while _mutex is held.

Definition at line 191 of file project_manager.hpp.

◆ _onRenamed

std::function<void(const std::filesystem::path&)> fray::ProjectManager::_onRenamed
private

Owned callback invoked with the new working path while _mutex is held.

Definition at line 187 of file project_manager.hpp.

◆ _open

std::atomic<bool> fray::ProjectManager::_open {false}
private

Whether a project is open, readable without _mutex.

Definition at line 122 of file project_manager.hpp.

◆ _opening

std::atomic<bool> fray::ProjectManager::_opening {false}
private

Whether an opening attempt is in progress, readable without _mutex.

Definition at line 126 of file project_manager.hpp.

◆ _packageOnClose

bool fray::ProjectManager::_packageOnClose = false
private

Whether the current close attempt packages the project, guarded by _mutex.

Definition at line 136 of file project_manager.hpp.

◆ _pendingDatabase

bool fray::ProjectManager::_pendingDatabase = false
private

Whether opening awaits a database choice, guarded by _mutex.

Definition at line 146 of file project_manager.hpp.

◆ _prepareLogClose

std::function<bool()> fray::ProjectManager::_prepareLogClose
private

Coordinates the owned log session before project files are closed or packaged.

Definition at line 193 of file project_manager.hpp.

◆ _projectId

std::string fray::ProjectManager::_projectId
private

Persistent project identifier used for logging and identity, guarded by _mutex.

Definition at line 118 of file project_manager.hpp.

◆ _queue

std::shared_ptr<threads::TaskQueue> fray::ProjectManager::_queue
private

Shared project queue used to post watcher work.

Definition at line 171 of file project_manager.hpp.

◆ _registered

std::vector<threads::ThreadManager::Id> fray::ProjectManager::_registered
private

IDs registered for this project, guarded by _threadMutex.

Definition at line 179 of file project_manager.hpp.

◆ _renameChanges

std::vector<watch::Change> fray::ProjectManager::_renameChanges
private

Watcher changes received while a rename is pending, guarded by _mutex.

Definition at line 211 of file project_manager.hpp.

◆ _renameDispatcher

std::function<bool(RenameRequest)> fray::ProjectManager::_renameDispatcher
private

Dispatches a managed rename to its log and project queues.

Definition at line 203 of file project_manager.hpp.

◆ _renamePending

std::atomic<bool> fray::ProjectManager::_renamePending {false}
private

True while a managed rename is in progress.

Definition at line 209 of file project_manager.hpp.

◆ _renameStatus

std::optional<RenameStatus> fray::ProjectManager::_renameStatus
private

Latest managed or standalone rename state, guarded by _renameStatusMutex.

Definition at line 207 of file project_manager.hpp.

◆ _renameStatusMutex

std::mutex fray::ProjectManager::_renameStatusMutex
mutableprivate

Protects the observable rename status.

Definition at line 205 of file project_manager.hpp.

◆ _standaloneCloseQueue

std::shared_ptr<threads::TaskQueue> fray::ProjectManager::_standaloneCloseQueue
private

Queue used for asynchronous closes of standalone projects.

Definition at line 219 of file project_manager.hpp.

◆ _standaloneCloseThread

std::thread fray::ProjectManager::_standaloneCloseThread
private

Worker draining the standalone close queue.

Definition at line 221 of file project_manager.hpp.

◆ _states

std::unordered_map<std::string, std::unique_ptr<state::StateManager> > fray::ProjectManager::_states
private

Owns named state stores; map and lifetimes are guarded by _mutex.

Definition at line 110 of file project_manager.hpp.

◆ _subscription

watch::FileWatcher::Subscription fray::ProjectManager::_subscription = 0
private

Watcher subscription for project changes, or zero when absent.

Definition at line 163 of file project_manager.hpp.

◆ _threadMutex

std::mutex fray::ProjectManager::_threadMutex
mutableprivate

Protects registered-thread bookkeeping and thread-manager access in thread methods.

Definition at line 175 of file project_manager.hpp.

◆ _threads

threads::ThreadManager* fray::ProjectManager::_threads = nullptr
private

Non-owning thread manager supplied at open; it must outlive registered threads.

Definition at line 167 of file project_manager.hpp.

◆ _watcher

watch::FileWatcher fray::ProjectManager::_watcher
private

Owns project folder watching and dispatch.

Definition at line 155 of file project_manager.hpp.

◆ _watcherThread

threads::ThreadManager::Id fray::ProjectManager::_watcherThread = 0
private

Registered watcher thread ID, or zero when none exists.

Definition at line 183 of file project_manager.hpp.

◆ pendingStep

std::optional<PendingStep> fray::ProjectManager::pendingStep

Pending asynchronous history step, accessed only on the project queue.

Definition at line 1281 of file project_manager.hpp.


The documentation for this class was generated from the following files: