00001
00002
00003
00004
00005
00006
00007
00008 #ifndef STORE_H
00009 #define STORE_H
00010
00011 #include <limits.h>
00012 #include <map>
00013 #include <string>
00014 #include <vector>
00015
00016 class reader;
00017 class writer;
00018 class CrawlHashTable;
00019 class CrawlVector;
00020 struct item_def;
00021 struct coord_def;
00022 struct level_pos;
00023 class level_id;
00024 class dlua_chunk;
00025 class monster;
00026
00027 typedef uint8_t hash_size;
00028 typedef uint8_t vec_size;
00029 typedef uint8_t store_flags;
00030
00031 #define VEC_MAX_SIZE 255
00032 #define HASH_MAX_SIZE 255
00033
00034
00035
00036 enum store_val_type
00037 {
00038 SV_NONE = 0,
00039 SV_BOOL,
00040 SV_BYTE,
00041 SV_SHORT,
00042 SV_INT,
00043 SV_FLOAT,
00044 SV_STR,
00045 SV_COORD,
00046 SV_ITEM,
00047 SV_HASH,
00048 SV_VEC,
00049 SV_LEV_ID,
00050 SV_LEV_POS,
00051 SV_MONST,
00052 SV_LUA,
00053 NUM_STORE_VAL_TYPES
00054 };
00055
00056 enum store_flag_type
00057 {
00058 SFLAG_UNSET = (1 << 0),
00059 SFLAG_CONST_VAL = (1 << 1),
00060 SFLAG_CONST_TYPE = (1 << 2),
00061 SFLAG_NO_ERASE = (1 << 3),
00062 };
00063
00064
00065
00066
00067 typedef union StoreUnion StoreUnion;
00068 union StoreUnion
00069 {
00070 bool boolean;
00071 char byte;
00072 short _short;
00073 int _int;
00074 float _float;
00075 void* ptr;
00076 };
00077
00078
00079 class CrawlStoreValue
00080 {
00081 public:
00082 CrawlStoreValue();
00083 CrawlStoreValue(const CrawlStoreValue &other);
00084
00085 ~CrawlStoreValue();
00086
00087
00088 CrawlStoreValue(const bool val);
00089 CrawlStoreValue(const char &val);
00090 CrawlStoreValue(const short &val);
00091 CrawlStoreValue(const int &val);
00092 CrawlStoreValue(const float &val);
00093 CrawlStoreValue(const std::string &val);
00094 CrawlStoreValue(const char* val);
00095 CrawlStoreValue(const coord_def &val);
00096 CrawlStoreValue(const item_def &val);
00097 CrawlStoreValue(const CrawlHashTable &val);
00098 CrawlStoreValue(const CrawlVector &val);
00099 CrawlStoreValue(const level_id &val);
00100 CrawlStoreValue(const level_pos &val);
00101 CrawlStoreValue(const monster& val);
00102 CrawlStoreValue(const dlua_chunk &val);
00103
00104 CrawlStoreValue &operator = (const CrawlStoreValue &other);
00105
00106 protected:
00107 store_val_type type;
00108 store_flags flags;
00109 StoreUnion val;
00110
00111 public:
00112 store_flags get_flags() const;
00113 store_flags set_flags(store_flags flags);
00114 store_flags unset_flags(store_flags flags);
00115 store_val_type get_type() const;
00116
00117 CrawlHashTable &new_table();
00118
00119 CrawlVector &new_vector(store_flags flags,
00120 vec_size max_size = VEC_MAX_SIZE);
00121 CrawlVector &new_vector(store_val_type type, store_flags flags = 0,
00122 vec_size max_size = VEC_MAX_SIZE);
00123
00124 bool &get_bool();
00125 char &get_byte();
00126 short &get_short();
00127 int &get_int();
00128 float &get_float();
00129 std::string &get_string();
00130 coord_def &get_coord();
00131 CrawlHashTable &get_table();
00132 CrawlVector &get_vector();
00133 item_def &get_item();
00134 level_id &get_level_id();
00135 level_pos &get_level_pos();
00136 monster &get_monster();
00137 dlua_chunk &get_lua();
00138
00139 bool get_bool() const;
00140 char get_byte() const;
00141 short get_short() const;
00142 int get_int() const;
00143 float get_float() const;
00144 std::string get_string() const;
00145 coord_def get_coord() const;
00146 level_id get_level_id() const;
00147 level_pos get_level_pos() const;
00148
00149 const CrawlHashTable& get_table() const;
00150 const CrawlVector& get_vector() const;
00151 const item_def& get_item() const;
00152 const monster& get_monster() const;
00153 const dlua_chunk& get_lua() const;
00154
00155 #if 0
00156
00157 void set_bool(const bool val);
00158 void set_byte(const char val);
00159 void set_short(const short val);
00160 void set_int(const int val);
00161 void set_float(const float val);
00162 void set_string(const std::string &val);
00163 void set_coord(const coord_def &val);
00164 void set_table(const CrawlHashTable &val);
00165 void set_vector(const CrawlVector &val);
00166 void set_item(const item_def &val);
00167 #endif
00168
00169 public:
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180 CrawlStoreValue &operator [] (const std::string &key);
00181 CrawlStoreValue &operator [] (const vec_size &index);
00182
00183 const CrawlStoreValue &operator [] (const std::string &key) const;
00184 const CrawlStoreValue &operator [] (const vec_size &index) const;
00185
00186
00187 operator bool&();
00188 operator char&();
00189 operator short&();
00190 operator int&();
00191 operator float&();
00192 operator std::string&();
00193 operator coord_def&();
00194 operator CrawlHashTable&();
00195 operator CrawlVector&();
00196 operator item_def&();
00197 operator level_id&();
00198 operator level_pos&();
00199 operator monster& ();
00200 operator dlua_chunk&();
00201
00202 operator bool() const;
00203 operator char() const;
00204 operator short() const;
00205 operator int() const;
00206 operator float() const;
00207 operator std::string() const;
00208 operator coord_def() const;
00209 operator level_id() const;
00210 operator level_pos() const;
00211
00212
00213 CrawlStoreValue &operator = (const bool &val);
00214 CrawlStoreValue &operator = (const char &val);
00215 CrawlStoreValue &operator = (const short &val);
00216 CrawlStoreValue &operator = (const int &val);
00217 CrawlStoreValue &operator = (const float &val);
00218 CrawlStoreValue &operator = (const std::string &val);
00219 CrawlStoreValue &operator = (const char* val);
00220 CrawlStoreValue &operator = (const coord_def &val);
00221 CrawlStoreValue &operator = (const CrawlHashTable &val);
00222 CrawlStoreValue &operator = (const CrawlVector &val);
00223 CrawlStoreValue &operator = (const item_def &val);
00224 CrawlStoreValue &operator = (const level_id &val);
00225 CrawlStoreValue &operator = (const level_pos &val);
00226 CrawlStoreValue &operator = (const monster& val);
00227 CrawlStoreValue &operator = (const dlua_chunk &val);
00228
00229
00230 std::string &operator += (const std::string &val);
00231
00232
00233 int operator ++ ();
00234 int operator -- ();
00235
00236
00237 int operator ++ (int);
00238 int operator -- (int);
00239
00240 protected:
00241 CrawlStoreValue(const store_flags flags,
00242 const store_val_type type = SV_NONE);
00243
00244 void write(writer &) const;
00245 void read(reader &);
00246
00247 void unset(bool force = false);
00248
00249 friend class CrawlHashTable;
00250 friend class CrawlVector;
00251 };
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266 class CrawlHashTable
00267 {
00268 public:
00269 CrawlHashTable();
00270 CrawlHashTable(const CrawlHashTable& other);
00271
00272 ~CrawlHashTable();
00273
00274 typedef std::map<std::string, CrawlStoreValue> hash_map_type;
00275 typedef hash_map_type::iterator iterator;
00276 typedef hash_map_type::const_iterator const_iterator;
00277
00278 protected:
00279
00280
00281 hash_map_type *hash_map;
00282
00283 void init_hash_map();
00284
00285 friend class CrawlStoreValue;
00286
00287 public:
00288 CrawlHashTable &operator = (const CrawlHashTable &other);
00289
00290 void write(writer &) const;
00291 void read(reader &);
00292
00293 bool exists(const std::string &key) const;
00294 void assert_validity() const;
00295
00296
00297
00298 const CrawlStoreValue& get_value(const std::string &key) const;
00299 const CrawlStoreValue& operator[] (const std::string &key) const;
00300
00301
00302
00303
00304
00305
00306
00307
00308 CrawlStoreValue& get_value(const std::string &key);
00309 CrawlStoreValue& operator[] (const std::string &key);
00310
00311
00312 hash_size size() const;
00313 bool empty() const;
00314
00315 void erase(const std::string key);
00316 void clear();
00317
00318 const_iterator begin() const;
00319 const_iterator end() const;
00320
00321 iterator begin();
00322 iterator end();
00323 };
00324
00325
00326
00327
00328
00329 class CrawlVector
00330 {
00331 public:
00332 CrawlVector();
00333 CrawlVector(store_flags flags, vec_size max_size = VEC_MAX_SIZE);
00334 CrawlVector(store_val_type type, store_flags flags = 0,
00335 vec_size max_size = VEC_MAX_SIZE);
00336
00337 ~CrawlVector();
00338
00339 typedef std::vector<CrawlStoreValue> vector_type;
00340 typedef vector_type::iterator iterator;
00341 typedef vector_type::const_iterator const_iterator;
00342
00343 protected:
00344 store_val_type type;
00345 store_flags default_flags;
00346 vec_size max_size;
00347 vector_type vector;
00348
00349 friend class CrawlStoreValue;
00350
00351 public:
00352 void write(writer &) const;
00353 void read(reader &);
00354
00355 store_flags get_default_flags() const;
00356 store_flags set_default_flags(store_flags flags);
00357 store_flags unset_default_flags(store_flags flags);
00358 store_val_type get_type() const;
00359 void assert_validity() const;
00360 void set_max_size(vec_size size);
00361 vec_size get_max_size() const;
00362
00363
00364
00365 const CrawlStoreValue& get_value(const vec_size &index) const;
00366 const CrawlStoreValue& operator[] (const vec_size &index) const;
00367
00368 CrawlStoreValue& get_value(const vec_size &index);
00369 CrawlStoreValue& operator[] (const vec_size &index);
00370
00371
00372 vec_size size() const;
00373 bool empty() const;
00374
00375
00376
00377 CrawlStoreValue& pop_back();
00378 void push_back(CrawlStoreValue val);
00379 void insert(const vec_size index, CrawlStoreValue val);
00380
00381
00382 void resize(const vec_size size);
00383 void erase(const vec_size index);
00384 void clear();
00385
00386 const_iterator begin() const;
00387 const_iterator end() const;
00388
00389 iterator begin();
00390 iterator end();
00391 };
00392 #endif