Remove duplicated code of native handles

This commit is contained in:
Arkshine 2015-07-11 18:01:04 +02:00
parent 883c852897
commit dbc9c7e0da
13 changed files with 330 additions and 583 deletions

View File

@ -27,8 +27,6 @@
* or <http://www.sourcemod.net/license.php>. * or <http://www.sourcemod.net/license.php>.
*/ */
#include <stdlib.h>
#include <string.h>
#include "CDataPack.h" #include "CDataPack.h"
#define DATAPACK_INITIAL_SIZE 512 #define DATAPACK_INITIAL_SIZE 512

View File

@ -31,6 +31,7 @@
#define _INCLUDE_SOURCEMOD_CDATAPACK_H_ #define _INCLUDE_SOURCEMOD_CDATAPACK_H_
#include "amxmodx.h" #include "amxmodx.h"
#include "natives_handles.h"
/** /**
* @brief Contains functions for packing data abstractly to/from plugins. * @brief Contains functions for packing data abstractly to/from plugins.
@ -174,81 +175,7 @@ private:
}; };
}; };
class CDataPackHandles extern Handle<CDataPack> DataPackHandles;
{
private:
CVector<CDataPack *> m_packs;
public:
CDataPackHandles() {}
~CDataPackHandles()
{
this->clear();
}
void clear()
{
for (size_t i = 0; i < m_packs.size(); i++)
{
if (m_packs[i] != NULL)
{
delete m_packs[i];
}
}
m_packs.clear();
}
CDataPack *lookup(int handle)
{
handle--;
if (handle < 0 || handle >= static_cast<int>(m_packs.size()))
{
return NULL;
}
return m_packs[handle];
}
int create()
{
for (size_t i = 0; i < m_packs.size(); ++i)
{
if (m_packs[i] == NULL)
{
// reuse handle
m_packs[i] = new CDataPack;
return static_cast<int>(i) + 1;
}
}
m_packs.push_back(new CDataPack);
return m_packs.size();
}
bool destroy(int handle)
{
handle--;
if (handle < 0 || handle >= static_cast<int>(m_packs.size()))
{
return false;
}
if (m_packs[handle] == NULL)
{
return false;
}
delete m_packs[handle];
m_packs[handle] = NULL;
return true;
}
};
extern CDataPackHandles g_DataPackHandles;
extern AMX_NATIVE_INFO g_DatapackNatives[]; extern AMX_NATIVE_INFO g_DatapackNatives[];
#endif //_INCLUDE_SOURCEMOD_CDATAPACK_H_ #endif //_INCLUDE_SOURCEMOD_CDATAPACK_H_

View File

@ -29,18 +29,18 @@
#include "CDataPack.h" #include "CDataPack.h"
CDataPackHandles g_DataPackHandles; Handle<CDataPack> DataPackHandles;
static cell AMX_NATIVE_CALL CreateDataPack(AMX* amx, cell* params) static cell AMX_NATIVE_CALL CreateDataPack(AMX* amx, cell* params)
{ {
return static_cast<cell>(g_DataPackHandles.create()); return static_cast<cell>(DataPackHandles.create());
} }
static cell AMX_NATIVE_CALL WritePackCell(AMX* amx, cell* params) static cell AMX_NATIVE_CALL WritePackCell(AMX* amx, cell* params)
{ {
CDataPack *d = g_DataPackHandles.lookup(params[1]); CDataPack *d = DataPackHandles.lookup(params[1]);
if (d == NULL) if (!d)
{ {
LogError(amx, AMX_ERR_NATIVE, "Invalid datapack handle provided (%d)", params[1]); LogError(amx, AMX_ERR_NATIVE, "Invalid datapack handle provided (%d)", params[1]);
return 0; return 0;
@ -53,9 +53,9 @@ static cell AMX_NATIVE_CALL WritePackCell(AMX* amx, cell* params)
static cell AMX_NATIVE_CALL WritePackFloat(AMX* amx, cell* params) static cell AMX_NATIVE_CALL WritePackFloat(AMX* amx, cell* params)
{ {
CDataPack *d = g_DataPackHandles.lookup(params[1]); CDataPack *d = DataPackHandles.lookup(params[1]);
if (d == NULL) if (!d)
{ {
LogError(amx, AMX_ERR_NATIVE, "Invalid datapack handle provided (%d)", params[1]); LogError(amx, AMX_ERR_NATIVE, "Invalid datapack handle provided (%d)", params[1]);
return 0; return 0;
@ -68,9 +68,9 @@ static cell AMX_NATIVE_CALL WritePackFloat(AMX* amx, cell* params)
static cell AMX_NATIVE_CALL WritePackString(AMX* amx, cell* params) static cell AMX_NATIVE_CALL WritePackString(AMX* amx, cell* params)
{ {
CDataPack *d = g_DataPackHandles.lookup(params[1]); CDataPack *d = DataPackHandles.lookup(params[1]);
if (d == NULL) if (!d)
{ {
LogError(amx, AMX_ERR_NATIVE, "Invalid datapack handle provided (%d)", params[1]); LogError(amx, AMX_ERR_NATIVE, "Invalid datapack handle provided (%d)", params[1]);
return 0; return 0;
@ -86,9 +86,9 @@ static cell AMX_NATIVE_CALL WritePackString(AMX* amx, cell* params)
static cell AMX_NATIVE_CALL ReadPackCell(AMX* amx, cell* params) static cell AMX_NATIVE_CALL ReadPackCell(AMX* amx, cell* params)
{ {
CDataPack *d = g_DataPackHandles.lookup(params[1]); CDataPack *d = DataPackHandles.lookup(params[1]);
if (d == NULL) if (!d)
{ {
LogError(amx, AMX_ERR_NATIVE, "Invalid datapack handle provided (%d)", params[1]); LogError(amx, AMX_ERR_NATIVE, "Invalid datapack handle provided (%d)", params[1]);
return 0; return 0;
@ -105,9 +105,9 @@ static cell AMX_NATIVE_CALL ReadPackCell(AMX* amx, cell* params)
static cell AMX_NATIVE_CALL ReadPackFloat(AMX* amx, cell* params) static cell AMX_NATIVE_CALL ReadPackFloat(AMX* amx, cell* params)
{ {
CDataPack *d = g_DataPackHandles.lookup(params[1]); CDataPack *d = DataPackHandles.lookup(params[1]);
if (d == NULL) if (!d)
{ {
LogError(amx, AMX_ERR_NATIVE, "Invalid datapack handle provided (%d)", params[1]); LogError(amx, AMX_ERR_NATIVE, "Invalid datapack handle provided (%d)", params[1]);
return 0; return 0;
@ -126,9 +126,9 @@ static cell AMX_NATIVE_CALL ReadPackFloat(AMX* amx, cell* params)
static cell AMX_NATIVE_CALL ReadPackString(AMX* amx, cell* params) static cell AMX_NATIVE_CALL ReadPackString(AMX* amx, cell* params)
{ {
CDataPack *d = g_DataPackHandles.lookup(params[1]); CDataPack *d = DataPackHandles.lookup(params[1]);
if (d == NULL) if (!d)
{ {
LogError(amx, AMX_ERR_NATIVE, "Invalid datapack handle provided (%d)", params[1]); LogError(amx, AMX_ERR_NATIVE, "Invalid datapack handle provided (%d)", params[1]);
return 0; return 0;
@ -148,9 +148,9 @@ static cell AMX_NATIVE_CALL ReadPackString(AMX* amx, cell* params)
static cell AMX_NATIVE_CALL ResetPack(AMX* amx, cell* params) static cell AMX_NATIVE_CALL ResetPack(AMX* amx, cell* params)
{ {
CDataPack *d = g_DataPackHandles.lookup(params[1]); CDataPack *d = DataPackHandles.lookup(params[1]);
if (d == NULL) if (!d)
{ {
LogError(amx, AMX_ERR_NATIVE, "Invalid datapack handle provided (%d)", params[1]); LogError(amx, AMX_ERR_NATIVE, "Invalid datapack handle provided (%d)", params[1]);
return 0; return 0;
@ -168,9 +168,9 @@ static cell AMX_NATIVE_CALL ResetPack(AMX* amx, cell* params)
static cell AMX_NATIVE_CALL GetPackPosition(AMX* amx, cell* params) static cell AMX_NATIVE_CALL GetPackPosition(AMX* amx, cell* params)
{ {
CDataPack *d = g_DataPackHandles.lookup(params[1]); CDataPack *d = DataPackHandles.lookup(params[1]);
if (d == NULL) if (!d)
{ {
LogError(amx, AMX_ERR_NATIVE, "Invalid datapack handle provided (%d)", params[1]); LogError(amx, AMX_ERR_NATIVE, "Invalid datapack handle provided (%d)", params[1]);
return 0; return 0;
@ -181,9 +181,9 @@ static cell AMX_NATIVE_CALL GetPackPosition(AMX* amx, cell* params)
static cell AMX_NATIVE_CALL SetPackPosition(AMX* amx, cell* params) static cell AMX_NATIVE_CALL SetPackPosition(AMX* amx, cell* params)
{ {
CDataPack *d = g_DataPackHandles.lookup(params[1]); CDataPack *d = DataPackHandles.lookup(params[1]);
if (d == NULL) if (!d)
{ {
LogError(amx, AMX_ERR_NATIVE, "Invalid datapack handle provided (%d)", params[1]); LogError(amx, AMX_ERR_NATIVE, "Invalid datapack handle provided (%d)", params[1]);
return 0; return 0;
@ -200,9 +200,9 @@ static cell AMX_NATIVE_CALL SetPackPosition(AMX* amx, cell* params)
static cell AMX_NATIVE_CALL IsPackEnded(AMX* amx, cell* params) static cell AMX_NATIVE_CALL IsPackEnded(AMX* amx, cell* params)
{ {
CDataPack *d = g_DataPackHandles.lookup(params[1]); CDataPack *d = DataPackHandles.lookup(params[1]);
if (d == NULL) if (!d)
{ {
LogError(amx, AMX_ERR_NATIVE, "Invalid datapack handle provided (%d)", params[1]); LogError(amx, AMX_ERR_NATIVE, "Invalid datapack handle provided (%d)", params[1]);
return 0; return 0;
@ -215,14 +215,14 @@ static cell AMX_NATIVE_CALL DestroyDataPack(AMX* amx, cell* params)
{ {
cell *ptr = get_amxaddr(amx, params[1]); cell *ptr = get_amxaddr(amx, params[1]);
CDataPack *d = g_DataPackHandles.lookup(*ptr); CDataPack *d = DataPackHandles.lookup(*ptr);
if (d == NULL) if (!d)
{ {
return 0; return 0;
} }
if (g_DataPackHandles.destroy(*ptr)) if (DataPackHandles.destroy(*ptr))
{ {
*ptr = 0; *ptr = 0;
return 1; return 1;
@ -246,5 +246,5 @@ AMX_NATIVE_INFO g_DatapackNatives[] =
{ "SetPackPosition", SetPackPosition }, { "SetPackPosition", SetPackPosition },
{ "IsPackEnded" , IsPackEnded }, { "IsPackEnded" , IsPackEnded },
{ "DestroyDataPack", DestroyDataPack }, { "DestroyDataPack", DestroyDataPack },
{NULL, NULL} { nullptr , nullptr}
}; };

View File

@ -11,12 +11,7 @@
#include "datastructs.h" #include "datastructs.h"
#include <am-utility.h> #include <am-utility.h>
// Note: All handles start at 1. 0 and below are invalid handles. Handle<CellArray> ArrayHandles;
// This way, a plugin that doesn't initialize a vector or
// string will not be able to modify another plugin's data
// on accident.
ke::Vector<CellArray*> VectorHolder;
// Array:ArrayCreate(cellsize=1, reserved=32); // Array:ArrayCreate(cellsize=1, reserved=32);
static cell AMX_NATIVE_CALL ArrayCreate(AMX* amx, cell* params) static cell AMX_NATIVE_CALL ArrayCreate(AMX* amx, cell* params)
@ -40,32 +35,17 @@ static cell AMX_NATIVE_CALL ArrayCreate(AMX* amx, cell* params)
reserved = 0; reserved = 0;
} }
// Scan through the vector list to see if any are NULL. return ArrayHandles.create(cellsize, reserved);
// NULL means the vector was previously destroyed.
for (unsigned int i=0; i < VectorHolder.length(); ++i)
{
if (VectorHolder[i]==NULL)
{
VectorHolder[i] = new CellArray(cellsize, reserved);
return i + 1;
}
}
// None are NULL, create a new vector
CellArray* NewVector = new CellArray(cellsize, reserved);
VectorHolder.append(NewVector);
return VectorHolder.length();
} }
// native ArrayClear(Array:which); // native ArrayClear(Array:which);
static cell AMX_NATIVE_CALL ArrayClear(AMX* amx, cell* params) static cell AMX_NATIVE_CALL ArrayClear(AMX* amx, cell* params)
{ {
CellArray* vec = HandleToVector(amx, params[1]); CellArray* vec = ArrayHandles.lookup(params[1]);
if (vec == NULL) if (!vec)
{ {
LogError(amx, AMX_ERR_NATIVE, "Invalid array handle provided (%d)", params[1]);
return 0; return 0;
} }
@ -77,10 +57,11 @@ static cell AMX_NATIVE_CALL ArrayClear(AMX* amx, cell* params)
// native ArraySize(Array:which); // native ArraySize(Array:which);
static cell AMX_NATIVE_CALL ArraySize(AMX* amx, cell* params) static cell AMX_NATIVE_CALL ArraySize(AMX* amx, cell* params)
{ {
CellArray* vec = HandleToVector(amx, params[1]); CellArray* vec = ArrayHandles.lookup(params[1]);
if (vec == NULL) if (!vec)
{ {
LogError(amx, AMX_ERR_NATIVE, "Invalid array handle provided (%d)", params[1]);
return 0; return 0;
} }
@ -90,10 +71,11 @@ static cell AMX_NATIVE_CALL ArraySize(AMX* amx, cell* params)
// native bool:ArrayResize(Array:which, newsize); // native bool:ArrayResize(Array:which, newsize);
static cell AMX_NATIVE_CALL ArrayResize(AMX* amx, cell* params) static cell AMX_NATIVE_CALL ArrayResize(AMX* amx, cell* params)
{ {
CellArray* vec = HandleToVector(amx, params[1]); CellArray* vec = ArrayHandles.lookup(params[1]);
if (vec == NULL) if (!vec)
{ {
LogError(amx, AMX_ERR_NATIVE, "Invalid array handle provided (%d)", params[1]);
return 0; return 0;
} }
@ -109,38 +91,25 @@ static cell AMX_NATIVE_CALL ArrayResize(AMX* amx, cell* params)
// native Array:ArrayClone(Array:which); // native Array:ArrayClone(Array:which);
static cell AMX_NATIVE_CALL ArrayClone(AMX* amx, cell* params) static cell AMX_NATIVE_CALL ArrayClone(AMX* amx, cell* params)
{ {
CellArray* vec = HandleToVector(amx, params[1]); CellArray* vec = ArrayHandles.lookup(params[1]);
if (vec == NULL) if (!vec)
{ {
LogError(amx, AMX_ERR_NATIVE, "Invalid array handle provided (%d)", params[1]);
return 0; return 0;
} }
CellArray *clonevec = vec->clone(); return ArrayHandles.clone(vec->clone());
// Scan through the vector list to see if any are NULL.
// NULL means the vector was previously destroyed.
for (unsigned int i = 0; i < VectorHolder.length(); ++i)
{
if (VectorHolder[i] == NULL)
{
VectorHolder[i] = clonevec;
return i + 1;
}
}
VectorHolder.append(clonevec);
return VectorHolder.length();
} }
// native ArrayGetArray(Array:which, item, any:output[], size = -1); // native ArrayGetArray(Array:which, item, any:output[], size = -1);
static cell AMX_NATIVE_CALL ArrayGetArray(AMX* amx, cell* params) static cell AMX_NATIVE_CALL ArrayGetArray(AMX* amx, cell* params)
{ {
CellArray* vec = HandleToVector(amx, params[1]); CellArray* vec = ArrayHandles.lookup(params[1]);
if (vec == NULL) if (!vec)
{ {
LogError(amx, AMX_ERR_NATIVE, "Invalid array handle provided (%d)", params[1]);
return 0; return 0;
} }
@ -173,10 +142,11 @@ static cell AMX_NATIVE_CALL ArrayGetArray(AMX* amx, cell* params)
// native any:ArrayGetCell(Array:which, item, block = 0, bool:asChar = false); // native any:ArrayGetCell(Array:which, item, block = 0, bool:asChar = false);
static cell AMX_NATIVE_CALL ArrayGetCell(AMX* amx, cell* params) static cell AMX_NATIVE_CALL ArrayGetCell(AMX* amx, cell* params)
{ {
CellArray* vec = HandleToVector(amx, params[1]); CellArray* vec = ArrayHandles.lookup(params[1]);
if (vec == NULL) if (!vec)
{ {
LogError(amx, AMX_ERR_NATIVE, "Invalid array handle provided (%d)", params[1]);
return 0; return 0;
} }
@ -224,10 +194,11 @@ static cell AMX_NATIVE_CALL ArrayGetCell(AMX* amx, cell* params)
// native ArrayGetString(Array:which, item, output[], size); // native ArrayGetString(Array:which, item, output[], size);
static cell AMX_NATIVE_CALL ArrayGetString(AMX* amx, cell* params) static cell AMX_NATIVE_CALL ArrayGetString(AMX* amx, cell* params)
{ {
CellArray* vec = HandleToVector(amx, params[1]); CellArray* vec = ArrayHandles.lookup(params[1]);
if (vec == NULL) if (!vec)
{ {
LogError(amx, AMX_ERR_NATIVE, "Invalid array handle provided (%d)", params[1]);
return 0; return 0;
} }
@ -246,10 +217,11 @@ static cell AMX_NATIVE_CALL ArrayGetString(AMX* amx, cell* params)
// native ArraySetArray(Array:which, item, const any:input[], size =-1); // native ArraySetArray(Array:which, item, const any:input[], size =-1);
static cell AMX_NATIVE_CALL ArraySetArray(AMX* amx, cell* params) static cell AMX_NATIVE_CALL ArraySetArray(AMX* amx, cell* params)
{ {
CellArray* vec = HandleToVector(amx, params[1]); CellArray* vec = ArrayHandles.lookup(params[1]);
if (vec == NULL) if (!vec)
{ {
LogError(amx, AMX_ERR_NATIVE, "Invalid array handle provided (%d)", params[1]);
return 0; return 0;
} }
@ -282,10 +254,11 @@ static cell AMX_NATIVE_CALL ArraySetArray(AMX* amx, cell* params)
// native ArraySetCell(Array:which, item, any:input, block = 0, bool:asChar = false); // native ArraySetCell(Array:which, item, any:input, block = 0, bool:asChar = false);
static cell AMX_NATIVE_CALL ArraySetCell(AMX* amx, cell* params) static cell AMX_NATIVE_CALL ArraySetCell(AMX* amx, cell* params)
{ {
CellArray* vec = HandleToVector(amx, params[1]); CellArray* vec = ArrayHandles.lookup(params[1]);
if (vec == NULL) if (!vec)
{ {
LogError(amx, AMX_ERR_NATIVE, "Invalid array handle provided (%d)", params[1]);
return 0; return 0;
} }
@ -331,10 +304,11 @@ static cell AMX_NATIVE_CALL ArraySetCell(AMX* amx, cell* params)
// native ArraySetString(Array:which, item, const input[]); // native ArraySetString(Array:which, item, const input[]);
static cell AMX_NATIVE_CALL ArraySetString(AMX* amx, cell* params) static cell AMX_NATIVE_CALL ArraySetString(AMX* amx, cell* params)
{ {
CellArray* vec = HandleToVector(amx, params[1]); CellArray* vec = ArrayHandles.lookup(params[1]);
if (vec == NULL) if (!vec)
{ {
LogError(amx, AMX_ERR_NATIVE, "Invalid array handle provided (%d)", params[1]);
return 0; return 0;
} }
@ -357,10 +331,11 @@ static cell AMX_NATIVE_CALL ArraySetString(AMX* amx, cell* params)
// native ArrayPushArray(Array:which, const any:input[], size = -1); // native ArrayPushArray(Array:which, const any:input[], size = -1);
static cell AMX_NATIVE_CALL ArrayPushArray(AMX* amx, cell* params) static cell AMX_NATIVE_CALL ArrayPushArray(AMX* amx, cell* params)
{ {
CellArray* vec = HandleToVector(amx, params[1]); CellArray* vec = ArrayHandles.lookup(params[1]);
if (vec == NULL) if (!vec)
{ {
LogError(amx, AMX_ERR_NATIVE, "Invalid array handle provided (%d)", params[1]);
return 0; return 0;
} }
@ -391,10 +366,11 @@ static cell AMX_NATIVE_CALL ArrayPushArray(AMX* amx, cell* params)
// native ArrayPushCell(Array:which, any:input); // native ArrayPushCell(Array:which, any:input);
static cell AMX_NATIVE_CALL ArrayPushCell(AMX* amx, cell* params) static cell AMX_NATIVE_CALL ArrayPushCell(AMX* amx, cell* params)
{ {
CellArray* vec = HandleToVector(amx, params[1]); CellArray* vec = ArrayHandles.lookup(params[1]);
if (vec == NULL) if (!vec)
{ {
LogError(amx, AMX_ERR_NATIVE, "Invalid array handle provided (%d)", params[1]);
return 0; return 0;
} }
@ -414,10 +390,11 @@ static cell AMX_NATIVE_CALL ArrayPushCell(AMX* amx, cell* params)
// native ArrayPushString(Array:which, const input[]); // native ArrayPushString(Array:which, const input[]);
static cell AMX_NATIVE_CALL ArrayPushString(AMX* amx, cell* params) static cell AMX_NATIVE_CALL ArrayPushString(AMX* amx, cell* params)
{ {
CellArray* vec = HandleToVector(amx, params[1]); CellArray* vec = ArrayHandles.lookup(params[1]);
if (vec == NULL) if (!vec)
{ {
LogError(amx, AMX_ERR_NATIVE, "Invalid array handle provided (%d)", params[1]);
return 0; return 0;
} }
@ -436,10 +413,11 @@ static cell AMX_NATIVE_CALL ArrayPushString(AMX* amx, cell* params)
// native DoNotUse : ArrayGetStringHandle(Array : which, item); // native DoNotUse : ArrayGetStringHandle(Array : which, item);
static cell AMX_NATIVE_CALL ArrayGetStringHandle(AMX* amx, cell* params) static cell AMX_NATIVE_CALL ArrayGetStringHandle(AMX* amx, cell* params)
{ {
CellArray* vec = HandleToVector(amx, params[1]); CellArray* vec = ArrayHandles.lookup(params[1]);
if (vec == NULL) if (!vec)
{ {
LogError(amx, AMX_ERR_NATIVE, "Invalid array handle provided (%d)", params[1]);
return 0; return 0;
} }
@ -464,10 +442,11 @@ static cell AMX_NATIVE_CALL ArrayGetStringHandle(AMX* amx, cell* params)
// native ArrayInsertArrayAfter(Array:which, item, const any:input[]); // native ArrayInsertArrayAfter(Array:which, item, const any:input[]);
static cell AMX_NATIVE_CALL ArrayInsertArrayAfter(AMX* amx, cell* params) static cell AMX_NATIVE_CALL ArrayInsertArrayAfter(AMX* amx, cell* params)
{ {
CellArray* vec = HandleToVector(amx, params[1]); CellArray* vec = ArrayHandles.lookup(params[1]);
if (vec == NULL) if (!vec)
{ {
LogError(amx, AMX_ERR_NATIVE, "Invalid array handle provided (%d)", params[1]);
return 0; return 0;
} }
@ -489,10 +468,11 @@ static cell AMX_NATIVE_CALL ArrayInsertArrayAfter(AMX* amx, cell* params)
// native ArrayInsertCellAfter(Array:which, item, any:input); // native ArrayInsertCellAfter(Array:which, item, any:input);
static cell AMX_NATIVE_CALL ArrayInsertCellAfter(AMX* amx, cell* params) static cell AMX_NATIVE_CALL ArrayInsertCellAfter(AMX* amx, cell* params)
{ {
CellArray* vec = HandleToVector(amx, params[1]); CellArray* vec = ArrayHandles.lookup(params[1]);
if (vec == NULL) if (!vec)
{ {
LogError(amx, AMX_ERR_NATIVE, "Invalid array handle provided (%d)", params[1]);
return 0; return 0;
} }
@ -512,10 +492,11 @@ static cell AMX_NATIVE_CALL ArrayInsertCellAfter(AMX* amx, cell* params)
// native ArrayInsertStringAfter(Array:which, item, const input[]); // native ArrayInsertStringAfter(Array:which, item, const input[]);
static cell AMX_NATIVE_CALL ArrayInsertStringAfter(AMX* amx, cell* params) static cell AMX_NATIVE_CALL ArrayInsertStringAfter(AMX* amx, cell* params)
{ {
CellArray* vec = HandleToVector(amx, params[1]); CellArray* vec = ArrayHandles.lookup(params[1]);
if (vec == NULL) if (!vec)
{ {
LogError(amx, AMX_ERR_NATIVE, "Invalid array handle provided (%d)", params[1]);
return 0; return 0;
} }
@ -536,10 +517,11 @@ static cell AMX_NATIVE_CALL ArrayInsertStringAfter(AMX* amx, cell* params)
// native ArrayInsertArrayBefore(Array:which, item, const any:input[]); // native ArrayInsertArrayBefore(Array:which, item, const any:input[]);
static cell AMX_NATIVE_CALL ArrayInsertArrayBefore(AMX* amx, cell* params) static cell AMX_NATIVE_CALL ArrayInsertArrayBefore(AMX* amx, cell* params)
{ {
CellArray* vec = HandleToVector(amx, params[1]); CellArray* vec = ArrayHandles.lookup(params[1]);
if (vec == NULL) if (!vec)
{ {
LogError(amx, AMX_ERR_NATIVE, "Invalid array handle provided (%d)", params[1]);
return 0; return 0;
} }
@ -561,10 +543,11 @@ static cell AMX_NATIVE_CALL ArrayInsertArrayBefore(AMX* amx, cell* params)
// native ArrayInsertCellBefore(Array:which, item, const any:input); // native ArrayInsertCellBefore(Array:which, item, const any:input);
static cell AMX_NATIVE_CALL ArrayInsertCellBefore(AMX* amx, cell* params) static cell AMX_NATIVE_CALL ArrayInsertCellBefore(AMX* amx, cell* params)
{ {
CellArray* vec = HandleToVector(amx, params[1]); CellArray* vec = ArrayHandles.lookup(params[1]);
if (vec == NULL) if (!vec)
{ {
LogError(amx, AMX_ERR_NATIVE, "Invalid array handle provided (%d)", params[1]);
return 0; return 0;
} }
@ -583,10 +566,11 @@ static cell AMX_NATIVE_CALL ArrayInsertCellBefore(AMX* amx, cell* params)
// native ArrayInsertStringBefore(Array:which, item, const input[]); // native ArrayInsertStringBefore(Array:which, item, const input[]);
static cell AMX_NATIVE_CALL ArrayInsertStringBefore(AMX* amx, cell* params) static cell AMX_NATIVE_CALL ArrayInsertStringBefore(AMX* amx, cell* params)
{ {
CellArray* vec = HandleToVector(amx, params[1]); CellArray* vec = ArrayHandles.lookup(params[1]);
if (vec == NULL) if (!vec)
{ {
LogError(amx, AMX_ERR_NATIVE, "Invalid array handle provided (%d)", params[1]);
return 0; return 0;
} }
@ -607,10 +591,11 @@ static cell AMX_NATIVE_CALL ArrayInsertStringBefore(AMX* amx, cell* params)
// native ArraySwap(Array:which, item1, item2); // native ArraySwap(Array:which, item1, item2);
static cell AMX_NATIVE_CALL ArraySwap(AMX* amx, cell* params) static cell AMX_NATIVE_CALL ArraySwap(AMX* amx, cell* params)
{ {
CellArray* vec = HandleToVector(amx, params[1]); CellArray* vec = ArrayHandles.lookup(params[1]);
if (vec == NULL) if (!vec)
{ {
LogError(amx, AMX_ERR_NATIVE, "Invalid array handle provided (%d)", params[1]);
return 0; return 0;
} }
@ -637,10 +622,11 @@ static cell AMX_NATIVE_CALL ArraySwap(AMX* amx, cell* params)
// native ArrayDeleteItem(Array:which, item); // native ArrayDeleteItem(Array:which, item);
static cell AMX_NATIVE_CALL ArrayDeleteItem(AMX* amx, cell* params) static cell AMX_NATIVE_CALL ArrayDeleteItem(AMX* amx, cell* params)
{ {
CellArray* vec = HandleToVector(amx, params[1]); CellArray* vec = ArrayHandles.lookup(params[1]);
if (vec == NULL) if (!vec)
{ {
LogError(amx, AMX_ERR_NATIVE, "Invalid array handle provided (%d)", params[1]);
return 0; return 0;
} }
@ -662,23 +648,18 @@ static cell AMX_NATIVE_CALL ArrayDestroy(AMX* amx, cell* params)
{ {
cell *handle = get_amxaddr(amx, params[1]); cell *handle = get_amxaddr(amx, params[1]);
if (*handle == 0) CellArray* vec = ArrayHandles.lookup(*handle);
if (!vec)
{ {
return 0; return 0;
} }
CellArray* vec = HandleToVector(amx, *handle); if (ArrayHandles.destroy(*handle))
if (vec == NULL)
{ {
return 0;
}
delete vec;
VectorHolder[*handle-1] = NULL;
*handle = 0; *handle = 0;
return 1;
}
return 1; return 1;
} }
@ -714,11 +695,12 @@ int SortArrayList(const void *elem1, const void *elem2)
// native ArraySort(Array:array, const comparefunc[], data[]="", data_size=0); // native ArraySort(Array:array, const comparefunc[], data[]="", data_size=0);
static cell AMX_NATIVE_CALL ArraySort(AMX* amx, cell* params) static cell AMX_NATIVE_CALL ArraySort(AMX* amx, cell* params)
{ {
int handle = params[1]; cell handle = params[1];
CellArray* vec = HandleToVector(amx, handle); CellArray* vec = ArrayHandles.lookup(handle);
if (vec == NULL) if (!vec)
{ {
LogError(amx, AMX_ERR_NATIVE, "Invalid array handle provided (%d)", handle);
return 0; return 0;
} }
@ -795,11 +777,11 @@ int SortArrayListExArray(const void *elem1, const void *elem2)
// native ArraySortEx(Array:array, const comparefunc[], data[]="", data_size=0); // native ArraySortEx(Array:array, const comparefunc[], data[]="", data_size=0);
static cell AMX_NATIVE_CALL ArraySortEx(AMX* amx, cell* params) static cell AMX_NATIVE_CALL ArraySortEx(AMX* amx, cell* params)
{ {
int handle = params[1]; CellArray* vec = ArrayHandles.lookup(params[1]);
CellArray* vec = HandleToVector(amx, handle);
if (vec == NULL) if (!vec)
{ {
LogError(amx, AMX_ERR_NATIVE, "Invalid array handle provided (%d)", params[1]);
return 0; return 0;
} }
@ -863,10 +845,11 @@ extern int amxstring_len(cell* a);
// native ArrayFindString(Array:which, const item[]); // native ArrayFindString(Array:which, const item[]);
static cell AMX_NATIVE_CALL ArrayFindString(AMX* amx, cell* params) static cell AMX_NATIVE_CALL ArrayFindString(AMX* amx, cell* params)
{ {
CellArray* vec = HandleToVector(amx, params[1]); CellArray* vec = ArrayHandles.lookup(params[1]);
if (vec == NULL) if (!vec)
{ {
LogError(amx, AMX_ERR_NATIVE, "Invalid array handle provided (%d)", params[1]);
return -1; return -1;
} }
@ -891,10 +874,11 @@ static cell AMX_NATIVE_CALL ArrayFindString(AMX* amx, cell* params)
// native ArrayFindValue(Array:which, any:item); // native ArrayFindValue(Array:which, any:item);
static cell AMX_NATIVE_CALL ArrayFindValue(AMX* amx, cell* params) static cell AMX_NATIVE_CALL ArrayFindValue(AMX* amx, cell* params)
{ {
CellArray* vec = HandleToVector(amx, params[1]); CellArray* vec = ArrayHandles.lookup(params[1]);
if (vec == NULL) if (!vec)
{ {
LogError(amx, AMX_ERR_NATIVE, "Invalid array handle provided (%d)", params[1]);
return -1; return -1;
} }
@ -939,6 +923,5 @@ AMX_NATIVE_INFO g_DataStructNatives[] =
{ "ArraySortEx" , ArraySortEx }, { "ArraySortEx" , ArraySortEx },
{ "ArrayFindString" , ArrayFindString }, { "ArrayFindString" , ArrayFindString },
{ "ArrayFindValue" , ArrayFindValue }, { "ArrayFindValue" , ArrayFindValue },
{ nullptr , nullptr }
{ NULL, NULL }
}; };

View File

@ -10,12 +10,12 @@
#ifndef DATASTRUCTS_H #ifndef DATASTRUCTS_H
#define DATASTRUCTS_H #define DATASTRUCTS_H
#include <am-vector.h> #include "natives_handles.h"
class CellArray class CellArray
{ {
public: public:
CellArray(size_t blocksize, size_t basesize = 0) : m_Data(NULL), m_BlockSize(blocksize), m_AllocSize(0), m_BaseSize(basesize > 0 ? basesize : 8), m_Size(0) CellArray(size_t blocksize, size_t basesize = 0) : m_Data(nullptr), m_BlockSize(blocksize), m_AllocSize(0), m_BaseSize(basesize > 0 ? basesize : 8), m_Size(0)
{ {
} }
@ -99,7 +99,7 @@ public:
/* Make sure it'll fit */ /* Make sure it'll fit */
if (!GrowIfNeeded(1)) if (!GrowIfNeeded(1))
{ {
return NULL; return nullptr;
} }
/* move everything up */ /* move everything up */
@ -185,29 +185,6 @@ private:
size_t m_Size; size_t m_Size;
}; };
extern ke::Vector<CellArray*> VectorHolder; extern Handle<CellArray> ArrayHandles;
inline CellArray* HandleToVector(AMX* amx, int handle)
{
if (handle <= 0 || handle > (int)VectorHolder.length())
{
LogError(amx, AMX_ERR_NATIVE, "Invalid array handle provided (%d)", handle);
return NULL;
}
CellArray* ret = VectorHolder[handle - 1];
if (ret == NULL)
{
LogError(amx, AMX_ERR_NATIVE, "Invalid array handle provided (%d)", handle);
return NULL;
}
return ret;
}
#endif #endif

