added unregister_forward()

This commit is contained in:
David Anderson 2006-02-03 03:27:03 +00:00
parent df082de817
commit 2ad9a320de
3 changed files with 115 additions and 31 deletions

View File

@ -70,32 +70,58 @@ template <class T> class CVector
// change size // change size
if (size == m_Size) if (size == m_Size)
return true; return true;
if (!size)
{
if (m_Data)
{
delete [] m_Data;
m_Data = NULL;
m_Size = 0;
}
return true;
}
T *newData = new T[size]; T *newData = new T[size];
if (!newData) if (!newData)
return false; return false;
if (m_Data) if (m_Data)
{ {
size_t end = (m_Size < size) ? (m_Size) : size; size_t end = (m_CurrentUsedSize < size) ? (m_CurrentUsedSize) : size;
for (size_t i=0; i<end; i++) for (size_t i=0; i<end; i++)
newData[i] = m_Data[i]; newData[i] = m_Data[i];
delete [] m_Data; delete [] m_Data;
} }
if (m_Size < size)
m_CurrentSize = size;
m_Data = newData; m_Data = newData;
m_Size = size; m_Size = size;
if (m_CurrentUsedSize > m_Size)
m_CurrentUsedSize = m_Size;
return true; return true;
} }
void FreeMemIfPossible() void FreeMemIfPossible()
{ {
if (!m_Data)
return;
if (!m_CurrentUsedSize)
{
ChangeSize(0);
return;
}
size_t newSize = m_Size;
while (m_CurrentUsedSize <= newSize / 2)
newSize /= 2;
if (newSize != m_Size)
ChangeSize(newSize);
} }
protected: protected:
T *m_Data; T *m_Data;
size_t m_Size; size_t m_Size;
size_t m_CurrentUsedSize; size_t m_CurrentUsedSize;
size_t m_CurrentSize;
public: public:
class iterator class iterator
{ {
@ -189,7 +215,7 @@ public:
iterator & operator-=(size_t offset) iterator & operator-=(size_t offset)
{ {
m_Ptr += offset; m_Ptr -= offset;
return (*this); return (*this);
} }
@ -203,7 +229,7 @@ public:
iterator operator-(size_t offset) const iterator operator-(size_t offset) const
{ {
iterator tmp(*this); iterator tmp(*this);
tmp.m_Ptr += offset; tmp.m_Ptr -= offset;
return tmp; return tmp;
} }
@ -277,12 +303,12 @@ public:
return m_Size; return m_Size;
} }
iterator begin() iterator begin() const
{ {
return iterator(m_Data); return iterator(m_Data);
} }
iterator end() iterator end() const
{ {
return iterator(m_Data + m_CurrentUsedSize); return iterator(m_Data + m_CurrentUsedSize);
} }
@ -296,7 +322,9 @@ public:
bool reserve(size_t newSize) bool reserve(size_t newSize)
{ {
if (newSize > m_Size)
return ChangeSize(newSize); return ChangeSize(newSize);
return true;
} }
bool push_back(const T & elem) bool push_back(const T & elem)
@ -317,14 +345,15 @@ public:
--m_CurrentUsedSize; --m_CurrentUsedSize;
if (m_CurrentUsedSize < 0) if (m_CurrentUsedSize < 0)
m_CurrentUsedSize = 0; m_CurrentUsedSize = 0;
// :TODO: free memory sometimes
FreeMemIfPossible();
} }
bool resize(size_t newSize) bool resize(size_t newSize)
{ {
if (!ChangeSize(newSize)) if (!ChangeSize(newSize))
return false; return false;
FreeMemIfPossible(); m_CurrentUsedSize = newSize;
return true; return true;
} }
@ -397,15 +426,13 @@ public:
return m_Data[m_CurrentUsedSize - 1]; return m_Data[m_CurrentUsedSize - 1];
} }
bool insert(iterator where, const T & value) iterator insert(iterator where, const T & value)
{ {
// we have to insert before
// if it is begin, don't decrement
if (where != m_Data)
--where;
// validate iter // validate iter
if (where < m_Data || where >= (m_Data + m_CurrentUsedSize)) if (where < m_Data || where > (m_Data + m_CurrentUsedSize))
return false; return iterator(0);
size_t ofs = where - begin();
++m_CurrentUsedSize; ++m_CurrentUsedSize;
if (!GrowIfNeeded()) if (!GrowIfNeeded())
@ -414,34 +441,50 @@ public:
return false; return false;
} }
memmove(where.base() + 1, where.base(), m_CurrentUsedSize - (where - m_Data)); where = begin() + ofs;
memcpy(where.base(), &value, sizeof(T));
return true; // Move subsequent entries
for (T *ptr = m_Data + m_CurrentUsedSize - 2; ptr >= where.base(); --ptr)
*(ptr + 1) = *ptr;
*where.base() = value;
return where;
} }
void erase(iterator where) iterator erase(iterator where)
{ {
// validate iter // validate iter
if (where < m_Data || where >= (m_Data + m_CurrentUsedSize)) if (where < m_Data || where >= (m_Data + m_CurrentUsedSize))
return false; return iterator(0);
size_t ofs = where - begin();
if (m_CurrentUsedSize > 1) if (m_CurrentUsedSize > 1)
{ {
// move // move
memmove(where.base(), where.base() + 1, m_CurrentUsedSize - 1); T *theend = m_Data + m_CurrentUsedSize;
for (T *ptr = where.base() + 1; ptr < theend; ++ptr)
*(ptr - 1) = *ptr;
} }
--m_CurrentUsedSize; --m_CurrentUsedSize;
// :TODO: free memory sometimes
FreeMemIfPossible();
return begin() + ofs;
} }
void clear() void clear()
{ {
m_Size = 0; m_Size = 0;
m_CurrentUsedSize = 0; m_CurrentUsedSize = 0;
if (m_Data)
{
delete [] m_Data; delete [] m_Data;
m_Data = NULL; m_Data = NULL;
} }
}
}; };
#endif // __CVECTOR_H__ #endif // __CVECTOR_H__

