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
30namespace QDEngine {
31
32template <class T>
34public:
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
59private:
60
62
63 object_list_t _object_list;
64 object_map_t _object_map;
65};
66
67template <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
79template <class T>
80const 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
89template <class T>
90T *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
98template <class T>
99bool qdObjectMapContainer<T>::remove_object(T *p) {
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
115template <class T>
116bool 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
123template <class T>
124bool 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
136template <class T>
137qdObjectMapContainer<T>::qdObjectMapContainer() {
138}
139
140template <class T>
141qdObjectMapContainer<T>::~qdObjectMapContainer() {
142 clear();
143}
144
145template <class T>
146bool qdObjectMapContainer<T>::clear() {
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
Definition: qd_object_map_container.h:33
Базовый класс для игровых ресурсов.
Definition: console.h:28