View File

@ -399,16 +399,11 @@ int C_Spawn(edict_t *pent)
FlagMan.LoadFile(); FlagMan.LoadFile();
for (unsigned int i=0; i<VectorHolder.length(); i++) ArrayHandles.clear();
{ TrieHandles.clear();
delete VectorHolder[i]; TrieSnapshotHandles.clear();
}; DataPackHandles.clear();
VectorHolder.clear(); TextParsersHandles.clear();
g_TrieHandles.clear();
g_TrieSnapshotHandles.clear();
g_DataPackHandles.clear();
g_TextParsersHandles.clear();
GameConfigHandle.clear(); GameConfigHandle.clear();
char map_pluginsfile_path[256]; char map_pluginsfile_path[256];

View File

@ -7,8 +7,16 @@
// Additional exceptions apply. For full license details, see LICENSE.txt or visit: // Additional exceptions apply. For full license details, see LICENSE.txt or visit:
// https://alliedmods.net/amxmodx-license // https://alliedmods.net/amxmodx-license
#ifndef _NATIVES_HANDLES_H_
#define _NATIVES_HANDLES_H_
#include <am-vector.h> #include <am-vector.h>
// Note: All handles start at 1. 0 and below are invalid handles.
// This way, a plugin that doesn't initialize a vector or
// string will not be able to modify another plugin's data
// on accident.
template <typename T> template <typename T>
class Handle class Handle
{ {
@ -49,19 +57,37 @@ class Handle
return m_handles[handle]; return m_handles[handle];
} }
int create() template <typename... Targs>
int create(Targs... Fargs)
{ {
for (size_t i = 0; i < m_handles.length(); ++i) for (size_t i = 0; i < m_handles.length(); ++i)
{ {
if (!m_handles[i]) if (!m_handles[i])
{ {
m_handles[i] = new T; m_handles[i] = new T(Fargs...);
return static_cast<int>(i) + 1; return static_cast<int>(i) + 1;
} }
} }
m_handles.append(new T); m_handles.append(new T(Fargs...));
return m_handles.length();
}
int clone(T *data)
{
for (size_t i = 0; i < m_handles.length(); ++i)
{
if (!m_handles[i])
{
m_handles[i] = data;
return static_cast<int>(i) + 1;
}
}
m_handles.append(data);
return m_handles.length(); return m_handles.length();
} }
@ -86,3 +112,5 @@ class Handle
return true; return true;
} }
}; };
#endif // _NATIVE_HANDLES_H_

