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