git-svn-id: https://scorched3d.svn.sourceforge.net/svnroot/scorched3d/trunk/scorched...
[scorched3d/parasti.git] / src / common / landscapedef / LandscapeDefinitionsItem.h
blob89564277ce34c15bc0d78297248b54a53ac212a5
1 ////////////////////////////////////////////////////////////////////////////////
2 // Scorched3D (c) 2000-2009
3 //
4 // This file is part of Scorched3D.
5 //
6 // Scorched3D is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
11 // Scorched3D is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with Scorched3D; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 ////////////////////////////////////////////////////////////////////////////////
21 #if !defined(__INCLUDE_LandscapeDefinitionsItemh_INCLUDE__)
22 #define __INCLUDE_LandscapeDefinitionsItemh_INCLUDE__
24 #include <common/Defines.h>
25 #include <XML/XMLFile.h>
26 #include <map>
27 #include <string>
29 class LandscapeDefinitions;
31 template <class T>
32 class LandscapeDefinitionsItem
34 public:
35 LandscapeDefinitionsItem(const char *typeName) :
36 typeName_(typeName)
40 void clearItems()
42 typename std::map<std::string, T *>::iterator itor;
43 for (itor = items_.begin();
44 itor != items_.end();
45 itor++)
47 T *item = (*itor).second;
48 delete item;
50 items_.clear();
53 T *getItem(LandscapeDefinitions *defns,
54 const char *fileName, bool load, bool cache)
56 T *item = 0;
57 typename std::map<std::string, T *>::iterator itor;
58 itor = items_.find(fileName);
59 if (itor != items_.end())
61 item = (*itor).second;
63 else if (load)
65 std::string dataFile = S3D::getDataFile(fileName);
66 if (!S3D::fileExists(dataFile.c_str()))
68 S3D::dialogMessage("Scorched Landscape",
69 S3D::formatStringBuffer("Failed to find file \"%s\"\n"
70 "When loading %s file",
71 dataFile.c_str(),
72 typeName_.c_str()));
73 return 0;
76 XMLFile file;
77 if (!file.readFile(dataFile.c_str()) ||
78 !file.getRootNode())
80 S3D::dialogMessage("Scorched Landscape",
81 S3D::formatStringBuffer("Failed to parse \"%s\"\n"
82 "%s",
83 dataFile.c_str(),
84 file.getParserError()));
85 return 0;
88 item = new T;
89 if (!item->readXML(defns, file.getRootNode()))
91 S3D::dialogMessage("Scorched Landscape",
92 S3D::formatStringBuffer("Failed to parse \"%s\"",
93 dataFile.c_str()));
94 return 0;
97 if (cache)
99 items_[fileName] = item;
103 return item;
106 protected:
107 std::map<std::string, T *> items_;
108 std::string typeName_;
111 #endif