ScummVM API documentation
qd_object_map_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 #ifndef QDENGINE_QDCORE_QD_OBJECT_MAP_CONTAINER_H
23 #define QDENGINE_QDCORE_QD_OBJECT_MAP_CONTAINER_H
24 
25 #include "common/system.h"
26 
27 #include "qdengine/qdengine.h"
28 
29 
30 namespace QDEngine {
31 
32 template <class T>
34 public:
36 
39 
40  const object_list_t &get_list() const {
41  return _object_list;
42  }
43 
44  T *get_object(const char *name);
45  const T *get_object(const char *name) const;
46 
47  bool add_object(T *p);
48  bool remove_object(T *p);
49  bool rename_object(T *p, const char *name);
50  bool remove_object(const char *name);
51  bool is_in_list(const char *name) const {
52  return (get_object(name) != 0);
53  }
54  bool is_in_list(const T *p) const {
55  return (get_object(p->name()) != 0);
56  }
57  bool clear();
58 
59 private:
60 
62 
63  object_list_t _object_list;
64  object_map_t _object_map;
65 };
66 
67 template <class T>
69  typename object_map_t::iterator it = _object_map.find(p->name());
70  if (it != _object_map.end())
71  return false;
72 
73  _object_map[p->name()] = p;
74  _object_list.push_back(p);
75 
76  return true;
77 }
78 
79 template <class T>
80 const T *qdObjectMapContainer<T>::get_object(const char *name) const {
81 
82  typename object_map_t::const_iterator it = _object_map.find(name);
83  if (it != _object_map.end())
84  return it->_value;
85 
86  return NULL;
87 }
88 
89 template <class T>
90 T *qdObjectMapContainer<T>::get_object(const char *name) {
91  typename object_map_t::iterator it = _object_map.find(name);
92  if (it != _object_map.end())
93  return it->_value;
94 
95  return NULL;
96 }
97 
98 template <class T>
100  typename object_list_t::iterator it;
101  for (it = _object_list.begin(); it != _object_list.end(); it++) {
102  if (*it == p) {
103  _object_list.erase(it);
104  typename object_map_t::iterator im = _object_map.find(p->name());
105  if (im != _object_map.end())
106  _object_map.erase(im);
107 
108  return true;
109  }
110  }
111 
112  return false;
113 }
114 
115 template <class T>
116 bool qdObjectMapContainer<T>::remove_object(const char *name) {
117  T *p = get_object(name);
118  if (!p) return false;
119 
120  return remove_object(p);
121 }
122 
123 template <class T>
124 bool qdObjectMapContainer<T>::rename_object(T *p, const char *name) {
125  typename object_map_t::iterator im = _object_map.find(p->name());
126  if (im != _object_map.end()) {
127  _object_map.erase(im);
128  p->set_name(name);
129  _object_map[p->name()] = p;
130 
131  return true;
132  }
133  return false;
134 }
135 
136 template <class T>
138 }
139 
140 template <class T>
142  clear();
143 }
144 
145 template <class T>
147  _object_map.clear();
148 
149  for (typename object_list_t::iterator it = _object_list.begin(); it != _object_list.end(); ++it)
150  delete *it;
151 
152  _object_list.clear();
153 
154  return true;
155 }
156 
157 } // namespace QDEngine
158 
159 #endif // QDENGINE_QDCORE_QD_OBJECT_MAP_CONTAINER_H
void clear(bool shrinkArray=0)
Definition: hashmap.h:427
iterator end()
Definition: list.h:240
Базовый класс для игровых ресурсов.
Definition: console.h:28
iterator begin()
Definition: list.h:227
void clear()
Definition: list.h:206
void erase(iterator entry)
Definition: hashmap.h:710
Definition: list_intern.h:51
iterator erase(iterator pos)
Definition: list.h:95
void push_back(const t_T &element)
Definition: list.h:140
Definition: qd_object_map_container.h:33