Skip to content

Commit

Permalink
C4Set: Replace elements with zero-initialized ones on clear
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
isilkor committed Jan 1, 2019
1 parent 15ad4fb commit 31c7805
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 6 deletions.
2 changes: 0 additions & 2 deletions src/script/C4PropList.h
Expand Up @@ -54,8 +54,6 @@ class C4Property
C4String * Key{nullptr};
C4Value Value;
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; }
const char *GetSafeKey() const { if (Key && Key->GetCStr()) return Key->GetCStr(); return ""; } // get key as C string; return "" if undefined. never return nullptr
};
Expand Down
8 changes: 4 additions & 4 deletions src/script/C4StringTable.h
Expand Up @@ -2,7 +2,7 @@
* OpenClonk, http://www.openclonk.org
*
* 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
* "COPYING" for details.
Expand Down Expand Up @@ -145,7 +145,7 @@ template<typename T> class C4Set
void ClearTable()
{
for (unsigned int i = 0; i < Capacity; ++i)
Table[i] = nullptr;
Table[i] = T{};
}
void MaintainCapacity()
{
Expand Down Expand Up @@ -240,13 +240,13 @@ template<typename T> class C4Set
r = &Table[++h % Capacity];
}
assert(*r);
*r = nullptr;
*r = T{};
--Size;
// Move entries which might have collided with e
while (*(r = &Table[++h % Capacity]))
{
T m = *r;
*r = nullptr;
*r = T{};
AddInternal(std::move(m));
}
}
Expand Down

0 comments on commit 31c7805

Please sign in to comment.