ACF $AcfVersion:0$
TFileSerializerComp.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/QDir>
8
9// ACF includes
11#include <istd/CSystem.h>
14
15
16namespace ifile
17{
18
19
25template <class ReadArchive, class WriteArchive>
27{
28public:
30
31 I_BEGIN_COMPONENT(TFileSerializerComp);
32 I_ASSIGN(m_autoCreateDirectoryAttrPtr, "AutoCreatePath", "Create directory/file path automatically if not exists", true, false);
33 I_END_COMPONENT;
34
35 // reimplemented (ifile::IFilePersistence)
38 const QString& filePath = QString(),
39 ibase::IProgressManager* progressManagerPtr = NULL) const override;
41 const istd::IChangeable& data,
42 const QString& filePath = QString(),
43 ibase::IProgressManager* progressManagerPtr = NULL) const override;
44
45 // Wrapper classes for archives
46 class ReadArchiveEx: public ReadArchive
47 {
48 public:
49 typedef ReadArchive BaseClass;
50
51 ReadArchiveEx(const QString& filePath, const istd::ILogger* loggerPtr)
52 : ReadArchive(filePath),
53 m_loggerPtr(loggerPtr)
54 {
55 }
56
57 virtual bool SendLogMessage(istd::IInformationProvider::InformationCategory category, int id, const QString& message, const QString& messageSource, int flags = 0) const
58 {
59 if (m_loggerPtr != nullptr){
60 QString correctedMessage = message;
61 QString correctedMessageSource = messageSource;
62
63 BaseClass::DecorateMessage(category, id, flags, correctedMessage, correctedMessageSource);
64
65 return m_loggerPtr->SendLogMessage(istd::IInformationProvider::IC_INFO, id, correctedMessage, correctedMessageSource, flags);
66 }
67
68 return false;
69 }
70
71 protected:
72 // reimplemented (istd::ILogger)
73 virtual bool IsLogConsumed(
75 const int* flagsPtr = nullptr) const override
76 {
78
79 return (m_loggerPtr != nullptr) && m_loggerPtr->IsLogConsumed(&slaveCategory, flagsPtr);
80 }
81
82 private:
83 const istd::ILogger* m_loggerPtr;
84 };
85
86 class WriteArchiveEx: public WriteArchive
87 {
88 public:
89 typedef WriteArchive BaseClass;
90
91 WriteArchiveEx(const QString& filePath, const iser::IVersionInfo* infoPtr, const istd::ILogger* loggerPtr)
92 : WriteArchive(filePath, infoPtr),
93 m_loggerPtr(loggerPtr)
94 {
95 }
96
97 virtual bool SendLogMessage(istd::IInformationProvider::InformationCategory category, int id, const QString& message, const QString& messageSource, int flags = 0) const
98 {
99 if (m_loggerPtr != nullptr){
100 QString correctedMessage = message;
101 QString correctedMessageSource = messageSource;
102
103 BaseClass::DecorateMessage(category, id, flags, correctedMessage, correctedMessageSource);
104
105 return m_loggerPtr->SendLogMessage(category, id, correctedMessage, correctedMessageSource, flags);
106 }
107
108 return false;
109 }
110
111 protected:
112 // reimplemented (istd::ILogger)
113 virtual bool IsLogConsumed(
115 const int* flagsPtr = nullptr) const override
116 {
118
119 return (m_loggerPtr != nullptr) && m_loggerPtr->IsLogConsumed(&slaveCategory, flagsPtr);
120 }
121
122 private:
123 const istd::ILogger* m_loggerPtr;
124 };
125
126protected:
130 virtual void OnReadError(const ReadArchive& archive, const istd::IChangeable& data, const QString& filePath) const;
131
132 I_ATTR(bool, m_autoCreateDirectoryAttrPtr);
133};
134
135
136// public methods
137
138// reimplemented (ifile::IFilePersistence)
139
140template <class ReadArchive, class WriteArchive>
142 istd::IChangeable& data,
143 const QString& filePath,
144 ibase::IProgressManager* /*progressManagerPtr*/) const
145{
146 if (IsOperationSupported(&data, &filePath, QF_LOAD | QF_FILE, *m_beQuiteOnLoadAttrPtr)){
147 ReadArchiveEx archive(filePath, this);
148
149 Q_ASSERT(!archive.IsStoring());
150
154 iser::ISerializable* serializablePtr = dynamic_cast<iser::ISerializable*>(&data);
155 if (serializablePtr == nullptr){
156 serializablePtr = CompCastPtr<iser::ISerializable>(&data);
157 }
158
159 Q_ASSERT(serializablePtr != nullptr);
160
161 if (serializablePtr->Serialize(archive)){
162 return OS_OK;
163 }
164 else{
165 OnReadError(archive, data, filePath);
166 }
167 }
168
169 return OS_FAILED;
170}
171
172
173template <class ReadArchive, class WriteArchive>
175 const istd::IChangeable& data,
176 const QString& filePath,
177 ibase::IProgressManager* /*progressManagerPtr*/) const
178{
179 if (*m_autoCreateDirectoryAttrPtr){
180 QFileInfo fileInfo(filePath);
181
182 if (!istd::CSystem::EnsurePathExists(fileInfo.dir().absolutePath())){
183 SendErrorMessage(MI_FILE_NOT_EXIST, QObject::tr("Cannot create path to file"));
184 }
185 }
186
187 if (IsOperationSupported(&data, &filePath, QF_SAVE | QF_FILE, false)){
188 WriteArchiveEx archive(filePath, GetVersionInfo(), this);
189 Q_ASSERT(archive.IsStoring());
190
194 const iser::ISerializable* serializablePtr = dynamic_cast<const iser::ISerializable*>(&data);
195 if(serializablePtr == nullptr){
196 serializablePtr = CompCastPtr<iser::ISerializable>(&data);
197 }
198 Q_ASSERT(serializablePtr != nullptr);
199
200 if (!CheckMinimalVersion(*serializablePtr, archive.GetVersionInfo())){
201 SendWarningMessage(MI_UNSUPPORTED_VERSION, QObject::tr("Archive version is not supported, possible lost of data"));
202 }
203
204 if ((const_cast<iser::ISerializable*>(serializablePtr))->Serialize(archive)){
205 return OS_OK;
206 }
207 else{
208 SendInfoMessage(MI_CANNOT_SAVE, QObject::tr("Cannot serialize object to file: '%1'").arg(filePath));
209 }
210 }
211
212 return OS_FAILED;
213}
214
215
216// protected methods
217
218template <class ReadArchive, class WriteArchive>
220 const ReadArchive& /*archive*/,
221 const istd::IChangeable& /*data*/,
222 const QString& filePath) const
223{
224 SendWarningMessage(MI_CANNOT_LOAD, QString(QObject::tr("Cannot load object from file ")) + filePath);
225}
226
227
228} // namespace ifile
229
230
Consume information about progress of some process.
Base implementation of file serializer.
OperationState
Result of operation.
ReadArchiveEx(const QString &filePath, const istd::ILogger *loggerPtr)
virtual bool IsLogConsumed(const istd::IInformationProvider::InformationCategory *, const int *flagsPtr=nullptr) const override
virtual bool SendLogMessage(istd::IInformationProvider::InformationCategory category, int id, const QString &message, const QString &messageSource, int flags=0) const
virtual bool IsLogConsumed(const istd::IInformationProvider::InformationCategory *, const int *flagsPtr=nullptr) const override
virtual bool SendLogMessage(istd::IInformationProvider::InformationCategory category, int id, const QString &message, const QString &messageSource, int flags=0) const
WriteArchiveEx(const QString &filePath, const iser::IVersionInfo *infoPtr, const istd::ILogger *loggerPtr)
Template implementation of file serializer using loading and storing archive implementation.
virtual ifile::IFilePersistence::OperationState LoadFromFile(istd::IChangeable &data, const QString &filePath=QString(), ibase::IProgressManager *progressManagerPtr=NULL) const override
This function loads data data from file filePath.
virtual ifile::IFilePersistence::OperationState SaveToFile(const istd::IChangeable &data, const QString &filePath=QString(), ibase::IProgressManager *progressManagerPtr=NULL) const override
This function saves data data to file filePath.
virtual void OnReadError(const ReadArchive &archive, const istd::IChangeable &data, const QString &filePath) const
Called if read error is occurred.
CFileSerializerCompBase BaseClass
virtual void DecorateMessage(istd::IInformationProvider::InformationCategory category, int id, int flags, QString &message, QString &messageSource) const override
Decorate message parts before outputting.
Common class for all classes which objects can be archived or restored from archive.
virtual bool Serialize(IArchive &archive)=0
Load or store state of this object as a archive stream.
Provides access to version information.
static bool EnsurePathExists(const QString &filePath)
Ensure that the given path exists.
Common interface for data model objects, which can be changed.
Definition IChangeable.h:28
InformationCategory
Category of information.
@ IC_INFO
Normal information level.
Common interface for classes sending some log info.
Definition ILogger.h:23
virtual bool SendLogMessage(IInformationProvider::InformationCategory category, int id, const QString &message, const QString &messageSource, int flags=0) const
Send any message to log.
Definition ILogger.h:72
virtual bool IsLogConsumed(const IInformationProvider::InformationCategory *categoryPtr=NULL, const int *flagsPtr=NULL) const
Check if any log message is consumed.
Definition ILogger.h:64
#define NULL
Definition istd.h:74
Contains interfaces and implementations of file system related components.