From d6874673632d09b8900bb743749d79749242dc16 Mon Sep 17 00:00:00 2001 From: nosoop Date: Wed, 31 Oct 2018 02:20:13 -0700 Subject: [PATCH] Add iterator remove support --- smsdk_config.h | 2 +- types/iterator_container.h | 17 ++++++++++++----- types/string_multimap.cpp | 19 +++++++++++++++++-- types/string_multimap.h | 3 +++ 4 files changed, 33 insertions(+), 8 deletions(-) diff --git a/smsdk_config.h b/smsdk_config.h index 488fa6d..2608b72 100644 --- a/smsdk_config.h +++ b/smsdk_config.h @@ -40,7 +40,7 @@ /* Basic information exposed publicly */ #define SMEXT_CONF_NAME "More ADTs" #define SMEXT_CONF_DESCRIPTION "Provides additional data structures to SourceMod." -#define SMEXT_CONF_VERSION "0.1.1" +#define SMEXT_CONF_VERSION "0.1.2" #define SMEXT_CONF_AUTHOR "nosoop" #define SMEXT_CONF_URL "http://github.com/nosoop/SMExt-MoreADTs" #define SMEXT_CONF_LOGTAG "mADT" diff --git a/types/iterator_container.h b/types/iterator_container.h index 146b21f..ac63fe8 100644 --- a/types/iterator_container.h +++ b/types/iterator_container.h @@ -6,22 +6,29 @@ template class IteratorContainer { public: - IteratorContainer(typename T::iterator begin, typename T::iterator end): - _it(begin), _end(end) {}; + IteratorContainer(T* source, typename T::iterator begin, typename T::iterator end): + _struct(source), _it(begin), _end(end), _removed(false) {}; bool Next() { // TODO implement a way to mark the current entry as deleted - // if (_deleted) { - // _struct->erase(_current); - // } + if (_removed) { + _struct->erase(_current); + _removed = false; + } _current = _it; return _it++ != _end; } + void MarkRemoved() { + _removed = true; + } + typename T::iterator Current() { return _current; } private: + T* _struct; typename T::iterator _current, _it, _end; + bool _removed; }; diff --git a/types/string_multimap.cpp b/types/string_multimap.cpp index 3524d5f..b738df9 100644 --- a/types/string_multimap.cpp +++ b/types/string_multimap.cpp @@ -238,7 +238,7 @@ cell_t sm_BuildStringMultiMapIterator(IPluginContext *pContext, const cell_t *pa return pContext->ThrowNativeError("Invalid StringMultiMap handle %x (error %d)", hndl, err); } - StringMultiMapIterator *pIter = new StringMultiMapIterator(pMultiMap->begin(), + StringMultiMapIterator *pIter = new StringMultiMapIterator(pMultiMap, pMultiMap->begin(), pMultiMap->end()); return g_pHandleSys->CreateHandle(g_StringMultiMapIteratorType, pIter, @@ -259,7 +259,7 @@ cell_t sm_BuildStringMultiMapKeyIterator(IPluginContext *pContext, const cell_t pContext->LocalToString(params[2], &key); auto range = pMultiMap->equal_range(key); - StringMultiMapIterator *pIter = new StringMultiMapIterator(range.first, range.second); + StringMultiMapIterator *pIter = new StringMultiMapIterator(pMultiMap, range.first, range.second); return g_pHandleSys->CreateHandle(g_StringMultiMapIteratorType, pIter, pContext->GetIdentity(), myself->GetIdentity(), NULL); @@ -431,3 +431,18 @@ cell_t sm_SetStringMultiMapIteratorArray(IPluginContext *pContext, const cell_t return 0; } + +/* native void StringMultiMapIterator.Erase(); */ +cell_t sm_RemoveOnStringMultiMapIterator(IPluginContext *pContext, const cell_t *params) { + Handle_t hndl = static_cast(params[1]); + + StringMultiMapIterator *pMultiMapIter; + HandleError err; + if ((err = ReadStringMultiMapIterHandle(hndl, &pMultiMapIter)) != HandleError_None) { + return pContext->ThrowNativeError("Invalid StringMultiMapIterator handle %x (error %d)", hndl, err); + } + + pMultiMapIter->MarkRemoved(); + + return 0; +} diff --git a/types/string_multimap.h b/types/string_multimap.h index 3a969f2..521c26d 100644 --- a/types/string_multimap.h +++ b/types/string_multimap.h @@ -33,6 +33,7 @@ cell_t sm_GetStringMultiMapIteratorValue(IPluginContext *pContext, const cell_t cell_t sm_SetStringMultiMapIteratorValue(IPluginContext *pContext, const cell_t *params); cell_t sm_GetStringMultiMapIteratorArray(IPluginContext *pContext, const cell_t *params); cell_t sm_SetStringMultiMapIteratorArray(IPluginContext *pContext, const cell_t *params); +cell_t sm_RemoveOnStringMultiMapIterator(IPluginContext *pContext, const cell_t *params); const sp_nativeinfo_t g_StringMultiMapNatives[] = { @@ -60,5 +61,7 @@ const sp_nativeinfo_t g_StringMultiMapNatives[] = { { "StringMultiMapIterator.GetArray", sm_GetStringMultiMapIteratorArray }, { "StringMultiMapIterator.SetArray", sm_SetStringMultiMapIteratorArray }, + { "StringMultiMapIterator.Remove", sm_RemoveOnStringMultiMapIterator }, + {NULL, NULL}, };