TSerializableContainer.h
Go to the documentation of this file.
1 #ifndef ibase_TSerializableContainer_included
2 #define ibase_TSerializableContainer_included
3 
4 
5 // ACF includes
6 #include <iser/IArchive.h>
7 #include <iser/ISerializable.h>
8 #include <iser/CArchiveTag.h>
9 #include <ibase/TContainer.h>
10 
11 
12 namespace ibase
13 {
14 
15 
20 template <typename ItemClass, class ContainerType = typename TContainer<ItemClass>::Container>
21 class TSerializableContainer: public TContainer<ItemClass, ContainerType>, virtual public iser::ISerializable
22 {
23 public:
25 
26  // reimplemented (iser::Serializable)
27  virtual bool Serialize(iser::IArchive& archive);
28 
29 protected:
30  // abstract methods
31 
35  virtual bool SerializeItem(ItemClass& item, iser::IArchive& archive) = 0;
36 };
37 
38 
39 // public methods
40 
41 // reimplemented (iser::Serializable)
42 
43 template <typename ItemClass, class ContainerType>
45 {
46  static iser::CArchiveTag itemsTag("Items", "List of items", iser::CArchiveTag::TT_MULTIPLE);
47  static iser::CArchiveTag itemTag("Item", "Item", iser::CArchiveTag::TT_GROUP, &itemsTag);
48 
49  istd::CChangeNotifier notifier(archive.IsStoring()? NULL: this);
50  Q_UNUSED(notifier);
51 
52  int itemCount = int(BaseClass::m_items.size());
53 
54  bool retVal = archive.BeginMultiTag(itemsTag, itemTag, itemCount);
55  if (!retVal){
56  return false;
57  }
58 
59  if (!archive.IsStoring()){
60  BaseClass::Reset();
61 
62 #if QT_VERSION >= 0x040700
63  BaseClass::m_items.reserve(itemCount);
64 #endif
65  for (int index = 0; index < itemCount; index++){
66  BaseClass::PushBack(ItemClass());
67 
68  ItemClass& item = BaseClass::GetAt(index);
69 
70  bool retVal = archive.BeginTag(itemTag);
71 
72  retVal = retVal && SerializeItem(item, archive);
73 
74  retVal = retVal && archive.EndTag(itemTag);
75  }
76  }
77  else{
78  for (int index = 0; index < itemCount; index++){
79  ItemClass& item = BaseClass::GetAt(index);
80 
81  bool retVal = archive.BeginTag(itemTag);
82 
83  retVal = retVal && SerializeItem(item, archive);
84 
85  retVal = retVal && archive.EndTag(itemTag);
86  }
87  }
88 
89  return retVal;
90 }
91 
92 
93 } // namespace ibase
94 
95 
96 #endif // !ibase_TSerializableContainer_included
97 
98 
virtual bool IsStoring() const =0
Check if this archive is loading or storing.
Common implementation of an abstract container.
Definition: TContainer.h:21
TContainer< ItemClass, ContainerType > BaseClass
virtual bool SerializeItem(ItemClass &item, iser::IArchive &archive)=0
Serialize a single item in the container.
Common class for all classes which objects can be archived or restored from archive.
Definition: ISerializable.h:23
virtual bool BeginTag(const CArchiveTag &tag)=0
Begin of archive tag.
Multiple tag containing variable number of child tags.
Definition: CArchiveTag.h:42
Normal tag used for grouping of tags or processed elements.
Definition: CArchiveTag.h:37
virtual bool BeginMultiTag(const CArchiveTag &tag, const CArchiveTag &subTag, int &count)=0
Begin of archive tag containing set of subelements of the same type.
Represent input/output persistence archive.
Definition: IArchive.h:30
Help class which provides the automatic update mechanism of the model.
virtual bool Serialize(iser::IArchive &archive)
Load or store state of this object as a archive stream.
#define NULL
Definition: istd.h:64
Common implementation for an abstract serializable container.
Process tag used to group data in archive stream.
Definition: CArchiveTag.h:21
virtual bool EndTag(const CArchiveTag &tag)=0
End of archive tag.

© 2007-2017 Witold Gantzke and Kirill Lepskiy