xoreos  0.0.5
objectcontainer.cpp
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 #include <algorithm>
26 
27 #include "src/common/error.h"
28 
29 #include "src/aurora/types.h"
30 
32 
33 namespace Aurora {
34 
35 namespace NWScript {
36 
38 }
39 
41 }
42 
44  Common::StackLock stackLock(_mutex);
45 
46  _objects.clear();
47  _objectsByID.clear();
48  _objectsByTag.clear();
49 }
50 
52  Common::StackLock stackLock(_mutex);
53 
54  assert(std::find(_objects.begin(), _objects.end(), &object) == _objects.end());
55 
56  _objects.push_back(&object);
57  _objectsByID.insert(std::make_pair(object.getID(), &object));
58  _objectsByTag.insert(std::make_pair(object.getTag(), &object));
59 }
60 
62  Common::StackLock stackLock(_mutex);
63 
64  _objects.remove(&object);
65  _objectsByID.erase(object.getID());
66 
67  std::pair<ObjectTagMap::iterator, ObjectTagMap::iterator> tag =
68  _objectsByTag.equal_range(object.getTag());
69 
70  for (ObjectTagMap::iterator o = tag.first; o != tag.second; ++o) {
71  if (o->second == &object) {
72  _objectsByTag.erase(o);
73  break;
74  }
75  }
76 }
77 
79  ObjectIDMap::const_iterator o = _objectsByID.find(id);
80  if (o != _objectsByID.end())
81  return o->second;
82 
83  return 0;
84 }
85 
87  SearchList ctx(_objects);
88 
89  return ctx.get();
90 }
91 
93  SearchTagMap ctx(_objectsByTag, tag);
94 
95  return ctx.get();
96 }
97 
99  return new SearchList(_objects);
100 }
101 
103  return new SearchTagMap(_objectsByTag, tag);
104 }
105 
107  _mutex.lock();
108 }
109 
111  _mutex.unlock();
112 }
113 
114 } // End of namespace NWScript
115 
116 } // End of namespace Aurora
A class holding an UTF-8 string.
Definition: ustring.h:48
Object * getFirstObject() const
Return the first object.
Basic exceptions to throw.
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 * get()
Return the current object in the search context.
Convenience class that locks a mutex on creation and unlocks it on destruction.
Definition: mutex.h:71
Basic type definitions to handle files used in BioWare&#39;s Aurora engine.
Object * getObjectByID(uint32 id) const
Find a specific object by ID.
uint32_t uint32
Definition: types.h:204
void lock()
Definition: mutex.cpp:41
void unlock()
Definition: mutex.cpp:47
An NWScript object container.
Object * getFirstObjectByTag(const Common::UString &tag) const
Return the first object with this tag.
void addObject(Object &object)
Add an object to this container.