ACF $AcfVersion:0$
TSerializableContainer.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// ACF includes
6#include <iser/IArchive.h>
8#include <iser/CArchiveTag.h>
9#include <ibase/TContainer.h>
10
11
12namespace ibase
13{
14
15
20template <typename ItemClass, class ContainerType = typename TContainer<ItemClass>::Container>
21class TSerializableContainer: public TContainer<ItemClass, ContainerType>, virtual public iser::ISerializable
22{
23public:
25
26 // reimplemented (iser::Serializable)
27 virtual bool Serialize(iser::IArchive& archive) override;
28
29protected:
30 // abstract methods
31
35 virtual bool SerializeItem(ItemClass& item, iser::IArchive& archive, iser::CArchiveTag* parentTagPtr = NULL) = 0;
36};
37
38
39// public methods
40
41// reimplemented (iser::Serializable)
42
43template <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 retVal = 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 retVal = retVal && archive.BeginTag(itemTag);
82
83 retVal = retVal && SerializeItem(item, archive);
84
85 retVal = retVal && archive.EndTag(itemTag);
86 }
87 }
88
89 retVal = retVal && archive.EndTag(itemsTag);
90
91 return retVal;
92}
93
94
95} // namespace ibase
96
97
98
99
Common implementation of an abstract container.
Definition TContainer.h:26
Common implementation for an abstract serializable container.
virtual bool SerializeItem(ItemClass &item, iser::IArchive &archive, iser::CArchiveTag *parentTagPtr=NULL)=0
Serialize a single item in the container.
TContainer< ItemClass, ContainerType > BaseClass
virtual bool Serialize(iser::IArchive &archive) override
Load or store state of this object as a archive stream.
Process tag used to group data in archive stream.
Definition CArchiveTag.h:22
@ TT_GROUP
Normal tag used for grouping of tags or processed elements.
Definition CArchiveTag.h:37
@ TT_MULTIPLE
Multiple tag containing variable number of child tags.
Definition CArchiveTag.h:42
Represents an input/output persistence archive for object serialization.
Definition IArchive.h:164
virtual bool EndTag(const CArchiveTag &tag)=0
Ends a tagged section in the archive.
virtual bool BeginMultiTag(const CArchiveTag &tag, const CArchiveTag &subTag, int &count)=0
Begins a tagged section containing multiple elements of the same type.
virtual bool IsStoring() const =0
Checks if this archive is in storing (writing) or loading (reading) mode.
virtual bool BeginTag(const CArchiveTag &tag)=0
Begins a tagged section in the archive.
Common class for all classes which objects can be archived or restored from archive.
Help class which provides the automatic update mechanism of the model.
#define NULL
Definition istd.h:74
This namespace contains basic implementations of standard primitives on the component level.