ICF 3.1.1.10
Technical documentation of ICF Libraries
TMultiAttribute.h
Go to the documentation of this file.
1/********************************************************************************
2** This file is part of the ICF Framework. Copyright (C) Witold Gantzke & Kirill Lepskiy
3** ICF Framework may be used under the terms of the LGPL License v. 2.1 by the Free Software Foundation.
4********************************************************************************/
5
6#pragma once
7
8
9// Qt includes
10#include <QtCore/QVector>
11
12// ICF includes
14#include <istd/CClassInfo.h>
15#include <iser/IArchive.h>
16#include <iser/IObject.h>
17#include <iser/CArchiveTag.h>
20
21
22namespace iattr
23{
24
25
31template <typename Value>
32class TMultiAttribute: public iser::TCopySerializedWrap<iser::IObject>
33{
34public:
35 typedef Value ValueType;
37
43
45 TMultiAttribute(const TMultiAttribute& attribute);
46
52 TMultiAttribute(int elementsCount, Value* valuesPtr);
53
54 virtual int GetValuesCount() const;
55 virtual const Value& GetValueAt(int index) const;
56 virtual void SetValueAt(int index, const Value& value);
57 virtual void InsertValue(const Value& value);
58 virtual void Reset();
59 virtual int FindValue(const Value& value) const;
60
61 // reimplemented (iser::IObject)
62 QByteArray GetFactoryId() const;
63
64 // reimplemented (iser::ISerializable)
65 virtual bool Serialize(iser::IArchive& archive);
66
67 // static methods
68 static QByteArray GetTypeName();
69
70protected:
71 struct Wrap
72 {
73 Value value;
74
75 bool operator == (const Wrap& object) const
76 {
77 return (object.value == value);
78 }
79 };
80
81 QVector<Wrap> m_values;
82};
83
84
85// public methods
86
87template <typename Value>
92
93
94template <typename Value>
96 :BaseClass(),
97 m_values(attribute.m_values)
98{
99}
100
101
102template <typename Value>
103TMultiAttribute<Value>::TMultiAttribute(int elementsCount, Value* valuesPtr)
104 :BaseClass()
105{
106 for (int i = 0; i < elementsCount; ++i){
107 Wrap wrap;
108 wrap.value = valuesPtr[i];
109 m_values.push_back(wrap);
110 }
111}
112
113
114template <typename Value>
116{
117 return int(m_values.size());
118}
119
120
121template <typename Value>
122const Value& TMultiAttribute<Value>::GetValueAt(int index) const
123{
124 Q_ASSERT(index >= 0);
125 Q_ASSERT(index < GetValuesCount());
126
127 return m_values[index].value;
128}
129
130
131template <typename Value>
132void TMultiAttribute<Value>::SetValueAt(int index, const Value& value)
133{
134 Q_ASSERT(index >= 0);
135 Q_ASSERT(index < GetValuesCount());
136
137 m_values[index].value = value;
138}
139
140
141template <typename Value>
143{
144 Wrap wrap;
145 wrap.value = value;
146 m_values.push_back(wrap);
147}
148
149
150template <typename Value>
152{
153 m_values.clear();
154}
155
156
157template <typename Value>
158int TMultiAttribute<Value>::FindValue(const Value& value) const
159{
160 Wrap valueWrap;
161 valueWrap.value = value;
162
163 return m_values.indexOf(valueWrap);
164}
165
166
167// reimplemented (iser::IObject)
168
169template <typename Value>
171{
172 return GetTypeName();
173}
174
175
176// reimplemented (iser::ISerializable)
177
178template <typename Value>
180{
181 static iser::CArchiveTag valuesTag("Values", "List of attribute values", iser::CArchiveTag::Type::Multiple);
182 static iser::CArchiveTag valueTag("Value", "Single Value", iser::CArchiveTag::Type::Leaf, &valuesTag);
183
184 bool isChanging = archive.IsChanging();
185 istd::CChangeNotifier notifier(isChanging? this: nullptr);
186
187 return archive.ProcessAnyList(valuesTag, valueTag, int(m_values.size()),
188 [&](int i, auto& archive) {
189 return archive.ProcessLeaf(valueTag, m_values[i].value);
190 },
191 [&](int valuesCount) {
192 m_values.resize(valuesCount);
193
194 return true;
195 });
196}
197
198
199// static methods
200
201template <typename Value>
203{
204 if (typeid(Value) == typeid(QByteArray)){
205 return "iattr::TMultiAttribute<QByteArray>";
206 }
207
209}
210
211
212template <>
214{
215 return "Integer[]";
216}
217
218
219template <>
221{
222 return "Real[]";
223}
224
225
226template <>
228{
229 return "Boolean[]";
230}
231
232
233template <>
235{
236 return "String[]";
237}
238
239
240template <>
242{
243 return "Id[]";
244}
245
246
247// typedefs
248
254
255
256} // namespace iattr
257
258
@ 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)
Load or store state of this object as a archive stream.
QByteArray GetFactoryId() const
iser::TCopySerializedWrap< iser::IObject > BaseClass
QVector< Wrap > m_values
virtual int GetValuesCount() const
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:25
@ Multiple
Multiple tag containing variable number of child tags.
@ Leaf
Leaf tag, it can contain only one primitive element.
Represent input/output persistence archive.
Definition IArchive.h:34
virtual bool ProcessAnyList(const CArchiveTag &listTag, const CArchiveTag &subTag, int count, std::function< bool(int, IArchive &)> iterArchiveFn, std::function< bool(int)> resizeFn=nullptr)=0
Process some list of elements.
virtual bool IsChanging() const =0
Check if this archive processing change the object state.
Help class which provides the automatic update mechanism of the model.
const QByteArray & GetName() const
Get undecorated and platform undependent class name.
Definition CClassInfo.h:150
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

© Witold Gantzke and Kirill Lepskiy