Adonthell
0.4
|
00001 /* 00002 $Id: storage.h,v 1.24 2003/02/23 23:14:34 ksterker Exp $ 00003 00004 Copyright (C) 2000/2001/2002 Kai Sterker <kaisterker@linuxgames.com> 00005 Part of the Adonthell Project http://adonthell.linuxgames.com 00006 00007 This program is free software; you can redistribute it and/or modify 00008 it under the terms of the GNU General Public License. 00009 This program is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY. 00011 00012 See the COPYING file for more details. 00013 */ 00014 00015 00016 /** 00017 * @file storage.h 00018 * @author Kai Sterker <kaisterker@linuxgames.com> 00019 * 00020 * @brief Declares the storage and objects classes. 00021 * 00022 * 00023 */ 00024 00025 00026 #ifndef STORAGE_H_ 00027 #define STORAGE_H_ 00028 00029 #include <string.h> 00030 #include <map> 00031 #include <vector> 00032 00033 #include "types.h" 00034 #include "str_hash.h" 00035 00036 #ifndef SWIG 00037 using namespace std; 00038 #endif 00039 00040 00041 /** 00042 * Base storage class. If you want to access attributes of an object of yours 00043 * you have to derive that object's class from 'storage' and store the attributes 00044 * in the hash_map. 00045 * 00046 */ 00047 class storage 00048 { 00049 public: 00050 /** 00051 * Default constructor. 00052 * 00053 */ 00054 storage () { changed = 1; } 00055 00056 /** 00057 * Destructor. 00058 * 00059 */ 00060 ~storage (); 00061 00062 /** 00063 * Sets key to value. 00064 * 00065 * @param key key. 00066 * @param value value. 00067 */ 00068 void set_val (string key, s_int32 value); 00069 00070 /** 00071 * Returns the value of a key. 00072 * 00073 * @param key key to return. 00074 * 00075 * @return value of key. 00076 */ 00077 s_int32 get_val (string key); 00078 00079 /** 00080 * Returns the next (key, value) pair of the storage. 00081 * 00082 * 00083 * @return Next element. 00084 */ 00085 pair<string, s_int32> next (); 00086 00087 #ifndef SWIG 00088 /** 00089 * Returns the value of a key. 00090 * 00091 * @attention Not available from Python. From Python, use get () 00092 * instead. 00093 * 00094 * @param key key to return 00095 * 00096 * @return value of key. 00097 */ 00098 s_int32& operator[] (string key); 00099 #endif 00100 00101 private: 00102 #ifndef SWIG 00103 hash_map<string, s_int32> data; 00104 hash_map<string, s_int32>::iterator i; 00105 u_int8 changed; 00106 #endif 00107 00108 public: 00109 #ifndef SWIG 00110 /** 00111 * Storage iterator, similar to STL iterator. 00112 * 00113 */ 00114 typedef hash_map<string, s_int32>::iterator iterator; 00115 00116 /** 00117 * Returns an iterator to the beginning of the storage. 00118 * 00119 * 00120 * @return iterator to the beginning of the storage. 00121 */ 00122 iterator begin () 00123 { 00124 return data.begin (); 00125 } 00126 00127 /** 00128 * Returns an iterator to the end of the storage. 00129 * 00130 * 00131 * @return iterator to the end of the storage. 00132 */ 00133 iterator end () 00134 { 00135 return data.end (); 00136 } 00137 00138 /** 00139 * Returns the size (number of elements) of the storage. 00140 * 00141 * 00142 * @return size of the storage. 00143 */ 00144 u_int32 size () const 00145 { 00146 return data.size (); 00147 } 00148 #endif 00149 }; 00150 00151 00152 /** 00153 * The global container for access to all the different %game objects 00154 * from within a script 00155 */ 00156 class objects 00157 { 00158 public: 00159 /** 00160 * Default constructor. 00161 * 00162 */ 00163 objects () { changed = 1; } 00164 00165 /** 00166 * Associates an object to a key. 00167 * 00168 * @param key key. 00169 * @param val storage associated to key. 00170 */ 00171 void set_val (const char * key, storage* val); 00172 00173 /** 00174 * Returns a storage associated to a key. 00175 * 00176 * @param key key to return. 00177 * 00178 * @return storage associated to key. 00179 */ 00180 storage* get_val (const char * key); 00181 00182 /** 00183 * Erases a storage from it's key. 00184 * 00185 * @param key key to erase. 00186 */ 00187 void erase (const char * key); 00188 00189 /** 00190 * Returns the next storage in the object. 00191 * 00192 * 00193 * @return next storage in the object. 00194 */ 00195 storage* next (); 00196 00197 private: 00198 #ifndef SWIG 00199 /* 00200 * Checks two strings for their order (needed for the map) 00201 * 00202 */ 00203 struct ltstr 00204 { 00205 bool operator()(const char* s1, const char* s2) const 00206 { 00207 return strcmp (s1, s2) < 0; 00208 } 00209 }; 00210 00211 map<const char*, storage*, ltstr> data; 00212 map<const char*, storage*, ltstr>::iterator i; 00213 u_int8 changed; 00214 #endif 00215 }; 00216 00217 #ifndef SWIG 00218 00219 /** 00220 * Stores %objects of any kind. 00221 * 00222 * Please see the hash_map documentation in STL documentation for a detailed 00223 * description of this class. 00224 * 00225 */ 00226 template <class mytype> 00227 class dictionary : public hash_map<string, mytype> 00228 { 00229 }; 00230 00231 #endif 00232 00233 #endif // STORAGE_H_