xoreos  0.0.5
objectcontainer.h
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos 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 xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 #ifndef AURORA_NWSCRIPT_OBJECTCONTAINER_H
26 #define AURORA_NWSCRIPT_OBJECTCONTAINER_H
27 
28 #include <list>
29 #include <map>
30 
31 #include "src/common/mutex.h"
32 
34 
35 namespace Aurora {
36 
37 namespace NWScript {
38 
39 class ObjectSearch {
40 public:
42  virtual ~ObjectSearch() { }
43 
45  virtual Object *get() = 0;
47  virtual Object *next() = 0;
48 };
49 
50 template<typename T> class SearchRange : public ObjectSearch {
51 public:
52  typedef T type;
53  typedef typename T::const_iterator iterator;
54  typedef std::pair<iterator, iterator> range;
55 
56  SearchRange(const range &r) : _range(r) { }
58 
59  Object *get() {
60  if (_range.first == _range.second)
61  return 0;
62 
63  return getObject(_range.first);
64  }
65 
66  Object *next() {
67  if (_range.first == _range.second)
68  return 0;
69 
70  Object *object = getObject(_range.first);
71 
72  ++_range.first;
73 
74  return object;
75  }
76 
77 protected:
78  virtual Object *getObject(const iterator &t) = 0;
79 
80 private:
82 };
83 
84 class SearchList : public SearchRange< std::list<Object *> > {
85 public:
86  SearchList(const type &l) : SearchRange<type>(std::make_pair(l.begin(), l.end())) { }
88 
89  Object *getObject(const iterator &t) { return *t; }
90 };
91 
92 class SearchTagMap : public SearchRange< std::multimap<Common::UString, Object *> > {
93 public:
94  SearchTagMap(const type &m, const Common::UString &tag) : SearchRange<type>(m.equal_range(tag)) { }
96 
97  Object *getObject(const iterator &t) { return t->second; };
98 };
99 
101 public:
102  ObjectContainer();
104 
105  void clearObjects();
106 
108  void addObject(Object &object);
110  void removeObject(Object &object);
111 
113  Object *getObjectByID(uint32 id) const;
114 
116  Object *getFirstObject() const;
118  Object *getFirstObjectByTag(const Common::UString &tag) const;
119 
121  ObjectSearch *findObjects() const;
123  ObjectSearch *findObjectsByTag(const Common::UString &tag) const;
124 
125 
126 protected:
127  void lock();
128  void unlock();
129 
130 
131 private:
132  typedef std::map<uint32, Object *> ObjectIDMap;
135 
137 
141 };
142 
143 } // End of namespace NWScript
144 
145 } // End of namespace Aurora
146 
147 #endif // AURORA_NWSCRIPT_OBJECTCONTAINER_H
virtual Object * next()=0
Move to the next object in the search context and return the previous one.
A class holding an UTF-8 string.
Definition: ustring.h:48
std::map< uint32, Object * > ObjectIDMap
virtual Object * getObject(const iterator &t)=0
STL namespace.
Object * getObject(const iterator &t)
Object * getFirstObject() const
Return the first object.
A mutex.
Definition: mutex.h:40
std::pair< iterator, iterator > range
Object * getObject(const iterator &t)
ObjectSearch * findObjectsByTag(const Common::UString &tag) const
Return a search context to iterate over all objects with this tag.
ObjectSearch * findObjects() const
Return a search context to iterate over all objects.
void removeObject(Object &object)
Remove an object from this container.
Object * next()
Move to the next object in the search context and return the previous one.
Thread mutex classes.
Object * getObjectByID(uint32 id) const
Find a specific object by ID.
An NWScript object.
uint32_t uint32
Definition: types.h:204
SearchTagMap(const type &m, const Common::UString &tag)
Object * getFirstObjectByTag(const Common::UString &tag) const
Return the first object with this tag.
void addObject(Object &object)
Add an object to this container.