ACF $AcfVersion:0$
CAutoPersistenceComp.h
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-2.1-or-later OR GPL-2.0-or-later OR GPL-3.0-or-later OR LicenseRef-ACF-Commercial
2#pragma once
3
4
5// Qt includes
6#include <QtCore/QObject>
7#include <QtCore/QTimer>
8#include <QtCore/QFuture>
9#include <QtCore/QFileSystemWatcher>
10
11// ACF includes
12#include <istd/IChangeable.h>
13#include <imod/IModel.h>
20
21#if QT_VERSION >= 0x050000
22#include <QtCore/QLockFile>
23#endif
24
25namespace ifile
26{
27
28
45 public QObject,
48{
49 Q_OBJECT
50
51public:
54
72
73 I_BEGIN_COMPONENT(CAutoPersistenceComp);
74 I_ASSIGN(m_objectCompPtr, "Object", "Object will be restored and stored", true, "Object");
75 I_ASSIGN_TO(m_objectModelCompPtr, m_objectCompPtr, false);
76 I_ASSIGN_TO(m_serializeableObjectCompPtr, m_objectCompPtr, false);
77 I_ASSIGN(m_fileLoaderCompPtr, "FileLoader", "File loader used to store and restore object", true, "FileLoader");
78 I_ASSIGN(m_filePathCompPtr, "FilePath", "File path where object data will be stored if non anonymous loader is used", false, "FilePath");
79 I_ASSIGN_TO(m_filePathModelCompPtr, m_filePathCompPtr, false);
80 I_ASSIGN(m_restoreOnBeginAttrPtr, "RestoreOnBegin", "Flag indicating that object should be restored on begin", true, true);
81 I_ASSIGN(m_storeOnBeginAttrPtr, "StoreOnBegin", "Flag indicating that object should be stored on begin if the storage file doesn't exist", true, false);
82 I_ASSIGN(m_storeOnEndAttrPtr, "StoreOnEnd", "Flag indicating that object should be stored on end", true, true);
83 I_ASSIGN(m_storeOnChangeAttrPtr, "StoreOnChange", "Flag indicating that object should be stored on each data change", true, false);
84 I_ASSIGN(m_storeIntervalAttrPtr, "StoreInterval", "Time interval in seconds for automatic object storing", false, 10);
85 I_ASSIGN(m_reloadOnFileChangeAttrPtr, "AutoReload", "Update data model if the file was changed", true, false);
86 I_ASSIGN(m_runtimeStatusCompPtr, "RuntimeStatus", "Application's runtime status", false, "RuntimeStatus");
87 I_ASSIGN_TO(m_runtimeStatusModelCompPtr, m_runtimeStatusCompPtr, false);
88 I_ASSIGN(m_staleLockTimeAttrPtr, "StaleLockTime", "Time in seconds after which a lock file is considered stale.", true, 30.0);
89 I_ASSIGN(m_tryLockTimeoutAttrPtr, "TryLockTimeout", "Lock function will wait for at most TryLockTimeout seconds for the lock file to become available."
90 "\nNOTE: Lock will wait forever until the lock file can be locked when set to negative", true, 0.0);
91 I_ASSIGN(m_enableLockForLoadAttrPtr, "EnableLockForRead", "When enabled lock is also set when reading from file."
92 "\nNOTE: On NTFS file systems, ownership and permissions checking is disabled (in QT) by default for performance reasons."
93 "\nEnable this flag when you are known what you are doing.", true, false);
94 I_END_COMPONENT;
95
100
101protected:
106
110 virtual bool LoadObject(const QString& filePath);
114 virtual bool StoreObject(const istd::IChangeable& object);
115
116 // reimplemented (icomp::CComponentBase)
117 virtual void OnComponentCreated() override;
118 virtual void OnComponentDestroyed() override;
119
120 // reimplemented (imod::CMultiModelDispatcherBase)
121 virtual void OnModelChanged(int modelId, const istd::IChangeable::ChangeSet& changeSet) override;
122
123private Q_SLOTS:
127 void OnTimeout();
128
129 void OnFileContentsChanged(const QString& path);
130
131private:
135 bool TryStartIntervalStore();
136
137 bool LockFile(const QString& filePath, bool store) const;
138 void UnlockFile() const;
139
140private:
141 I_REF(ibase::IRuntimeStatusProvider, m_runtimeStatusCompPtr);
142 I_REF(imod::IModel, m_runtimeStatusModelCompPtr);
143
147 I_REF(istd::IChangeable, m_objectCompPtr);
148 I_REF(iser::ISerializable, m_serializeableObjectCompPtr);
149 I_REF(imod::IModel, m_objectModelCompPtr);
150
154 I_REF(ifile::IFilePersistence, m_fileLoaderCompPtr);
155
159 I_REF(ifile::IFileNameParam, m_filePathCompPtr);
160 I_REF(imod::IModel, m_filePathModelCompPtr);
161
165 I_ATTR(bool, m_restoreOnBeginAttrPtr);
166
171 I_ATTR(bool, m_storeOnBeginAttrPtr);
172
176 I_ATTR(bool, m_storeOnEndAttrPtr);
177
181 I_ATTR(bool, m_storeOnChangeAttrPtr);
182
187 I_ATTR(double, m_storeIntervalAttrPtr);
188
192 I_ATTR(bool, m_reloadOnFileChangeAttrPtr);
193
199 I_ATTR(double, m_staleLockTimeAttrPtr);
200
206 I_ATTR(double, m_tryLockTimeoutAttrPtr);
207
213 I_ATTR(bool, m_enableLockForLoadAttrPtr);
214
215 iser::CMemoryWriteArchive m_lastStoredObjectState;
216
220 std::atomic<bool> m_isObjectChanged;
221
225 bool m_isLoadedFromFile;
226
227 QTimer m_storingTimer;
228
229 QFuture<void> m_storingFuture;
230
234 mutable istd::IChangeableUniquePtr m_objectShadowPtr;
235
236 QFileSystemWatcher m_fileWatcher;
237
238 QMutex m_loadSaveMutex;
239
240 mutable bool m_blockLoadingOnFileChanges;
241
242#if QT_VERSION >= 0x050000
243 mutable istd::TDelPtr<QLockFile> m_lockFilePtr;
244#endif
245 mutable QMutex m_fileLockMutex;
246};
247
248
249} // namespace ifile
250
251
Provider of the run-time status.
The component for automatic object persistence.
virtual void OnComponentCreated() override
Called when component is created.
@ MI_RUNTIME_STATUS
Runtime status was changed.
@ MI_FILEPATH
File path was changed.
virtual bool LoadObject(const QString &filePath)
Load working object data from some file.
virtual void OnModelChanged(int modelId, const istd::IChangeable::ChangeSet &changeSet) override
Called when some model observed by dispatcher will be changed.
virtual bool StoreObject(const istd::IChangeable &object)
Store some object data to working file.
void SaveObjectSnapshot()
Store object copy in a separate thread.
CAutoPersistenceComp()
Default constructor.
imod::CMultiModelDispatcherBase BaseClass2
virtual void OnComponentDestroyed() override
Called when component is destroyed.
ilog::CLoggerComponentBase BaseClass
Common interface for any file system item name (e.g directory, file or URL path)
Interface providing loading and saving of data objects.
Template wrapper adding comprehensive logging functionality to components.
Generic implementation of a data model changes notifier.
Common interface for model objects, that supports Model/Observer design pattern.
Definition IModel.h:25
Implementation of archive using memory buffer to store the persistent objects.
Common class for all classes which objects can be archived or restored from archive.
Set of change flags (its IDs).
Definition IChangeable.h:36
Common interface for data model objects, which can be changed.
Definition IChangeable.h:28
Pointer wrapper providing automatic deleting pointed object during destruction.
Definition TDelPtr.h:21
Contains interfaces and implementations of file system related components.