Add Clone() implementation for StringMap#852
Conversation
The implementation is a shallow copy (which I think is all we can sanely do and matches ArrayList), was something else intended? |
|
@asherkin unless i'm mistaken, these are deep copies. Any modifications to one structure will leave the other completely unaffected. Trie stuff like CCellArray::clone() also looks like a deep copy sourcemod/core/logic/CellArray.h Lines 183 to 197 in 3baa703 |
KyleSanderson
left a comment
There was a problem hiding this comment.
Is this actively desired? It seems like a thing that could be used but giving some lineage on the drive behind a PR helps better understand intentions.
|
@KyleSanderson I’m unsure if ArrayStack’s clone is used (or if people use ArrayStack at all), but I think being able to clone a StringMap is a really good addition into core. |
|
Stack is used by many; cloning a stack I'm not sure on as it likely predates most of us. The trick is to jump on BZ (or now here) and looking at why something was added. Nice-to-haves are legit, just wanted some lineage. All good. |
|
Ah, my bad. All should be well now. |
|
For ref/lineage: simple tests seem to pass as-expected. public void OnPluginStart()
{
StringMap original = new StringMap();
original.SetValue("bool", true);
original.SetValue("handle", new ArrayList());
original.SetString("string", "hello world");
StringMap copy = original.Clone();
delete original;
bool ret = false;
bool value;
ret = copy.GetValue("bool", value);
PrintToServer("bool test: %s", (ret && value)?"successful":"unsuccessful");
ArrayList handle;
ret = copy.GetValue("handle", handle);
PrintToServer("hndl test: %s", (ret && handle != null)?"successful":"unsuccessful");
char string[32];
ret = copy.GetString("string", string, sizeof string);
PrintToServer("string test: %s", (ret && strlen(string) > 0)?"successful":"unsuccessful");
delete copy;
delete handle;
} |
KyleSanderson
left a comment
There was a problem hiding this comment.
That's it, mention the PR # like every other line item cause and you're probably golden.
We implemented a clone for
ArrayList, butArrayStackandStringMapwere left without one. This pull request adds deep-copy cloning to said structures. Construct generally looks likeFixes #796