From aefbb02ff564f3b513476f82d144c7c92b2bbb05 Mon Sep 17 00:00:00 2001 From: Matthew Cline Date: Thu, 29 Oct 2009 01:03:34 -0700 Subject: store.{cc,h}: Handle level_id and level_pos level_id and level_pos instances can now be stored in CrawlHashTables and CrawlVectors. --- crawl-ref/source/store.cc | 236 +++++++++++++++++++++++++++++++++++++++++++--- crawl-ref/source/store.h | 46 ++++++--- 2 files changed, 254 insertions(+), 28 deletions(-) (limited to 'crawl-ref') diff --git a/crawl-ref/source/store.cc b/crawl-ref/source/store.cc index cb0b539b30..cf771633fe 100644 --- a/crawl-ref/source/store.cc +++ b/crawl-ref/source/store.cc @@ -11,6 +11,9 @@ #include "externs.h" #include "tags.h" +#include "travel.h" + +#include CrawlStoreValue::CrawlStoreValue() : type(SV_NONE), flags(SFLAG_UNSET) @@ -86,6 +89,22 @@ CrawlStoreValue::CrawlStoreValue(const CrawlStoreValue &other) break; } + case SV_LEV_ID: + { + level_id* id; + id = new level_id(*static_cast(other.val.ptr)); + val.ptr = static_cast(id); + break; + } + + case SV_LEV_POS: + { + level_pos* pos; + pos = new level_pos(*static_cast(other.val.ptr)); + val.ptr = static_cast(pos); + break; + } + case NUM_STORE_VAL_TYPES: ASSERT(false); } @@ -175,6 +194,20 @@ CrawlStoreValue::CrawlStoreValue(const CrawlVector &_val) get_vector() = _val; } +CrawlStoreValue::CrawlStoreValue(const level_id &_val) + : type(SV_LEV_ID), flags(SFLAG_UNSET) +{ + val.ptr = NULL; + get_level_id() = _val; +} + +CrawlStoreValue::CrawlStoreValue(const level_pos &_val) + : type(SV_LEV_POS), flags(SFLAG_UNSET) +{ + val.ptr = NULL; + get_level_pos() = _val; +} + CrawlStoreValue::~CrawlStoreValue() { unset(true); @@ -252,6 +285,22 @@ void CrawlStoreValue::unset(bool force) break; } + case SV_LEV_ID: + { + level_id* id = static_cast(val.ptr); + delete id; + val.ptr = NULL; + break; + } + + case SV_LEV_POS: + { + level_pos* pos = static_cast(val.ptr); + delete pos; + val.ptr = NULL; + break; + } + case SV_NONE: ASSERT(false); @@ -320,7 +369,15 @@ CrawlStoreValue &CrawlStoreValue::operator = (const CrawlStoreValue &other) COPY_PTR(CrawlVector); break; - case NUM_STORE_VAL_TYPES: + case SV_LEV_ID: + COPY_PTR(level_id); + break; + + case SV_LEV_POS: + COPY_PTR(level_pos); + break; + + case NUM_STORE_VAL_TYPES: DEBUGSTR("CrawlStoreValue has type NUM_STORE_VAL_TYPES"); break; } @@ -418,6 +475,20 @@ void CrawlStoreValue::write(writer &th) const break; } + case SV_LEV_ID: + { + level_id* id = static_cast(val.ptr); + id->save(th); + break; + } + + case SV_LEV_POS: + { + level_pos* pos = static_cast(val.ptr); + pos->save(th); + break; + } + case SV_NONE: ASSERT(false); @@ -498,6 +569,24 @@ void CrawlStoreValue::read(reader &th) break; } + case SV_LEV_ID: + { + level_id* id = new level_id(); + id->load(th); + val.ptr = (void*) id; + + break; + } + + case SV_LEV_POS: + { + level_pos* pos = new level_pos(); + pos->load(th); + val.ptr = (void*) pos; + + break; + } + case SV_NONE: ASSERT(false); @@ -686,6 +775,17 @@ CrawlVector &CrawlStoreValue::get_vector() GET_VAL_PTR(SV_VEC, CrawlVector*, new CrawlVector()); } +level_id &CrawlStoreValue::get_level_id() +{ + GET_VAL_PTR(SV_LEV_ID, level_id*, new level_id()); +} + +level_pos &CrawlStoreValue::get_level_pos() +{ + GET_VAL_PTR(SV_LEV_POS, level_pos*, new level_pos()); +} + + CrawlStoreValue &CrawlStoreValue::operator [] (const std::string &key) { return get_table()[key]; @@ -762,6 +862,18 @@ const CrawlVector& CrawlStoreValue::get_vector() const return *((CrawlVector*)val.ptr); } +level_id CrawlStoreValue::get_level_id() const +{ + GET_CONST_SETUP(SV_LEV_ID); + return *((level_id*)val.ptr); +} + +level_pos CrawlStoreValue::get_level_pos() const +{ + GET_CONST_SETUP(SV_LEV_POS); + return *((level_pos*)val.ptr); +} + const CrawlStoreValue &CrawlStoreValue::operator [] (const std::string &key) const { @@ -777,16 +889,18 @@ const CrawlStoreValue &CrawlStoreValue::operator ///////////////////// // Typecast operators #ifdef TARGET_COMPILER_VC -CrawlStoreValue::operator bool&() { return get_bool(); } -CrawlStoreValue::operator char&() { return get_byte(); } -CrawlStoreValue::operator short&() { return get_short(); } -CrawlStoreValue::operator float&() { return get_float(); } -CrawlStoreValue::operator long&() { return get_long(); } -CrawlStoreValue::operator std::string&() { return get_string(); } -CrawlStoreValue::operator coord_def&() { return get_coord(); } -CrawlStoreValue::operator CrawlHashTable&() { return get_table(); } -CrawlStoreValue::operator CrawlVector&() { return get_vector(); } -CrawlStoreValue::operator item_def&() { return get_item(); } +CrawlStoreValue::operator bool&() { return get_bool(); } +CrawlStoreValue::operator char&() { return get_byte(); } +CrawlStoreValue::operator short&() { return get_short(); } +CrawlStoreValue::operator float&() { return get_float(); } +CrawlStoreValue::operator long&() { return get_long(); } +CrawlStoreValue::operator std::string&() { return get_string(); } +CrawlStoreValue::operator coord_def&() { return get_coord(); } +CrawlStoreValue::operator CrawlHashTable&() { return get_table(); } +CrawlStoreValue::operator CrawlVector&() { return get_vector(); } +CrawlStoreValue::operator item_def&() { return get_item(); } +CrawlStoreValue::operator level_id&() { return get_level_id(); } +CrawlStoreValue::operator level_pos&() { return get_level_pos(); } #else &CrawlStoreValue::operator bool() { @@ -837,6 +951,16 @@ CrawlStoreValue::operator item_def&() { return get_item(); } { return get_item(); } + +&CrawlStoreValue::operator level_id() +{ + return get_level_id(); +} + +&CrawlStoreValue::operator level_pos() +{ + return get_level_pos(); +} #endif /////////////////////////// @@ -890,6 +1014,16 @@ CrawlStoreValue::operator coord_def() const return get_coord(); } +CrawlStoreValue::operator level_id() const +{ + return get_level_id(); +} + +CrawlStoreValue::operator level_pos() const +{ + return get_level_pos(); +} + /////////////////////// // Assignment operators CrawlStoreValue &CrawlStoreValue::operator = (const bool &_val) @@ -958,6 +1092,18 @@ CrawlStoreValue &CrawlStoreValue::operator = (const item_def &_val) return (*this); } +CrawlStoreValue &CrawlStoreValue::operator = (const level_id &_val) +{ + get_level_id() = _val; + return (*this); +} + +CrawlStoreValue &CrawlStoreValue::operator = (const level_pos &_val) +{ + get_level_pos() = _val; + return (*this); +} + /////////////////////////////////////////////////// // Non-assignment operators which affect the lvalue #define INT_OPERATOR_UNARY(op) \ @@ -1159,11 +1305,16 @@ void CrawlHashTable::assert_validity() const ASSERT(val.type != SV_NONE); ASSERT(!(val.flags & SFLAG_UNSET)); + if (type != SV_NONE) + ASSERT(type == val.type); + switch (val.type) { case SV_STR: case SV_COORD: case SV_ITEM: + case SV_LEV_ID: + case SV_LEV_POS: ASSERT(val.val.ptr != NULL); break; @@ -1178,6 +1329,17 @@ void CrawlHashTable::assert_validity() const break; } + case SV_VEC: + { + ASSERT(val.val.ptr != NULL); + + CrawlVector* nested; + nested = static_cast(val.val.ptr); + + nested->assert_validity(); + break; + } + default: break; } @@ -1430,6 +1592,9 @@ void CrawlVector::assert_validity() const { const CrawlStoreValue &val = vector[i]; + if (type != SV_NONE) + ASSERT(val.type == SV_NONE || val.type == type); + // A vector might be resize()'d and filled up with unset // values, which are then set one by one, so we can't // assert over that here. @@ -1441,6 +1606,8 @@ void CrawlVector::assert_validity() const case SV_STR: case SV_COORD: case SV_ITEM: + case SV_LEV_ID: + case SV_LEV_POS: ASSERT(val.val.ptr != NULL); break; @@ -1448,8 +1615,8 @@ void CrawlVector::assert_validity() const { ASSERT(val.val.ptr != NULL); - CrawlVector* nested; - nested = static_cast(val.val.ptr); + CrawlHashTable* nested; + nested = static_cast(val.val.ptr); nested->assert_validity(); break; @@ -1487,7 +1654,6 @@ vec_size CrawlVector::get_max_size() const return max_size; } - //////////////////////////////// // Accessors to contained values @@ -1545,6 +1711,47 @@ CrawlStoreValue& CrawlVector::pop_back() void CrawlVector::push_back(CrawlStoreValue val) { +#ifdef DEBUG + if (type != SV_NONE) + ASSERT(type == val.type); + + switch (val.type) + { + case SV_STR: + case SV_COORD: + case SV_ITEM: + case SV_LEV_ID: + case SV_LEV_POS: + ASSERT(val.val.ptr != NULL); + break; + + case SV_HASH: + { + ASSERT(val.val.ptr != NULL); + + CrawlHashTable* nested; + nested = static_cast(val.val.ptr); + + nested->assert_validity(); + break; + } + + case SV_VEC: + { + ASSERT(val.val.ptr != NULL); + + CrawlVector* nested; + nested = static_cast(val.val.ptr); + + nested->assert_validity(); + break; + } + + default: + break; + } +#endif + assert_validity(); ASSERT(vector.size() < max_size); ASSERT(type == SV_NONE @@ -1557,6 +1764,7 @@ void CrawlVector::push_back(CrawlStoreValue val) val.flags |= SFLAG_CONST_TYPE; } vector.push_back(val); + assert_validity(); } void CrawlVector::insert(const vec_size index, CrawlStoreValue val) diff --git a/crawl-ref/source/store.h b/crawl-ref/source/store.h index 7076b3812f..1eae58f2b2 100644 --- a/crawl-ref/source/store.h +++ b/crawl-ref/source/store.h @@ -19,6 +19,8 @@ class CrawlHashTable; class CrawlVector; struct item_def; struct coord_def; +struct level_pos; +class level_id; typedef unsigned char hash_size; typedef unsigned char vec_size; @@ -42,6 +44,8 @@ enum store_val_type SV_ITEM, SV_HASH, SV_VEC, + SV_LEV_ID, + SV_LEV_POS, NUM_STORE_VAL_TYPES }; @@ -88,6 +92,8 @@ public: CrawlStoreValue(const item_def &val); CrawlStoreValue(const CrawlHashTable &val); CrawlStoreValue(const CrawlVector &val); + CrawlStoreValue(const level_id &val); + CrawlStoreValue(const level_pos &val); // Only needed for doing some assertion checking. CrawlStoreValue &operator = (const CrawlStoreValue &other); @@ -121,14 +127,18 @@ public: CrawlHashTable &get_table(); CrawlVector &get_vector(); item_def &get_item(); - - bool get_bool() const; - char get_byte() const; - short get_short() const; - long get_long() const; - float get_float() const; - std::string get_string() const; - coord_def get_coord() const; + level_id &get_level_id(); + level_pos &get_level_pos(); + + bool get_bool() const; + char get_byte() const; + short get_short() const; + long get_long() const; + float get_float() const; + std::string get_string() const; + coord_def get_coord() const; + level_id get_level_id() const; + level_pos get_level_pos() const; const CrawlHashTable& get_table() const; const CrawlVector& get_vector() const; @@ -177,6 +187,8 @@ public: operator CrawlHashTable&(); operator CrawlVector&(); operator item_def&(); + operator level_id&(); + operator level_pos&(); #else &operator bool(); &operator char(); @@ -188,15 +200,19 @@ public: &operator CrawlHashTable(); &operator CrawlVector(); &operator item_def(); + &operator level_id(); + &operator level_pos(); #endif - operator bool() const; - operator char() const; - operator short() const; - operator long() const; - operator float() const; + operator bool() const; + operator char() const; + operator short() const; + operator long() const; + operator float() const; operator std::string() const; - operator coord_def() const; + operator coord_def() const; + operator level_id() const; + operator level_pos() const; // Assignment operators CrawlStoreValue &operator = (const bool &val); @@ -210,6 +226,8 @@ public: CrawlStoreValue &operator = (const CrawlHashTable &val); CrawlStoreValue &operator = (const CrawlVector &val); CrawlStoreValue &operator = (const item_def &val); + CrawlStoreValue &operator = (const level_id &val); + CrawlStoreValue &operator = (const level_pos &val); // Misc operators std::string &operator += (const std::string &val); -- cgit v1.2.3-54-g00ecf