not using c++ std lib

This commit is contained in:
Pavol Marko 2004-03-14 13:57:27 +00:00
parent 812ee46260
commit 41a56a8fda
2 changed files with 182 additions and 149 deletions

View File

@ -58,11 +58,54 @@ EventsMngr::ClEvent::ClEvent(CPluginMngr::CPlugin* plugin, int func, int flags)
m_Stamp = 0.0f; m_Stamp = 0.0f;
m_Done = false; m_Done = false;
m_Conditions = NULL;
} }
EventsMngr::ClEvent::~ClEvent() EventsMngr::ClEvent::~ClEvent()
{ {
cond_t *tmp1 = m_Conditions;
cond_t *tmp2 = NULL;
while (tmp1)
{
tmp2 = tmp1->next;
delete tmp1;
tmp1 = tmp2;
}
m_Conditions = NULL;
}
void EventsMngr::NextParam()
{
const int INITIAL_PARSEVAULT_SIZE = 32;
if (m_ParsePos < m_ParseVaultSize)
return;
MsgDataEntry *tmp = NULL;
int tmpSize = 0;
if (m_ParseVault)
{
// copy to tmp
tmp = new MsgDataEntry[m_ParseVaultSize];
memcpy(tmp, m_ParseVault, m_ParseVaultSize * sizeof(MsgDataEntry));
tmpSize = m_ParseVaultSize;
delete [] m_ParseVault;
m_ParseVault = NULL;
}
if (m_ParseVaultSize > 0)
m_ParseVaultSize *= 2;
else
m_ParseVaultSize = INITIAL_PARSEVAULT_SIZE;
m_ParseVault = new MsgDataEntry[m_ParseVaultSize];
if (tmp)
{
memcpy(m_ParseVault, tmp, tmpSize * sizeof(MsgDataEntry));
delete [] tmp;
tmp = NULL;
}
} }
int EventsMngr::ClEvent::getFunction() int EventsMngr::ClEvent::getFunction()
@ -72,12 +115,20 @@ int EventsMngr::ClEvent::getFunction()
EventsMngr::EventsMngr() EventsMngr::EventsMngr()
{ {
m_ParseVault = NULL;
m_ParseVaultSize = 0;
clearEvents(); clearEvents();
} }
EventsMngr::~EventsMngr() EventsMngr::~EventsMngr()
{ {
clearEvents(); clearEvents();
// delete parsevault
if (m_ParseVault)
{
delete [] m_ParseVault;
m_ParseVault = NULL;
}
} }
@ -109,23 +160,34 @@ void EventsMngr::ClEvent::registerFilter(char *filter)
if (!*value) if (!*value)
return; return;
CondMapPair pair; cond_t *tmpCond = new cond_t;
if (!tmpCond)
return;
// type character // type character
pair.second.type = *value; tmpCond->type = *value;
// set a null here so param id can be recognized later // set a null here so param id can be recognized, and save it
*value++ = 0; *value++ = 0;
tmpCond->paramId = atoi(filter);
// rest of line // rest of line
pair.second.sValue = value; tmpCond->sValue.set(value);
pair.second.fValue = atof(value); tmpCond->fValue = atof(value);
pair.second.iValue = atoi(value); tmpCond->iValue = atoi(value);
// param id tmpCond->next = NULL;
pair.first = atoi(filter);
m_Conditions.insert(pair); if (m_Conditions)
{
cond_t *tmp = m_Conditions;
while (tmp->next)
tmp = tmp->next;
tmp->next = tmpCond;
}
else
m_Conditions = tmpCond;
} }
EventsMngr::ClEvent* EventsMngr::registerEvent(CPluginMngr::CPlugin* plugin, int func, int flags, int msgid) EventsMngr::ClEvent* EventsMngr::registerEvent(CPluginMngr::CPlugin* plugin, int func, int flags, int msgid)
@ -138,14 +200,13 @@ EventsMngr::ClEvent* EventsMngr::registerEvent(CPluginMngr::CPlugin* plugin, int
if (!event) if (!event)
return NULL; return NULL;
m_Events[msgid].push_back(event); m_Events[msgid].put(event);
return event; return event;
} }
void EventsMngr::parserInit(int msg_type, float* timer, CPlayer* pPlayer, int index) void EventsMngr::parserInit(int msg_type, float* timer, CPlayer* pPlayer, int index)
{ {
if (msg_type < 0 || msg_type > MAX_AMX_REG_MSG) if (msg_type < 0 || msg_type > MAX_AMX_REG_MSG)
return; return;
@ -153,37 +214,38 @@ void EventsMngr::parserInit(int msg_type, float* timer, CPlayer* pPlayer, int in
m_Timer = timer; m_Timer = timer;
// don't parse if nothing to do // don't parse if nothing to do
if (m_Events[msg_type].empty()) if (!m_Events[msg_type].size())
return; return;
for(ClEventVecIter iter = m_Events[msg_type].begin(); iter != m_Events[msg_type].end(); ++iter) for(ClEventVecIter iter = m_Events[msg_type].begin(); iter; ++iter)
{ {
if ((*iter)->m_Done) if ((*iter).m_Done)
continue; continue;
if (!(*iter)->m_Plugin->isExecutable((*iter)->m_Func))
if (!(*iter).m_Plugin->isExecutable((*iter).m_Func))
{ {
(*iter)->m_Done = true; (*iter).m_Done = true;
continue; continue;
} }
if (pPlayer) if (pPlayer)
{ {
if (!(*iter)->m_FlagPlayer || (pPlayer->IsAlive() ? !(*iter)->m_FlagAlive : !(*iter)->m_FlagDead ) ) if (!(*iter).m_FlagPlayer || (pPlayer->IsAlive() ? !(*iter).m_FlagAlive : !(*iter).m_FlagDead ) )
{ {
(*iter)->m_Done = true; (*iter).m_Done = true;
continue; continue;
} }
} }
else if (!(*iter)->m_FlagWorld) else if (!(*iter).m_FlagWorld)
{ {
(*iter)->m_Done = true; (*iter).m_Done = true;
continue; continue;
} }
if ((*iter)->m_FlagOnce && (*iter)->m_Stamp == (float)(*timer)) if ((*iter).m_FlagOnce && (*iter).m_Stamp == (float)(*timer))
{ {
(*iter)->m_Done = true; (*iter).m_Done = true;
continue; continue;
} }
m_ParseNotDone = true; m_ParseNotDone = true;
@ -191,22 +253,13 @@ void EventsMngr::parserInit(int msg_type, float* timer, CPlayer* pPlayer, int in
if (m_ParseNotDone) if (m_ParseNotDone)
{ {
// we don't clear it (performance)
if (m_ParseVault.size() < 1)
{
m_ParseVault.reserve(32); // 32 as default
m_ParseVault.push_back(MsgDataVault());
}
m_ParsePos = 0; m_ParsePos = 0;
NextParam();
m_ParseVault[m_ParsePos].type = MSG_INTEGER; m_ParseVault[m_ParsePos].type = MSG_INTEGER;
m_ParseVault[m_ParsePos].iValue = index; m_ParseVault[m_ParsePos].iValue = index;
}
m_ParseFun = &m_Events[msg_type]; m_ParseFun = &m_Events[msg_type];
} }
else
{
m_ParseFun = NULL;
}
}
void EventsMngr::parseValue(int iValue) void EventsMngr::parseValue(int iValue)
{ {
@ -214,46 +267,42 @@ void EventsMngr::parseValue(int iValue)
if (!m_ParseNotDone || !m_ParseFun) if (!m_ParseNotDone || !m_ParseFun)
return; return;
// grow if needed // grow if needed
if (m_ParseVault.size() <= static_cast<size_t>(++m_ParsePos)) ++m_ParsePos;
{ NextParam();
MsgDataVault tmp;
m_ParseVault.push_back(tmp);
}
m_ParseVault[m_ParsePos].type = MSG_INTEGER; m_ParseVault[m_ParsePos].type = MSG_INTEGER;
m_ParseVault[m_ParsePos].iValue = iValue; m_ParseVault[m_ParsePos].iValue = iValue;
// loop through the registered funcs, and decide whether they have to be called // loop through the registered funcs, and decide whether they have to be called or not
bool skip; // if they shouldnt, their m_Done is set to true
for (ClEventVecIter iter = m_ParseFun->begin(); iter != m_ParseFun->end(); ++iter) for (ClEventVecIter iter = m_ParseFun->begin(); iter; ++iter)
{ {
if ((*iter)->m_Done) if ((*iter).m_Done)
continue; continue; // already skipped; don't bother with parsing
skip = false; // loop through conditions
ClEvent::CondMapIter condIter = (*iter)->m_Conditions.find(m_ParsePos); bool execute;
if (condIter == (*iter)->m_Conditions.end()) for (ClEvent::cond_t *condIter = (*iter).m_Conditions; condIter; condIter = condIter->next)
continue;
do
{ {
switch(condIter->second.type) if (condIter->paramId == m_ParsePos)
{ {
case '=': if (condIter->second.iValue == iValue) skip=true; break; execute = false;
case '!': if (condIter->second.iValue != iValue) skip=true; break; switch(condIter->type)
case '&': if (iValue & condIter->second.iValue) skip=true; break; {
case '<': if (iValue < condIter->second.iValue) skip=true; break; case '=': if (condIter->iValue == iValue) execute=true; break;
case '>': if (iValue > condIter->second.iValue) skip=true; break; case '!': if (condIter->iValue != iValue) execute=true; break;
case '&': if (iValue & condIter->iValue) execute=true; break;
case '<': if (iValue < condIter->iValue) execute=true; break;
case '>': if (iValue > condIter->iValue) execute=true; break;
} }
if (skip) if (execute)
break;
} while ( ++condIter != (*iter)->m_Conditions.end() );
if (skip)
continue; continue;
(*iter).m_Done = true; // don't execute
(*iter)->m_Done = true; break;
}
}
} }
} }
@ -263,44 +312,40 @@ void EventsMngr::parseValue(float fValue)
if (!m_ParseNotDone || !m_ParseFun) if (!m_ParseNotDone || !m_ParseFun)
return; return;
// grow if needed // grow if needed
if (m_ParseVault.size() <= static_cast<size_t>(++m_ParsePos)) ++m_ParsePos;
{ NextParam();
MsgDataVault tmp;
m_ParseVault.push_back(tmp);
}
m_ParseVault[m_ParsePos].type = MSG_FLOAT; m_ParseVault[m_ParsePos].type = MSG_FLOAT;
m_ParseVault[m_ParsePos].fValue = fValue; m_ParseVault[m_ParsePos].fValue = fValue;
// loop through the registered funcs, and decide whether they have to be called // loop through the registered funcs, and decide whether they have to be called or not
bool skip; // if they shouldnt, their m_Done is set to true
for (ClEventVecIter iter = m_ParseFun->begin(); iter != m_ParseFun->end(); ++iter) for (ClEventVecIter iter = m_ParseFun->begin(); iter; ++iter)
{ {
if ((*iter)->m_Done) if ((*iter).m_Done)
continue; continue; // already skipped; don't bother with parsing
skip = false; // loop through conditions
ClEvent::CondMapIter condIter = (*iter)->m_Conditions.find(m_ParsePos); for (ClEvent::cond_t *condIter = (*iter).m_Conditions; condIter; condIter = condIter->next)
if (condIter == (*iter)->m_Conditions.end())
continue;
do
{ {
switch(condIter->second.type) if (condIter->paramId == m_ParsePos)
{ {
case '=': if (condIter->second.fValue == fValue) skip=true; break; bool execute = false;
case '!': if (condIter->second.fValue != fValue) skip=true; break; switch(condIter->type)
case '<': if (fValue < condIter->second.fValue) skip=true; break; {
case '>': if (fValue > condIter->second.fValue) skip=true; break; case '=': if (condIter->fValue == fValue) execute=true; break;
case '!': if (condIter->fValue != fValue) execute=true; break;
case '<': if (fValue < condIter->fValue) execute=true; break;
case '>': if (fValue > condIter->fValue) execute=true; break;
} }
if (skip) if (execute)
break;
} while ( ++condIter != (*iter)->m_Conditions.end() );
if (skip)
continue; continue;
(*iter)->m_Done = true; (*iter).m_Done = true; // don't execute
break;
}
}
} }
} }
@ -311,42 +356,37 @@ void EventsMngr::parseValue(const char *sz)
return; return;
// grow if needed // grow if needed
if (m_ParseVault.size() <= static_cast<size_t>(++m_ParsePos)) ++m_ParsePos;
{ NextParam();
MsgDataVault tmp;
m_ParseVault.push_back(tmp);
}
m_ParseVault[m_ParsePos].type = MSG_STRING; m_ParseVault[m_ParsePos].type = MSG_STRING;
m_ParseVault[m_ParsePos].sValue = sz; m_ParseVault[m_ParsePos].sValue = sz;
// loop through the registered funcs, and decide whether they have to be called // loop through the registered funcs, and decide whether they have to be called or not
bool skip; // if they shouldnt, their m_Done is set to true
for (ClEventVecIter iter = m_ParseFun->begin(); iter != m_ParseFun->end(); ++iter) for (ClEventVecIter iter = m_ParseFun->begin(); iter; ++iter)
{ {
if ((*iter)->m_Done) if ((*iter).m_Done)
continue; continue; // already skipped; don't bother with parsing
skip = false; // loop through conditions
ClEvent::CondMapIter condIter = (*iter)->m_Conditions.find(m_ParsePos); for (ClEvent::cond_t *condIter = (*iter).m_Conditions; condIter; condIter = condIter->next)
if (condIter == (*iter)->m_Conditions.end())
continue;
do
{ {
switch(condIter->second.type) if (condIter->paramId == m_ParsePos)
{ {
case '=': if (!strcmp(sz, condIter->second.sValue.c_str())) skip=true; break; bool execute = false;
case '!': if (strcmp(sz, condIter->second.sValue.c_str())) skip=true; break; switch(condIter->type)
case '&': if (strstr(sz, condIter->second.sValue.c_str())) skip=true; break; {
case '=': if (!strcmp(sz, condIter->sValue.str())) execute=true; break;
case '!': if (strcmp(sz, condIter->sValue.str())) execute=true; break;
case '&': if (strstr(sz, condIter->sValue.str())) execute=true; break;
} }
if (skip) if (execute)
break;
} while ( ++condIter != (*iter)->m_Conditions.end() );
if (skip)
continue; continue;
(*iter)->m_Done = true; (*iter).m_Done = true; // don't execute
break;
}
}
} }
} }
@ -363,20 +403,20 @@ void EventsMngr::executeEvents()
try try
{ {
#endif // #ifdef ENABLEEXEPTIONS #endif // #ifdef ENABLEEXEPTIONS
for (ClEventVecIter iter = m_ParseFun->begin(); iter != m_ParseFun->end(); ++iter) for (ClEventVecIter iter = m_ParseFun->begin(); iter; ++iter)
{ {
if ( (*iter)->m_Done ) if ( (*iter).m_Done )
{ {
(*iter)->m_Done = false; (*iter).m_Done = false;
continue; continue;
} }
(*iter)->m_Stamp = *m_Timer; (*iter).m_Stamp = (float)*m_Timer;
if ((err = amx_Exec((*iter)->m_Plugin->getAMX(), NULL, (*iter)->m_Func, 1, m_ParseVault.size() ? m_ParseVault[0].iValue : 0)) != AMX_ERR_NONE) if ((err = amx_Exec((*iter).m_Plugin->getAMX(), NULL, (*iter).m_Func, 1, m_ParseVaultSize ? m_ParseVault[0].iValue : 0)) != AMX_ERR_NONE)
{ {
UTIL_Log("[AMXX] Run time error %d on line %ld (plugin \"%s\")", err, UTIL_Log("[AMXX] Run time error %d on line %ld (plugin \"%s\")", err,
(*iter)->m_Plugin->getAMX()->curline, (*iter)->m_Plugin->getName()); (*iter).m_Plugin->getAMX()->curline, (*iter).m_Plugin->getName());
} }
} }
@ -452,11 +492,6 @@ void EventsMngr::clearEvents(void)
{ {
for (int i = 0; i < MAX_AMX_REG_MSG; ++i) for (int i = 0; i < MAX_AMX_REG_MSG; ++i)
{ {
for (ClEventVecIter iter = m_Events[i].begin(); iter != m_Events[i].end(); ++iter)
{
if (*iter)
delete *iter;
}
m_Events[i].clear(); m_Events[i].clear();
} }
} }

View File

@ -32,10 +32,6 @@
#ifndef __CEVENTS_H__ #ifndef __CEVENTS_H__
#define __CEVENTS_H__ #define __CEVENTS_H__
#include <vector>
#include <map>
#include <string>
#define MAX_AMX_REG_MSG MAX_REG_MSGS+16 #define MAX_AMX_REG_MSG MAX_REG_MSGS+16
enum { enum {
@ -90,46 +86,48 @@ public:
// conditions // conditions
struct cond_t struct cond_t
{ {
std::string sValue; // value int paramId; // the message parameter id
float fValue;
int iValue; String sValue; // value (string)
int type; float fValue; // value (float)
int iValue; // value (int)
int type; // type (can be int, float, string)
cond_t *next;
}; };
typedef std::pair<int, cond_t> CondMapPair; cond_t *m_Conditions;
typedef std::map<int, cond_t> CondMap;
typedef CondMap::iterator CondMapIter;
CondMap m_Conditions;
public:
// constructors & destructors // constructors & destructors
ClEvent(CPluginMngr::CPlugin* plugin, int func, int flags); ClEvent(CPluginMngr::CPlugin* plugin, int func, int flags);
~ClEvent(); ~ClEvent();
public:
inline CPluginMngr::CPlugin* getPlugin(); inline CPluginMngr::CPlugin* getPlugin();
inline int getFunction(); inline int getFunction();
void registerFilter(char* filter); // add a condition void registerFilter(char* filter); // add a condition
}; };
private: private:
struct MsgDataVault struct MsgDataEntry
{ {
float fValue; float fValue;
int iValue; int iValue;
const char* sValue; const char* sValue;
MsgParamType type; MsgParamType type;
}; };
typedef std::vector<MsgDataVault> MsgDataVaultVec; MsgDataEntry *m_ParseVault;
typedef MsgDataVaultVec::iterator MsgDataVaultVecIter; int m_ParseVaultSize;
MsgDataVaultVec m_ParseVault; void NextParam(); // make sure a new parameter can be added
typedef std::vector<ClEvent*> ClEventVec; typedef CList<ClEvent> ClEventVec;
typedef ClEventVec::iterator ClEventVecIter; typedef ClEventVec::iterator ClEventVecIter;
ClEventVec m_Events[MAX_AMX_REG_MSG]; ClEventVec m_Events[MAX_AMX_REG_MSG];
ClEventVec *m_ParseFun; // current Event vector ClEventVec *m_ParseFun; // current Event vector
bool m_ParseNotDone; bool m_ParseNotDone;
int m_ParsePos; // is -1 less then args. num. int m_ParsePos; // is args. num. - 1
float* m_Timer; float* m_Timer;
ClEvent* getValidEvent(ClEvent* a ); ClEvent* getValidEvent(ClEvent* a );