#include <CSLoadable.h>
This class encapsulates the functionality of loading a needed resource only once. Upon a second call of the loader it gives back an allready loaded resource.
A MUST for this to work is that this class is also responsible for freeing the (only) once loaded resources.
If a resource is a virtual bool isSingleCreate(void) const; the resource will be put in a map, where it can be found later on for reuse. (map key is the filename of the resource)
The object, that is created using this class (via a filename) must provide a virtual T *create(const std::string &filename) = 0; -method.
Per default an object is destroyed using c++ "delete". But the destroy-method can be overloaded by children!
Definition at line 40 of file CSLoadable.h.
Public Member Functions | |
virtual T * | load (const std::string &filename) |
virtual void | unload (T *image) |
virtual void | quit () |
virtual | ~Loadable () |
Protected Member Functions | |
virtual std::map< std::string, T * > * | getMap () |
virtual void | destroy (T *object) |
virtual bool | isSingleCreate (void) const |
virtual T * | create (const std::string &filename)=0 |
Loadable () |
|
Definition at line 49 of file CSLoadable.h.
00050 { 00051 mMap.clear(); 00052 } |
|
Definition at line 62 of file CSLoadable.h.
00063 { 00064 quit(); 00065 } |
|
Definition at line 45 of file CSLoadable.h.
00045 {return &mMap;}
|
|
Reimplemented in CSBitmapLoader. Definition at line 123 of file CSLoadable.h. Referenced by Loadable< T >::unload().
00124 {
00125 delete object;
00126 }
|
|
Reimplemented in CSSpriteLoader, CSTileLoader, CSTileMapLoader, and CSWorldLoader. Definition at line 129 of file CSLoadable.h. Referenced by Loadable< T >::load().
00130 { 00131 return true; 00132 } |
|
Implemented in CSActionLoader, CSAnimationLoader, CSBitmapLoader, CSBitmapDirectLoader, CSFontLoader, CSMusicLoader, CSPictureLoader, CSPictureDirectLoader, CSSampleLoader, CSSpriteLoader, CSTileLoader, CSTileMapLoader, CSTileSetLoader, and CSWorldLoader. Referenced by Loadable< T >::load(). |
|
only produce singletons (and add to mapping) if "isSingleCreate()" return true (default) Definition at line 71 of file CSLoadable.h. References Loadable< T >::create(), Loadable< T >::isSingleCreate(), LOG_LEVEL_DEBUG, and LOG_MESSAGE.
00072 { 00073 if (isSingleCreate()) 00074 { 00075 LOG_MESSAGE((LOG_LEVEL_DEBUG),"Getting file from map: " + filename) 00076 std::map<std::string, T *>::iterator iter = mMap.find(filename.c_str()); 00077 if (iter != mMap.end()) 00078 { 00079 return iter->second; 00080 } 00081 } 00082 00083 LOG_MESSAGE((LOG_LEVEL_DEBUG),"Loading file from disk: " + filename) 00084 T *anObject = create(filename.c_str()); 00085 if (isSingleCreate()) 00086 { 00087 mMap.insert(std::map<std::string, T *>::value_type(filename.c_str(), anObject)); 00088 } 00089 return anObject; 00090 } |
Here is the call graph for this function:
|
delete single object Definition at line 94 of file CSLoadable.h. References Loadable< T >::destroy(), and SDLMain::shutdown(). Referenced by Loadable< T >::quit().
00095 { 00096 //! delete single object 00097 std::map<std::string, T *>::iterator iter; 00098 for (iter = mMap.begin(); iter!=mMap.end(); iter++) 00099 { 00100 if (iter->second == anObject) 00101 { 00102 destroy(iter->second); 00103 mMap.erase(iter); 00104 return; 00105 } 00106 } 00107 SDLMain::shutdown((std::string)"Couldn't properly delete map object." + SDL_GetError(), 2); 00108 } |
Here is the call graph for this function:
|
unloads all knonw (singleton) < instances of this loader class Definition at line 111 of file CSLoadable.h. References Loadable< T >::unload(). Referenced by Loadable< CSBitmap >::~Loadable().
00112 { 00113 // delete all object 00114 std::map<std::string, T *>::iterator iter; 00115 for (iter = mMap.begin(); iter!=mMap.end(); iter = mMap.begin()) 00116 { 00117 unload(iter->second); 00118 } 00119 mMap.clear(); 00120 } |
Here is the call graph for this function: