Crazy Eddies GUI System  0.7.2
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;
82 };
83 
84 //----------------------------------------------------------------------------//
85 
108 template<typename T, typename U>
110 {
111 public:
121  NamedXMLResourceManager(const String& resource_type);
122 
124  virtual ~NamedXMLResourceManager();
125 
148  T& create(const String& xml_filename, const String& resource_group = "",
150 
159  void destroy(const String& object_name);
160 
170  void destroy(const T& object);
171 
173  void destroyAll();
174 
185  T& get(const String& object_name) const;
186 
188  bool isDefined(const String& object_name) const;
189 
191  void createAll(const String& pattern, const String& resource_group);
192 
193 protected:
195  typedef std::map<String, T*, String::FastLessCompare> ObjectRegistry;
197  void destroyObject(typename ObjectRegistry::iterator ob);
199  T& doExistingObjectAction(const String object_name, T* object,
200  const XMLResourceExistsAction action);
202  virtual void doPostObjectAdditionAction(T& object);
207 };
208 
209 //----------------------------------------------------------------------------//
210 template<typename T, typename U>
212  const String& resource_type) :
213  d_resourceType(resource_type)
214 {
215 }
216 
217 //----------------------------------------------------------------------------//
218 template<typename T, typename U>
220 {
221 }
222 
223 //----------------------------------------------------------------------------//
224 template<typename T, typename U>
226  const String& resource_group,
228 {
229  U xml_loader(xml_filename, resource_group);
230  return doExistingObjectAction(xml_loader.getObjectName(),
231  &xml_loader.getObject(), action);
232 }
233 
234 //----------------------------------------------------------------------------//
235 template<typename T, typename U>
237 {
238  typename ObjectRegistry::iterator i(d_objects.find(object_name));
239 
240  // exit if no such object.
241  if (i == d_objects.end())
242  return;
243 
244  destroyObject(i);
245 }
246 
247 //----------------------------------------------------------------------------//
248 template<typename T, typename U>
250 {
251  // don't want to force a 'getName' function on T here, so we'll look for the
252  // object the hard way.
253  typename ObjectRegistry::iterator i(d_objects.begin());
254  for (; i != d_objects.end(); ++i)
255  if (i->second == &object)
256  {
257  destroyObject(i);
258  return;
259  }
260 }
261 
262 //----------------------------------------------------------------------------//
263 template<typename T, typename U>
265 {
266  while (!d_objects.empty())
267  destroyObject(d_objects.begin());
268 }
269 
270 //----------------------------------------------------------------------------//
271 template<typename T, typename U>
272 T& NamedXMLResourceManager<T, U>::get(const String& object_name) const
273 {
274  typename ObjectRegistry::const_iterator i(d_objects.find(object_name));
275 
276  if (i == d_objects.end())
277  CEGUI_THROW(UnknownObjectException("NamedXMLResourceManager::get: "
278  "No object of type '" + d_resourceType + "' named '" + object_name +
279  "' is present in the collection."));
280 
281  return *i->second;
282 }
283 
284 //----------------------------------------------------------------------------//
285 template<typename T, typename U>
286 bool NamedXMLResourceManager<T, U>::isDefined(const String& object_name) const
287 {
288  return d_objects.find(object_name) != d_objects.end();
289 }
290 
291 //----------------------------------------------------------------------------//
292 template<typename T, typename U>
294  typename ObjectRegistry::iterator ob)
295 {
296  char addr_buff[32];
297  sprintf(addr_buff, "(%p)", static_cast<void*>(ob->second));
298  Logger::getSingleton().logEvent("Object of type '" + d_resourceType +
299  "' named '" + ob->first + "' has been destroyed. " +
300  addr_buff, Informative);
301 
302  // Set up event args for event notification
303  ResourceEventArgs args(d_resourceType, ob->first);
304 
305  delete ob->second;
306  d_objects.erase(ob);
307 
308  // fire event signalling an object has been destroyed
309  fireEvent(EventResourceDestroyed, args, EventNamespace);
310 }
311 
312 //----------------------------------------------------------------------------//
313 template<typename T, typename U>
315  const String object_name,
316  T* object,
317  const XMLResourceExistsAction action)
318 {
319  String event_name;
320 
321  if (isDefined(object_name))
322  {
323  switch (action)
324  {
325  case XREA_RETURN:
326  Logger::getSingleton().logEvent("---- Returning existing instance "
327  "of " + d_resourceType + " named '" + object_name + "'.");
328  // delete any new object we already had created
329  delete object;
330  // return existing instance of object.
331  return *d_objects[object_name];
332 
333  case XREA_REPLACE:
334  Logger::getSingleton().logEvent("---- Replacing existing instance "
335  "of " + d_resourceType + " named '" + object_name +
336  "' (DANGER!).");
337  destroy(object_name);
338  event_name = EventResourceReplaced;
339  break;
340 
341  case XREA_THROW:
342  delete object;
343  CEGUI_THROW(AlreadyExistsException(
344  "NamedXMLResourceManager::checkExistingObjectAction: "
345  "an object of type '" + d_resourceType + "' named '" +
346  object_name + "' already exists in the collection."));
347 
348  default:
349  delete object;
350  CEGUI_THROW(InvalidRequestException(
351  "NamedXMLResourceManager::checkExistingObjectAction: "
352  "Invalid CEGUI::XMLResourceExistsAction was specified."));
353  }
354  }
355  else
356  event_name = EventResourceCreated;
357 
358  d_objects[object_name] = object;
359  doPostObjectAdditionAction(*object);
360 
361  // fire event about this resource change
362  ResourceEventArgs args(d_resourceType, object_name);
363  fireEvent(event_name, args, EventNamespace);
364 
365  return *object;
366 }
367 
368 //----------------------------------------------------------------------------//
369 template<typename T, typename U>
371 {
372  // do nothing by default.
373 }
374 
375 //----------------------------------------------------------------------------//
376 template<typename T, typename U>
378  const String& resource_group)
379 {
380  std::vector<String> names;
381  const size_t num = System::getSingleton().getResourceProvider()->
382  getResourceGroupFileNames(names, pattern, resource_group);
383 
384  for (size_t i = 0; i < num; ++i)
385  create(names[i], resource_group);
386 }
387 
388 //----------------------------------------------------------------------------//
389 
390 } // End of CEGUI namespace section
391 
392 #endif // end of guard _CEGUINamedXMLResourceManager_h_