10#include <QtCore/QPair>
28template <
class InterfaceClass>
31 QPair<std::shared_ptr<InterfaceClass>, QByteArray> >
34 typedef QPair<std::shared_ptr<InterfaceClass>, QByteArray>
ItemClass;
43 InterfaceClass*
InsertElement(
int index,
const QByteArray& elementFactoryKey);
48 InterfaceClass*
AddElement(
const QByteArray& elementFactoryKey);
55 InterfaceClass*
GetElement(
int elementIndex)
const;
73 virtual std::unique_ptr<InterfaceClass>
CreateElement(
const QByteArray& itemKey);
84template <
class InterfaceClass>
87 m_itemFactoryPtr =
nullptr;
91template <
class InterfaceClass>
98template <
class InterfaceClass>
101 auto elementPtr = CreateElement(elementFactoryKey);
102 if (element !=
nullptr){
103 BaseClass::PushBack(
ItemClass(elementPtr, elementFactoryKey));
106 return elementPtr.get();
110template <
class InterfaceClass>
113 auto elementPtr = CreateElement(elementFactoryKey);
114 if (element !=
nullptr){
115 BaseClass::InsertAt(
ItemClass(elementPtr, elementFactoryKey), index);
118 return elementPtr.get();
122template <
class InterfaceClass>
125 if (elementIndex < BaseClass::GetItemsCount() && elementIndex >= 0){
126 return const_cast<InterfaceClass*
>(BaseClass::GetAt(elementIndex).first.get());
133template <
class InterfaceClass>
136 for (
int itemIndex = 0; itemIndex < BaseClass::GetItemsCount(); itemIndex++){
137 InterfaceClass* elementPtr = GetElement(itemIndex);
138 if (elementPtr == &elementRef){
147template <
class InterfaceClass>
150 if (elementIndex < BaseClass::GetItemsCount() && elementIndex >= 0){
151 return BaseClass::GetAt(elementIndex).second;
158template <
class InterfaceClass>
161 m_itemFactoryPtr = itemFactoryPtr;
167template <
class InterfaceClass>
181 int itemCount = BaseClass::GetItemsCount();
183 bool retVal = archive.
BeginMultiTag(itemsTag, itemTag, itemCount);
188 for (
int index = 0; index < itemCount; index++){
189 retVal = retVal && archive.
BeginTag(itemTag);
195 itemKey = BaseClass::GetAt(index).second;
198 retVal = retVal && archive.
BeginTag(keyTag);
199 retVal = retVal && archive.
Process(itemKey);
200 retVal = retVal && archive.
EndTag(keyTag);
203 item.second = itemKey;
204 auto interfacePtr = CreateElement(itemKey);
205 if (interfacePtr !=
nullptr){
206 item.first = std::shared_ptr<InterfaceClass>{interfacePtr.release()};
208 BaseClass::PushBack(item);
215 ItemClass& containerItem = BaseClass::GetAt(index);
217 retVal = retVal && SerializeItem(containerItem, archive, &itemTag);
219 retVal = retVal && archive.
EndTag(itemTag);
222 retVal = retVal && archive.
EndTag(itemsTag);
230template <
class InterfaceClass>
233 if (m_itemFactoryPtr !=
nullptr){
234 auto polymorphicPtr = m_itemFactoryPtr->CreateInstance(itemKey);
235 if (polymorphicPtr !=
nullptr){
236 InterfaceClass* interfacePtr =
dynamic_cast<InterfaceClass*
>(polymorphicPtr);
237 if (interfacePtr !=
nullptr){
238 OnElementCreated(interfacePtr);
240 return std::shared_ptr<InterfaceClass>{interfacePtr};
243 delete polymorphicPtr;
254template <
class InterfaceClass>
262template <
class InterfaceClass>
266 if (serializablePtr !=
nullptr){
267 return serializablePtr->
Serialize(archive);
Specific container implementation for factorisable items.
virtual bool SerializeItem(ItemClass &item, iser::IArchive &archive, iser::CArchiveTag *parentTagPtr=nullptr)
Serialize a single item in the container.
InterfaceClass * GetElement(int elementIndex) const
Gets an element with given index elementIndex from the container.
int GetElementIndex(const InterfaceClass &elementRef) const
Gets the index of given element from the container.
ibase::TSerializableContainer< ItemClass > BaseClass
QPair< std::shared_ptr< InterfaceClass >, QByteArray > ItemClass
virtual ~TFactorisableContainer()
InterfaceClass * InsertElement(int index, const QByteArray &elementFactoryKey)
Insert an element into the container at given index.
QByteArray GetElementKey(int elementIndex) const
Gets the element key associated with the element with given elementIndex from the container.
virtual void OnElementCreated(InterfaceClass *elementPtr)
virtual std::unique_ptr< InterfaceClass > CreateElement(const QByteArray &itemKey)
istd::TIFactory< InterfaceClass > * m_itemFactoryPtr
void RegisterItemFactory(istd::TIFactory< InterfaceClass > *itemFactoryPtr)
bool Serialize(iser::IArchive &archive)
Load or store state of this object as a archive stream.
InterfaceClass * AddElement(const QByteArray &elementFactoryKey)
Add an element to the container.
Common implementation for an abstract serializable container.
Process tag used to group data in archive stream.
@ TT_GROUP
Normal tag used for grouping of tags or processed elements.
@ TT_LEAF
Leaf tag, it can contain only one primitive element.
@ TT_MULTIPLE
Multiple tag containing variable number of child tags.
Represent input/output persistence archive.
virtual bool Process(bool &value)=0
Process primitive type.
virtual bool EndTag(const CArchiveTag &tag)=0
End of archive tag.
virtual bool BeginMultiTag(const CArchiveTag &tag, const CArchiveTag &subTag, int &count)=0
Begin of archive tag containing set of subelements of the same type.
virtual bool IsChanging() const =0
Check if this archive processing change the object state.
virtual bool BeginTag(const CArchiveTag &tag)=0
Begin of archive tag.
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.
Help class which provides the automatic update mechanism of the model.
static const ChangeSet & GetAllChanges()
Get anonymous change set.
Generic interface for a factory.
This namespace contains standard classes based on Qt.