00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #if !defined(__INCLUDE_LandscapeDefinitionsItemh_INCLUDE__)
00022 #define __INCLUDE_LandscapeDefinitionsItemh_INCLUDE__
00023
00024 #include <common/Defines.h>
00025 #include <XML/XMLFile.h>
00026 #include <map>
00027 #include <string>
00028
00029 class LandscapeDefinitions;
00030
00031 template <class T>
00032 class LandscapeDefinitionsItem
00033 {
00034 public:
00035 LandscapeDefinitionsItem(const char *typeName) :
00036 typeName_(typeName)
00037 {
00038 }
00039
00040 void clearItems()
00041 {
00042 typename std::map<std::string, T *>::iterator itor;
00043 for (itor = items_.begin();
00044 itor != items_.end();
00045 itor++)
00046 {
00047 T *item = (*itor).second;
00048 delete item;
00049 }
00050 items_.clear();
00051 }
00052
00053 T *getItem(LandscapeDefinitions *defns,
00054 const char *fileName, bool load, bool cache)
00055 {
00056 T *item = 0;
00057 typename std::map<std::string, T *>::iterator itor;
00058 itor = items_.find(fileName);
00059 if (itor != items_.end())
00060 {
00061 item = (*itor).second;
00062 }
00063 else if (load)
00064 {
00065 std::string dataFile = S3D::getDataFile(fileName);
00066 if (!S3D::fileExists(dataFile.c_str()))
00067 {
00068 S3D::dialogMessage("Scorched Landscape",
00069 S3D::formatStringBuffer("Failed to find file \"%s\"\n"
00070 "When loading %s file",
00071 dataFile.c_str(),
00072 typeName_.c_str()));
00073 return 0;
00074 }
00075
00076 XMLFile file;
00077 if (!file.readFile(dataFile.c_str()) ||
00078 !file.getRootNode())
00079 {
00080 S3D::dialogMessage("Scorched Landscape",
00081 S3D::formatStringBuffer("Failed to parse \"%s\"\n"
00082 "%s",
00083 dataFile.c_str(),
00084 file.getParserError()));
00085 return 0;
00086 }
00087
00088 item = new T;
00089 if (!item->readXML(defns, file.getRootNode()))
00090 {
00091 S3D::dialogMessage("Scorched Landscape",
00092 S3D::formatStringBuffer("Failed to parse \"%s\"",
00093 dataFile.c_str()));
00094 return 0;
00095 }
00096
00097 if (cache)
00098 {
00099 items_[fileName] = item;
00100 }
00101 }
00102
00103 return item;
00104 }
00105
00106 protected:
00107 std::map<std::string, T *> items_;
00108 std::string typeName_;
00109 };
00110
00111 #endif