From f9c6775dfd44e962f047be7671198bce2e08a356 Mon Sep 17 00:00:00 2001 From: rubidium Date: Thu, 2 Aug 2007 13:27:45 +0000 Subject: (svn r10753) -Codechange: make the sign struct use the pool item class as super class. --- src/signs.cpp | 84 ++++++++++++++++------------------------------------------- src/signs.h | 38 ++++++++++++--------------- 2 files changed, 39 insertions(+), 83 deletions(-) diff --git a/src/signs.cpp b/src/signs.cpp index f2a7cefa8..8e05f2c01 100644 --- a/src/signs.cpp +++ b/src/signs.cpp @@ -13,24 +13,29 @@ #include "saveload.h" #include "command.h" #include "variables.h" +#include "misc/autoptr.hpp" SignID _new_sign_id; uint _total_signs; -/** - * Called if a new block is added to the sign-pool - */ -static void SignPoolNewBlock(uint start_item) +/* Initialize the sign-pool */ +DEFINE_OLD_POOL_GENERIC(Sign, Sign) + +Sign::Sign(StringID string) { - Sign *si; + this->str = string; +} - /* We don't use FOR_ALL here, because FOR_ALL skips invalid items. - * TODO - This is just a temporary stage, this will be removed. */ - for (si = GetSign(start_item); si != NULL; si = (si->index + 1U < GetSignPoolSize()) ? GetSign(si->index + 1U) : NULL) si->index = start_item++; +Sign::~Sign() +{ + this->QuickFree(); + this->str = STR_NULL; } -/* Initialize the sign-pool */ -DEFINE_OLD_POOL(Sign, Sign, SignPoolNewBlock, NULL) +void Sign::QuickFree() +{ + DeleteName(this->str); +} /** * @@ -76,45 +81,6 @@ static void MarkSignDirty(Sign *si) si->sign.top + ScaleByZoom(12, ZOOM_LVL_MAX)); } -/** - * - * Allocates a new sign - * - * @return The pointer to the new sign, or NULL if there is no more free space - */ -static Sign *AllocateSign() -{ - Sign *si; - - /* We don't use FOR_ALL here, because FOR_ALL skips invalid items. - * TODO - This is just a temporary stage, this will be removed. */ - for (si = GetSign(0); si != NULL; si = (si->index + 1U < GetSignPoolSize()) ? GetSign(si->index + 1U) : NULL) { - if (!IsValidSign(si)) { - uint index = si->index; - - memset(si, 0, sizeof(Sign)); - si->index = index; - - return si; - } - } - - /* Check if we can add a block to the pool */ - if (AddBlockToPool(&_Sign_pool)) - return AllocateSign(); - - return NULL; -} - -/** - * Destroy a sign placed on the map - * @param si Pointer to the Sign to remove - */ -void DestroySign(Sign *si) -{ - DeleteName(si->str); -} - /** * Place a sign at the given coordinates. Ownership of sign has * no effect whatsoever except for the colour the sign gets for easy recognition, @@ -126,18 +92,16 @@ void DestroySign(Sign *si) */ CommandCost CmdPlaceSign(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) { - Sign *si; - /* Try to locate a new sign */ - si = AllocateSign(); + Sign *si = new Sign(STR_280A_SIGN); if (si == NULL) return_cmd_error(STR_2808_TOO_MANY_SIGNS); + AutoPtrT s_auto_delete = si; /* When we execute, really make the sign */ if (flags & DC_EXEC) { int x = TileX(tile) * TILE_SIZE; int y = TileY(tile) * TILE_SIZE; - si->str = STR_280A_SIGN; si->x = x; si->y = y; si->owner = _current_player; // owner of the sign; just eyecandy @@ -148,6 +112,7 @@ CommandCost CmdPlaceSign(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) _sign_sort_dirty = true; _new_sign_id = si->index; _total_signs++; + s_auto_delete.Detach(); } return CommandCost(); @@ -197,7 +162,7 @@ CommandCost CmdRenameSign(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) Sign *si = GetSign(p1); MarkSignDirty(si); - DeleteSign(si); + delete si; InvalidateWindow(WC_SIGN_LIST, 0); _sign_sort_dirty = true; @@ -242,8 +207,8 @@ void PlaceProc_Sign(TileIndex tile) void InitializeSigns() { _total_signs = 0; - CleanPool(&_Sign_pool); - AddBlockToPool(&_Sign_pool); + _Sign_pool.CleanPool(); + _Sign_pool.AddBlockToPool(); } static const SaveLoad _sign_desc[] = { @@ -282,12 +247,7 @@ static void Load_SIGN() _total_signs = 0; int index; while ((index = SlIterateArray()) != -1) { - Sign *si; - - if (!AddBlockIfNeeded(&_Sign_pool, index)) - error("Signs: failed loading savegame: too many signs"); - - si = GetSign(index); + Sign *si = new (index) Sign(); SlObject(si, _sign_desc); _total_signs++; diff --git a/src/signs.h b/src/signs.h index 53551586f..d33578807 100644 --- a/src/signs.h +++ b/src/signs.h @@ -7,7 +7,10 @@ #include "oldpool.h" -struct Sign { +struct Sign; +DECLARE_OLD_POOL(Sign, Sign, 2, 16000) + +struct Sign : PoolItem { StringID str; ViewportSign sign; int32 x; @@ -15,7 +18,17 @@ struct Sign { byte z; PlayerByte owner; // placed by this player. Anyone can delete them though. OWNER_NONE for gray signs from old games. - SignID index; + /** + * Creates a new sign + */ + Sign(StringID string = STR_NULL); + + /** Destroy the sign */ + ~Sign(); + + bool IsValid() const { return this->str != STR_NULL; } + + void QuickFree(); }; enum { @@ -24,7 +37,6 @@ enum { extern SignID _new_sign_id; -DECLARE_OLD_POOL(Sign, Sign, 2, 16000) static inline SignID GetMaxSignIndex() { @@ -42,28 +54,12 @@ static inline uint GetNumSigns() return _total_signs; } -/** - * Check if a Sign really exists. - */ -static inline bool IsValidSign(const Sign *si) -{ - return si->str != STR_NULL; -} - static inline bool IsValidSignID(uint index) { - return index < GetSignPoolSize() && IsValidSign(GetSign(index)); -} - -void DestroySign(Sign *si); - -static inline void DeleteSign(Sign *si) -{ - DestroySign(si); - si->str = STR_NULL; + return index < GetSignPoolSize() && GetSign(index)->IsValid(); } -#define FOR_ALL_SIGNS_FROM(ss, start) for (ss = GetSign(start); ss != NULL; ss = (ss->index + 1U < GetSignPoolSize()) ? GetSign(ss->index + 1U) : NULL) if (IsValidSign(ss)) +#define FOR_ALL_SIGNS_FROM(ss, start) for (ss = GetSign(start); ss != NULL; ss = (ss->index + 1U < GetSignPoolSize()) ? GetSign(ss->index + 1U) : NULL) if (ss->IsValid()) #define FOR_ALL_SIGNS(ss) FOR_ALL_SIGNS_FROM(ss, 0) VARDEF bool _sign_sort_dirty; -- cgit v1.2.3-54-g00ecf