TFileSerializerComp.h
Go to the documentation of this file.
1 #ifndef ifile_TFileSerializerComp_included
2 #define ifile_TFileSerializerComp_included
3 
4 
5 // Qt includes
6 #include <QtCore/QObject>
7 #include <QtCore/QDir>
8 
9 // ACF includes
10 #include <istd/CChangeNotifier.h>
11 #include <istd/CSystem.h>
12 #include <ibase/IProgressManager.h>
14 
15 
16 namespace ifile
17 {
18 
19 
25 template <class ReadArchive, class WriteArchive>
27 {
28 public:
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)
36  virtual int LoadFromFile(
37  istd::IChangeable& data,
38  const QString& filePath = QString(),
39  ibase::IProgressManager* progressManagerPtr = NULL) const;
40  virtual int SaveToFile(
41  const istd::IChangeable& data,
42  const QString& filePath = QString(),
43  ibase::IProgressManager* progressManagerPtr = NULL) const;
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 != NULL){
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 = NULL) const
76  {
78 
79  return (m_loggerPtr != NULL) && 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 != NULL){
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 = NULL) const
116  {
118 
119  return (m_loggerPtr != NULL) && m_loggerPtr->IsLogConsumed(&slaveCategory, flagsPtr);
120  }
121 
122  private:
123  const istd::ILogger* m_loggerPtr;
124  };
125 
126 protected:
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 
140 template <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, false)){
147  ReadArchiveEx archive(filePath, this);
148 
149  Q_ASSERT(!archive.IsStoring());
150 
154  iser::ISerializable* serializablePtr = dynamic_cast<iser::ISerializable*>(&data);
155  if (serializablePtr == NULL){
156  serializablePtr = CompCastPtr<iser::ISerializable>(&data);
157  }
158 
159  Q_ASSERT(serializablePtr != NULL);
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 
173 template <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 == NULL){
196  serializablePtr = CompCastPtr<iser::ISerializable>(&data);
197  }
198  Q_ASSERT(serializablePtr != NULL);
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 
218 template <class ReadArchive, class WriteArchive>
219 void TFileSerializerComp<ReadArchive, WriteArchive>::OnReadError(const ReadArchive& /*archive*/, const istd::IChangeable& /*data*/, const QString& filePath) const
220 {
221  SendWarningMessage(MI_CANNOT_LOAD, QString(QObject::tr("Cannot load object from file ")) + filePath);
222 }
223 
224 
225 } // namespace ifile
226 
227 
228 #endif // !ifile_TFileSerializerComp_included
229 
230 
static bool EnsurePathExists(const QString &filePath)
Ensure that the given path exists.
ReadArchiveEx(const QString &filePath, const istd::ILogger *loggerPtr)
Common interface for classes sending some log info.
Definition: ILogger.h:20
Provides access to version information.
Definition: IVersionInfo.h:22
WriteArchiveEx(const QString &filePath, const iser::IVersionInfo *infoPtr, const istd::ILogger *loggerPtr)
virtual bool SendLogMessage(istd::IInformationProvider::InformationCategory category, int id, const QString &message, const QString &messageSource, int flags=0) const
Base implementation of file serializer.
Common class for all classes which objects can be archived or restored from archive.
Definition: ISerializable.h:23
Template implementation of file serializer using loading and storing archive implementation.
virtual bool IsLogConsumed(const IInformationProvider::InformationCategory *categoryPtr=NULL, const int *flagsPtr=NULL) const
Check if any log message is consumed.
Definition: ILogger.h:62
virtual int SaveToFile(const istd::IChangeable &data, const QString &filePath=QString(), ibase::IProgressManager *progressManagerPtr=NULL) const
This function saves data data to file filePath.
Common interface for data model objects, which can be changed.
Definition: IChangeable.h:24
virtual bool IsLogConsumed(const istd::IInformationProvider::InformationCategory *, const int *flagsPtr=NULL) const
virtual int LoadFromFile(istd::IChangeable &data, const QString &filePath=QString(), ibase::IProgressManager *progressManagerPtr=NULL) const
This function loads data data from file filePath.
InformationCategory
Category of information.
#define NULL
Definition: istd.h:64
virtual bool IsLogConsumed(const istd::IInformationProvider::InformationCategory *, const int *flagsPtr=NULL) const
virtual void DecorateMessage(istd::IInformationProvider::InformationCategory category, int id, int flags, QString &message, QString &messageSource) const
CFileSerializerCompBase BaseClass
virtual void OnReadError(const ReadArchive &archive, const istd::IChangeable &data, const QString &filePath) const
Called if read error is occurred.
Consume information about progress of some process.
virtual bool SendLogMessage(istd::IInformationProvider::InformationCategory category, int id, const QString &message, const QString &messageSource, int flags=0) const
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:70

© 2007-2017 Witold Gantzke and Kirill Lepskiy