Added GetKey() / SetKey() in con_set's Entry, so ScriptVariable can override it and set the key appropriately

This commit is contained in:
smallmodel 2023-11-28 19:28:19 +01:00
parent 2ac2800ecb
commit b10ce9a426
No known key found for this signature in database
GPG key ID: 9F2D623CEDF08512
6 changed files with 105 additions and 62 deletions

View file

@ -279,7 +279,6 @@ void con_set<key, value>::Archive(Archiver& arc)
Entry *e;
int hash;
int i;
int total;
arc.ArchiveUnsigned(&tableLength);
arc.ArchiveUnsigned(&threshold);
@ -296,12 +295,15 @@ void con_set<key, value>::Archive(Archiver& arc)
e = new Entry;
e->Archive(arc);
hash = HashCode<key>(e->key) % tableLength;
hash = HashCode<key>(e->GetKey()) % tableLength;
e->next = table[hash];
table[hash] = e;
}
} else {
#ifndef NDEBUG
int total;
total = 0;
for (i = 0; i < tableLength; i++) {
@ -310,7 +312,15 @@ void con_set<key, value>::Archive(Archiver& arc)
total++;
}
}
// it must match the number of elements
assert(total == count);
#else
for (i = 0; i < tableLength; i++) {
for (e = table[i]; e != NULL; e = e->next) {
e->Archive(arc);
}
}
#endif
}
}

View file

