ScummVM API documentation
Container.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 /*
23  * Copyright (C) 2006-2010 - Frictional Games
24  *
25  * This file is part of HPL1 Engine.
26  */
27 
28 #ifndef HPL_CONTAINER_H
29 #define HPL_CONTAINER_H
30 
31 #include "common/array.h"
32 #include "common/list.h"
33 #include "hpl1/engine/system/MemoryManager.h"
34 #include "common/stablemap.h"
35 
36 namespace hpl {
37 
38 //---------------------------------
39 
41  friend class cSerializeClass;
42 
43 public:
44  virtual ~iContainerIterator() {}
45 
46 protected:
47  virtual bool HasNext() = 0;
48 
49  virtual void *NextPtr() = 0;
50 };
51 
52 //---------------------------------
53 
54 class iContainer {
55  friend class cSerializeClass;
56 
57 public:
58  virtual ~iContainer() = default;
59  virtual size_t Size() = 0;
60  virtual void Clear() = 0;
61 
62 protected:
63  virtual void AddVoidPtr(void **apPtr) = 0;
64  virtual void AddVoidClass(void *apClass) = 0;
65 
66  virtual iContainerIterator *CreateIteratorPtr() = 0;
67 };
68 
69 //---------------------------------
70 
72 public:
73  virtual ~iContainerKeyPair() = default;
74  virtual size_t Size() = 0;
75 
76  virtual void AddVoidPtr(void *apKey, void **apClass) = 0;
77  virtual void AddVoidClass(void *apKey, void *apClass) = 0;
78 };
79 
80 //---------------------------------
81 
82 template<class T>
84  void *NextPtr() {
85  return &Next();
86  }
87 
88 public:
90  mpVec = apVec;
91  mIt = apVec->begin();
92  }
93 
94  bool HasNext() {
95  return mIt != mpVec->end();
96  }
97 
98  T &Next() {
99  T &val = *mIt;
100  mIt++;
101  return val;
102  }
103 
104  T &PeekNext() {
105  return *mIt;
106  }
107 
108  void Erase() {
109  if (mIt != mpVec->end())
110  mIt = mpVec->erase(mIt);
111  }
112 
113 private:
114  Common::Array<T> *mpVec;
115  typename Common::Array<T>::iterator mIt;
116 };
117 
119 
120 template<class T>
121 class cContainerVec : public iContainer {
122 private:
123  void AddVoidPtr(void **apPtr) {
124  mvVector.push_back(*((T *)apPtr));
125  }
126  void AddVoidClass(void *apClass) {
127  mvVector.push_back(*((T *)apClass));
128  }
129  iContainerIterator *CreateIteratorPtr() {
130  return hplNew(cContainerVecIterator<T>, (&mvVector));
131  }
132 
133 public:
134  cContainerVec() {}
135 
137  size_t Size() {
138  return mvVector.size();
139  }
140 
141  void Clear() {
142  mvVector.clear();
143  }
144 
146 
147  void Reserve(size_t alSize) {
148  mvVector.reserve(alSize);
149  }
150 
151  void Resize(size_t alSize) {
152  mvVector.resize(alSize);
153  }
154 
155  void Add(T aVal) {
156  mvVector.push_back(aVal);
157  }
158 
160 
161  cContainerVecIterator<T> GetIterator() {
162  return cContainerVecIterator<T>(&mvVector);
163  }
164 
166 
167  T &operator[](size_t alX) {
168  return mvVector[alX];
169  }
170 
172 
173  Common::Array<T> mvVector;
174 };
175 
176 //---------------------------------
177 
178 template<class T>
180  void *NextPtr() {
181  return &Next();
182  }
183 
184 public:
186  mpVec = apVec;
187  mIt = apVec->begin();
188  }
189 
190  virtual ~cContainerListIterator() {}
191 
192  bool HasNext() {
193  return mIt != mpVec->end();
194  }
195 
196  T &Next() {
197  T &val = *mIt;
198  mIt++;
199  return val;
200  }
201 
202  T &PeekNext() {
203  return *mIt;
204  }
205 
206  void Erase() {
207  if (mIt != mpVec->end())
208  mIt = mpVec->erase(mIt);
209  }
210 
211 private:
212  Common::List<T> *mpVec;
213  typename Common::List<T>::iterator mIt;
214 };
215 
217 
218 template<class T>
219 class cContainerList : public iContainer {
220 private:
221  void AddVoidPtr(void **apPtr) {
222  mvVector.push_back(*((T *)apPtr));
223  }
224  void AddVoidClass(void *apClass) {
225  mvVector.push_back(*((T *)apClass));
226  }
227  iContainerIterator *CreateIteratorPtr() {
228  return hplNew(cContainerListIterator<T>, (&mvVector));
229  }
230 
231 public:
232  cContainerList() {}
233  virtual ~cContainerList() {}
234 
236  size_t Size() {
237  return mvVector.size();
238  }
239 
240  void Clear() {
241  mvVector.clear();
242  }
244 
245  void Add(T aVal) {
246  mvVector.push_back(aVal);
247  }
248 
250 
251  cContainerListIterator<T> GetIterator() {
252  return cContainerListIterator<T>(&mvVector);
253  }
254 
256 
257  Common::List<T> mvVector;
258 };
259 
260 //---------------------------------
261 
262 } // namespace hpl
263 
264 #endif // HPL_CONTAINER_H
Definition: AI.h:36
Definition: Container.h:40
Definition: array.h:52
Definition: Container.h:54
iterator begin()
Definition: array.h:374
T * iterator
Definition: array.h:54
Definition: SerializeClass.h:284
iterator begin()
Definition: list.h:227
Definition: Container.h:121
Definition: Container.h:71
Definition: Container.h:219
Definition: list_intern.h:51
Definition: Container.h:83
Definition: Container.h:179