initial commit, lordsawar source, slightly modified
[lordsawar] / src / Itemlist.h
1 // Copyright (C) 2004, 2005 Ulf Lorenz
2 // Copyright (C) 2007, 2008 Ben Asselstine
3 //
4 //  This program is free software; you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation; either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU Library General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program; if not, write to the Free Software
16 //  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 
17 //  02110-1301, USA.
18
19 #ifndef ITEMLIST_H
20 #define ITEMLIST_H
21
22 #include <map>
23 #include <sigc++/trackable.h>
24
25 #include "ItemProto.h"
26
27 //! A list of Item objects.
28 /** 
29  * The Itemlist holds all item templates (i.e. types of items) together.
30  * 
31  * It is implemented as a singleton. Upon creation, it reads the item
32  * description file and initialises an internal list.
33  *
34  * For easier access, the Itemlist is derived from map. Given an item index,
35  * you can get the item belonging to the index by the []-operator using the
36  * item index as index.
37  */
38 class Itemlist : public std::map<guint32, ItemProto*>, public sigc::trackable
39 {
40     public:
41
42         //! The xml tag of this object.
43         /** 
44          * @note This tag appears in the items configuration file, or in a 
45          * saved-game file.
46          */
47         static std::string d_tag; 
48
49         //! Returns the singleton instance.
50         static Itemlist* getInstance();
51
52         //! Reads in the itemlist from a file
53         static Itemlist* getInstance(XML_Helper *helper);
54
55         //! Creates a new singleton instance. Deletes an existing one.
56         //This list of items comes from loading the items.xml file.
57         static void createStandardInstance();
58
59         //! Explicitly deletes the singleton instance.
60         static void deleteInstance();
61         
62         //! Save the item data.  See XML_Helper for details.
63         bool save(XML_Helper* helper) const;
64
65         void remove(ItemProto *item);
66
67     protected:
68         //! Default constructor.
69         Itemlist();
70         //! Loading constructor.
71         Itemlist(XML_Helper* helper);
72         //! Destructor.
73         ~Itemlist();
74
75     private:
76
77         //! Callback for loading an Item from an opened saved-game file.
78         bool loadItemProto(std::string tag, XML_Helper* helper);
79
80         //! Erases an Item from the list and frees the Item too.
81         void flErase(iterator it);
82
83         //! Clears the itemlist of all it's items.
84         void flClear();
85
86         static Itemlist* d_instance;
87 };
88
89 #endif //ITEMLIST_H