Crazy Eddies GUI System  0.7.1
CEGUINamedXMLResourceManager.h
1 /***********************************************************************
2  filename: CEGUINamedXMLResourceManager.h
3  created: Fri Jul 17 2009
4  author: Paul D Turner <paul@cegui.org.uk>
5 *************************************************************************/
6 /***************************************************************************
7  * Copyright (C) 2004 - 2009 Paul D Turner & The CEGUI Development Team
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining
10  * a copy of this software and associated documentation files (the
11  * "Software"), to deal in the Software without restriction, including
12  * without limitation the rights to use, copy, modify, merge, publish,
13  * distribute, sublicense, and/or sell copies of the Software, and to
14  * permit persons to whom the Software is furnished to do so, subject to
15  * the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
24  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
25  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26  * OTHER DEALINGS IN THE SOFTWARE.
27  ***************************************************************************/
28 #ifndef _CEGUINamedXMLResourceManager_h_
29 #define _CEGUINamedXMLResourceManager_h_
30 
31 #include "CEGUIEventSet.h"
32 #include "CEGUIString.h"
33 #include "CEGUIExceptions.h"
34 #include "CEGUILogger.h"
35 #include "CEGUIInputEvent.h"
36 #include "CEGUISystem.h"
37 #include <map>
38 
39 // Start of CEGUI namespace section
40 namespace CEGUI
41 {
44 {
51 };
52 
53 //----------------------------------------------------------------------------//
54 
56 class CEGUIEXPORT ResourceEventSet : public EventSet
57 {
58 public:
60  static const String EventNamespace;
67 };
68 
69 //----------------------------------------------------------------------------//
70 
93 template<typename T, typename U>
95 {
96 public:
106  NamedXMLResourceManager(const String& resource_type);
107 
109  virtual ~NamedXMLResourceManager();
110 
133  T& create(const String& xml_filename, const String& resource_group = "",
135 
144  void destroy(const String& object_name);
145 
155  void destroy(const T& object);
156 
158  void destroyAll();
159 
170  T& get(const String& object_name) const;
171 
173  bool isDefined(const String& object_name) const;
174 
176  void createAll(const String& pattern, const String& resource_group);
177 
178 protected:
180  typedef std::map<String, T*, String::FastLessCompare> ObjectRegistry;
182  void destroyObject(typename ObjectRegistry::iterator ob);
184  T& doExistingObjectAction(const String object_name, T* object,
185  const XMLResourceExistsAction action);
187  virtual void doPostObjectAdditionAction(T& object);
192 };
193 
194 //----------------------------------------------------------------------------//
195 template<typename T, typename U>
197  const String& resource_type) :
198  d_resourceType(resource_type)
199 {
200 }
201 
202 //----------------------------------------------------------------------------//
203 template<typename T, typename U>
205 {
206 }
207 
208 //----------------------------------------------------------------------------//
209 template<typename T, typename U>
211  const String& resource_group,
213 {
214  U xml_loader(xml_filename, resource_group);
215  return doExistingObjectAction(xml_loader.getObjectName(),
216  &xml_loader.getObject(), action);
217 }
218 
219 //----------------------------------------------------------------------------//
220 template<typename T, typename U>
222 {
223  typename ObjectRegistry::iterator i(d_objects.find(object_name));
224 
225  // exit if no such object.
226  if (i == d_objects.end())
227  return;
228 
229  destroyObject(i);
230 }
231 
232 //----------------------------------------------------------------------------//
233 template<typename T, typename U>
235 {
236  // don't want to force a 'getName' function on T here, so we'll look for the
237  // object the hard way.
238  typename ObjectRegistry::iterator i(d_objects.begin());
239  for (; i != d_objects.end(); ++i)
240  if (i->second == &object)
241  {
242  destroyObject(i);
243  return;
244  }
245 }
246 
247 //----------------------------------------------------------------------------//
248 template<typename T, typename U>
250 {
251  while (!d_objects.empty())
252  destroyObject(d_objects.begin());
253 }
254 
255 //----------------------------------------------------------------------------//
256 template<typename T, typename U>
257 T& NamedXMLResourceManager<T, U>::get(const String& object_name) const
258 {
259  typename ObjectRegistry::const_iterator i(d_objects.find(object_name));
260 
261  if (i == d_objects.end())
262  throw UnknownObjectException("NamedXMLResourceManager::get: "
263  "No object of type '" + d_resourceType + "' named '" + object_name +
264  "' is present in the collection.");
265 
266  return *i->second;
267 }
268 
269 //----------------------------------------------------------------------------//
270 template<typename T, typename U>
271 bool NamedXMLResourceManager<T, U>::isDefined(const String& object_name) const
272 {
273  return d_objects.find(object_name) != d_objects.end();
274 }
275 
276 //----------------------------------------------------------------------------//
277 template<typename T, typename U>
279  typename ObjectRegistry::iterator ob)
280 {
281  char addr_buff[32];
282  sprintf(addr_buff, "(%p)", static_cast<void*>(ob->second));
283  Logger::getSingleton().logEvent("Object of type '" + d_resourceType +
284  "' named '" + ob->first + "' has been destroyed. " +
285  addr_buff, Informative);
286 
287  // Set up event args for event notification
288  ResourceEventArgs args(d_resourceType, ob->first);
289 
290  delete ob->second;
291  d_objects.erase(ob);
292 
293  // fire event signalling an object has been destroyed
294  fireEvent(EventResourceDestroyed, args, EventNamespace);
295 }
296 
297 //----------------------------------------------------------------------------//
298 template<typename T, typename U>
300  const String object_name,
301  T* object,
302  const XMLResourceExistsAction action)
303 {
304  String event_name;
305 
306  if (isDefined(object_name))
307  {
308  switch (action)
309  {
310  case XREA_RETURN:
311  Logger::getSingleton().logEvent("---- Returning existing instance "
312  "of " + d_resourceType + " named '" + object_name + "'.");
313  // delete any new object we already had created
314  delete object;
315  // return existing instance of object.
316  return *d_objects[object_name];
317 
318  case XREA_REPLACE:
319  Logger::getSingleton().logEvent("---- Replacing existing instance "
320  "of " + d_resourceType + " named '" + object_name +
321  "' (DANGER!).");
322  destroy(object_name);
323  event_name = EventResourceReplaced;
324  break;
325 
326  case XREA_THROW:
327  delete object;
329  "NamedXMLResourceManager::checkExistingObjectAction: "
330  "an object of type '" + d_resourceType + "' named '" +
331  object_name + "' already exists in the collection.");
332 
333  default:
334  delete object;
336  "NamedXMLResourceManager::checkExistingObjectAction: "
337  "Invalid CEGUI::XMLResourceExistsAction was specified.");
338  }
339  }
340  else
341  event_name = EventResourceCreated;
342 
343  d_objects[object_name] = object;
344  doPostObjectAdditionAction(*object);
345 
346  // fire event about this resource change
347  ResourceEventArgs args(d_resourceType, object_name);
348  fireEvent(event_name, args, EventNamespace);
349 
350  return *object;
351 }
352 
353 //----------------------------------------------------------------------------//
354 template<typename T, typename U>
356 {
357  // do nothing by default.
358 }
359 
360 //----------------------------------------------------------------------------//
361 template<typename T, typename U>
363  const String& resource_group)
364 {
365  std::vector<String> names;
366  const size_t num = System::getSingleton().getResourceProvider()->
367  getResourceGroupFileNames(names, pattern, resource_group);
368 
369  for (size_t i = 0; i < num; ++i)
370  create(names[i], resource_group);
371 }
372 
373 //----------------------------------------------------------------------------//
374 
375 } // End of CEGUI namespace section
376 
377 #endif // end of guard _CEGUINamedXMLResourceManager_h_