Changeset - r27875:69bbd9979d6f
[Not reviewed]
master
0 1 0
Peter Nelson - 9 months ago 2023-09-08 19:41:46
peter1138@openttd.org
Codechange: Use std::array for TemporaryStorageArray.

This removes the need for initialisation with memset().
1 file changed with 6 insertions and 11 deletions:
0 comments (0 inline, 0 general)
src/newgrf_storage.h
Show inline comments
 
@@ -131,17 +131,12 @@ struct PersistentStorageArray : BasePers
 
 */
 
template <typename TYPE, uint SIZE>
 
struct TemporaryStorageArray {
 
	TYPE storage[SIZE]; ///< Memory to for the storage array
 
	uint16_t init[SIZE];  ///< Storage has been assigned, if this equals 'init_key'.
 
	uint16_t init_key;    ///< Magic key to 'init'.
 
	using StorageType = std::array<TYPE, SIZE>;
 
	using StorageInitType = std::array<uint16_t, SIZE>;
 

	
 
	/** Simply construct the array */
 
	TemporaryStorageArray()
 
	{
 
		memset(this->storage, 0, sizeof(this->storage)); // not exactly needed, but makes code analysers happy
 
		memset(this->init, 0, sizeof(this->init));
 
		this->init_key = 1;
 
	}
 
	StorageType storage{}; ///< Memory for the storage array
 
	StorageInitType init{}; ///< Storage has been assigned, if this equals 'init_key'.
 
	uint16_t init_key{1}; ///< Magic key to 'init'.
 

	
 
	/**
 
	 * Stores some value at a given position.
 
@@ -181,7 +176,7 @@ struct TemporaryStorageArray {
 
		this->init_key++;
 
		if (this->init_key == 0) {
 
			/* When init_key wraps around, we need to reset everything */
 
			memset(this->init, 0, sizeof(this->init));
 
			this->init = {};
 
			this->init_key = 1;
 
		}
 
	}
0 comments (0 inline, 0 general)