ICF 3.1.1.10
Technical documentation of ICF Libraries
CAutoPersistenceComp.h
Go to the documentation of this file.
1/********************************************************************************
2** This file is part of the ICF Framework. Copyright (C) Witold Gantzke & Kirill Lepskiy
3** ICF Framework may be used under the terms of the LGPL License v. 2.1 by the Free Software Foundation.
4********************************************************************************/
5
6#pragma once
7
8
9// Qt includes
10#include <QtCore/QObject>
11#include <QtCore/QTimer>
12#include <QtCore/QFileSystemWatcher>
13#include <QtCore/QLockFile>
14#include <QtCore/QReadWriteLock>
15#include <QtCore/QFuture>
16
17// ICF includes
18#include <istd/IChangeable.h>
19#include <imod/IModel.h>
26
27
28namespace ifile
29{
30
31
33{
34public:
36
37 I_BEGIN_BASE_COMPONENT(CAutoPersistenceCompBase);
38 I_ASSIGN(m_objectCompPtr, "Object", "Object will be restored and stored", true, "Object");
39 I_ASSIGN_TO(m_objectModelCompPtr, m_objectCompPtr, false);
40 I_ASSIGN(m_fileLoaderCompPtr, "FileLoader", "File loader used to store and restore object", true, "FileLoader");
41 I_ASSIGN(m_filePathCompPtr, "FilePath", "File path where object data will be stored if non anonymous loader is used", false, "FilePath");
42 I_ASSIGN_TO(m_filePathModelCompPtr, m_filePathCompPtr, false);
43 I_ASSIGN(m_prototypeFilePathAttrPtr, "PrototypeFilePath", "File path of prototype file, will be used for load if the file pointed at normal file path does not exist", false, "");
44 I_END_COMPONENT;
45
46protected:
50 I_REF(istd::IChangeable, m_objectCompPtr);
51 I_REF(imod::IModel, m_objectModelCompPtr);
52
56 I_REF(ifile::IFilePersistence, m_fileLoaderCompPtr);
57
61 I_REF(ifile::IFileNameParam, m_filePathCompPtr);
62 I_REF(imod::IModel, m_filePathModelCompPtr);
63
67 I_ATTR(QString, m_prototypeFilePathAttrPtr);
68};
69
70
87 public QObject,
90{
91 Q_OBJECT
92
93public:
96
114
115 I_BEGIN_COMPONENT(CAutoPersistenceComp);
116 I_ASSIGN(m_restoreOnBeginAttrPtr, "RestoreOnBegin", "Flag indicating that object should be restored on begin", true, true);
117 I_ASSIGN(m_storeOnBeginAttrPtr, "StoreOnBegin", "Flag indicating that object should be stored on begin if the storage file doesn't exist", true, false);
118 I_ASSIGN(m_storeOnEndAttrPtr, "StoreOnEnd", "Flag indicating that object should be stored on end", true, true);
119 I_ASSIGN(m_storeOnChangeAttrPtr, "StoreOnChange", "Flag indicating that object should be stored on each data change", true, false);
120 I_ASSIGN(m_storeIntervalAttrPtr, "StoreInterval", "Time interval in seconds for automatic object storing", false, 10);
121 I_ASSIGN(m_reloadOnFileChangeAttrPtr, "AutoReload", "Update data model if the file was changed", true, false);
122 I_ASSIGN(m_runtimeStatusCompPtr, "RuntimeStatus", "Application's runtime status", false, "RuntimeStatus");
123 I_ASSIGN_TO(m_runtimeStatusModelCompPtr, m_runtimeStatusCompPtr, false);
124 I_ASSIGN(m_staleLockTimeAttrPtr, "StaleLockTime", "Time in seconds after which a lock file is considered stale.", true, 30.0);
125 I_ASSIGN(m_tryLockTimeoutAttrPtr, "TryLockTimeout", "Lock function will wait for at most TryLockTimeout seconds for the lock file to become available."
126 "\nNOTE: Lock will wait forever until the lock file can be locked when set to negative", true, 0.0);
127 I_ASSIGN(m_enableLockForLoadAttrPtr, "EnableLockForRead", "When enabled lock is also set when reading from file."
128 "\nNOTE: On NTFS file systems, ownership and permissions checking is disabled (in QT) by default for performance reasons."
129 "\nEnable this flag when you are known what you are doing.", true, false);
130 I_END_COMPONENT;
131
136
137protected:
142
146 virtual bool LoadObject(const QString& filePath, bool allowPrototypeFile);
150 virtual bool StoreObject(const istd::IChangeable& object);
151
152 // reimplemented (icomp::CComponentBase)
153 void OnComponentCreated() override;
154 void OnComponentDestroyed() override;
155
156 // reimplemented (imod::CMultiModelDispatcherBase)
157 void OnModelChanged(int modelId, const istd::IChangeable::ChangeSet& changeSet) override;
158
159private Q_SLOTS:
163 void OnTimeout();
164
165 void OnFileContentsChanged(const QString& path);
166
167private:
171 bool TryStartIntervalStore();
172
173 bool LockFile(const QString& filePath, bool store) const;
174 void UnlockFile() const;
175
176private:
177 I_REF(ibase::IRuntimeStatusProvider, m_runtimeStatusCompPtr);
178 I_REF(imod::IModel, m_runtimeStatusModelCompPtr);
179
183 I_ATTR(bool, m_restoreOnBeginAttrPtr);
184
189 I_ATTR(bool, m_storeOnBeginAttrPtr);
190
194 I_ATTR(bool, m_storeOnEndAttrPtr);
195
199 I_ATTR(bool, m_storeOnChangeAttrPtr);
200
205 I_ATTR(double, m_storeIntervalAttrPtr);
206
210 I_ATTR(bool, m_reloadOnFileChangeAttrPtr);
211
217 I_ATTR(double, m_staleLockTimeAttrPtr);
218
224 I_ATTR(double, m_tryLockTimeoutAttrPtr);
225
231 I_ATTR(bool, m_enableLockForLoadAttrPtr);
232
233 iser::CMemoryWriteArchive m_lastStoredObjectState;
234
238 bool m_isObjectChanged;
239
243 bool m_isLoadedFromFile;
244
245 QTimer m_storingTimer;
246
247 QFuture<bool> m_storingFuture;
248
252 mutable std::unique_ptr<istd::IChangeable> m_objectShadowPtr;
253
254 QFileSystemWatcher m_fileWatcher;
255
256 QReadWriteLock m_loadSaveMutex;
257
258 mutable bool m_blockLoadingOnFileChanges;
259
260 mutable std::unique_ptr<QLockFile> m_lockFilePtr;
261 mutable QReadWriteLock m_fileLockMutex;
262};
263
264
265} // namespace ifile
266
267
Provider of the run-time status.
ilog::CLoggerComponentBase BaseClass
The component for automatic object persistence.
void OnComponentCreated() override
@ MI_RUNTIME_STATUS
Runtime status was changed.
@ MI_FILEPATH
File path was changed.
imod::CMultiModelDispatcherBase BaseClass2
virtual bool StoreObject(const istd::IChangeable &object)
Store some object data to working file.
bool SaveObjectSnapshot()
Store object copy in a separate thread.
void OnComponentDestroyed() override
virtual bool LoadObject(const QString &filePath, bool allowPrototypeFile)
Load working object data from some file.
CAutoPersistenceComp()
Default constructor.
CAutoPersistenceCompBase BaseClass
void OnModelChanged(int modelId, const istd::IChangeable::ChangeSet &changeSet) override
Called when some model observed by dispatcher will be changed.
Common interface for any file system item name (e.g directory, file or URL path)
Interface providing loading and saving of data objects.
Wrapper provider of log-functionality for component based implementations.
Generic implementation of a data model changes notifier.
Common interface for model objects, that supports Model/Observer design pattern.
Definition IModel.h:29
Implementation of archive using memory buffer to store the persistent objects.
Set of change flags (its IDs).
Definition IChangeable.h:38
Common interface for data model objects, which can be changed.
Definition IChangeable.h:32
Contains interfaces and implementations of file system related components.

© Witold Gantzke and Kirill Lepskiy