@ -1199,7 +1199,7 @@ void ScriptMaster::PrintThread(int iThreadNum)
int i = 0;
for (entry = en.NextElement(); entry != NULL; entry = en.NextElement()) {
str& name = Director.GetString(entry->key);
str& name = Director.GetString(entry->GetKey());
if (i > 0) {
status += ", ";

View file

@ -29,6 +29,9 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
class Class;
class Archiver;
template<typename key, typename value>
class con_map;
template<typename key, typename value>
class con_map_enum;
@ -53,11 +56,15 @@ class con_set
public:
class Entry
{
public:
k key;
v value;
friend con_set<k, v>;
friend con_set_enum<k, v>;
Entry *next;
private:
Entry* next;
k key;
public:
v value;
public:
void *operator new(size_t size);
@ -68,6 +75,8 @@ public:
#ifdef ARCHIVE_SUPPORTED
void Archive(Archiver& arc);
#endif
k& GetKey();
void SetKey(const k& newKey);
};
public:
@ -137,6 +146,18 @@ con_set<k, v>::Entry::Entry()
next = NULL;
}
template<typename k, typename v>
void con_set<k, v>::Entry::SetKey(const k& newKey)
{
key = newKey;
}
template<typename k, typename v>
k& con_set<k, v>::Entry::GetKey()
{
return key;
}
template<typename key, typename value>
con_set<key, value>::con_set()
{
@ -217,7 +238,7 @@ void con_set<key, value>::resize(int count)
old = e->next;
// insert the old entry to the table hashindex
index = HashCode<key>(e->key) % tableLength;
index = HashCode<key>(e->GetKey()) % tableLength;
e->next = table[index];
table[index] = e;
@ -238,7 +259,7 @@ typename con_set<k, v>::Entry *con_set<k, v>::findKeyEntry(const k& key) const
entry = table[HashCode<k>(key) % tableLength];
for (; entry != NULL; entry = entry->next) {
if (entry->key == key) {
if (entry->GetKey() == key) {
return entry;
}
}
@ -283,7 +304,7 @@ typename con_set<k, v>::Entry *con_set<k, v>::addNewKeyEntry(const k& key)
entry->next = table[index];
}
entry->key = key;
entry->SetKey(key);
table[index] = entry;
@ -299,13 +320,15 @@ bool con_set<key, value>::isEmpty(void)
template<typename k, typename v>
bool con_set<k, v>::remove(const k& key)
{
int index = HashCode<k>(key) % tableLength;
int hash;
Entry *prev = NULL;
Entry *entry;
for (entry = table[index]; entry != NULL; entry = entry->next) {
// just to make sure we're using the correct overloaded operator
if (!(entry->key == key)) {
hash = HashCode<k>(key) % tableLength;
for (entry = table[hash]; entry != NULL; entry = entry->next) {
// just to make sure we're using the correct overloaded operator for the key
if (!(entry->GetKey() == key)) {
prev = entry;
continue;
}
@ -317,7 +340,7 @@ bool con_set<k, v>::remove(const k& key)
if (prev) {
prev->next = entry->next;
} else {
table[index] = entry->next;
table[hash] = entry->next;
}
count--;
@ -345,7 +368,7 @@ template<typename key, typename value>
key *con_set<key, value>::firstKeyValue(void)
{
if (defaultEntry) {
return &defaultEntry->key;
return &defaultEntry->GetKey();
} else {
return NULL;
}
@ -373,7 +396,7 @@ bool con_set<k, v>::keyExists(const k& key)
Entry *entry;
for (entry = table; entry != NULL; entry = entry->next) {
if (entry->key == key) {
if (entry->GetKey() == key) {
return true;
}
}
@ -582,7 +605,7 @@ key *con_map_enum<key, value>::CurrentKey(void)
Entry *entry = m_Set_Enum.CurrentElement();
if (entry) {
return &entry->key;
return &entry->GetKey();
} else {
return NULL;
}
@ -606,7 +629,7 @@ key *con_map_enum<key, value>::NextKey(void)
Entry *entry = m_Set_Enum.NextElement();
if (entry) {
return &entry->key;
return &entry->GetKey();
} else {
return NULL;
}

View file

@ -3204,7 +3204,7 @@ void Listener::CancelWaitingAll()
ConList stoppedListeners;
for (e = en.NextElement(); e != NULL; e = en.NextElement()) {
CancelWaitingSources(e->key, e->value, stoppedListeners);
CancelWaitingSources(e->GetKey(), e->value, stoppedListeners);
}
delete m_WaitForList;
@ -3504,7 +3504,7 @@ void Listener::UnregisterAll(void)
en = *m_NotifyList;
for (e = en.NextElement(); e != NULL; e = en.NextElement()) {
UnregisterTargets(e->key, e->value, stoppedListeners, stoppedNames);
UnregisterTargets(e->GetKey(), e->value, stoppedListeners, stoppedNames);
}
delete m_NotifyList;

View file

@ -1,6 +1,6 @@
/*
===========================================================================
Copyright (C) 2015 the OpenMoHAA team
Copyright (C) 2023 the OpenMoHAA team
This file is part of OpenMoHAA source code.
@ -25,6 +25,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#include "scriptvariable.h"
#include "scriptexception.h"
#include "../qcommon/str.h"
#include "../qcommon/con_set.h"
#ifdef GAME_DLL
# include "../fgame/archive.h"
@ -77,19 +78,6 @@ void con_set<ScriptVariable, ScriptVariable>::Entry::Archive(Archiver& arc)
value.ArchiveInternal(arc);
}
template<>
void con_set<short3, ScriptVariable>::Entry::Archive(Archiver& arc)
{
if (arc.Loading()) {
value.Archive(arc);
# ifdef WITH_SCRIPT_ENGINE
key = value.GetKey();
# endif
} else {
value.Archive(arc);
}
}
void ScriptArrayHolder::Archive(Archiver& arc)
{
arc.ArchiveUnsigned(&refCount);
@ -1512,9 +1500,10 @@ void ScriptVariable::operator+=(const ScriptVariable& value)
case VARIABLE_STRING
+ VARIABLE_LISTENER *VARIABLE_MAX: // ( string ) + ( listener )
case VARIABLE_CONSTSTRING
+ VARIABLE_LISTENER *VARIABLE_MAX: // ( const string ) + ( listener )
case VARIABLE_STRING + VARIABLE_VECTOR *VARIABLE_MAX: // ( string ) + ( vector )
case VARIABLE_CONSTSTRING + VARIABLE_VECTOR *VARIABLE_MAX: // ( const string ) + ( vector )
+ VARIABLE_LISTENER *VARIABLE_MAX: // ( const string ) + ( listener )
case VARIABLE_STRING + VARIABLE_VECTOR *VARIABLE_MAX: // ( string ) + ( vector )
case VARIABLE_CONSTSTRING
+ VARIABLE_VECTOR *VARIABLE_MAX: // ( const string ) + ( vector )
setStringValue(stringValue() + value.stringValue());
break;
@ -1972,8 +1961,9 @@ bool ScriptVariable::operator==(const ScriptVariable& value)
+ VARIABLE_STRING *VARIABLE_MAX: // ( int ) == ( string )
case VARIABLE_FLOAT + VARIABLE_STRING *VARIABLE_MAX: // ( float ) == ( string )
case VARIABLE_CHAR
+ VARIABLE_STRING *VARIABLE_MAX: // ( char ) == ( string )
case VARIABLE_CONSTSTRING + VARIABLE_STRING *VARIABLE_MAX: // ( const string ) == ( string )
+ VARIABLE_STRING *VARIABLE_MAX: // ( char ) == ( string )
case VARIABLE_CONSTSTRING
+ VARIABLE_STRING *VARIABLE_MAX: // ( const string ) == ( string )
case VARIABLE_LISTENER
+ VARIABLE_STRING *VARIABLE_MAX: // ( listener ) == ( string )
case VARIABLE_VECTOR + VARIABLE_STRING *VARIABLE_MAX: // ( vector ) == ( string )
@ -1997,9 +1987,10 @@ bool ScriptVariable::operator==(const ScriptVariable& value)
case VARIABLE_CONSTSTRING + VARIABLE_CHAR *VARIABLE_MAX: // ( const string ) == ( char )
case VARIABLE_STRING + VARIABLE_LISTENER *VARIABLE_MAX: // ( string ) == ( listener )
case VARIABLE_CONSTSTRING
+ VARIABLE_LISTENER *VARIABLE_MAX: // ( const string ) == ( listener )
case VARIABLE_STRING + VARIABLE_VECTOR *VARIABLE_MAX: // ( string ) == ( vector )
case VARIABLE_CONSTSTRING + VARIABLE_VECTOR *VARIABLE_MAX: // ( const string ) == ( vector )
+ VARIABLE_LISTENER *VARIABLE_MAX: // ( const string ) == ( listener )
case VARIABLE_STRING + VARIABLE_VECTOR *VARIABLE_MAX: // ( string ) == ( vector )
case VARIABLE_CONSTSTRING
+ VARIABLE_VECTOR *VARIABLE_MAX: // ( const string ) == ( vector )
{
str lval = stringValue();
str rval = value.stringValue();
@ -2455,6 +2446,24 @@ ScriptVariable ScriptVariable::operator--(int)
#ifdef WITH_SCRIPT_ENGINE
template<>
class con_set<short3, ScriptVariable>::Entry
{
friend con_set<short3, ScriptVariable>;
friend con_set_enum<short3, ScriptVariable>;
private:
Entry *next;
ScriptVariable value;
public:
# ifdef ARCHIVE_SUPPORTED
void Archive(Archiver& arc) { value.Archive(arc); }
# endif
short3& GetKey() { return value.GetKey(); }
void SetKey(const short3& newKey) { value.SetKey(newKey); }
};
ScriptVariableList::ScriptVariableList() {}
void ScriptVariableList::ClearList(void)

View file

@ -1,6 +1,6 @@
/*
===========================================================================
Copyright (C) 2015 the OpenMoHAA team
Copyright (C) 2023 the OpenMoHAA team
This file is part of OpenMoHAA source code.
@ -64,7 +64,8 @@ static const char *typenames[] = {
"array",
"pointer",
"vector",
"double"};
"double"
};
class ScriptArrayHolder;
class ScriptConstArrayHolder;
@ -110,9 +111,9 @@ public:
~ScriptVariable();
void Archive(Archiver &arc);
void Archive(Archiver& arc);
static void Archive(Archiver& arc, ScriptVariable **obj);
void ArchiveInternal(Archiver &arc);
void ArchiveInternal(Archiver& arc);
void CastBoolean(void);
void CastConstArrayValue(void);
@ -152,7 +153,7 @@ public:
str& getName(void);
short3& GetKey();
void SetKey(const short3 &key);
void SetKey(const short3& key);
#endif
Entity *entityValue(void);
@ -201,7 +202,7 @@ public:
#endif
Vector vectorValue(void) const;
void setVectorValue(const Vector &newvector);
void setVectorValue(const Vector& newvector);
class PathNode *pathNodeValue(void) const;
class Waypoint *waypointValue(void) const;
@ -218,16 +219,16 @@ public:
ScriptVariable& operator[](ScriptVariable& index);
ScriptVariable *operator[](unsigned index) const;
ScriptVariable *operator*();
void operator+=(const ScriptVariable &value);
void operator-=(const ScriptVariable &value);
void operator*=(const ScriptVariable &value);
void operator/=(const ScriptVariable &value);
void operator%=(const ScriptVariable &value);
void operator&=(const ScriptVariable &value);
void operator^=(const ScriptVariable &value);
void operator|=(const ScriptVariable &value);
void operator<<=(const ScriptVariable &value);
void operator>>=(const ScriptVariable &value);
void operator+=(const ScriptVariable& value);
void operator-=(const ScriptVariable& value);
void operator*=(const ScriptVariable& value);
void operator/=(const ScriptVariable& value);
void operator%=(const ScriptVariable& value);
void operator&=(const ScriptVariable& value);
void operator^=(const ScriptVariable& value);
void operator|=(const ScriptVariable& value);
void operator<<=(const ScriptVariable& value);
void operator>>=(const ScriptVariable& value);
bool operator!=(const ScriptVariable& value);
bool operator==(const ScriptVariable& value);
@ -245,7 +246,7 @@ public:
public:
ScriptArrayHolder();
void Archive(Archiver &arc);
void Archive(Archiver& arc);
static void Archive(Archiver& arc, ScriptArrayHolder *& arrayValue);
};
@ -257,7 +258,7 @@ public:
unsigned int size;
public:
void Archive(Archiver &arc);
void Archive(Archiver& arc);
static void Archive(Archiver& arc, ScriptConstArrayHolder *& constArrayValue);
ScriptConstArrayHolder(ScriptVariable *pVar, unsigned int size);
@ -272,7 +273,7 @@ public:
Container<ScriptVariable *> list;
public:
void Archive(Archiver &arc);
void Archive(Archiver& arc);
static void Archive(Archiver& arc, ScriptPointer *& pointerValue);
void Clear();