TContainer.h
Go to the documentation of this file.
1 #ifndef ibase_TContainer_included
2 #define ibase_TContainer_included
3 
4 
5 // Qt includes
6 #include <QtCore/QList>
7 
8 // ACF includes
9 #include <istd/IContainerInfo.h>
10 #include <istd/CChangeNotifier.h>
11 
12 
13 namespace ibase
14 {
15 
16 
20 template <typename ItemClass, class ContainerType = QList<ItemClass> >
21 class TContainer: virtual public istd::IContainerInfo
22 {
23 public:
24  typedef ContainerType Container;
25 
27  {
28  CF_ELEMENT_ADDED = 0x382b230,
31  };
32 
33  virtual void Reserve(int count);
34  virtual void Resize(int count);
35 
36  const ItemClass& GetAt(int index) const;
37  ItemClass& GetAt(int index);
38  ItemClass& PushBack(const ItemClass& item);
39  ItemClass& PushFront(const ItemClass& item);
40  ItemClass& InsertAt(const ItemClass& item, int index);
41  void PopBack();
42  void PopFront();
43  void RemoveAt(int index);
44  virtual void Reset();
45 
46  TContainer& operator=(const TContainer& container);
47 
48  // reimplemented (istd::IContainerInfo)
49  virtual int GetItemsCount() const;
50  virtual bool IsEmpty() const;
51  virtual bool IsIndexValid(int index) const;
52 
53  // reimplemented (istd::IChangeable)
54  virtual bool CopyFrom(const IChangeable& object, CompatibilityMode mode = CM_WITHOUT_REFS);
55 
56 protected:
57  static const ChangeSet s_elementAddChange;
58  static const ChangeSet s_elementRemoveChange;
59  static const ChangeSet s_resetChange;
60 
61  typedef ContainerType Items;
62 
64 };
65 
66 
67 // public methods
68 
69 template <typename ItemClass, typename ContainerType>
71 {
72 }
73 
74 
75 template <typename ItemClass, typename ContainerType>
77 {
78 }
79 
80 
81 template <typename ItemClass, typename ContainerType>
83 {
84  Q_ASSERT(TContainer::IsIndexValid(index));
85 
86  return m_items[index];
87 }
88 
89 
90 template <typename ItemClass, typename ContainerType>
91 const ItemClass& TContainer<ItemClass, ContainerType>::GetAt(int index) const
92 {
93  Q_ASSERT(TContainer::IsIndexValid(index));
94 
95  return m_items[index];
96 }
97 
98 
99 template <typename ItemClass, typename ContainerType>
100 ItemClass& TContainer<ItemClass, ContainerType>::PushBack(const ItemClass& item)
101 {
102  istd::CChangeNotifier notifier(this, &s_elementAddChange);
103  Q_UNUSED(notifier);
104 
105  m_items.push_back(item);
106 
107  return m_items.back();
108 }
109 
110 
111 template <typename ItemClass, typename ContainerType>
112 ItemClass& TContainer<ItemClass, ContainerType>::PushFront(const ItemClass& item)
113 {
114  istd::CChangeNotifier notifier(this, &s_elementAddChange);
115  Q_UNUSED(notifier);
116 
117  m_items.push_front(item);
118 
119  return m_items.front();
120 }
121 
122 
123 template <typename ItemClass, typename ContainerType>
124 ItemClass& TContainer<ItemClass, ContainerType>::InsertAt(const ItemClass& item, int index)
125 {
126  istd::CChangeNotifier notifier(this, &s_elementAddChange);
127  Q_UNUSED(notifier);
128 
129  if ((index < 0) || (index >= m_items.size())){
130  m_items.push_back(item);
131 
132  return m_items.back();
133  }
134  else{
135  m_items.insert(index, item);
136 
137  return m_items[index];
138  }
139 }
140 
141 
142 template <typename ItemClass, typename ContainerType>
144 {
145  istd::CChangeNotifier notifier(this, &s_elementRemoveChange);
146  Q_UNUSED(notifier);
147 
148  m_items.pop_back();
149 }
150 
151 
152 template <typename ItemClass, typename ContainerType>
154 {
155  istd::CChangeNotifier notifier(this, &s_elementRemoveChange);
156  Q_UNUSED(notifier);
157 
158  m_items.pop_front();
159 }
160 
161 
162 template <typename ItemClass, typename ContainerType>
164 {
165  Q_ASSERT(index >= 0);
166  Q_ASSERT(index < int(m_items.size()));
167 
168  if (index < int(m_items.size())){
169  istd::CChangeNotifier notifier(this, &s_elementRemoveChange);
170  Q_UNUSED(notifier);
171 
172  m_items.erase(m_items.begin() + index);
173  }
174 }
175 
176 
177 template <typename ItemClass, typename ContainerType>
179 {
180  istd::CChangeNotifier notifier(this, &s_resetChange);
181  Q_UNUSED(notifier);
182 
183  m_items.clear();
184 }
185 
186 
187 template <typename ItemClass, typename ContainerType>
189 {
190  m_items = container.m_items;
191 
192  return *this;
193 }
194 
195 
196 // reimplemented (istd::IContainerInfo)
197 
198 template <typename ItemClass, typename ContainerType>
200 {
201  return int(m_items.size());
202 }
203 
204 
205 template <typename ItemClass, typename ContainerType>
207 {
208  return (m_items.size() == 0);
209 }
210 
211 
212 template <typename ItemClass, typename ContainerType>
214 {
215  return (index >= 0 && index < int(m_items.size()));
216 }
217 
218 
219 // reimplemented (istd::IChangeable)
220 
221 template <typename ItemClass, typename ContainerType>
222 bool TContainer<ItemClass, ContainerType>::CopyFrom(const IChangeable& object, CompatibilityMode /*mode*/)
223 {
224  const TContainer* containerPtr = dynamic_cast<const TContainer*>(&object);
225  if (containerPtr != NULL){
226  m_items = containerPtr->m_items;
227 
228  return true;
229  }
230 
231  return false;
232 }
233 
234 
235 // protected static members
236 
237 template <typename ItemClass, typename ContainerType>
239 
240 template <typename ItemClass, typename ContainerType>
242 
243 template <typename ItemClass, typename ContainerType>
245 
246 
247 } // namespace ibase
248 
249 
250 #endif // !ibase_TContainer_included
251 
252 
virtual bool IsIndexValid(int index) const
Definition: TContainer.h:213
ItemClass & InsertAt(const ItemClass &item, int index)
Definition: TContainer.h:124
virtual void Resize(int count)
Definition: TContainer.h:76
External references are simple ignored.
Definition: IChangeable.h:197
CompatibilityMode
Control how relationship betweeen objects are interpreted.
Definition: IChangeable.h:186
const ItemClass & GetAt(int index) const
Definition: TContainer.h:91
virtual int GetItemsCount() const
Returns number of elements in the container.
Definition: TContainer.h:199
Common implementation of an abstract container.
Definition: TContainer.h:21
void RemoveAt(int index)
Definition: TContainer.h:163
static const ChangeSet s_resetChange
Definition: TContainer.h:59
TContainer & operator=(const TContainer &container)
Definition: TContainer.h:188
Help class which provides the automatic update mechanism of the model.
ContainerType Items
Definition: TContainer.h:61
Common interface for all container implementations.
#define NULL
Definition: istd.h:64
virtual void Reserve(int count)
Definition: TContainer.h:70
virtual bool CopyFrom(const IChangeable &object, CompatibilityMode mode=CM_WITHOUT_REFS)
Definition: TContainer.h:222
virtual bool IsEmpty() const
Returns a true if container is empty, otherwise a false.
Definition: TContainer.h:206
static const ChangeSet s_elementAddChange
Definition: TContainer.h:57
Set of change flags (its IDs).
Definition: IChangeable.h:30
static const ChangeSet s_elementRemoveChange
Definition: TContainer.h:58
ChangeFlags
Data model change notification flags.
Definition: IChangeable.h:111
virtual void Reset()
Definition: TContainer.h:178
ItemClass & PushBack(const ItemClass &item)
Definition: TContainer.h:100
ContainerType Container
Definition: TContainer.h:24
ItemClass & PushFront(const ItemClass &item)
Definition: TContainer.h:112

© 2007-2017 Witold Gantzke and Kirill Lepskiy