Adonthell  0.4
storage.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2000/2001/2002 Kai Sterker <kai.sterker@gmail.com>
3  Part of the Adonthell Project <http://adonthell.nongnu.org>
4 
5  Adonthell is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  Adonthell is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with Adonthell. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 
20 /**
21  * @file storage.h
22  * @author Kai Sterker <kai.sterker@gmail.com>
23  *
24  * @brief Declares the storage and objects classes.
25  *
26  *
27  */
28 
29 
30 #ifndef STORAGE_H_
31 #define STORAGE_H_
32 
33 #include <string>
34 #include <map>
35 #include <vector>
36 
37 #include "types.h"
38 #include "str_hash.h"
39 
40 #ifndef SWIG
41 using namespace std;
42 #endif
43 
44 
45 /**
46  * Base storage class. If you want to access attributes of an object of yours
47  * you have to derive that object's class from 'storage' and store the attributes
48  * in the hash_map.
49  *
50  */
51 class storage
52 {
53 public:
54  /**
55  * Default constructor.
56  *
57  */
58  storage () { changed = 1; }
59 
60  /**
61  * Destructor.
62  *
63  */
64  ~storage ();
65 
66  /**
67  * Sets key to value.
68  *
69  * @param key key.
70  * @param value value.
71  */
72  void set_val (string key, s_int32 value);
73 
74  /**
75  * Returns the value of a key.
76  *
77  * @param key key to return.
78  *
79  * @return value of key.
80  */
81  s_int32 get_val (string key);
82 
83  /**
84  * Returns the next (key, value) pair of the storage.
85  *
86  *
87  * @return Next element.
88  */
89  pair<string, s_int32> next ();
90 
91 #ifndef SWIG
92  /**
93  * Returns the value of a key.
94  *
95  * @attention Not available from Python. From Python, use get ()
96  * instead.
97  *
98  * @param key key to return
99  *
100  * @return value of key.
101  */
102  s_int32& operator[] (string key);
103 #endif
104 
105 private:
106 #ifndef SWIG
107  hash_map<string, s_int32> data;
108  hash_map<string, s_int32>::iterator i;
109  u_int8 changed;
110 #endif
111 
112 public:
113 #ifndef SWIG
114  /**
115  * Storage iterator, similar to STL iterator.
116  *
117  */
118  typedef hash_map<string, s_int32>::iterator iterator;
119 
120  /**
121  * Returns an iterator to the beginning of the storage.
122  *
123  *
124  * @return iterator to the beginning of the storage.
125  */
127  {
128  return data.begin ();
129  }
130 
131  /**
132  * Returns an iterator to the end of the storage.
133  *
134  *
135  * @return iterator to the end of the storage.
136  */
138  {
139  return data.end ();
140  }
141 
142  /**
143  * Returns the size (number of elements) of the storage.
144  *
145  *
146  * @return size of the storage.
147  */
148  u_int32 size () const
149  {
150  return data.size ();
151  }
152 #endif
153 };
154 
155 
156 /**
157  * The global container for access to all the different %game objects
158  * from within a script
159  */
160 class objects
161 {
162 public:
163  /**
164  * Default constructor.
165  *
166  */
167  objects () { changed = 1; }
168 
169  /**
170  * Associates an object to a key.
171  *
172  * @param key key.
173  * @param val storage associated to key.
174  */
175  void set_val (const char * key, storage* val);
176 
177  /**
178  * Returns a storage associated to a key.
179  *
180  * @param key key to return.
181  *
182  * @return storage associated to key.
183  */
184  storage* get_val (const char * key);
185 
186  /**
187  * Erases a storage from it's key.
188  *
189  * @param key key to erase.
190  */
191  void erase (const char * key);
192 
193  /**
194  * Returns the next storage in the object.
195  *
196  *
197  * @return next storage in the object.
198  */
199  storage* next ();
200 
201 private:
202 #ifndef SWIG
203  /*
204  * Checks two strings for their order (needed for the map)
205  *
206  */
207  struct ltstr
208  {
209  bool operator()(const char* s1, const char* s2) const
210  {
211  return strcmp (s1, s2) < 0;
212  }
213  };
214 
215  map<const char*, storage*, ltstr> data;
216  map<const char*, storage*, ltstr>::iterator i;
217  u_int8 changed;
218 #endif
219 };
220 
221 #ifndef SWIG
222 
223 /**
224  * Stores %objects of any kind.
225  *
226  * Please see the hash_map documentation in STL documentation for a detailed
227  * description of this class.
228  *
229  */
230 template <class mytype>
231 class dictionary : public hash_map<string, mytype>
232 {
233 };
234 
235 #endif
236 
237 #endif // STORAGE_H_
str_hash.h
Declares the hash<string> type, to be able to declare hash_maps with strings as keys.
storage::iterator
hash_map< string, s_int32 >::iterator iterator
Storage iterator, similar to STL iterator.
Definition: storage.h:118
objects::objects
objects()
Default constructor.
Definition: storage.h:167
types.h
Declares some basic types.
dictionary
Stores objects of any kind.
Definition: storage.h:231
u_int32
#define u_int32
32 bits long unsigned integer
Definition: types.h:41
s_int32
#define s_int32
32 bits long signed integer
Definition: types.h:50
storage::begin
iterator begin()
Returns an iterator to the beginning of the storage.
Definition: storage.h:126
storage::storage
storage()
Default constructor.
Definition: storage.h:58
u_int8
#define u_int8
8 bits long unsigned integer
Definition: types.h:35
objects
The global container for access to all the different game objects from within a script.
Definition: storage.h:160
storage
Base storage class.
Definition: storage.h:51
storage::size
u_int32 size() const
Returns the size (number of elements) of the storage.
Definition: storage.h:148
storage::end
iterator end()
Returns an iterator to the end of the storage.
Definition: storage.h:137