ACF $AcfVersion:0$
TMultiAttribute.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// Qt includes
6#include <QtCore/QVector>
7
8// ACF includes
10#include <istd/CClassInfo.h>
11#include <iser/IArchive.h>
12#include <iser/IObject.h>
13#include <iser/CArchiveTag.h>
16
17
18namespace iattr
19{
20
21
27template <typename Value>
28class TMultiAttribute: public iser::TCopySerializedWrap<iser::IObject>
29{
30public:
31 typedef Value ValueType;
33
39
41 TMultiAttribute(const TMultiAttribute& attribute);
42
48 TMultiAttribute(int elementsCount, Value* valuesPtr);
49
50 virtual int GetValuesCount() const;
51 virtual const Value& GetValueAt(int index) const;
52 virtual void SetValueAt(int index, const Value& value);
53 virtual void InsertValue(const Value& value);
54 virtual void Reset();
55 virtual int FindValue(const Value& value) const;
56
57 // reimplemented (iser::IObject)
58 virtual QByteArray GetFactoryId() const override;
59
60 // reimplemented (iser::ISerializable)
61 virtual bool Serialize(iser::IArchive& archive) override;
62
63 // static methods
64 static QByteArray GetTypeName();
65
66protected:
67 struct Wrap
68 {
69 Value value;
70
71 bool operator == (const Wrap& object) const
72 {
73 return (object.value == value);
74 }
75 };
76
77 QVector<Wrap> m_values;
78};
79
80
81// public methods
82
83template <typename Value>
88
89
90template <typename Value>
92 :BaseClass(),
93 m_values(attribute.m_values)
94{
95}
96
97
98template <typename Value>
99TMultiAttribute<Value>::TMultiAttribute(int elementsCount, Value* valuesPtr)
100 :BaseClass()
101{
102 for (int i = 0; i < elementsCount; ++i){
103 Wrap wrap;
104 wrap.value = valuesPtr[i];
105 m_values.push_back(wrap);
106 }
107}
108
109
110template <typename Value>
112{
113 return int(m_values.size());
114}
115
116
117template <typename Value>
118const Value& TMultiAttribute<Value>::GetValueAt(int index) const
119{
120 Q_ASSERT(index >= 0);
121 Q_ASSERT(index < GetValuesCount());
122
123 return m_values[index].value;
124}
125
126
127template <typename Value>
128void TMultiAttribute<Value>::SetValueAt(int index, const Value& value)
129{
130 Q_ASSERT(index >= 0);
131 Q_ASSERT(index < GetValuesCount());
132
133 m_values[index].value = value;
134}
135
136
137template <typename Value>
139{
140 Wrap wrap;
141 wrap.value = value;
142 m_values.push_back(wrap);
143}
144
145
146template <typename Value>
148{
149 m_values.clear();
150}
151
152
153template <typename Value>
154int TMultiAttribute<Value>::FindValue(const Value& value) const
155{
156 Wrap valueWrap;
157 valueWrap.value = value;
158
159 return m_values.indexOf(valueWrap);
160}
161
162
163// reimplemented (iser::IObject)
164
165template <typename Value>
167{
168 return GetTypeName();
169}
170
171
172// reimplemented (iser::ISerializable)
173
174template <typename Value>
176{
177 static iser::CArchiveTag valuesTag("Values", "List of attribute values", iser::CArchiveTag::TT_MULTIPLE);
178 static iser::CArchiveTag valueTag("Value", "Single Value", iser::CArchiveTag::TT_LEAF, &valuesTag);
179
180 bool isStoring = archive.IsStoring();
181
182 istd::CChangeNotifier notifier(isStoring? NULL: this);
183
184 bool retVal = true;
185
186 int valuesCount = 0;
187
188 if (isStoring){
189 valuesCount = int(m_values.size());
190 }
191
192 retVal = retVal && archive.BeginMultiTag(valuesTag, valueTag, valuesCount);
193
194 if (!isStoring){
195 if (!retVal){
196 return false;
197 }
198
199 m_values.resize(valuesCount);
200 }
201
202 for (int i = 0; i < valuesCount; ++i){
203 retVal = retVal && archive.BeginTag(valueTag);
204 retVal = retVal && archive.Process(m_values[i].value);
205 retVal = retVal && archive.EndTag(valueTag);
206 }
207
208 retVal = retVal && archive.EndTag(valuesTag);
209
210 return retVal;
211}
212
213
214// static methods
215
216template <typename Value>
218{
219 if (typeid(Value) == typeid(QByteArray)){
220 return "iattr::TMultiAttribute<QByteArray>";
221 }
222
223 return istd::CClassInfo::GetName<TMultiAttribute<Value> >();
224}
225
226
227template <>
229{
230 return "Integer[]";
231}
232
233
234template <>
236{
237 return "Real[]";
238}
239
240
241template <>
243{
244 return "Boolean[]";
245}
246
247
248template <>
250{
251 return "String[]";
252}
253
254
255template <>
257{
258 return "Id[]";
259}
260
261
262// typedefs
263
269
270
271} // namespace iattr
272
273
274
275
@ AF_OBLIGATORY
Active if user declared this attribute as obligatory.
@ AF_NULLABLE
Active if this attribute can be unset (null).
@ AF_MULTIPLE
Attribute is multiple type.
@ AF_VALUE
Attribute is simple value type.
Template implementation of multiple component attribute.
virtual int FindValue(const Value &value) const
virtual void SetValueAt(int index, const Value &value)
static QByteArray GetTypeName()
virtual bool Serialize(iser::IArchive &archive) override
Load or store state of this object as a archive stream.
QVector< Wrap > m_values
iser::TCopySerializedWrap< iser::IObject > BaseClass
virtual int GetValuesCount() const
virtual QByteArray GetFactoryId() const override
virtual void InsertValue(const Value &value)
virtual const Value & GetValueAt(int index) const
Process tag used to group data in archive stream.
Definition CArchiveTag.h:22
@ TT_LEAF
Leaf tag, it can contain only one primitive element.
Definition CArchiveTag.h:48
@ 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 Process(bool &value)=0
Processes (reads or writes) a boolean value.
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.
Help class which provides the automatic update mechanism of the model.
#define NULL
Definition istd.h:74
This namespace containes implementations of attributes concept.
TMultiAttribute< bool > CBooleanListAttribute
TMultiAttribute< QByteArray > CIdListAttribute
TMultiAttribute< QString > CStringListAttribute
TMultiAttribute< int > CIntegerListAttribute
TMultiAttribute< double > CRealListAttribute
bool operator==(const Wrap &object) const