ScummVM API documentation
qd_resource_dispatcher.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_RESOURCE_DISPATCHER_H
23#define QDENGINE_QDCORE_QD_RESOURCE_DISPATCHER_H
24
25#include "qdengine/qdcore/qd_resource.h"
26
27
28namespace QDEngine {
29
31template<class T>
33public:
35 virtual ~qdResourceDispatcher() {}
36
38 bool register_resource(qdResource *res, const T *res_owner) {
39 qdResourceHandle<T> hres(res, res_owner);
41
42 if (it != _handles.end()) return false;
43 _handles.push_back(hres);
44
45 return true;
46 }
47
49 bool unregister_resource(qdResource *res, const T *res_owner) {
50 qdResourceHandle<T> hres(res, res_owner);
52
53 if (it != _handles.end()) {
54 _handles.erase(it);
55 return true;
56 }
57
58 return false;
59 }
60
62 bool is_registered(const qdResource *res, const T *res_owner = NULL) const {
63 if (res_owner) {
64 qdResourceHandle<T> hres(const_cast<qdResource *>(res), res_owner);
66 return (it != _handles.end());
67 } else {
69 return (it != _handles.end());
70 }
71 }
72
73 const T *find_owner(const qdResource *res) const {
75 if (_handles.end() == it) return NULL;
76 return (*it).resource_owner();
77 }
78
80 void load_resources(const T *owner = NULL) const {
81 if (owner) {
82 for (typename handle_container_t::const_iterator it = _handles.begin(); it != _handles.end(); ++it) {
83 if (it->resource_owner() == owner)
84 it->load_resource();
85 }
86 } else {
87 for (typename handle_container_t::const_iterator it = _handles.begin(); it != _handles.end(); ++it)
88 it->load_resource();
89 }
90 }
91
93 void release_resources(const T *owner = NULL, const T *hold_owner = NULL) const {
94 if (owner) {
95 for (typename handle_container_t::const_iterator it = _handles.begin(); it != _handles.end(); ++it) {
96 if (it->resource_owner() == owner && (!hold_owner || !is_registered(it->resource(), hold_owner)))
97 it->release_resource();
98 }
99 } else {
100 if (hold_owner) {
101 for (typename handle_container_t::const_iterator it = _handles.begin(); it != _handles.end(); ++it) {
102 if (it->resource_owner() != hold_owner)
103 it->release_resource();
104 }
105 } else {
106 for (typename handle_container_t::const_iterator it = _handles.begin(); it != _handles.end(); ++it)
107 it->release_resource();
108 }
109 }
110 }
111
113 bool load_resource(qdResource *res, const T *res_owner) {
114 qdResourceHandle<T> hres(res, res_owner);
115 register_resource(res, res_owner);
116 return hres.load_resource();
117 }
118
120 bool release_resource(qdResource *res, const T *res_owner) {
121 unregister_resource(res, res_owner);
122 if (!is_registered(res)) {
123 qdResourceHandle<T> hres(res, res_owner);
124 return hres.release_resource();
125 }
126
127 return false;
128 }
129
130protected:
131
133 template<class U>
135 public:
136 qdResourceHandle(qdResource *res, const U *res_owner) : _resource(res), _resource_owner(res_owner) {}
137 qdResourceHandle(const qdResourceHandle<U> &h) : _resource(h._resource), _resource_owner(h._resource_owner) {}
139
140 qdResourceHandle<U> &operator = (const qdResourceHandle<U> &h) {
141 if (this == &h) return *this;
142 _resource = h._resource;
143 _resource_owner = h._resource_owner;
144 return *this;
145 }
146
147 bool operator == (const qdResource &res) const {
148 return (_resource == &res);
149 }
150 bool operator == (const qdResourceHandle<U> &h) const {
151 return (_resource == h._resource && _resource_owner == h._resource_owner);
152 }
153
156 return _resource;
157 }
159 const U *resource_owner() const {
160 return _resource_owner;
161 }
162
164 bool load_resource() const {
165 if (!_resource->is_resource_loaded())
166 return _resource->load_resource();
167 return true;
168 }
170 bool release_resource() const {
171 if (_resource->is_resource_loaded())
172 return _resource->free_resource();
173 return true;
174 }
175
176 private:
177
179 mutable qdResource *_resource;
181 const T *_resource_owner;
182 };
183
184 typedef Std::list< qdResourceHandle<T> > handle_container_t;
185
188};
189
190} // namespace QDEngine
191
192#endif // QDENGINE_QDCORE_QD_RESOURCE_DISPATCHER_H
iterator begin()
Definition: list.h:266
ListInternal::ConstIterator< qdResourceHandle< T > > const_iterator
Definition: list.h:53
iterator erase(iterator pos)
Definition: list.h:112
void push_back(const t_T &element)
Definition: list.h:174
iterator end()
Definition: list.h:279
Хэндл для управления ресурсами.
Definition: qd_resource_dispatcher.h:134
qdResource * resource() const
Возвращает указатель на ресурс.
Definition: qd_resource_dispatcher.h:155
bool release_resource() const
Выгружает ресурс из памяти.
Definition: qd_resource_dispatcher.h:170
const U * resource_owner() const
Возвращает указатель на владельца ресурса.
Definition: qd_resource_dispatcher.h:159
bool load_resource() const
Загружает ресурс в память.
Definition: qd_resource_dispatcher.h:164
Диспетчер ресурсов.
Definition: qd_resource_dispatcher.h:32
bool unregister_resource(qdResource *res, const T *res_owner)
Отмена регистрации ресурса.
Definition: qd_resource_dispatcher.h:49
void release_resources(const T *owner=NULL, const T *hold_owner=NULL) const
Выгружает из памяти данные ресурсов.
Definition: qd_resource_dispatcher.h:93
handle_container_t _handles
Хэндлы ресурсов.
Definition: qd_resource_dispatcher.h:187
void load_resources(const T *owner=NULL) const
Загружает в память данные для ресурсов.
Definition: qd_resource_dispatcher.h:80
bool is_registered(const qdResource *res, const T *res_owner=NULL) const
Возвращает true, если ресурс res (опционально - с владельцем res_owner) есть в списке.
Definition: qd_resource_dispatcher.h:62
bool register_resource(qdResource *res, const T *res_owner)
Регистрация ресурса.
Definition: qd_resource_dispatcher.h:38
bool load_resource(qdResource *res, const T *res_owner)
Загружает в память данные ресурса, если они еще не загружены.
Definition: qd_resource_dispatcher.h:113
bool release_resource(qdResource *res, const T *res_owner)
Выгружает из памяти данные ресурса, если на него нет больше ссылок.
Definition: qd_resource_dispatcher.h:120
Definition: qd_resource.h:34
bool is_resource_loaded() const
Возвращает true, если данные ресурса загружены в память.
Definition: qd_resource.h:68
virtual bool free_resource()=0
Выгружает из памяти данные ресурса.
virtual bool load_resource()=0
Загружает в память данные ресурса.
Definition: list.h:39
In find(In first, In last, const T &v)
Definition: algorithm.h:225
Базовый класс для игровых ресурсов.
Definition: console.h:28
Definition: list_intern.h:108
Definition: list_intern.h:54