ResourceHandler.h

Go to the documentation of this file.
00001 
00005 /* Copyright © 2009 James Legg.
00006     This program is free software: you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation, either version 3 of the License, or
00009     (at your option) any later version.
00010 */
00011 #ifndef RESOURCEHANDLER_H_
00012 #define RESOURCEHANDLER_H_
00013 
00014 #include <map>
00015 
00016 namespace Engine
00017 {
00018 
00031 template<class Content, class Key, class ContentConstructionInformation>
00032 class ResourceHandler
00033 {
00034     // We only want one resource handler, so block public access to the default
00035     // constructor, copy constructor, and assigment operator.
00036     ResourceHandler();
00037     ResourceHandler(const ResourceHandler & source);
00038     ResourceHandler & operator=(const ResourceHandler & source);
00039 public:
00049     static ResourceHandler<Content, Key, ContentConstructionInformation> & get_instance();
00050     virtual ~ResourceHandler();
00051     
00059     void check_load(const Key & key, ContentConstructionInformation construction_information);
00066     Content * get(const Key & key);
00067 private:
00068     std::map<Key, Content *> data;
00069 };
00070 
00071 template<class Content, class Key, class ContentConstructionInformation>
00072 ResourceHandler<Content, Key, ContentConstructionInformation>::ResourceHandler()
00073 {
00074 }
00075 
00076 template<class Content, class Key, class ContentConstructionInformation>
00077 ResourceHandler<Content, Key, ContentConstructionInformation>::~ResourceHandler()
00078 {
00079     // delete all of the data.
00080     typename std::map<Key, Content *>::iterator iterator;
00081     for (iterator = data.begin(); iterator != data.end(); iterator++)
00082     {
00083         delete iterator->second;
00084     }
00085 }
00086 
00087 template<class Content, class Key, class ConstructionInfo>
00088 ResourceHandler<Content, Key, ConstructionInfo> & ResourceHandler<Content, Key, ConstructionInfo>::get_instance()
00089 {
00090     static ResourceHandler<Content, Key, ConstructionInfo> resource_handler;
00091     return resource_handler;
00092 }
00093 
00094 template<class Content, class Key, class ContentConstructionInformation>
00095 void ResourceHandler<Content, Key, ContentConstructionInformation>::check_load(const Key & key,
00096                                                                  ContentConstructionInformation construction_information)
00097 {
00098     // Does the object already exist?
00099     typename std::map<Key, Content *>::iterator it = data.find(key);
00100     if (it == data.end())
00101     {
00102         // We didn't find it. Constuct it.
00103         data[key] = new Content(construction_information);
00104     }
00105 }
00106 
00107 template<class Content, class Key, class ConstructionInfo>
00108 Content * ResourceHandler<Content, Key, ConstructionInfo>::get(const Key & key)
00109 {
00110     typename std::map<Key, Content *>::iterator it = data.find(key);
00111     if (it == data.end())
00112     {
00113         // not found
00114         return 0;
00115     }
00116     // found, return the pointer.
00117     return it->second;
00118 }
00119 
00120 }
00121 
00122 #endif /*RESOURCEHANDLER_H_*/

Get Racer at SourceForge.net. Fast, secure and Free Open Source software downloads

Generated at Mon Sep 6 00:41:12 2010 by Doxygen version 1.4.7 for Racer version svn335.