View File

@ -491,7 +491,36 @@ SIMPLE_INT_HOOK_EDICT_EDICT(ShouldCollide);
static cell AMX_NATIVE_CALL unregister_forward(AMX *amx, cell *params)
{
int func = params[1];
int func_id = params[2];
int post = params[3];
if (func >= FM_LAST_DONT_USE_ME || func < 1)
{
MF_LogError(amx, AMX_ERR_NATIVE, "Invalid function: %d", func);
return 0;
}
CVector<int>::iterator begin, end=Engine[func].end();
for (begin=Engine[func].begin(); begin!=end; begin++)
{
if ((*begin) == func_id)
{
Engine[func].erase(begin);
if (!Engine[func].size())
{
//:TODO: we should probably clear this here!
//but, we have no reverse lookup possible.
}
return 1;
}
}
return 0;
}
static cell AMX_NATIVE_CALL register_forward(AMX *amx, cell *params) static cell AMX_NATIVE_CALL register_forward(AMX *amx, cell *params)
@ -501,7 +530,7 @@ static cell AMX_NATIVE_CALL register_forward(AMX *amx, cell *params)
int post = params[3]; int post = params[3];
if (func >= FM_LAST_DONT_USE_ME || func < 1) if (func >= FM_LAST_DONT_USE_ME || func < 1)
{ {
MF_RaiseAmxError(amx, AMX_ERR_NATIVE); MF_LogError(amx, AMX_ERR_NATIVE, "Invalid function: %d", func);
return 0; return 0;
} }
@ -1099,7 +1128,10 @@ static cell AMX_NATIVE_CALL register_forward(AMX *amx, cell *params)
} }
if (!fId) if (!fId)
{
MF_LogError(amx, AMX_ERR_NATIVE, "Function not found (%d, %s)", func, funcname);
return 0; return 0;
}
if (post) if (post)
{ {
@ -1108,11 +1140,12 @@ static cell AMX_NATIVE_CALL register_forward(AMX *amx, cell *params)
Engine[func].push_back(fId); Engine[func].push_back(fId);
} }
return 1; return fId;
} }
AMX_NATIVE_INFO forward_natives[] = { AMX_NATIVE_INFO forward_natives[] = {
{ "register_forward", register_forward }, { "register_forward", register_forward },
{ "unregister_forward", unregister_forward },
{ "forward_return", fm_return }, { "forward_return", fm_return },
{ NULL, NULL } { NULL, NULL }
}; };

View File

@ -35,9 +35,17 @@ native Float:get_pdata_float(_index,_Offset,_linuxdiff=5);
native set_pdata_float(_index,_Offset,Float:_Value,_linuxdiff=5); native set_pdata_float(_index,_Offset,Float:_Value,_linuxdiff=5);
/* Registers a forward */ /* Registers a forward.
* Returns an id you can pass to unregister_forward
*/
native register_forward(_forwardType,_function[],_post=0); native register_forward(_forwardType,_function[],_post=0);
/* Unregisters a forward.
* The registerId must be from register_forward, and
* post/forwardtype must match what you registered the forward as.
*/
native unregister_forward(_forwardType, registerId, post=0);
/* Returns data for metamod */ /* Returns data for metamod */
native forward_return(type,{Float,Sql,Result,_}:...); native forward_return(type,{Float,Sql,Result,_}:...);