TMultiAttribute.h
Go to the documentation of this file.
1 #ifndef iattr_TMultiAttribute_included
2 #define iattr_TMultiAttribute_included
3 
4 
5 // Qt includes
6 #include <QtCore/QVector>
7 
8 // ACF includes
9 #include <istd/CChangeNotifier.h>
10 #include <istd/CClassInfo.h>
11 #include <iser/IArchive.h>
12 #include <iser/IObject.h>
13 #include <iser/CArchiveTag.h>
16 
17 
18 namespace iattr
19 {
20 
21 
27 template <typename Value>
28 class TMultiAttribute: public iser::TCopySerializedWrap<iser::IObject>
29 {
30 public:
31  typedef Value ValueType;
33 
35  {
38  };
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  QByteArray GetFactoryId() const;
59 
60  // reimplemented (iser::ISerializable)
61  virtual bool Serialize(iser::IArchive& archive);
62 
63  // static methods
64  static QByteArray GetTypeName();
65 
66 protected:
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 
83 template <typename Value>
85  :BaseClass()
86 {
87 }
88 
89 
90 template <typename Value>
92  :BaseClass(),
93  m_values(attribute.m_values)
94 {
95 }
96 
97 
98 template <typename Value>
99 TMultiAttribute<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 
110 template <typename Value>
112 {
113  return int(m_values.size());
114 }
115 
116 
117 template <typename Value>
118 const 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 
127 template <typename Value>
128 void 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 
137 template <typename Value>
138 void TMultiAttribute<Value>::InsertValue(const Value& value)
139 {
140  Wrap wrap;
141  wrap.value = value;
142  m_values.push_back(wrap);
143 }
144 
145 
146 template <typename Value>
148 {
149  m_values.clear();
150 }
151 
152 
153 template <typename Value>
154 int 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 
165 template <typename Value>
167 {
168  return GetTypeName();
169 }
170 
171 
172 // reimplemented (iser::ISerializable)
173 
174 template <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 
216 template <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 
227 template <>
229 {
230  return "Integer[]";
231 }
232 
233 
234 template <>
236 {
237  return "Real[]";
238 }
239 
240 
241 template <>
243 {
244  return "Boolean[]";
245 }
246 
247 
248 template <>
250 {
251  return "String[]";
252 }
253 
254 
255 template <>
257 {
258  return "Id[]";
259 }
260 
261 
262 // typedefs
263 
269 
270 
271 } // namespace iattr
272 
273 
274 #endif // !iattr_TMultiAttribute_included
275 
276 
virtual bool Serialize(iser::IArchive &archive)
Load or store state of this object as a archive stream.
static QByteArray GetTypeName()
virtual void SetValueAt(int index, const Value &value)
Template implementation of multiple component attribute.
virtual bool IsStoring() const =0
Check if this archive is loading or storing.
iser::TCopySerializedWrap< iser::IObject > BaseClass
bool operator==(const Wrap &object) const
virtual bool Process(bool &value)=0
Process primitive type.
virtual bool BeginTag(const CArchiveTag &tag)=0
Begin of archive tag.
Multiple tag containing variable number of child tags.
Definition: CArchiveTag.h:42
TMultiAttribute< int > CIntegerListAttribute
virtual bool BeginMultiTag(const CArchiveTag &tag, const CArchiveTag &subTag, int &count)=0
Begin of archive tag containing set of subelements of the same type.
QByteArray GetFactoryId() const
Represent input/output persistence archive.
Definition: IArchive.h:30
Active if user declared this attribute as obligatory.
virtual int FindValue(const Value &value) const
virtual const Value & GetValueAt(int index) const
Help class which provides the automatic update mechanism of the model.
Leaf tag, it can contain only one primitive element.
Definition: CArchiveTag.h:48
TMultiAttribute< double > CRealListAttribute
#define NULL
Definition: istd.h:64
TMultiAttribute< QString > CStringListAttribute
TMultiAttribute< QByteArray > CIdListAttribute
virtual void InsertValue(const Value &value)
TMultiAttribute< bool > CBooleanListAttribute
Process tag used to group data in archive stream.
Definition: CArchiveTag.h:21
virtual bool EndTag(const CArchiveTag &tag)=0
End of archive tag.
virtual int GetValuesCount() const
Attribute is simple value type.
Active if this attribute can be unset (null).
QVector< Wrap > m_values

© 2007-2017 Witold Gantzke and Kirill Lepskiy