00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef _CEGUINamedXMLResourceManager_h_
00029 #define _CEGUINamedXMLResourceManager_h_
00030
00031 #include "CEGUIEventSet.h"
00032 #include "CEGUIString.h"
00033 #include "CEGUIExceptions.h"
00034 #include "CEGUILogger.h"
00035 #include "CEGUIInputEvent.h"
00036 #include "CEGUISystem.h"
00037 #include <map>
00038
00039
00040 namespace CEGUI
00041 {
00043 enum XMLResourceExistsAction
00044 {
00046 XREA_RETURN,
00048 XREA_REPLACE,
00050 XREA_THROW
00051 };
00052
00053
00054
00056 class CEGUIEXPORT ResourceEventSet : public EventSet
00057 {
00058 public:
00060 static const String EventNamespace;
00062 static const String EventResourceCreated;
00064 static const String EventResourceDestroyed;
00066 static const String EventResourceReplaced;
00067 };
00068
00069
00070
00093 template<typename T, typename U>
00094 class NamedXMLResourceManager : public ResourceEventSet
00095 {
00096 public:
00106 NamedXMLResourceManager(const String& resource_type);
00107
00109 virtual ~NamedXMLResourceManager();
00110
00133 T& create(const String& xml_filename, const String& resource_group = "",
00134 XMLResourceExistsAction action = XREA_RETURN);
00135
00144 void destroy(const String& object_name);
00145
00155 void destroy(const T& object);
00156
00158 void destroyAll();
00159
00170 T& get(const String& object_name) const;
00171
00173 bool isDefined(const String& object_name) const;
00174
00176 void createAll(const String& pattern, const String& resource_group);
00177
00178 protected:
00180 typedef std::map<String, T*, String::FastLessCompare> ObjectRegistry;
00182 void destroyObject(typename ObjectRegistry::iterator ob);
00184 T& doExistingObjectAction(const String object_name, T* object,
00185 const XMLResourceExistsAction action);
00187 virtual void doPostObjectAdditionAction(T& object);
00189 const String d_resourceType;
00191 ObjectRegistry d_objects;
00192 };
00193
00194
00195 template<typename T, typename U>
00196 NamedXMLResourceManager<T, U>::NamedXMLResourceManager(
00197 const String& resource_type) :
00198 d_resourceType(resource_type)
00199 {
00200 }
00201
00202
00203 template<typename T, typename U>
00204 NamedXMLResourceManager<T, U>::~NamedXMLResourceManager()
00205 {
00206 }
00207
00208
00209 template<typename T, typename U>
00210 T& NamedXMLResourceManager<T, U>::create(const String& xml_filename,
00211 const String& resource_group,
00212 XMLResourceExistsAction action)
00213 {
00214 U xml_loader(xml_filename, resource_group);
00215 return doExistingObjectAction(xml_loader.getObjectName(),
00216 &xml_loader.getObject(), action);
00217 }
00218
00219
00220 template<typename T, typename U>
00221 void NamedXMLResourceManager<T, U>::destroy(const String& object_name)
00222 {
00223 typename ObjectRegistry::iterator i(d_objects.find(object_name));
00224
00225
00226 if (i == d_objects.end())
00227 return;
00228
00229 destroyObject(i);
00230 }
00231
00232
00233 template<typename T, typename U>
00234 void NamedXMLResourceManager<T, U>::destroy(const T& object)
00235 {
00236
00237
00238 typename ObjectRegistry::iterator i(d_objects.begin());
00239 for (; i != d_objects.end(); ++i)
00240 if (i->second == &object)
00241 {
00242 destroyObject(i);
00243 return;
00244 }
00245 }
00246
00247
00248 template<typename T, typename U>
00249 void NamedXMLResourceManager<T, U>::destroyAll()
00250 {
00251 while (!d_objects.empty())
00252 destroyObject(d_objects.begin());
00253 }
00254
00255
00256 template<typename T, typename U>
00257 T& NamedXMLResourceManager<T, U>::get(const String& object_name) const
00258 {
00259 typename ObjectRegistry::const_iterator i(d_objects.find(object_name));
00260
00261 if (i == d_objects.end())
00262 throw UnknownObjectException("NamedXMLResourceManager::get: "
00263 "No object of type '" + d_resourceType + "' named '" + object_name +
00264 "' is present in the collection.");
00265
00266 return *i->second;
00267 }
00268
00269
00270 template<typename T, typename U>
00271 bool NamedXMLResourceManager<T, U>::isDefined(const String& object_name) const
00272 {
00273 return d_objects.find(object_name) != d_objects.end();
00274 }
00275
00276
00277 template<typename T, typename U>
00278 void NamedXMLResourceManager<T, U>::destroyObject(
00279 typename ObjectRegistry::iterator ob)
00280 {
00281 char addr_buff[32];
00282 sprintf(addr_buff, "(%p)", static_cast<void*>(ob->second));
00283 Logger::getSingleton().logEvent("Object of type '" + d_resourceType +
00284 "' named '" + ob->first + "' has been destroyed. " +
00285 addr_buff, Informative);
00286
00287
00288 ResourceEventArgs args(d_resourceType, ob->first);
00289
00290 delete ob->second;
00291 d_objects.erase(ob);
00292
00293
00294 fireEvent(EventResourceDestroyed, args, EventNamespace);
00295 }
00296
00297
00298 template<typename T, typename U>
00299 T& NamedXMLResourceManager<T, U>::doExistingObjectAction(
00300 const String object_name,
00301 T* object,
00302 const XMLResourceExistsAction action)
00303 {
00304 String event_name;
00305
00306 if (isDefined(object_name))
00307 {
00308 switch (action)
00309 {
00310 case XREA_RETURN:
00311 Logger::getSingleton().logEvent("---- Returning existing instance "
00312 "of " + d_resourceType + " named '" + object_name + "'.");
00313
00314 delete object;
00315
00316 return *d_objects[object_name];
00317
00318 case XREA_REPLACE:
00319 Logger::getSingleton().logEvent("---- Replacing existing instance "
00320 "of " + d_resourceType + " named '" + object_name +
00321 "' (DANGER!).");
00322 destroy(object_name);
00323 event_name = EventResourceReplaced;
00324 break;
00325
00326 case XREA_THROW:
00327 delete object;
00328 throw AlreadyExistsException(
00329 "NamedXMLResourceManager::checkExistingObjectAction: "
00330 "an object of type '" + d_resourceType + "' named '" +
00331 object_name + "' already exists in the collection.");
00332
00333 default:
00334 delete object;
00335 throw InvalidRequestException(
00336 "NamedXMLResourceManager::checkExistingObjectAction: "
00337 "Invalid CEGUI::XMLResourceExistsAction was specified.");
00338 }
00339 }
00340 else
00341 event_name = EventResourceCreated;
00342
00343 d_objects[object_name] = object;
00344 doPostObjectAdditionAction(*object);
00345
00346
00347 ResourceEventArgs args(d_resourceType, object_name);
00348 fireEvent(event_name, args, EventNamespace);
00349
00350 return *object;
00351 }
00352
00353
00354 template<typename T, typename U>
00355 void NamedXMLResourceManager<T, U>::doPostObjectAdditionAction(T& )
00356 {
00357
00358 }
00359
00360
00361 template<typename T, typename U>
00362 void NamedXMLResourceManager<T, U>::createAll(const String& pattern,
00363 const String& resource_group)
00364 {
00365 std::vector<String> names;
00366 const size_t num = System::getSingleton().getResourceProvider()->
00367 getResourceGroupFileNames(names, pattern, resource_group);
00368
00369 for (size_t i = 0; i < num; ++i)
00370 create(names[i], resource_group);
00371 }
00372
00373
00374
00375 }
00376
00377 #endif // end of guard _CEGUINamedXMLResourceManager_h_