TMultiProperty.h
Go to the documentation of this file.
1 #ifndef iprop_TMultiProperty_included
2 #define iprop_TMultiProperty_included
3 
4 
5 // Qt includes
6 #include <QtCore/QVector>
7 #include <QtCore/QStringList>
8 
9 
10 // ACF includes
11 #include "istd/TChangeNotifier.h"
12 #include "istd/CClassInfo.h"
13 #include "iser/IArchive.h"
14 #include "iser/IObject.h"
15 #include "iser/CArchiveTag.h"
16 
17 // ACF-Solutions includes
18 #include "iprop/CPropertyBase.h"
19 
20 
21 namespace iprop
22 {
23 
24 
29 template <typename Value, class Container = QVector<Value> >
31 {
32 public:
33  typedef Value ValueType;
35  typedef Container ValueList;
36  typedef typename ValueList::iterator iterator;
37  typedef typename ValueList::const_iterator const_iterator;
38 
43 
54  IPropertiesManager* propertyOwnerPtr,
55  const QByteArray& propertyId,
56  const QByteArray& propertyDescription,
57  int propertyFlags,
58  int changeFlags = 0,
59  int elementsCount = 0,
60  Value* valuesPtr = NULL);
61 
62  void SetValues(const ValueList& valueList);
63 
67  template <class ContainerImpl>
68  void SetValues(typename ContainerImpl::const_iterator begin, typename ContainerImpl::const_iterator end);
69 
73  const ValueList& GetValues() const;
74 
78  virtual int GetValuesCount() const;
79 
83  virtual Value GetValueAt(int index) const;
84 
88  virtual void SetValueAt(int index, const Value& value);
89 
93  virtual void InsertValue(const Value& value);
94 
98  virtual void ResetValues();
99 
100  // STL compatibility
101  iterator begin();
102  const_iterator begin() const;
103  iterator end();
104  const_iterator end() const;
105 
106  // reimplemented (iser::IObject)
107  QByteArray GetFactoryId() const;
108 
109  // reimplemented (iser::ISerializable)
110  virtual bool Serialize(iser::IArchive& archive);
111 
112  // static methods
113  static QByteArray GetTypeName();
114 
115 protected:
116  Container m_values;
117 };
118 
119 
120 // public methods
121 
122 template <typename Value, class Container>
124 {
125 }
126 
127 
128 template <typename Value, class Container>
130  IPropertiesManager* propertyOwnerPtr,
131  const QByteArray& propertyId,
132  const QByteArray& propertyDescription,
133  int propertyFlags,
134  int changeFlags,
135  int elementsCount,
136  Value* valuesPtr)
137  :BaseClass(propertyOwnerPtr, propertyId, propertyDescription, propertyFlags, changeFlags)
138 {
139  for (int i = 0; i < elementsCount; ++i){
140  m_values.push_back(valuesPtr[i]);
141  }
142 }
143 
144 
145 template <typename Value, class Container>
147 {
148  SetValues<ValueList>(valueList.begin(), valueList.end());
149 }
150 
151 
152 template <typename Value, class Container>
153 template <class ContainerImpl>
154 void TMultiProperty<Value, Container>::SetValues(typename ContainerImpl::const_iterator beginIter, typename ContainerImpl::const_iterator endIter)
155 {
156  istd::CChangeNotifier changePtr(m_propertyOwnerPtr, m_changeFlags);
157 
158  ResetValues();
159 
160  while (beginIter != endIter){
161  InsertValue(*beginIter);
162 
163  ++beginIter;
164  }
165 }
166 
167 
168 template <typename Value, class Container>
170 {
171  return m_values;
172 }
173 
174 
175 template <typename Value, class Container>
177 {
178  return int(m_values.size());
179 }
180 
181 
182 template <typename Value, class Container>
184 {
185  Q_ASSERT(index >= 0);
186  Q_ASSERT(index < GetValuesCount());
187 
188  return m_values[index];
189 }
190 
191 
192 template <typename Value, class Container>
193 void TMultiProperty<Value, Container>::SetValueAt(int index, const Value& value)
194 {
195  Q_ASSERT(index >= 0);
196  Q_ASSERT(index < GetValuesCount());
197 
198  if (m_values[index] != value){
199  istd::CChangeNotifier changePtr(m_propertyOwnerPtr, m_changeFlags);
200 
201  m_values[index] = value;
202  }
203 }
204 
205 
206 template <typename Value, class Container>
208 {
209  m_values.push_back(value);
210 }
211 
212 
213 template <typename Value, class Container>
215 {
216  istd::CChangeNotifier changePtr(m_propertyOwnerPtr, m_changeFlags);
217 
218  m_values.clear();
219 }
220 
221 
222 // STL compatibility
223 
224 template <typename Value, class Container>
226 {
227  return m_values.begin();
228 }
229 
230 
231 template <typename Value, class Container>
233 {
234  return m_values.begin();
235 }
236 
237 
238 template <typename Value, class Container>
240 {
241  return m_values.end();
242 }
243 
244 
245 template <typename Value, class Container>
247 {
248  return m_values.end();
249 }
250 
251 
252 // reimplemented (iser::IObject)
253 
254 template <typename Value, class Container>
256 {
257  return GetTypeName();
258 }
259 
260 
261 // reimplemented (iser::ISerializable)
262 
263 template <typename Value, class Container>
265 {
266  static iser::CArchiveTag valuesTag("Values", "List of property values");
267  static iser::CArchiveTag valueTag("Value", "Single Value");
268 
269  bool isStoring = archive.IsStoring();
270 
271  istd::CChangeNotifier notifier(isStoring? NULL: this);
272 
273  bool retVal = true;
274 
275  if (isStoring){
276  int valuesCount = int(m_values.size());
277 
278  retVal = retVal && archive.BeginMultiTag(valuesTag, valueTag, valuesCount);
279  for (typename ValueList::iterator iter = m_values.begin(); iter != m_values.end(); ++iter){
280  retVal = retVal && archive.BeginTag(valueTag);
281  retVal = retVal && archive.Process(*iter);
282  retVal = retVal && archive.EndTag(valueTag);
283  }
284  retVal = retVal && archive.EndTag(valuesTag);
285  }
286  else{
287  int valuesCount = 0;
288 
289  retVal = retVal && archive.BeginMultiTag(valuesTag, valueTag, valuesCount);
290  if (!retVal){
291  return false;
292  }
293 
294  // load properties 'in place', without allocation of new elements
295  int itemIndex = 0;
296  for (typename ValueList::iterator iter = m_values.begin(); iter != m_values.end(); ++iter){
297  // remove rest of elements, if current number of elements is bigger than the stored
298  if (itemIndex >= valuesCount){
299  m_values.erase(iter, m_values.end());
300 
301  break;
302  }
303 
304  retVal = retVal && archive.BeginTag(valueTag);
305  retVal = retVal && archive.Process(*iter);
306  retVal = retVal && archive.EndTag(valueTag);
307 
308  ++itemIndex;
309  }
310 
311  // load rest of properties if current number of elements is smaller than the stored
312  while (itemIndex < valuesCount){
313  retVal = retVal && archive.BeginTag(valueTag);
314  Value value;
315  retVal = retVal && archive.Process(value);
316  m_values.push_back(value);
317  retVal = retVal && archive.EndTag(valueTag);
318 
319  ++itemIndex;
320  }
321 
322  retVal = retVal && archive.EndTag(valuesTag);
323  }
324 
325  return retVal;
326 }
327 
328 
329 // static methods
330 
331 template <typename Value, class Container>
333 {
334  return istd::CClassInfo::GetName<TMultiProperty<Value> >();
335 }
336 
337 
338 template <>
340 {
341  return "Integer[]";
342 }
343 
344 
345 template <>
347 {
348  return "Real[]";
349 }
350 
351 
352 template <>
354 {
355  return "Boolean[]";
356 }
357 
358 
359 template <>
361 {
362  return "String[]";
363 }
364 
365 
366 template <>
368 {
369  return "Id[]";
370 }
371 
372 
373 // typedefs
374 
380 
381 
382 } // namespace iprop
383 
384 
385 #endif // !iprop_TMultiProperty_included
386 
387 
virtual void InsertValue(const Value &value)
Insert new value into container.
ValueList::const_iterator const_iterator
TMultiProperty< QByteArray > CMultiStdStringProperty
CPropertyBase BaseClass
virtual void ResetValues()
Remove all values from the container.
const ValueList & GetValues() const
Get all values.
Basic implementation of an property.
Definition: CPropertyBase.h:22
Common interface for a container of properties.
QByteArray GetFactoryId() const
virtual bool BeginMultiTag(const CArchiveTag &tag, const CArchiveTag &subTag, int &count)=0
virtual int GetValuesCount() const
Get number of values in the multi property.
TMultiProperty< QString, QStringList > CMultiStringProperty
virtual Value GetValueAt(int index) const
Get value at the index index.
Template implementation of multiple property.
#define NULL
TMultiProperty< bool > CMultiBoolProperty
virtual void SetValueAt(int index, const Value &value)
Set value at the index index to new value value.
void SetValues(const ValueList &valueList)
ValueList::iterator iterator
TMultiProperty< int > CMultiIntProperty
static QByteArray GetTypeName()
TMultiProperty< double > CMultiDoubleProperty
virtual bool Serialize(iser::IArchive &archive)
TMultiProperty()
Default constructor.

© 2007-2011 Witold Gantzke and Kirill Lepskiy