OpenMW
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
store.hpp
Go to the documentation of this file.
1 #ifndef OPENMW_MWWORLD_STORE_H
2 #define OPENMW_MWWORLD_STORE_H
3 
4 #include <string>
5 #include <vector>
6 #include <map>
7 
8 #include "recordcmp.hpp"
9 
10 namespace ESM
11 {
12  struct Land;
13 }
14 
15 namespace Loading
16 {
17  class Listener;
18 }
19 
20 namespace MWWorld
21 {
22  struct RecordId
23  {
24  std::string mId;
25  bool mIsDeleted;
26 
27  RecordId(const std::string &id = "", bool isDeleted = false);
28  };
29 
30  class StoreBase
31  {
32  public:
33  virtual ~StoreBase() {}
34 
35  virtual void setUp() {}
36 
38  virtual void listIdentifier(std::vector<std::string> &list) const {}
39 
40  virtual size_t getSize() const = 0;
41  virtual int getDynamicSize() const { return 0; }
42  virtual RecordId load(ESM::ESMReader &esm) = 0;
43 
44  virtual bool eraseStatic(const std::string &id) {return false;}
45  virtual void clearDynamic() {}
46 
47  virtual void write (ESM::ESMWriter& writer, Loading::Listener& progress) const {}
48 
49  virtual RecordId read (ESM::ESMReader& reader) { return RecordId(); }
51  };
52 
53  template <class T>
55  {
56  protected:
57  typedef typename std::map<int, T> Static;
59 
60  public:
61  typedef typename std::map<int, T>::const_iterator iterator;
62 
63  IndexedStore();
64 
65  iterator begin() const;
66  iterator end() const;
67 
68  void load(ESM::ESMReader &esm);
69 
70  int getSize() const;
71  void setUp();
72 
73  const T *search(int index) const;
74  const T *find(int index) const;
75  };
76 
77  template <class T>
79  {
80  typedef typename std::vector<T *>::const_iterator Iter;
81 
83 
84  public:
86 
88  : mIter(orig.mIter)
89  {}
90 
91  SharedIterator(const Iter &iter)
92  : mIter(iter)
93  {}
94 
96  ++mIter;
97  return *this;
98  }
99 
101  SharedIterator iter = *this;
102  ++mIter;
103 
104  return iter;
105  }
106 
108  --mIter;
109  return *this;
110  }
111 
113  SharedIterator iter = *this;
114  --mIter;
115 
116  return iter;
117  }
118 
119  bool operator==(const SharedIterator &x) const {
120  return mIter == x.mIter;
121  }
122 
123  bool operator!=(const SharedIterator &x) const {
124  return !(*this == x);
125  }
126 
127  const T &operator*() const {
128  return **mIter;
129  }
130 
131  const T *operator->() const {
132  return &(**mIter);
133  }
134  };
135 
136  class ESMStore;
137 
138  template <class T>
139  class Store : public StoreBase
140  {
141  std::map<std::string, T> mStatic;
142  std::vector<T *> mShared; // Preserves the record order as it came from the content files (this
143  // is relevant for the spell autocalc code and selection order
144  // for heads/hairs in the character creation)
145  std::map<std::string, T> mDynamic;
146 
147  typedef std::map<std::string, T> Dynamic;
148  typedef std::map<std::string, T> Static;
149 
150  friend class ESMStore;
151 
152  public:
153  Store();
154  Store(const Store<T> &orig);
155 
157 
158  // setUp needs to be called again after
159  virtual void clearDynamic();
160  void setUp();
161 
162  const T *search(const std::string &id) const;
163 
167  bool isDynamic(const std::string &id) const;
168 
170  const T *searchRandom(const std::string &id) const;
171 
172  const T *find(const std::string &id) const;
173 
176  const T *findRandom(const std::string &id) const;
177 
178  iterator begin() const;
179  iterator end() const;
180 
181  size_t getSize() const;
182  int getDynamicSize() const;
183 
185  void listIdentifier(std::vector<std::string> &list) const;
186 
187  T *insert(const T &item);
188  T *insertStatic(const T &item);
189 
190  bool eraseStatic(const std::string &id);
191  bool erase(const std::string &id);
192  bool erase(const T &item);
193 
195  void write(ESM::ESMWriter& writer, Loading::Listener& progress) const;
196  RecordId read(ESM::ESMReader& reader);
197  };
198 
199  template <>
200  class Store<ESM::LandTexture> : public StoreBase
201  {
202  // For multiple ESM/ESP files we need one list per file.
203  typedef std::vector<ESM::LandTexture> LandTextureList;
204  std::vector<LandTextureList> mStatic;
205 
206  public:
207  Store();
208 
209  typedef std::vector<ESM::LandTexture>::const_iterator iterator;
210 
211  // Must be threadsafe! Called from terrain background loading threads.
212  // Not a big deal here, since ESM::LandTexture can never be modified or inserted/erased
213  const ESM::LandTexture *search(size_t index, size_t plugin) const;
214  const ESM::LandTexture *find(size_t index, size_t plugin) const;
215 
217  void resize(size_t num);
218 
219  size_t getSize() const;
220  size_t getSize(size_t plugin) const;
221 
222  RecordId load(ESM::ESMReader &esm, size_t plugin);
224 
225  iterator begin(size_t plugin) const;
226  iterator end(size_t plugin) const;
227  };
228 
229  template <>
230  class Store<ESM::Land> : public StoreBase
231  {
232  std::vector<ESM::Land *> mStatic;
233 
234  public:
236 
237  virtual ~Store();
238 
239  size_t getSize() const;
240  iterator begin() const;
241  iterator end() const;
242 
243  // Must be threadsafe! Called from terrain background loading threads.
244  // Not a big deal here, since ESM::Land can never be modified or inserted/erased
245  ESM::Land *search(int x, int y) const;
246  ESM::Land *find(int x, int y) const;
247 
249  void setUp();
250  };
251 
252  template <>
253  class Store<ESM::Cell> : public StoreBase
254  {
255  struct DynamicExtCmp
256  {
257  bool operator()(const std::pair<int, int> &left, const std::pair<int, int> &right) const {
258  if (left.first == right.first && left.second == right.second)
259  return false;
260 
261  if (left.first == right.first)
262  return left.second > right.second;
263 
264  // Exterior cells are listed in descending, row-major order,
265  // this is a workaround for an ambiguous chargen_plank reference in the vanilla game.
266  // there is one at -22,16 and one at -2,-9, the latter should be used.
267  return left.first > right.first;
268  }
269  };
270 
271  typedef std::map<std::string, ESM::Cell> DynamicInt;
272  typedef std::map<std::pair<int, int>, ESM::Cell, DynamicExtCmp> DynamicExt;
273 
276 
277  std::vector<ESM::Cell *> mSharedInt;
278  std::vector<ESM::Cell *> mSharedExt;
279 
282 
283  const ESM::Cell *search(const ESM::Cell &cell) const;
284  void handleMovedCellRefs(ESM::ESMReader& esm, ESM::Cell* cell);
285 
286  public:
288 
289  const ESM::Cell *search(const std::string &id) const;
290  const ESM::Cell *search(int x, int y) const;
291  const ESM::Cell *searchOrCreate(int x, int y);
292 
293  const ESM::Cell *find(const std::string &id) const;
294  const ESM::Cell *find(int x, int y) const;
295 
296  void setUp();
297 
299 
300  iterator intBegin() const;
301  iterator intEnd() const;
302  iterator extBegin() const;
303  iterator extEnd() const;
304 
305  // Return the northernmost cell in the easternmost column.
306  const ESM::Cell *searchExtByName(const std::string &id) const;
307 
308  // Return the northernmost cell in the easternmost column.
309  const ESM::Cell *searchExtByRegion(const std::string &id) const;
310 
311  size_t getSize() const;
312 
313  void listIdentifier(std::vector<std::string> &list) const;
314 
315  ESM::Cell *insert(const ESM::Cell &cell);
316 
317  bool erase(const ESM::Cell &cell);
318  bool erase(const std::string &id);
319 
320  bool erase(int x, int y);
321  };
322 
323  template <>
324  class Store<ESM::Pathgrid> : public StoreBase
325  {
326  private:
327  typedef std::map<std::string, ESM::Pathgrid> Interior;
328  typedef std::map<std::pair<int, int>, ESM::Pathgrid> Exterior;
329 
332 
334 
335  public:
336 
337  Store();
338 
339  void setCells(Store<ESM::Cell>& cells);
341  size_t getSize() const;
342 
343  void setUp();
344 
345  const ESM::Pathgrid *search(int x, int y) const;
346  const ESM::Pathgrid *search(const std::string& name) const;
347  const ESM::Pathgrid *find(int x, int y) const;
348  const ESM::Pathgrid* find(const std::string& name) const;
349  const ESM::Pathgrid *search(const ESM::Cell &cell) const;
350  const ESM::Pathgrid *find(const ESM::Cell &cell) const;
351  };
352 
353 
354  template <>
355  class Store<ESM::Skill> : public IndexedStore<ESM::Skill>
356  {
357  public:
358  Store();
359  };
360 
361  template <>
362  class Store<ESM::MagicEffect> : public IndexedStore<ESM::MagicEffect>
363  {
364  public:
365  Store();
366  };
367 
368  template <>
369  class Store<ESM::Attribute> : public IndexedStore<ESM::Attribute>
370  {
371  std::vector<ESM::Attribute> mStatic;
372 
373  public:
374  typedef std::vector<ESM::Attribute>::const_iterator iterator;
375 
376  Store();
377 
378  const ESM::Attribute *search(size_t index) const;
379  const ESM::Attribute *find(size_t index) const;
380 
381  void setUp();
382 
383  size_t getSize() const;
384  iterator begin() const;
385  iterator end() const;
386  };
387 
388 
389 } //end namespace
390 
391 #endif
bool erase(const std::string &id)
Definition: store.cpp:295
SharedIterator(const Iter &iter)
Definition: store.hpp:91
const T * find(const std::string &id) const
Definition: store.cpp:165
const T * search(int index) const
Definition: store.cpp:87
SharedIterator & operator++()
Definition: store.hpp:95
Store()
Definition: store.cpp:111
virtual bool eraseStatic(const std::string &id)
Definition: store.hpp:44
const T & operator*() const
Definition: store.hpp:127
virtual void listIdentifier(std::vector< std::string > &list) const
List identifiers of records contained in this Store (case-smashed). No-op for Stores that don't use s...
Definition: store.hpp:38
Definition: esmreader.hpp:21
RecordId(const std::string &id="", bool isDeleted=false)
Definition: store.cpp:46
std::vector< ESM::Attribute >::const_iterator iterator
Definition: store.hpp:374
void num(char i, bool last)
Definition: gen_iconv.cpp:12
virtual size_t getSize() const =0
Definition: store.hpp:30
std::map< std::string, ESM::Pathgrid > Interior
Definition: store.hpp:327
int list(Bsa::BSAFile &bsa, Arguments &info)
Definition: bsatool.cpp:183
std::map< std::string, T > mDynamic
Definition: store.hpp:145
virtual void clearDynamic()
Definition: store.cpp:122
bool operator!=(const SharedIterator &x) const
Definition: store.hpp:123
RecordId load(ESM::ESMReader &esm)
Definition: store.cpp:188
iterator end() const
Definition: store.cpp:60
Definition: store.hpp:139
bool operator()(const std::pair< int, int > &left, const std::pair< int, int > &right) const
Definition: store.hpp:257
Definition: store.hpp:253
std::vector< T * > mShared
Definition: store.hpp:142
const T * search(const std::string &id) const
Definition: store.cpp:131
Definition: loadltex.hpp:28
std::map< int, T >::const_iterator iterator
Definition: store.hpp:61
Definition: loadland.hpp:20
void load(ESM::ESMReader &esm)
Definition: store.cpp:65
std::vector< ESM::Cell * > mSharedExt
Definition: store.hpp:278
Definition: loadinglistener.hpp:8
std::map< std::string, T > mStatic
Definition: store.hpp:141
Definition: store.hpp:78
virtual void clearDynamic()
Definition: store.hpp:45
int getSize() const
Definition: store.cpp:78
DynamicInt mInt
Definition: store.hpp:274
Interior mInt
Definition: store.hpp:330
Definition: esmwriter.hpp:17
RecordId read(ESM::ESMReader &reader)
Read into dynamic storage.
Definition: store.cpp:329
std::map< std::pair< int, int >, ESM::Pathgrid > Exterior
Definition: store.hpp:328
SharedIterator & operator--()
Definition: store.hpp:107
void listIdentifier(std::vector< std::string > &list) const
Definition: store.cpp:232
T * insertStatic(const T &item)
Definition: store.cpp:255
Definition: esmstore.hpp:17
std::string mId
Definition: store.hpp:24
void setUp()
Definition: store.cpp:205
SharedIterator operator++(int)
Definition: store.hpp:100
iterator begin() const
Definition: store.cpp:210
std::map< std::string, ESM::Cell > DynamicInt
Definition: store.hpp:271
iterator end() const
Definition: store.cpp:215
void setUp()
Definition: store.cpp:83
bool eraseStatic(const std::string &id)
Definition: store.cpp:269
bool mIsDeleted
Definition: store.hpp:25
Definition: store.hpp:22
DynamicExt mExt
Definition: store.hpp:275
void write(ESM::ESMWriter &writer, Loading::Listener &progress) const
Definition: store.cpp:318
virtual void setUp()
Definition: store.hpp:35
std::map< std::pair< int, int >, ESM::Cell, DynamicExtCmp > DynamicExt
Definition: store.hpp:272
bool isDynamic(const std::string &id) const
Definition: store.cpp:150
std::vector< ESM::Cell * > mSharedInt
Definition: store.hpp:277
Definition: loadpgrd.hpp:16
Store< ESM::Cell > * mCells
Definition: store.hpp:333
Definition: loadcell.hpp:64
size_t getSize() const
Definition: store.cpp:221
SharedIterator operator--(int)
Definition: store.hpp:112
virtual ~StoreBase()
Definition: store.hpp:33
SharedIterator()
Definition: store.hpp:85
std::vector< ESM::Land * > mStatic
Definition: store.hpp:232
const T * find(int index) const
Definition: store.cpp:95
virtual RecordId read(ESM::ESMReader &reader)
Read into dynamic storage.
Definition: store.hpp:49
Definition: attr.hpp:12
DynamicExt mDynamicExt
Definition: store.hpp:281
DynamicInt mDynamicInt
Definition: store.hpp:280
std::vector< LandTextureList > mStatic
Definition: store.hpp:204
SharedIterator< ESM::Land > iterator
Definition: store.hpp:235
iterator begin() const
Definition: store.cpp:55
virtual void write(ESM::ESMWriter &writer, Loading::Listener &progress) const
Definition: store.hpp:47
Definition: store.hpp:54
std::map< std::string, T > Static
Definition: store.hpp:148
IndexedStore()
Definition: store.cpp:51
bool operator==(const SharedIterator &x) const
Definition: store.hpp:119
T * insert(const T &item)
Definition: store.cpp:241
const T * searchRandom(const std::string &id) const
Definition: store.cpp:156
Exterior mExt
Definition: store.hpp:331
const T * operator->() const
Definition: store.hpp:131
virtual RecordId load(ESM::ESMReader &esm)=0
std::vector< ESM::LandTexture >::const_iterator iterator
Definition: store.hpp:209
virtual int getDynamicSize() const
Definition: store.hpp:41
Iter mIter
Definition: store.hpp:82
int getDynamicSize() const
Definition: store.cpp:227
std::vector< ESM::Attribute > mStatic
Definition: store.hpp:371
Static mStatic
Definition: store.hpp:58
std::map< int, T > Static
Definition: store.hpp:57
SharedIterator< ESM::Cell > iterator
Definition: store.hpp:287
SharedIterator(const SharedIterator &orig)
Definition: store.hpp:87
std::map< std::string, T > Dynamic
Definition: store.hpp:147
std::vector< ESM::LandTexture > LandTextureList
Definition: store.hpp:203
const T * findRandom(const std::string &id) const
Definition: store.cpp:176
std::vector< T * >::const_iterator Iter
Definition: store.hpp:80
const char * name
Definition: crashcatcher.cpp:59
SharedIterator< T > iterator
Definition: store.hpp:156