(svn r10753) -Codechange: make the sign struct use the pool item class as super class.
authorrubidium
Thu, 02 Aug 2007 13:27:45 +0000
changeset 7384 353183a5cd3a
parent 7383 864c7b8e1c52
child 7385 69832b54e5af
(svn r10753) -Codechange: make the sign struct use the pool item class as super class.
src/signs.cpp
src/signs.h
--- a/src/signs.cpp	Thu Aug 02 13:18:57 2007 +0000
+++ b/src/signs.cpp	Thu Aug 02 13:27:45 2007 +0000
@@ -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;
-
-	/* 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++;
+	this->str = string;
 }
 
-/* Initialize the sign-pool */
-DEFINE_OLD_POOL(Sign, Sign, SignPoolNewBlock, NULL)
+Sign::~Sign()
+{
+	this->QuickFree();
+	this->str = STR_NULL;
+}
+
+void Sign::QuickFree()
+{
+	DeleteName(this->str);
+}
 
 /**
  *
@@ -77,45 +82,6 @@
 }
 
 /**
- *
- * 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,
  * but everybody is able to rename/remove it.
@@ -126,18 +92,16 @@
  */
 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<Sign> 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 @@
 		_sign_sort_dirty = true;
 		_new_sign_id = si->index;
 		_total_signs++;
+		s_auto_delete.Detach();
 	}
 
 	return CommandCost();
@@ -197,7 +162,7 @@
 			Sign *si = GetSign(p1);
 
 			MarkSignDirty(si);
-			DeleteSign(si);
+			delete si;
 
 			InvalidateWindow(WC_SIGN_LIST, 0);
 			_sign_sort_dirty = true;
@@ -242,8 +207,8 @@
 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 @@
 	_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++;
--- a/src/signs.h	Thu Aug 02 13:18:57 2007 +0000
+++ b/src/signs.h	Thu Aug 02 13:27:45 2007 +0000
@@ -7,7 +7,10 @@
 
 #include "oldpool.h"
 
-struct Sign {
+struct Sign;
+DECLARE_OLD_POOL(Sign, Sign, 2, 16000)
+
+struct Sign : PoolItem<Sign, SignID, &_Sign_pool> {
 	StringID     str;
 	ViewportSign sign;
 	int32        x;
@@ -15,7 +18,17 @@
 	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 @@
 
 extern SignID _new_sign_id;
 
-DECLARE_OLD_POOL(Sign, Sign, 2, 16000)
 
 static inline SignID GetMaxSignIndex()
 {
@@ -42,28 +54,12 @@
 	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));
+	return index < GetSignPoolSize() && GetSign(index)->IsValid();
 }
 
-void DestroySign(Sign *si);
-
-static inline void DeleteSign(Sign *si)
-{
-	DestroySign(si);
-	si->str = STR_NULL;
-}
-
-#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;