C4Set: Replace elements with zero-initialized ones on clear

C4Set used to set its removed elements to nullptr. This requires
some special handling from non-pointer entries like C4Property.
Overwriting the element with a zero-initialized one removes this
requirement, leading to improved type safety on the part of
C4Property.
master
Nicolas Hake 2019-01-01 21:29:19 +01:00
parent 15ad4fb690
commit 31c7805f10
2 changed files with 4 additions and 6 deletions

View File

@ -54,8 +54,6 @@ public:
C4String * Key{nullptr}; C4String * Key{nullptr};
C4Value Value; C4Value Value;
operator const void * () const { return Key; } operator const void * () const { return Key; }
C4Property & operator = (void * p)
{ assert(!p); if (Key) Key->DecRef(); Key = nullptr; Value.Set0(); return *this; }
bool operator < (const C4Property &cmp) const { return strcmp(GetSafeKey(), cmp.GetSafeKey())<0; } bool operator < (const C4Property &cmp) const { return strcmp(GetSafeKey(), cmp.GetSafeKey())<0; }
const char *GetSafeKey() const { if (Key && Key->GetCStr()) return Key->GetCStr(); return ""; } // get key as C string; return "" if undefined. never return nullptr const char *GetSafeKey() const { if (Key && Key->GetCStr()) return Key->GetCStr(); return ""; } // get key as C string; return "" if undefined. never return nullptr
}; };

View File

@ -2,7 +2,7 @@
* OpenClonk, http://www.openclonk.org * OpenClonk, http://www.openclonk.org
* *
* Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de/ * Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de/
* Copyright (c) 2009-2016, The OpenClonk Team and contributors * Copyright (c) 2009-2019, The OpenClonk Team and contributors
* *
* Distributed under the terms of the ISC license; see accompanying file * Distributed under the terms of the ISC license; see accompanying file
* "COPYING" for details. * "COPYING" for details.
@ -145,7 +145,7 @@ template<typename T> class C4Set
void ClearTable() void ClearTable()
{ {
for (unsigned int i = 0; i < Capacity; ++i) for (unsigned int i = 0; i < Capacity; ++i)
Table[i] = nullptr; Table[i] = T{};
} }
void MaintainCapacity() void MaintainCapacity()
{ {
@ -240,13 +240,13 @@ public:
r = &Table[++h % Capacity]; r = &Table[++h % Capacity];
} }
assert(*r); assert(*r);
*r = nullptr; *r = T{};
--Size; --Size;
// Move entries which might have collided with e // Move entries which might have collided with e
while (*(r = &Table[++h % Capacity])) while (*(r = &Table[++h % Capacity]))
{ {
T m = *r; T m = *r;
*r = nullptr; *r = T{};
AddInternal(std::move(m)); AddInternal(std::move(m));
} }
} }