LandscapeDefinitionsItem.h

Go to the documentation of this file.
00001 ////////////////////////////////////////////////////////////////////////////////
00002 //    Scorched3D (c) 2000-2009
00003 //
00004 //    This file is part of Scorched3D.
00005 //
00006 //    Scorched3D 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 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    Scorched3D is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with Scorched3D; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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

Generated on Mon Feb 16 15:14:50 2009 for Scorched3D by  doxygen 1.5.3