View File

@ -428,10 +428,11 @@ void sort_adt_random(CellArray *cArray)
static cell AMX_NATIVE_CALL SortADTArray(AMX *amx, cell *params) static cell AMX_NATIVE_CALL SortADTArray(AMX *amx, cell *params)
{ {
CellArray* vec = HandleToVector(amx, params[1]); CellArray* vec = ArrayHandles.lookup(params[1]);
if (vec == NULL) if (!vec)
{ {
LogError(amx, AMX_ERR_NATIVE, "Invalid array handle provided (%d)", params[1]);
return 0; return 0;
} }

View File

@ -6,6 +6,7 @@
// This software is licensed under the GNU General Public License, version 3 or higher. // This software is licensed under the GNU General Public License, version 3 or higher.
// Additional exceptions apply. For full license details, see LICENSE.txt or visit: // Additional exceptions apply. For full license details, see LICENSE.txt or visit:
// https://alliedmods.net/amxmodx-license // https://alliedmods.net/amxmodx-license
#include "amxmodx.h" #include "amxmodx.h"
#include "datastructs.h" #include "datastructs.h"
@ -20,32 +21,17 @@ static cell AMX_NATIVE_CALL CreateStack(AMX* amx, cell* params)
return -1; return -1;
} }
// Scan through the vector list to see if any are NULL. return ArrayHandles.create(cellsize);
// NULL means the vector was previously destroyed.
for (unsigned int i = 0; i < VectorHolder.length(); ++i)
{
if (VectorHolder[i] == NULL)
{
VectorHolder[i] = new CellArray(cellsize);
return i + 1;
}
}
// None are NULL, create a new vector
CellArray* NewVector = new CellArray(cellsize);
VectorHolder.append(NewVector);
return VectorHolder.length();
} }
// native PushStackCell(Stack:handle, any:value); // native PushStackCell(Stack:handle, any:value);
static cell AMX_NATIVE_CALL PushStackCell(AMX* amx, cell* params) static cell AMX_NATIVE_CALL PushStackCell(AMX* amx, cell* params)
{ {
CellArray* vec = HandleToVector(amx, params[1]); CellArray* vec = ArrayHandles.lookup(params[1]);
if (vec == NULL) if (!vec)
{ {
LogError(amx, AMX_ERR_NATIVE, "Invalid array handle provided (%d)", params[1]);
return 0; return 0;
} }
@ -65,10 +51,11 @@ static cell AMX_NATIVE_CALL PushStackCell(AMX* amx, cell* params)
// native PushStackString(Stack:handle, const value[]); // native PushStackString(Stack:handle, const value[]);
static cell AMX_NATIVE_CALL PushStackString(AMX* amx, cell* params) static cell AMX_NATIVE_CALL PushStackString(AMX* amx, cell* params)
{ {
CellArray* vec = HandleToVector(amx, params[1]); CellArray* vec = ArrayHandles.lookup(params[1]);
if (vec == NULL) if (!vec)
{ {
LogError(amx, AMX_ERR_NATIVE, "Invalid array handle provided (%d)", params[1]);
return 0; return 0;
} }
@ -91,10 +78,11 @@ static cell AMX_NATIVE_CALL PushStackString(AMX* amx, cell* params)
// native PushStackArray(Stack:handle, const any:values[], size= -1); // native PushStackArray(Stack:handle, const any:values[], size= -1);
static cell AMX_NATIVE_CALL PushStackArray(AMX* amx, cell* params) static cell AMX_NATIVE_CALL PushStackArray(AMX* amx, cell* params)
{ {
CellArray* vec = HandleToVector(amx, params[1]); CellArray* vec = ArrayHandles.lookup(params[1]);
if (vec == NULL) if (!vec)
{ {
LogError(amx, AMX_ERR_NATIVE, "Invalid array handle provided (%d)", params[1]);
return 0; return 0;
} }
@ -122,10 +110,11 @@ static cell AMX_NATIVE_CALL PushStackArray(AMX* amx, cell* params)
// native bool:PopStackCell(Stack:handle, &any:value, block = 0, bool:asChar = false); // native bool:PopStackCell(Stack:handle, &any:value, block = 0, bool:asChar = false);
static cell AMX_NATIVE_CALL PopStackCell(AMX* amx, cell* params) static cell AMX_NATIVE_CALL PopStackCell(AMX* amx, cell* params)
{ {
CellArray* vec = HandleToVector(amx, params[1]); CellArray* vec = ArrayHandles.lookup(params[1]);
if (vec == NULL) if (!vec)
{ {
LogError(amx, AMX_ERR_NATIVE, "Invalid array handle provided (%d)", params[1]);
return 0; return 0;
} }
@ -169,10 +158,11 @@ static cell AMX_NATIVE_CALL PopStackCell(AMX* amx, cell* params)
// native bool:PopStackString(Stack:handle, buffer[], maxlength, &written = 0); // native bool:PopStackString(Stack:handle, buffer[], maxlength, &written = 0);
static cell AMX_NATIVE_CALL PopStackString(AMX* amx, cell* params) static cell AMX_NATIVE_CALL PopStackString(AMX* amx, cell* params)
{ {
CellArray* vec = HandleToVector(amx, params[1]); CellArray* vec = ArrayHandles.lookup(params[1]);
if (vec == NULL) if (!vec)
{ {
LogError(amx, AMX_ERR_NATIVE, "Invalid array handle provided (%d)", params[1]);
return 0; return 0;
} }
@ -195,10 +185,11 @@ static cell AMX_NATIVE_CALL PopStackString(AMX* amx, cell* params)
// native bool:PopStackArray(Stack:handle, any:buffer[], size=-1); // native bool:PopStackArray(Stack:handle, any:buffer[], size=-1);
static cell AMX_NATIVE_CALL PopStackArray(AMX* amx, cell* params) static cell AMX_NATIVE_CALL PopStackArray(AMX* amx, cell* params)
{ {
CellArray* vec = HandleToVector(amx, params[1]); CellArray* vec = ArrayHandles.lookup(params[1]);
if (vec == NULL) if (!vec)
{ {
LogError(amx, AMX_ERR_NATIVE, "Invalid array handle provided (%d)", params[1]);
return 0; return 0;
} }
@ -227,10 +218,11 @@ static cell AMX_NATIVE_CALL PopStackArray(AMX* amx, cell* params)
// native bool:IsStackEmpty(Stack:handle); // native bool:IsStackEmpty(Stack:handle);
static cell AMX_NATIVE_CALL IsStackEmpty(AMX* amx, cell* params) static cell AMX_NATIVE_CALL IsStackEmpty(AMX* amx, cell* params)
{ {
CellArray* vec = HandleToVector(amx, params[1]); CellArray* vec = ArrayHandles.lookup(params[1]);
if (vec == NULL) if (!vec)
{ {
LogError(amx, AMX_ERR_NATIVE, "Invalid array handle provided (%d)", params[1]);
return 0; return 0;
} }
@ -242,23 +234,18 @@ static cell AMX_NATIVE_CALL DestroyStack(AMX* amx, cell* params)
{ {
cell *handle = get_amxaddr(amx, params[1]); cell *handle = get_amxaddr(amx, params[1]);
if (*handle == 0) CellArray* vec = ArrayHandles.lookup(*handle);
if (!vec)
{ {
return 0; return 0;
} }
CellArray *vec = HandleToVector(amx, *handle); if (ArrayHandles.destroy(*handle))
if (vec == NULL)
{ {
return 0;
}
delete vec;
VectorHolder[*handle - 1] = NULL;
*handle = 0; *handle = 0;
return 1;
}
return 1; return 1;
} }
@ -274,5 +261,5 @@ AMX_NATIVE_INFO g_StackNatives[] =
{ "PushStackCell", PushStackCell }, { "PushStackCell", PushStackCell },
{ "PushStackString", PushStackString }, { "PushStackString", PushStackString },
{ "DestroyStack", DestroyStack }, { "DestroyStack", DestroyStack },
{ NULL, NULL }, { nullptr, nullptr },
}; };

View File

@ -9,27 +9,25 @@
* https://alliedmods.net/amxmodx-license * https://alliedmods.net/amxmodx-license
*/ */
#include "amxmodx.h" #include "textparse.h"
#include <textparse.h>
#include <am-vector.h>
TextParserHandles<ParseInfo> g_TextParsersHandles; Handle<ParseInfo> TextParsersHandles;
cell createParser() cell createParser()
{ {
return g_TextParsersHandles.create(); return TextParsersHandles.create();
} }
cell destroyParser(cell *handle) cell destroyParser(cell *handle)
{ {
ParseInfo *p = g_TextParsersHandles.lookup(*handle); ParseInfo *p = TextParsersHandles.lookup(*handle);
if (p == NULL) if (!p)
{ {
return 0; return 0;
} }
if (g_TextParsersHandles.destroy(*handle)) if (TextParsersHandles.destroy(*handle))
{ {
*handle = 0; *handle = 0;
return 1; return 1;
@ -52,16 +50,16 @@ static cell AMX_NATIVE_CALL SMC_CreateParser(AMX *amx, cell *params)
// native SMC_SetParseStart(SMCParser:handle, const func[]); // native SMC_SetParseStart(SMCParser:handle, const func[]);
static cell AMX_NATIVE_CALL SMC_SetParseStart(AMX *amx, cell *params) static cell AMX_NATIVE_CALL SMC_SetParseStart(AMX *amx, cell *params)
{ {
ParseInfo *p = g_TextParsersHandles.lookup(params[1]); ParseInfo *p = TextParsersHandles.lookup(params[1]);
if (p == NULL) if (!p)
{ {
LogError(amx, AMX_ERR_NATIVE, "Invalid SMC parse handle (%d)", params[1]); LogError(amx, AMX_ERR_NATIVE, "Invalid SMC parse handle (%d)", params[1]);
return 0; return 0;
} }
int length = 0; int length = 0;
const char *funcName = NULL; const char *funcName = nullptr;
if ((funcName = get_amxstring(amx, params[2], 0, length)) && length) if ((funcName = get_amxstring(amx, params[2], 0, length)) && length)
{ {
@ -80,16 +78,16 @@ static cell AMX_NATIVE_CALL SMC_SetParseStart(AMX *amx, cell *params)
// native SMC_SetParseEnd(SMCParser:handle, const func[]); // native SMC_SetParseEnd(SMCParser:handle, const func[]);
static cell AMX_NATIVE_CALL SMC_SetParseEnd(AMX *amx, cell *params) static cell AMX_NATIVE_CALL SMC_SetParseEnd(AMX *amx, cell *params)
{ {
ParseInfo *p = g_TextParsersHandles.lookup(params[1]); ParseInfo *p = TextParsersHandles.lookup(params[1]);
if (p == NULL) if (!p)
{ {
LogError(amx, AMX_ERR_NATIVE, "Invalid SMC parse handle (%d)", params[1]); LogError(amx, AMX_ERR_NATIVE, "Invalid SMC parse handle (%d)", params[1]);
return 0; return 0;
} }
int length = 0; int length = 0;
const char *funcName = NULL; const char *funcName = nullptr;
if ((funcName = get_amxstring(amx, params[2], 0, length)) && length) if ((funcName = get_amxstring(amx, params[2], 0, length)) && length)
{ {
@ -108,16 +106,16 @@ static cell AMX_NATIVE_CALL SMC_SetParseEnd(AMX *amx, cell *params)
// native SMC_SetReaders(SMCParser:smc, const kvFunc[], const nsFunc[] = "", const esFunc[] = ""); // native SMC_SetReaders(SMCParser:smc, const kvFunc[], const nsFunc[] = "", const esFunc[] = "");
static cell AMX_NATIVE_CALL SMC_SetReaders(AMX *amx, cell *params) static cell AMX_NATIVE_CALL SMC_SetReaders(AMX *amx, cell *params)
{ {
ParseInfo *p = g_TextParsersHandles.lookup(params[1]); ParseInfo *p = TextParsersHandles.lookup(params[1]);
if (p == NULL) if (!p)
{ {
LogError(amx, AMX_ERR_NATIVE, "Invalid SMC parse handle (%d)", params[1]); LogError(amx, AMX_ERR_NATIVE, "Invalid SMC parse handle (%d)", params[1]);
return 0; return 0;
} }
int kvLength = 0, nsLength = 0, esLength = 0; int kvLength = 0, nsLength = 0, esLength = 0;
const char *funcName = NULL; const char *funcName = nullptr;
if ((funcName = get_amxstring(amx, params[2], 0, kvLength)) && kvLength) if ((funcName = get_amxstring(amx, params[2], 0, kvLength)) && kvLength)
{ {
@ -146,16 +144,16 @@ static cell AMX_NATIVE_CALL SMC_SetReaders(AMX *amx, cell *params)
// native SMC_SetRawLine(SMCParser:handle, const func[]); // native SMC_SetRawLine(SMCParser:handle, const func[]);
static cell AMX_NATIVE_CALL SMC_SetRawLine(AMX *amx, cell *params) static cell AMX_NATIVE_CALL SMC_SetRawLine(AMX *amx, cell *params)
{ {
ParseInfo *p = g_TextParsersHandles.lookup(params[1]); ParseInfo *p = TextParsersHandles.lookup(params[1]);
if (p == NULL) if (!p)
{ {
LogError(amx, AMX_ERR_NATIVE, "Invalid SMC parse handle (%d)", params[1]); LogError(amx, AMX_ERR_NATIVE, "Invalid SMC parse handle (%d)", params[1]);
return 0; return 0;
} }
int length = 0; int length = 0;
const char *funcName = NULL; const char *funcName = nullptr;
if ((funcName = get_amxstring(amx, params[2], 0, length)) && length) if ((funcName = get_amxstring(amx, params[2], 0, length)) && length)
{ {
@ -174,9 +172,9 @@ static cell AMX_NATIVE_CALL SMC_SetRawLine(AMX *amx, cell *params)
// native SMCError:SMC_ParseFile(SMCParser:handle, const file[], &line = 0, &col = 0); // native SMCError:SMC_ParseFile(SMCParser:handle, const file[], &line = 0, &col = 0);
static cell AMX_NATIVE_CALL SMC_ParseFile(AMX *amx, cell *params) static cell AMX_NATIVE_CALL SMC_ParseFile(AMX *amx, cell *params)
{ {
ParseInfo *p = g_TextParsersHandles.lookup(params[1]); ParseInfo *p = TextParsersHandles.lookup(params[1]);
if (p == NULL) if (!p)
{ {
LogError(amx, AMX_ERR_NATIVE, "Invalid SMC parse handle (%d)", params[1]); LogError(amx, AMX_ERR_NATIVE, "Invalid SMC parse handle (%d)", params[1]);
return 0; return 0;
@ -227,9 +225,9 @@ static cell AMX_NATIVE_CALL INI_CreateParser(AMX *amx, cell *params)
// native bool:INI_ParseFile(INIParser:handle, const file[], &line = 0, &col = 0); // native bool:INI_ParseFile(INIParser:handle, const file[], &line = 0, &col = 0);
static cell AMX_NATIVE_CALL INI_ParseFile(AMX *amx, cell *params) static cell AMX_NATIVE_CALL INI_ParseFile(AMX *amx, cell *params)
{ {
ParseInfo *p = g_TextParsersHandles.lookup(params[1]); ParseInfo *p = TextParsersHandles.lookup(params[1]);
if (p == NULL) if (!p)
{ {
LogError(amx, AMX_ERR_NATIVE, "Invalid INI parse handle (%d)", params[1]); LogError(amx, AMX_ERR_NATIVE, "Invalid INI parse handle (%d)", params[1]);
return 0; return 0;
@ -250,16 +248,16 @@ static cell AMX_NATIVE_CALL INI_ParseFile(AMX *amx, cell *params)
// native INI_SetParseStart(INIParser:handle, const func[]); // native INI_SetParseStart(INIParser:handle, const func[]);
static cell AMX_NATIVE_CALL INI_SetParseStart(AMX *amx, cell *params) static cell AMX_NATIVE_CALL INI_SetParseStart(AMX *amx, cell *params)
{ {
ParseInfo *p = g_TextParsersHandles.lookup(params[1]); ParseInfo *p = TextParsersHandles.lookup(params[1]);
if (p == NULL) if (!p)
{ {
LogError(amx, AMX_ERR_NATIVE, "Invalid INI parse handle (%d)", params[1]); LogError(amx, AMX_ERR_NATIVE, "Invalid INI parse handle (%d)", params[1]);
return 0; return 0;
} }
int length = 0; int length = 0;
const char *funcName = NULL; const char *funcName = nullptr;
if ((funcName = get_amxstring(amx, params[2], 0, length)) && length) if ((funcName = get_amxstring(amx, params[2], 0, length)) && length)
{ {
@ -278,16 +276,16 @@ static cell AMX_NATIVE_CALL INI_SetParseStart(AMX *amx, cell *params)
// native INI_SetParseEnd(INIParser:handle, const func[]); // native INI_SetParseEnd(INIParser:handle, const func[]);
static cell AMX_NATIVE_CALL INI_SetParseEnd(AMX *amx, cell *params) static cell AMX_NATIVE_CALL INI_SetParseEnd(AMX *amx, cell *params)
{ {
ParseInfo *p = g_TextParsersHandles.lookup(params[1]); ParseInfo *p = TextParsersHandles.lookup(params[1]);
if (p == NULL) if (!p)
{ {
LogError(amx, AMX_ERR_NATIVE, "Invalid INI parse handle (%d)", params[1]); LogError(amx, AMX_ERR_NATIVE, "Invalid INI parse handle (%d)", params[1]);
return 0; return 0;
} }
int length = 0; int length = 0;
const char *funcName = NULL; const char *funcName = nullptr;
if ((funcName = get_amxstring(amx, params[2], 0, length))) if ((funcName = get_amxstring(amx, params[2], 0, length)))
{ {
@ -306,16 +304,16 @@ static cell AMX_NATIVE_CALL INI_SetParseEnd(AMX *amx, cell *params)
// native INI_SetReaders(INIParser:smc, const kvFunc[], const nsFunc[] = "" ); // native INI_SetReaders(INIParser:smc, const kvFunc[], const nsFunc[] = "" );
static cell AMX_NATIVE_CALL INI_SetReaders(AMX *amx, cell *params) static cell AMX_NATIVE_CALL INI_SetReaders(AMX *amx, cell *params)
{ {
ParseInfo *p = g_TextParsersHandles.lookup(params[1]); ParseInfo *p = TextParsersHandles.lookup(params[1]);
if (p == NULL) if (!p)
{ {
LogError(amx, AMX_ERR_NATIVE, "Invalid INI parse handle (%d)", params[1]); LogError(amx, AMX_ERR_NATIVE, "Invalid INI parse handle (%d)", params[1]);
return 0; return 0;
} }
int kvLength = 0, nsLength = 0; int kvLength = 0, nsLength = 0;
const char *funcName = NULL; const char *funcName = nullptr;
if ((funcName = get_amxstring(amx, params[2], 0, kvLength)) && kvLength) if ((funcName = get_amxstring(amx, params[2], 0, kvLength)) && kvLength)
{ {
@ -339,16 +337,16 @@ static cell AMX_NATIVE_CALL INI_SetReaders(AMX *amx, cell *params)
// native INI_SetRawLine(INIParser:handle, const func[]); // native INI_SetRawLine(INIParser:handle, const func[]);
static cell AMX_NATIVE_CALL INI_SetRawLine(AMX *amx, cell *params) static cell AMX_NATIVE_CALL INI_SetRawLine(AMX *amx, cell *params)
{ {
ParseInfo *p = g_TextParsersHandles.lookup(params[1]); ParseInfo *p = TextParsersHandles.lookup(params[1]);
if (p == NULL) if (!p)
{ {
LogError(amx, AMX_ERR_NATIVE, "Invalid INI parse handle (%d)", params[1]); LogError(amx, AMX_ERR_NATIVE, "Invalid INI parse handle (%d)", params[1]);
return 0; return 0;
} }
int length = 0; int length = 0;
const char *funcName = NULL; const char *funcName = nullptr;
if ((funcName = get_amxstring(amx, params[2], 0, length)) && length) if ((funcName = get_amxstring(amx, params[2], 0, length)) && length)
{ {
@ -390,5 +388,5 @@ AMX_NATIVE_INFO g_TextParserNatives[] =
{ "INI_SetRawLine" , INI_SetRawLine }, { "INI_SetRawLine" , INI_SetRawLine },
{ "INI_DestroyParser" , INI_DestroyParser }, { "INI_DestroyParser" , INI_DestroyParser },
{ NULL, NULL }, { nullptr , nullptr },
}; };

View File

@ -14,6 +14,7 @@
#include "amxmodx.h" #include "amxmodx.h"
#include "CTextParsers.h" #include "CTextParsers.h"
#include "natives_handles.h"
class ParseInfo : class ParseInfo :
public ITextListener_SMC, public ITextListener_SMC,
@ -131,77 +132,6 @@ public:
int handle; int handle;
}; };
template <typename T> extern Handle<ParseInfo> TextParsersHandles;
class TextParserHandles
{
private:
ke::Vector<T *> m_textparsers;
public:
TextParserHandles() { }
~TextParserHandles()
{
this->clear();
}
void clear()
{
for (size_t i = 0; i < m_textparsers.length(); i++)
{
if (m_textparsers[i] != NULL)
{
delete m_textparsers[i];
}
}
m_textparsers.clear();
}
T *lookup(int handle)
{
handle--;
if (handle < 0 || handle >= static_cast<int>(m_textparsers.length()))
{
return NULL;
}
return m_textparsers[handle];
}
int create()
{
for (size_t i = 0; i < m_textparsers.length(); i++)
{
if (m_textparsers[i] == NULL)
{
// reuse handle
m_textparsers[i] = new T;
return static_cast<int>(i)+1;
}
}
m_textparsers.append(new T);
return m_textparsers.length();
}
bool destroy(int handle)
{
handle--;
if (handle < 0 || handle >= static_cast<int>(m_textparsers.length()))
{
return false;
}
if (m_textparsers[handle] == NULL)
{
return false;
}
delete m_textparsers[handle];
m_textparsers[handle] = NULL;
return true;
}
};
extern TextParserHandles<ParseInfo> g_TextParsersHandles;
#endif // _INCLUDE_TEXTPARSE_H_ #endif // _INCLUDE_TEXTPARSE_H_

View File

@ -7,28 +7,23 @@
// Additional exceptions apply. For full license details, see LICENSE.txt or visit: // Additional exceptions apply. For full license details, see LICENSE.txt or visit:
// https://alliedmods.net/amxmodx-license // https://alliedmods.net/amxmodx-license
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include "amxmodx.h"
#include "trie_natives.h" #include "trie_natives.h"
TrieHandles<CellTrie> g_TrieHandles; Handle<CellTrie> TrieHandles;
TrieHandles<TrieSnapshot> g_TrieSnapshotHandles; Handle<TrieSnapshot> TrieSnapshotHandles;
// native Trie:TrieCreate(); // native Trie:TrieCreate();
static cell AMX_NATIVE_CALL TrieCreate(AMX *amx, cell *params) static cell AMX_NATIVE_CALL TrieCreate(AMX *amx, cell *params)
{ {
return static_cast<cell>(g_TrieHandles.create()); return static_cast<cell>(TrieHandles.create());
} }
// native Trie::TrieClear(Trie:handle); // native Trie::TrieClear(Trie:handle);
static cell AMX_NATIVE_CALL TrieClear(AMX *amx, cell *params) static cell AMX_NATIVE_CALL TrieClear(AMX *amx, cell *params)
{ {
CellTrie *t = g_TrieHandles.lookup(params[1]); CellTrie *t = TrieHandles.lookup(params[1]);
if (t == NULL) if (!t)
{ {
LogError(amx, AMX_ERR_NATIVE, "Invalid map handle provided (%d)", params[1]); LogError(amx, AMX_ERR_NATIVE, "Invalid map handle provided (%d)", params[1]);
return 0; return 0;
@ -40,9 +35,9 @@ static cell AMX_NATIVE_CALL TrieClear(AMX *amx, cell *params)
// native TrieSetCell(Trie:handle, const key[], any:value, bool:replace = true); // native TrieSetCell(Trie:handle, const key[], any:value, bool:replace = true);
static cell AMX_NATIVE_CALL TrieSetCell(AMX *amx, cell *params) static cell AMX_NATIVE_CALL TrieSetCell(AMX *amx, cell *params)
{ {
CellTrie *t = g_TrieHandles.lookup(params[1]); CellTrie *t = TrieHandles.lookup(params[1]);
if (t == NULL) if (!t)
{ {
LogError(amx, AMX_ERR_NATIVE, "Invalid map handle provided (%d)", params[1]); LogError(amx, AMX_ERR_NATIVE, "Invalid map handle provided (%d)", params[1]);
return 0; return 0;
@ -76,9 +71,9 @@ static cell AMX_NATIVE_CALL TrieSetCell(AMX *amx, cell *params)
// native TrieSetString(Trie:handle, const key[], const data[], bool:replace = true); // native TrieSetString(Trie:handle, const key[], const data[], bool:replace = true);
static cell AMX_NATIVE_CALL TrieSetString(AMX *amx, cell *params) static cell AMX_NATIVE_CALL TrieSetString(AMX *amx, cell *params)
{ {
CellTrie *t = g_TrieHandles.lookup(params[1]); CellTrie *t = TrieHandles.lookup(params[1]);
if (t == NULL) if (!t)
{ {
LogError(amx, AMX_ERR_NATIVE, "Invalid map handle provided (%d)", params[1]); LogError(amx, AMX_ERR_NATIVE, "Invalid map handle provided (%d)", params[1]);
return 0; return 0;
@ -113,9 +108,9 @@ static cell AMX_NATIVE_CALL TrieSetString(AMX *amx, cell *params)
// native TrieSetArray(Trie:handle, const key[], const any:buffer[], buffsize, bool:replace = true) // native TrieSetArray(Trie:handle, const key[], const any:buffer[], buffsize, bool:replace = true)
static cell AMX_NATIVE_CALL TrieSetArray(AMX *amx, cell *params) static cell AMX_NATIVE_CALL TrieSetArray(AMX *amx, cell *params)
{ {
CellTrie *t = g_TrieHandles.lookup(params[1]); CellTrie *t = TrieHandles.lookup(params[1]);
if (t == NULL) if (!t)
{ {
LogError(amx, AMX_ERR_NATIVE, "Invalid map handle provided (%d)", params[1]); LogError(amx, AMX_ERR_NATIVE, "Invalid map handle provided (%d)", params[1]);
return 0; return 0;
@ -157,9 +152,9 @@ static cell AMX_NATIVE_CALL TrieSetArray(AMX *amx, cell *params)
// native bool:TrieGetCell(Trie:handle, const key[], &any:value); // native bool:TrieGetCell(Trie:handle, const key[], &any:value);
static cell AMX_NATIVE_CALL TrieGetCell(AMX *amx, cell *params) static cell AMX_NATIVE_CALL TrieGetCell(AMX *amx, cell *params)
{ {
CellTrie *t = g_TrieHandles.lookup(params[1]); CellTrie *t = TrieHandles.lookup(params[1]);
if (t == NULL) if (!t)
{ {
LogError(amx, AMX_ERR_NATIVE, "Invalid map handle provided (%d)", params[1]); LogError(amx, AMX_ERR_NATIVE, "Invalid map handle provided (%d)", params[1]);
return 0; return 0;
@ -189,9 +184,9 @@ static cell AMX_NATIVE_CALL TrieGetCell(AMX *amx, cell *params)
// native bool:TrieGetString(Trie:handle, const key[], buff[], len, &size = 0); // native bool:TrieGetString(Trie:handle, const key[], buff[], len, &size = 0);
static cell AMX_NATIVE_CALL TrieGetString(AMX *amx, cell *params) static cell AMX_NATIVE_CALL TrieGetString(AMX *amx, cell *params)
{ {
CellTrie *t = g_TrieHandles.lookup(params[1]); CellTrie *t = TrieHandles.lookup(params[1]);
if (t == NULL) if (!t)
{ {
LogError(amx, AMX_ERR_NATIVE, "Invalid map handle provided (%d)", params[1]); LogError(amx, AMX_ERR_NATIVE, "Invalid map handle provided (%d)", params[1]);
return 0; return 0;
@ -221,9 +216,9 @@ static cell AMX_NATIVE_CALL TrieGetString(AMX *amx, cell *params)
// native bool:TrieGetArray(Trie:handle, const key[], any:buff[], len, &size = 0); // native bool:TrieGetArray(Trie:handle, const key[], any:buff[], len, &size = 0);
static cell AMX_NATIVE_CALL TrieGetArray(AMX *amx, cell *params) static cell AMX_NATIVE_CALL TrieGetArray(AMX *amx, cell *params)
{ {
CellTrie *t = g_TrieHandles.lookup(params[1]); CellTrie *t = TrieHandles.lookup(params[1]);
if (t == NULL) if (!t)
{ {
LogError(amx, AMX_ERR_NATIVE, "Invalid map handle provided (%d)", params[1]); LogError(amx, AMX_ERR_NATIVE, "Invalid map handle provided (%d)", params[1]);
return 0; return 0;
@ -272,9 +267,9 @@ static cell AMX_NATIVE_CALL TrieGetArray(AMX *amx, cell *params)
// native bool:TrieKeyExists(Trie:handle, const key[]); // native bool:TrieKeyExists(Trie:handle, const key[]);
static cell AMX_NATIVE_CALL TrieKeyExists(AMX *amx, cell *params) static cell AMX_NATIVE_CALL TrieKeyExists(AMX *amx, cell *params)
{ {
CellTrie *t = g_TrieHandles.lookup(params[1]); CellTrie *t = TrieHandles.lookup(params[1]);
if (t == NULL) if (!t)
{ {
LogError(amx, AMX_ERR_NATIVE, "Invalid map handle provided (%d)", params[1]); LogError(amx, AMX_ERR_NATIVE, "Invalid map handle provided (%d)", params[1]);
return 0; return 0;
@ -289,9 +284,9 @@ static cell AMX_NATIVE_CALL TrieKeyExists(AMX *amx, cell *params)
// native bool:TrieDeleteKey(Trie:handle, const key[]); // native bool:TrieDeleteKey(Trie:handle, const key[]);
static cell AMX_NATIVE_CALL TrieDeleteKey(AMX *amx, cell *params) static cell AMX_NATIVE_CALL TrieDeleteKey(AMX *amx, cell *params)
{ {
CellTrie *t = g_TrieHandles.lookup(params[1]); CellTrie *t = TrieHandles.lookup(params[1]);
if (t == NULL) if (!t)
{ {
LogError(amx, AMX_ERR_NATIVE, "Invalid map handle provided (%d)", params[1]); LogError(amx, AMX_ERR_NATIVE, "Invalid map handle provided (%d)", params[1]);
return 0; return 0;
@ -317,14 +312,14 @@ static cell AMX_NATIVE_CALL TrieDestroy(AMX *amx, cell *params)
{ {
cell *ptr = get_amxaddr(amx, params[1]); cell *ptr = get_amxaddr(amx, params[1]);
CellTrie *t = g_TrieHandles.lookup(*ptr); CellTrie *t = TrieHandles.lookup(*ptr);
if (t == NULL) if (!t)
{ {
return 0; return 0;
} }
if (g_TrieHandles.destroy(*ptr)) if (TrieHandles.destroy(*ptr))
{ {
*ptr = 0; *ptr = 0;
return 1; return 1;
@ -336,9 +331,9 @@ static cell AMX_NATIVE_CALL TrieDestroy(AMX *amx, cell *params)
// native TrieGetSize(Trie:handle); // native TrieGetSize(Trie:handle);
static cell AMX_NATIVE_CALL TrieGetSize(AMX *amx, cell *params) static cell AMX_NATIVE_CALL TrieGetSize(AMX *amx, cell *params)
{ {
CellTrie *t = g_TrieHandles.lookup(params[1]); CellTrie *t = TrieHandles.lookup(params[1]);
if (t == NULL) if (!t)
{ {
LogError(amx, AMX_ERR_NATIVE, "Invalid map handle provided (%d)", params[1]); LogError(amx, AMX_ERR_NATIVE, "Invalid map handle provided (%d)", params[1]);
return 0; return 0;
@ -349,16 +344,16 @@ static cell AMX_NATIVE_CALL TrieGetSize(AMX *amx, cell *params)
static cell AMX_NATIVE_CALL TrieSnapshotCreate(AMX *amx, cell *params) static cell AMX_NATIVE_CALL TrieSnapshotCreate(AMX *amx, cell *params)
{ {
CellTrie *t = g_TrieHandles.lookup(params[1]); CellTrie *t = TrieHandles.lookup(params[1]);
if (t == NULL) if (!t)
{ {
LogError(amx, AMX_ERR_NATIVE, "Invalid map handle provided (%d)", params[1]); LogError(amx, AMX_ERR_NATIVE, "Invalid map handle provided (%d)", params[1]);
return 0; return 0;
} }
int index = g_TrieSnapshotHandles.create(); int index = TrieSnapshotHandles.create();
TrieSnapshot *snapshot = g_TrieSnapshotHandles.lookup(index); TrieSnapshot *snapshot = TrieSnapshotHandles.lookup(index);
snapshot->length = t->map.elements(); snapshot->length = t->map.elements();
snapshot->keys = new int[snapshot->length]; snapshot->keys = new int[snapshot->length];
@ -374,9 +369,9 @@ static cell AMX_NATIVE_CALL TrieSnapshotCreate(AMX *amx, cell *params)
static cell AMX_NATIVE_CALL TrieSnapshotLength(AMX *amx, cell *params) static cell AMX_NATIVE_CALL TrieSnapshotLength(AMX *amx, cell *params)
{ {
TrieSnapshot *snapshot = g_TrieSnapshotHandles.lookup(params[1]); TrieSnapshot *snapshot = TrieSnapshotHandles.lookup(params[1]);
if (snapshot == NULL) if (!snapshot)
{ {
LogError(amx, AMX_ERR_NATIVE, "Invalid snapshot handle provided (%d)", params[1]); LogError(amx, AMX_ERR_NATIVE, "Invalid snapshot handle provided (%d)", params[1]);
return 0; return 0;
@ -387,9 +382,9 @@ static cell AMX_NATIVE_CALL TrieSnapshotLength(AMX *amx, cell *params)
static cell AMX_NATIVE_CALL TrieSnapshotKeyBufferSize(AMX *amx, cell *params) static cell AMX_NATIVE_CALL TrieSnapshotKeyBufferSize(AMX *amx, cell *params)
{ {
TrieSnapshot *snapshot = g_TrieSnapshotHandles.lookup(params[1]); TrieSnapshot *snapshot = TrieSnapshotHandles.lookup(params[1]);
if (snapshot == NULL) if (!snapshot)
{ {
LogError(amx, AMX_ERR_NATIVE, "Invalid snapshot handle provided (%d)", params[1]); LogError(amx, AMX_ERR_NATIVE, "Invalid snapshot handle provided (%d)", params[1]);
return 0; return 0;
@ -408,9 +403,9 @@ static cell AMX_NATIVE_CALL TrieSnapshotKeyBufferSize(AMX *amx, cell *params)
static cell AMX_NATIVE_CALL TrieSnapshotGetKey(AMX *amx, cell *params) static cell AMX_NATIVE_CALL TrieSnapshotGetKey(AMX *amx, cell *params)
{ {
TrieSnapshot *snapshot = g_TrieSnapshotHandles.lookup(params[1]); TrieSnapshot *snapshot = TrieSnapshotHandles.lookup(params[1]);
if (snapshot == NULL) if (!snapshot)
{ {
LogError(amx, AMX_ERR_NATIVE, "Invalid snapshot handle provided (%d)", params[1]); LogError(amx, AMX_ERR_NATIVE, "Invalid snapshot handle provided (%d)", params[1]);
return 0; return 0;
@ -433,14 +428,14 @@ static cell AMX_NATIVE_CALL TrieSnapshotDestroy(AMX *amx, cell *params)
{ {
cell *ptr = get_amxaddr(amx, params[1]); cell *ptr = get_amxaddr(amx, params[1]);
TrieSnapshot *t = g_TrieSnapshotHandles.lookup(*ptr); TrieSnapshot *t = TrieSnapshotHandles.lookup(*ptr);
if (t == NULL) if (!t)
{ {
return 0; return 0;
} }
if (g_TrieSnapshotHandles.destroy(*ptr)) if (TrieSnapshotHandles.destroy(*ptr))
{ {
*ptr = 0; *ptr = 0;
return 1; return 1;
@ -473,6 +468,6 @@ AMX_NATIVE_INFO trie_Natives[] =
{ "TrieSnapshotGetKey" , TrieSnapshotGetKey }, { "TrieSnapshotGetKey" , TrieSnapshotGetKey },
{ "TrieSnapshotDestroy" , TrieSnapshotDestroy }, { "TrieSnapshotDestroy" , TrieSnapshotDestroy },
{ NULL, NULL } { nullptr , nullptr}
}; };

View File

@ -13,7 +13,7 @@
#include "amxmodx.h" #include "amxmodx.h"
#include <sm_stringhashmap.h> #include <sm_stringhashmap.h>
#include <sm_memtable.h> #include <sm_memtable.h>
#include "CVector.h" #include "natives_handles.h"
enum EntryType enum EntryType
{ {
@ -159,80 +159,8 @@ struct TrieSnapshot
BaseStringTable strings; BaseStringTable strings;
}; };
template <typename T> extern Handle<CellTrie> TrieHandles;
class TrieHandles extern Handle<TrieSnapshot> TrieSnapshotHandles;
{
private:
CVector<T *> m_tries;
public:
TrieHandles() { }
~TrieHandles()
{
this->clear();
}
void clear()
{
for (size_t i = 0; i < m_tries.size(); i++)
{
if (m_tries[i] != NULL)
{
delete m_tries[i];
}
}
m_tries.clear();
}
T *lookup(int handle)
{
handle--;
if (handle < 0 || handle >= static_cast<int>(m_tries.size()))
{
return NULL;
}
return m_tries[handle];
}
int create()
{
for (size_t i = 0; i < m_tries.size(); i++)
{
if (m_tries[i] == NULL)
{
// reuse handle
m_tries[i] = new T;
return static_cast<int>(i) + 1;
}
}
m_tries.push_back(new T);
return m_tries.size();
}
bool destroy(int handle)
{
handle--;
if (handle < 0 || handle >= static_cast<int>(m_tries.size()))
{
return false;
}
if (m_tries[handle] == NULL)
{
return false;
}
delete m_tries[handle];
m_tries[handle] = NULL;
return true;
}
};
extern TrieHandles<CellTrie> g_TrieHandles;
extern TrieHandles<TrieSnapshot> g_TrieSnapshotHandles;
extern AMX_NATIVE_INFO trie_Natives[]; extern AMX_NATIVE_INFO trie_Natives[];
#endif #endif