27 #ifndef _CEGUINamedXMLResourceManager_h_
28 #define _CEGUINamedXMLResourceManager_h_
30 #include "CEGUI/EventSet.h"
31 #include "CEGUI/String.h"
32 #include "CEGUI/Exceptions.h"
33 #include "CEGUI/Logger.h"
34 #include "CEGUI/InputEvent.h"
35 #include "CEGUI/System.h"
55 class CEGUIEXPORT ResourceEventSet :
public EventSet
59 static const String EventNamespace;
66 static const String EventResourceCreated;
73 static const String EventResourceDestroyed;
80 static const String EventResourceReplaced;
107 template<
typename T,
typename U>
108 class NamedXMLResourceManager :
public ResourceEventSet
167 T&
createFromFile(
const String& xml_filename,
const String& resource_group =
"",
198 void destroy(
const String& object_name);
224 T&
get(
const String& object_name)
const;
227 bool isDefined(
const String& object_name)
const;
230 void createAll(
const String& pattern,
const String& resource_group);
235 typedef std::map<String, T*, StringFastLessCompare
251 template<
typename T,
typename U>
253 const String& resource_type) :
254 d_resourceType(resource_type)
259 template<
typename T,
typename U>
260 NamedXMLResourceManager<T, U>::~NamedXMLResourceManager()
265 template<
typename T,
typename U>
266 T& NamedXMLResourceManager<T, U>::createFromContainer(
const RawDataContainer& source,
267 XMLResourceExistsAction action)
271 xml_loader.handleContainer(source);
272 return doExistingObjectAction(xml_loader.getObjectName(),
273 &xml_loader.getObject(), action);
277 template<
typename T,
typename U>
278 T& NamedXMLResourceManager<T, U>::createFromFile(
const String& xml_filename,
279 const String& resource_group,
280 XMLResourceExistsAction action)
284 xml_loader.handleFile(xml_filename, resource_group);
285 return doExistingObjectAction(xml_loader.getObjectName(),
286 &xml_loader.getObject(), action);
290 template<
typename T,
typename U>
291 T& NamedXMLResourceManager<T, U>::createFromString(
const String& source,
292 XMLResourceExistsAction action)
296 xml_loader.handleString(source);
297 return doExistingObjectAction(xml_loader.getObjectName(),
298 &xml_loader.getObject(), action);
302 template<
typename T,
typename U>
303 void NamedXMLResourceManager<T, U>::destroy(
const String& object_name)
305 typename ObjectRegistry::iterator i(d_objects.find(object_name));
308 if (i == d_objects.end())
315 template<
typename T,
typename U>
316 void NamedXMLResourceManager<T, U>::destroy(
const T&
object)
320 typename ObjectRegistry::iterator i(d_objects.begin());
321 for (; i != d_objects.end(); ++i)
322 if (i->second == &
object)
330 template<
typename T,
typename U>
331 void NamedXMLResourceManager<T, U>::destroyAll()
333 while (!d_objects.empty())
334 destroyObject(d_objects.begin());
338 template<
typename T,
typename U>
339 T& NamedXMLResourceManager<T, U>::get(
const String& object_name)
const
341 typename ObjectRegistry::const_iterator i(d_objects.find(object_name));
343 if (i == d_objects.end())
344 CEGUI_THROW(UnknownObjectException(
345 "No object of type '" + d_resourceType +
"' named '" + object_name +
346 "' is present in the collection."));
352 template<
typename T,
typename U>
353 bool NamedXMLResourceManager<T, U>::isDefined(
const String& object_name)
const
355 return d_objects.find(object_name) != d_objects.end();
359 template<
typename T,
typename U>
360 void NamedXMLResourceManager<T, U>::destroyObject(
361 typename ObjectRegistry::iterator ob)
364 sprintf(addr_buff,
"(%p)", static_cast<void*>(ob->second));
365 Logger::getSingleton().logEvent(
"Object of type '" + d_resourceType +
366 "' named '" + ob->first +
"' has been destroyed. " +
367 addr_buff, Informative);
370 ResourceEventArgs args(d_resourceType, ob->first);
372 CEGUI_DELETE_AO ob->second;
376 fireEvent(EventResourceDestroyed, args, EventNamespace);
380 template<
typename T,
typename U>
381 T& NamedXMLResourceManager<T, U>::doExistingObjectAction(
382 const String object_name,
384 const XMLResourceExistsAction action)
388 if (isDefined(object_name))
393 Logger::getSingleton().logEvent(
"---- Returning existing instance "
394 "of " + d_resourceType +
" named '" + object_name +
"'.");
396 CEGUI_DELETE_AO object;
398 return *d_objects[object_name];
401 Logger::getSingleton().logEvent(
"---- Replacing existing instance "
402 "of " + d_resourceType +
" named '" + object_name +
404 destroy(object_name);
405 event_name = EventResourceReplaced;
409 CEGUI_DELETE_AO object;
410 CEGUI_THROW(AlreadyExistsException(
411 "an object of type '" + d_resourceType +
"' named '" +
412 object_name +
"' already exists in the collection."));
415 CEGUI_DELETE_AO object;
416 CEGUI_THROW(InvalidRequestException(
417 "Invalid CEGUI::XMLResourceExistsAction was specified."));
421 event_name = EventResourceCreated;
423 d_objects[object_name] = object;
424 doPostObjectAdditionAction(*
object);
427 ResourceEventArgs args(d_resourceType, object_name);
428 fireEvent(event_name, args, EventNamespace);
434 template<
typename T,
typename U>
435 void NamedXMLResourceManager<T, U>::doPostObjectAdditionAction(T& )
441 template<
typename T,
typename U>
442 void NamedXMLResourceManager<T, U>::createAll(
const String& pattern,
443 const String& resource_group)
445 std::vector<String> names;
446 const size_t num = System::getSingleton().getResourceProvider()->
447 getResourceGroupFileNames(names, pattern, resource_group);
449 for (
size_t i = 0; i < num; ++i)
450 createFromFile(names[i], resource_group);
457 #endif // end of guard _CEGUINamedXMLResourceManager_h_
Main namespace for Crazy Eddie's GUI Library.
Definition: cegui/include/CEGUI/Affector.h:42
void destroyAll()
Destroy all objects.
Definition: cegui/include/CEGUI/NamedXMLResourceManager.h:331
virtual void doPostObjectAdditionAction(T &object)
Function called each time a new object is added to the collection.
Definition: cegui/include/CEGUI/NamedXMLResourceManager.h:435
void createAll(const String &pattern, const String &resource_group)
Create a new T object from files with names matching pattern in resource_group.
Definition: cegui/include/CEGUI/NamedXMLResourceManager.h:442
T & doExistingObjectAction(const String object_name, T *object, const XMLResourceExistsAction action)
function to enforce XMLResourceExistsAction policy.
Definition: cegui/include/CEGUI/NamedXMLResourceManager.h:381
Throw an AlreadyExistsException.
Definition: cegui/include/CEGUI/NamedXMLResourceManager.h:49
void destroyObject(typename ObjectRegistry::iterator ob)
implementation of object destruction.
Definition: cegui/include/CEGUI/NamedXMLResourceManager.h:360
std::map< String, T *, StringFastLessCompare CEGUI_MAP_ALLOC(String, T *)> ObjectRegistry
type of collection used to store and manage objects
Definition: debian/tmp/usr/include/cegui-0.8.7/CEGUI/NamedXMLResourceManager.h:236
NamedXMLResourceManager(const String &resource_type)
Constructor.
Definition: cegui/include/CEGUI/NamedXMLResourceManager.h:252
T & createFromFile(const String &xml_filename, const String &resource_group="", XMLResourceExistsAction action=XREA_RETURN)
Creates a new T object from an XML file and adds it to the collection.
Definition: cegui/include/CEGUI/NamedXMLResourceManager.h:278
void destroy(const String &object_name)
Destroy the object named object_name, or do nothing if such an object does not exist in the collectio...
Definition: cegui/include/CEGUI/NamedXMLResourceManager.h:303
Destroy the existing instance and replace with the newly loaded one.
Definition: cegui/include/CEGUI/NamedXMLResourceManager.h:47
T & createFromString(const String &source, XMLResourceExistsAction action=XREA_RETURN)
Creates a new T object from a string and adds it to the collection.
Definition: cegui/include/CEGUI/NamedXMLResourceManager.h:291
XMLResourceExistsAction
Possible actions when loading an XML resource that already exists.
Definition: cegui/include/CEGUI/NamedXMLResourceManager.h:42
Do not load the resource, return the existing instance.
Definition: cegui/include/CEGUI/NamedXMLResourceManager.h:45
T & createFromContainer(const RawDataContainer &source, XMLResourceExistsAction action=XREA_RETURN)
Creates a new T object from a RawDataContainer and adds it to the collection.
Definition: cegui/include/CEGUI/NamedXMLResourceManager.h:266
virtual ~NamedXMLResourceManager()
Destructor.
Definition: cegui/include/CEGUI/NamedXMLResourceManager.h:260
bool isDefined(const String &object_name) const
Return whether an object named object_name exists.
Definition: cegui/include/CEGUI/NamedXMLResourceManager.h:353
const String d_resourceType
String holding the text for the resource type managed.
Definition: cegui/include/CEGUI/NamedXMLResourceManager.h:245
String class used within the GUI system.
Definition: cegui/include/CEGUI/String.h:62
ObjectRegistry d_objects
the collection of objects
Definition: cegui/include/CEGUI/NamedXMLResourceManager.h:247