CPrimitiveTypesSerializer.h
Go to the documentation of this file.
1 #ifndef iser_CPrimitiveTypesSerializer_included
2 #define iser_CPrimitiveTypesSerializer_included
3 
4 
5 // STL includes
6 #include <typeinfo>
7 
8 // Qt includes
9 #include <QtCore/QDateTime>
10 #include <QtCore/QPointF>
11 #include <QtCore/QMetaObject>
12 #include <QtCore/QMetaEnum>
13 
14 // ACF includes
15 #include <istd/TRange.h>
16 #include <istd/TRanges.h>
17 #include <istd/TIndex.h>
18 #include <iser/IArchive.h>
19 
20 
21 namespace iser
22 {
23 
24 
29 {
30 public:
34  static bool SerializeRange(iser::IArchive& archive, istd::CRange& range);
35 
39  static bool SerializeIntRange(iser::IArchive& archive, istd::CIntRange& range);
40 
44  static bool SerializeRanges(iser::IArchive& archive, istd::CRanges& ranges);
45 
49  static bool SerializeIntRanges(iser::IArchive& archive, istd::CIntRanges& ranges);
50 
54  static bool SerializeDateTime(iser::IArchive& archive, QDateTime& dateTime);
55 
59  static bool SerializeDate(iser::IArchive& archive, QDate& date);
60 
64  template <int Dimensions>
65  static bool SerializeIndex(iser::IArchive& archive, istd::TIndex<Dimensions>& index);
66 
70  static bool SerializeQPointF(iser::IArchive& archive, QPointF& point);
71 
75  static bool SerializeQStringList(
76  iser::IArchive& archive,
77  QStringList& stringList,
78  const QByteArray& containerTagName = "Elements",
79  const QByteArray& elementTagName = "Element");
80 
87  template <typename EnumType, QByteArray (*ToString)(EnumType), bool (*FromString)(const QByteArray&, EnumType&)>
88  static bool SerializeEnum(
89  iser::IArchive& archive,
90  EnumType& enumValue);
91 
97  template <typename EnumType>
98  static bool SerializeEnum(
99  iser::IArchive& archive,
100  EnumType& enumValue,
101  const QObject* objectPtr);
102 };
103 
104 
105 // public template methods
106 
107 template <int Dimensions>
109 {
110  bool retVal = true;
111 
112  for (int i = 0; i < Dimensions; ++i){
113  retVal = retVal && archive.Process(index[i]);
114  }
115 
116  return retVal;
117 }
118 
119 
120 template <typename EnumType, QByteArray (*ToString)(EnumType), bool (*FromString)(const QByteArray&, EnumType&)>
122  iser::IArchive& archive,
123  EnumType& enumValue)
124 {
125  QByteArray enumValueAsText;
126 
127  bool retVal = true;
128 
129  // Try to get enumeration text generated by I_DECLARE_ENUM macro:
130  if (enumValueAsText.isEmpty() && (ToString != NULL)){
131  enumValueAsText = ToString(enumValue);
132  }
133 
134  // Enum value is defined in textual form:
135  if (!enumValueAsText.isEmpty()){
136  retVal = retVal && archive.Process(enumValueAsText);
137 
138  if (retVal && !archive.IsStoring()){
139  if (FromString != NULL){
140  retVal = FromString(enumValueAsText, enumValue);
141  }
142  else{
143  retVal = false;
144  }
145  }
146  }
147  else{
148  int value = enumValue;
149 
150  retVal = retVal && archive.Process(value);
151 
152  if (!archive.IsStoring()){
153  // TODO: check if the readed value is in range of the enum values!
154  enumValue = EnumType(value);
155  }
156  }
157 
158  return retVal;
159 }
160 
161 
162 template <typename EnumType>
164  iser::IArchive& archive,
165  EnumType& enumValue,
166  const QObject* objectPtr)
167 {
168  QByteArray enumValueAsText;
169  QMetaEnum foundEnumMeta;
170 
171  QString enumTypeName = typeid(EnumType).name();
172  enumTypeName = enumTypeName.mid(enumTypeName.lastIndexOf(":") + 1);
173 
174  if (objectPtr != NULL){
175  const QMetaObject* metaObjectPtr = objectPtr->metaObject();
176 
177  // Iterate over all enums of the class:
178  int enumeratorsCount = metaObjectPtr->enumeratorCount();
179  for (int enumeratorIndex = 0; enumeratorIndex < enumeratorsCount; ++enumeratorIndex){
180  QMetaEnum enumMeta = metaObjectPtr->enumerator(enumeratorIndex);
181  QString enumName = enumMeta.name();
182 
183  if (enumTypeName == enumName){
184  foundEnumMeta = enumMeta;
185 
186  // Find the enum value:
187  int keysCount = enumMeta.keyCount();
188  for (int keyIndex = 0; keyIndex < keysCount; ++keyIndex){
189 
190  if (enumMeta.value(keyIndex) == enumValue){
191  enumValueAsText = enumMeta.valueToKey(keyIndex);
192  break;
193  }
194  }
195  }
196  }
197  }
198 
199  bool retVal = true;
200 
201  // Enum value is defined in textual form:
202  if (!enumValueAsText.isEmpty()){
203  retVal = retVal && archive.Process(enumValueAsText);
204 
205  if (retVal && !archive.IsStoring()){
206  Q_ASSERT(foundEnumMeta.isValid());
207 #if QT_VERSION >= 0x050000
208  enumValue = EnumType(foundEnumMeta.keyToValue(enumValueAsText.constData(), &retVal));
209 #else
210  int value = foundEnumMeta.keyToValue(enumValueAsText.constData());
211  if (value >= 0){
212  enumValue = EnumType(value);
213  }
214  else{
215  return false;
216  }
217 #endif
218  }
219  }
220  else{
221  int value = enumValue;
222 
223  retVal = retVal && archive.Process(value);
224 
225  if (!archive.IsStoring()){
226  // TODO: check if the readed value is in range of the enum values!
227  enumValue = EnumType(value);
228  }
229  }
230 
231  return retVal;
232 }
233 
234 
235 #define I_SERIALIZE_FLAG(Enum, archive, flag) iser::CPrimitiveTypesSerializer::SerializeEnum<int, Enum##ToString, Enum##FromString>(archive, flag);
236 #define I_SERIALIZE_ENUM(Enum, archive, enumValue) iser::CPrimitiveTypesSerializer::SerializeEnum<Enum, ToString, FromString>(archive, enumValue);
237 
238 
239 } // namespace iser
240 
241 
242 #endif // !iser_CPrimitiveTypesSerializer_included
243 
static bool SerializeEnum(iser::IArchive &archive, EnumType &enumValue)
Method for serialization of the enumerated value using ACF's meta information extensions for the C++ ...
virtual bool IsStoring() const =0
Check if this archive is loading or storing.
static bool SerializeIndex(iser::IArchive &archive, istd::TIndex< Dimensions > &index)
Serialize a generic index object.
Set of ranges.
Definition: TRanges.h:25
Multidimensional index used to addressing fixed-size array.
Definition: TIndex.h:17
static bool SerializeIntRange(iser::IArchive &archive, istd::CIntRange &range)
Serialize an integer-based range object.
virtual bool Process(bool &value)=0
Process primitive type.
static bool SerializeIntRanges(iser::IArchive &archive, istd::CIntRanges &ranges)
Serialize list of integer-based ranges.
Represent input/output persistence archive.
Definition: IArchive.h:30
static bool SerializeQStringList(iser::IArchive &archive, QStringList &stringList, const QByteArray &containerTagName="Elements", const QByteArray &elementTagName="Element")
Serialize a QStringList object.
static bool SerializeRange(iser::IArchive &archive, istd::CRange &range)
Serialize a range object.
#define NULL
Definition: istd.h:64
static bool SerializeRanges(iser::IArchive &archive, istd::CRanges &ranges)
Serialize list of range objects.
static bool SerializeDateTime(iser::IArchive &archive, QDateTime &dateTime)
Serialize QDateTime object.
Implementation of serialize method for some common data types.
static bool SerializeQPointF(iser::IArchive &archive, QPointF &point)
Serialize a QPointF object.
static bool SerializeDate(iser::IArchive &archive, QDate &date)
Serialize QDate object.

© 2007-2017 Witold Gantzke and Kirill Lepskiy