diff --git a/OtherTypes.h b/OtherTypes.h index 8668836b..fec5c036 100644 --- a/OtherTypes.h +++ b/OtherTypes.h @@ -633,7 +633,6 @@ class CTimer : public CObject bOmitFromOutput = false; bOmitFromLog = false; bExecutingScript = false; - }; bool operator== (const CTimer & rhs) const; @@ -686,6 +685,8 @@ class CTimer : public CObject long nInvocationCount; // how many times procedure called long nMatched; // how many times the timer fired + unsigned long nCreateSequence; // for keeping timers in sequence of creation + // calculated field - when timer is next to go off (fire) CmcDateTime tFireTime; // when to fire it @@ -695,9 +696,16 @@ class CTimer : public CObject bool bSelected; // if true, selected for use in a plugin bool bExecutingScript; // if true, executing a script and cannot be deleted + static unsigned long GetNextTimerSequence () { return nNextCreateSequence++; } + + private: + static unsigned long nNextCreateSequence; + }; -typedef CTypedPtrMap CTimerMap; +typedef map CTimerMap; +typedef CTimerMap::iterator CTimerMapIterator; + // map for lookup name from pointer typedef map CTimerRevMap; diff --git a/dialogs/plugins/PluginWizard.cpp b/dialogs/plugins/PluginWizard.cpp index d7b4e60a..fd8edc02 100644 --- a/dialogs/plugins/PluginWizard.cpp +++ b/dialogs/plugins/PluginWizard.cpp @@ -586,13 +586,12 @@ BOOL CPluginWizardPage5::OnInitDialog() m_ctlList.InsertColumn(3, TranslateHeading ("Group"), LVCFMT_LEFT, 50); int iItem = 0; - POSITION pos; - CString strName; - for (pos = m_doc->m_TimerMap.GetStartPosition(); pos; ) + for (CTimerMapIterator timerIt = m_doc->m_TimerMap.begin (); + timerIt != m_doc->m_TimerMap.end (); + timerIt++) { - CTimer * t; - m_doc->m_TimerMap.GetNextAssoc (pos, strName, t); + CTimer * t = timerIt->second; if (t->bTemporary) continue; // ignore temporary ones @@ -643,14 +642,13 @@ void CPluginWizardPage5::OnSelectNone() void CPluginWizardPage5::OnOK() { - POSITION pos; - CString strName; // first deselect everything - for (pos = m_doc->m_TimerMap.GetStartPosition(); pos; ) + for (CTimerMapIterator timerIt = m_doc->m_TimerMap.begin (); + timerIt != m_doc->m_TimerMap.end (); + timerIt++) { - CTimer * t; - m_doc->m_TimerMap.GetNextAssoc (pos, strName, t); + CTimer * t = timerIt->second; t->bSelected = false; } @@ -659,15 +657,16 @@ void CPluginWizardPage5::OnOK() { CTimer * t = (CTimer *) m_ctlList.GetItemData (nItem); // find in Timer array (in case it doesn't exist any more) and select it - for (pos = m_doc->m_TimerMap.GetStartPosition(); pos; ) + for (CTimerMapIterator timerIt = m_doc->m_TimerMap.begin (); + timerIt != m_doc->m_TimerMap.end (); + timerIt++) { - CTimer * t2; - m_doc->m_TimerMap.GetNextAssoc (pos, strName, t2); - if (t == t2) - { - t->bSelected = true; - break; - } + CTimer * t2 = timerIt->second; + if (t == t2) + { + t->bSelected = true; + break; + } } } // end of each list item diff --git a/dialogs/world_prefs/TimerDlg.cpp b/dialogs/world_prefs/TimerDlg.cpp index 88b31b2e..7fc2e96a 100644 --- a/dialogs/world_prefs/TimerDlg.cpp +++ b/dialogs/world_prefs/TimerDlg.cpp @@ -142,13 +142,13 @@ void CTimerDlg::DoDataExchange(CDataExchange* pDX) } // end of doing a periodical timer - CString strTimerName; CTimer * timer_item; - POSITION pos; - for (pos = m_pTimerMap->GetStartPosition (); pos; ) + for (CTimerMapIterator timerIt = m_pTimerMap->begin (); + timerIt != m_pTimerMap->end (); + timerIt++) { - m_pTimerMap->GetNextAssoc (pos, strTimerName, timer_item); + timer_item = timerIt->second; // don't compare against itself diff --git a/dialogs/world_prefs/configuration.cpp b/dialogs/world_prefs/configuration.cpp index 0fcc887a..0d76c184 100644 --- a/dialogs/world_prefs/configuration.cpp +++ b/dialogs/world_prefs/configuration.cpp @@ -441,7 +441,6 @@ void CMUSHclientDoc:: LoadPrefsP15 (CPrefsP15 &page15) CTrigger * pTrigger; CAlias * pAlias; - CTimer * pTimer; CString strName; POSITION pos; LONGLONG iTimeTaken = 0; @@ -466,10 +465,11 @@ void CMUSHclientDoc:: LoadPrefsP15 (CPrefsP15 &page15) } // count number of timers fired - for (pos = m_TimerMap.GetStartPosition(); pos; ) + for (CTimerMapIterator timerIt = m_TimerMap.begin (); + timerIt != m_TimerMap.end (); + timerIt++) { - m_TimerMap.GetNextAssoc (pos, strName, pTimer); - nTotalTimers += pTimer->nMatched; + nTotalTimers += timerIt->second->nMatched; } page15.m_strBufferLines.Format ("%i / %ld", @@ -500,7 +500,7 @@ void CMUSHclientDoc:: LoadPrefsP15 (CPrefsP15 &page15) m_AliasMap.GetCount (), nTotalAliases); page15.m_strTimers.Format ("%i (%I64d fired)", - m_TimerMap.GetCount (), + m_TimerMap.size (), nTotalTimers); if (m_sockAddr.sin_addr.s_addr == INADDR_NONE) diff --git a/dialogs/world_prefs/prefspropertypages.cpp b/dialogs/world_prefs/prefspropertypages.cpp index a535c89a..014f7ab3 100644 --- a/dialogs/world_prefs/prefspropertypages.cpp +++ b/dialogs/world_prefs/prefspropertypages.cpp @@ -6203,10 +6203,10 @@ void CPrefsP16::DoDataExchange(CDataExchange* pDX) if (pDX->m_bSaveAndValidate) if (m_ctlUseDefaultTimers.GetCheck ()) if (m_ctlUseDefaultTimers.GetCheck () != m_bUseDefaultTimers) - if (m_doc->m_TimerMap.GetCount () > 0) + if (m_doc->m_TimerMap.size () > 0) if (::UMessageBox (TFormat ("By checking the option \"Override with default timers\" " " your existing %i timer%s will be PERMANENTLY discarded next time you open this world.\n\n" - "Are you SURE you want to do this?", PLURAL (m_doc->m_TimerMap.GetCount ())), + "Are you SURE you want to do this?", PLURAL (m_doc->m_TimerMap.size ())), MB_YESNO | MB_ICONQUESTION | MB_DEFBUTTON2 ) != IDYES) pDX->Fail (); diff --git a/doc_construct.cpp b/doc_construct.cpp index ef861ebb..4b1065ad 100644 --- a/doc_construct.cpp +++ b/doc_construct.cpp @@ -39,7 +39,6 @@ int i; m_VariableMap.InitHashTable (997); // allow for 1000 variables (at least) m_AliasMap.InitHashTable (293); // probably won't have many more than 300 aliases m_TriggerMap.InitHashTable (293); // probably won't have many more than 300 triggers - m_TimerMap.InitHashTable (293); // probably won't have many more than 300 timers SetDefaults (false); // set up numeric/boolean defaults SetAlphaDefaults (false); // set up alpha defaults @@ -561,8 +560,14 @@ int i; // delete timer map - DELETE_MAP (m_TimerMap, CTimer); - + for (CTimerMapIterator timerIt = m_TimerMap.begin (); + timerIt != m_TimerMap.end (); + timerIt++) + delete timerIt->second; + m_TimerMap.clear (); + m_TimerRevMap.clear (); + + // delete variables map DELETE_MAP (m_VariableMap, CVariable); diff --git a/evaluate.cpp b/evaluate.cpp index de7a13fa..79a6a085 100644 --- a/evaluate.cpp +++ b/evaluate.cpp @@ -613,7 +613,7 @@ if (strFileName.IsEmpty ()) replace = FALSE; } else - if (set_type == TIMER && !m_TimerMap.IsEmpty ()) + if (set_type == TIMER && !m_TimerMap.empty ()) { if (::TMessageBox ("Replace existing timers?\n" "If you reply \"No\", then timers from the file" @@ -661,7 +661,14 @@ CArchive * ar = NULL; case TIMER: if (replace) - DELETE_MAP (m_TimerMap, CTimer); + { + for (CTimerMapIterator timerIt = m_TimerMap.begin (); + timerIt != m_TimerMap.end (); + timerIt++) + delete timerIt->second; + m_TimerMap.clear (); + m_TimerRevMap.clear (); + } Load_World_XML (*ar, XML_TIMERS | XML_NO_PLUGINS | XML_IMPORT_MAIN_FILE_ONLY); break; diff --git a/plugins.cpp b/plugins.cpp index f159895f..4585718e 100644 --- a/plugins.cpp +++ b/plugins.cpp @@ -159,7 +159,6 @@ CPlugin::CPlugin (CMUSHclientDoc * pDoc) m_VariableMap.InitHashTable (293); // allow for 300 variables in this plugin m_AliasMap.InitHashTable (293); // ditto for aliases m_TriggerMap.InitHashTable (293); // ditto for triggers - m_TimerMap.InitHashTable (293); // ditto for timers m_tDateInstalled = CTime::GetCurrentTime(); // when plugin loaded m_dRequiredVersion = 0.0; @@ -186,7 +185,12 @@ CPlugin::~CPlugin () SaveState (); DELETE_MAP (m_TriggerMap, CTrigger); DELETE_MAP (m_AliasMap, CAlias); - DELETE_MAP (m_TimerMap, CTimer); + for (CTimerMapIterator timerIt = m_TimerMap.begin (); + timerIt != m_TimerMap.end (); + timerIt++) + delete timerIt->second; + m_TimerMap.clear (); + m_TimerRevMap.clear (); DELETE_MAP (m_VariableMap, CVariable); delete m_ScriptEngine; @@ -907,11 +911,11 @@ void CMUSHclientDoc::OnFilePluginwizard() a->bSelected = true; } // timers - for (pos = m_TimerMap.GetStartPosition(); pos; ) + for (CTimerMapIterator timerIt = m_TimerMap.begin (); + timerIt != m_TimerMap.end (); + timerIt++) { - CTimer * t; - m_TimerMap.GetNextAssoc (pos, strName, t); - t->bSelected = true; + timerIt->second->bSelected = true; } // variables for (pos = m_VariableMap.GetStartPosition(); pos; iCount++) @@ -1122,11 +1126,11 @@ void CMUSHclientDoc::OnFilePluginwizard() // ---------- timers ---------- iCount = 0; - for (pos = m_TimerMap.GetStartPosition(); pos; ) + for (CTimerMapIterator timerIt = m_TimerMap.begin (); + timerIt != m_TimerMap.end (); + timerIt++) { - CTimer * t; - m_TimerMap.GetNextAssoc (pos, strName, t); - if (t->bSelected) + if (timerIt->second->bSelected) iCount++; } @@ -1134,12 +1138,12 @@ void CMUSHclientDoc::OnFilePluginwizard() { ar.WriteString (NL "" NL NL); Save_Header_XML (ar, "timers", false); - for (pos = m_TimerMap.GetStartPosition(); pos; ) + for (CTimerMapIterator timerIt = m_TimerMap.begin (); + timerIt != m_TimerMap.end (); + timerIt++) { - CTimer * t; - m_TimerMap.GetNextAssoc (pos, strName, t); - if (t->bSelected) - Save_One_Timer_XML (ar, t); + if (timerIt->second->bSelected) + Save_One_Timer_XML (ar, timerIt->second); } Save_Footer_XML (ar, "timers"); } // end of having some @@ -1334,17 +1338,19 @@ void CMUSHclientDoc::OnFilePluginwizard() // ---------- timers ---------- iCount = 0; - for (pos = m_TimerMap.GetStartPosition(); pos; ) + for (CTimerMapIterator timerIt = m_TimerMap.begin (); + timerIt != m_TimerMap.end (); + ) { - CTimer * t; - m_TimerMap.GetNextAssoc (pos, strName, t); + CTimer * t = timerIt->second; if (t->bSelected) { + CTimerMapIterator it = timerIt++; // make copy before deleting iCount++; // delete its pointer delete t; // now delete its entry - m_TimerMap.RemoveKey (strName); + m_TimerMap.erase (it); } // end of selected Timer // show document modified diff --git a/scripting/methods/methods_info.cpp b/scripting/methods/methods_info.cpp index 58f46e65..f8dbf149 100644 --- a/scripting/methods/methods_info.cpp +++ b/scripting/methods/methods_info.cpp @@ -602,7 +602,7 @@ VARIANT CMUSHclientDoc::GetInfo(long InfoType) case 217: SetUpVariantLong (vaResult, (long) m_nBytesOut); break; case 218: SetUpVariantLong (vaResult, GetVariableMap ().GetCount ()); break; case 219: SetUpVariantLong (vaResult, GetTriggerMap ().GetCount ()); break; - case 220: SetUpVariantLong (vaResult, GetTimerMap ().GetCount ()); break; + case 220: SetUpVariantLong (vaResult, GetTimerMap ().size ()); break; case 221: SetUpVariantLong (vaResult, GetAliasMap ().GetCount ()); break; case 222: SetUpVariantLong (vaResult, m_QueuedCommandsList.GetCount ()); break; case 223: SetUpVariantLong (vaResult, m_strMapList.GetCount ()); break; diff --git a/scripting/methods/methods_plugins.cpp b/scripting/methods/methods_plugins.cpp index 967a2b74..01ce4c19 100644 --- a/scripting/methods/methods_plugins.cpp +++ b/scripting/methods/methods_plugins.cpp @@ -115,7 +115,7 @@ VARIANT CMUSHclientDoc::GetPluginInfo(LPCTSTR PluginID, short InfoType) case 8: SetUpVariantString (vaResult, pPlugin->m_strPurpose); break; case 9: SetUpVariantLong (vaResult, pPlugin->m_TriggerMap.GetCount ()); break; case 10: SetUpVariantLong (vaResult, pPlugin->m_AliasMap.GetCount ()); break; - case 11: SetUpVariantLong (vaResult, pPlugin->m_TimerMap.GetCount ()); break; + case 11: SetUpVariantLong (vaResult, pPlugin->m_TimerMap.size ()); break; case 12: SetUpVariantLong (vaResult, pPlugin->m_VariableMap.GetCount ()); break; case 13: if (pPlugin->m_tDateWritten.GetTime ()) // only if non-zero, otherwise return empty diff --git a/scripting/methods/methods_timers.cpp b/scripting/methods/methods_timers.cpp index 8aa3d17f..cef11711 100644 --- a/scripting/methods/methods_timers.cpp +++ b/scripting/methods/methods_timers.cpp @@ -89,8 +89,10 @@ CTimer * Timer_item; // trim spaces from name, make lower-case CheckObjectName (strTimerName, false); - if (!GetTimerMap ().Lookup (strTimerName, Timer_item)) + CTimerMapIterator timerIt = GetTimerMap ().find ((LPCTSTR) strTimerName); + if (timerIt == GetTimerMap ().end ()) return eTimerNotFound; + Timer_item = timerIt->second; if (Timer_item->bEnabled && Enabled) return eOK; // already enabled, document hasn't changed @@ -122,8 +124,10 @@ CTimer * Timer_item; // trim spaces from name, make lower-case CheckObjectName (strTimerName, false); - if (!GetTimerMap ().Lookup (strTimerName, Timer_item)) + CTimerMapIterator timerIt = GetTimerMap ().find ((LPCTSTR) strTimerName); + if (timerIt == GetTimerMap ().end ()) return eTimerNotFound; + Timer_item = timerIt->second; ResetOneTimer (Timer_item); @@ -147,18 +151,25 @@ long nStatus; bool bReplace = false; if (strTimerName.IsEmpty ()) - strTimerName.Format ("*timer%s", (LPCTSTR) App.GetUniqueString ()); + { + strTimerName.Format ("*timer%010ld", CTimer::GetNextTimerSequence ()); + } else // return if bad name if (nStatus = CheckObjectName (strTimerName)) return nStatus; - // if it already exists, error - if (GetTimerMap ().Lookup (strTimerName, timer_item)) + CTimerMapIterator timerIt = GetTimerMap ().find ((LPCTSTR) strTimerName); + if (timerIt != GetTimerMap ().end ()) + { + timer_item = timerIt->second; + + // if it already exists, error if (Flags & eReplace) bReplace = true; else return eTimerAlreadyExists; + } if (Hour < 0 || Hour > 23) @@ -192,11 +203,12 @@ bool bReplace = false; delete timer_item; // now delete its entry - GetTimerMap ().RemoveKey (strTimerName); + GetTimerMap ().erase (timerIt); } // create new timer item and insert in timer map - GetTimerMap ().SetAt (strTimerName, timer_item = new CTimer); + timer_item = new CTimer; + GetTimerMap () [(LPCTSTR) strTimerName] = timer_item; if ((Flags & eTemporary) == 0) if (!m_CurrentPlugin) // plugin mods don't really count @@ -248,9 +260,12 @@ CTimer * timer_item; // trim spaces from name, make lower-case CheckObjectName (strTimerName, false); - if (!GetTimerMap ().Lookup (strTimerName, timer_item)) + CTimerMapIterator timerIt = GetTimerMap ().find ((LPCTSTR) strTimerName); + if (timerIt == GetTimerMap ().end ()) return eTimerNotFound; + timer_item = timerIt->second; + // can't if executing a script if (timer_item->bExecutingScript) return eItemInUse; @@ -259,8 +274,7 @@ CTimer * timer_item; delete timer_item; // now delete its entry - if (!GetTimerMap ().RemoveKey (strTimerName)) - return eTimerNotFound; + GetTimerMap ().erase (timerIt); SortTimers (); @@ -272,12 +286,12 @@ CTimer * timer_item; long CMUSHclientDoc::IsTimer(LPCTSTR TimerName) { CString strTimerName = TimerName; -CTimer * timer_item; // trim spaces from name, make lower-case CheckObjectName (strTimerName, false); - if (!GetTimerMap ().Lookup (strTimerName, timer_item)) + CTimerMapIterator timerIt = GetTimerMap ().find ((LPCTSTR) strTimerName); + if (timerIt == GetTimerMap ().end ()) return eTimerNotFound; return eOK; @@ -286,24 +300,21 @@ CTimer * timer_item; VARIANT CMUSHclientDoc::GetTimerList() { COleSafeArray sa; // for wildcard list - - CString strTimerName; - CTimer * timer_item; long iCount = 0; - POSITION pos; - iCount = GetTimerMap ().GetCount (); + iCount = GetTimerMap ().size (); if (iCount) // cannot create empty array dimension { sa.CreateOneDim (VT_VARIANT, iCount); - for (iCount = 0, pos = GetTimerMap ().GetStartPosition(); pos; iCount++) + iCount = 0; + for (CTimerMapIterator timerIt = GetTimerMap ().begin (); + timerIt != GetTimerMap ().end (); + timerIt++, iCount++) { - GetTimerMap ().GetNextAssoc (pos, strTimerName, timer_item); - // the array must be a bloody array of variants, or VBscript kicks up - COleVariant v (strTimerName); + COleVariant v (timerIt->first.c_str ()); sa.PutElement (&iCount, &v); } // end of looping through each timer } // end of having at least one @@ -325,8 +336,10 @@ CTimer * timer_item; // trim spaces from name, make lower-case CheckObjectName (strTimerName, false); - if (!GetTimerMap ().Lookup (strTimerName, timer_item)) + CTimerMapIterator timerIt = GetTimerMap ().find ((LPCTSTR) strTimerName); + if (timerIt == GetTimerMap ().end ()) return eTimerNotFound; + timer_item = timerIt->second; if (timer_item->iType == CTimer::eAtTime) { @@ -377,9 +390,10 @@ CTimer * timer_item; vaResult.vt = VT_EMPTY; - // see if timer exists, if not return EMPTY - if (!GetTimerMap ().Lookup (strTimerName, timer_item)) - return vaResult; + CTimerMapIterator timerIt = GetTimerMap ().find ((LPCTSTR) strTimerName); + if (timerIt == GetTimerMap ().end ()) + return vaResult; + timer_item = timerIt->second; switch (InfoType) { @@ -481,7 +495,7 @@ CString strTimerName; CTimer * timer_item; // this is a temporary unlabelled timer, make up a name - strTimerName.Format ("*timer%s", (LPCTSTR) App.GetUniqueString ()); + strTimerName.Format ("*timer%010ld", CTimer::GetNextTimerSequence ()); if (iHours < 0 || iHours > 23) return eTimeInvalid; @@ -493,7 +507,8 @@ CTimer * timer_item; return eTimeInvalid; // create new timer item and insert in timer map - GetTimerMap ().SetAt (strTimerName, timer_item = new CTimer); + timer_item = new CTimer; + GetTimerMap () [(LPCTSTR) strTimerName] = timer_item;; timer_item->nUpdateNumber = App.GetUniqueNumber (); // for concurrency checks @@ -548,17 +563,18 @@ long CMUSHclientDoc::DoAfterNote(double Seconds, LPCTSTR NoteText) long CMUSHclientDoc::DeleteTemporaryTimers() { long iCount = 0; -POSITION pos; -CString strTimerName; CTimer * timer_item; - for (pos = GetTimerMap ().GetStartPosition(); pos; ) + for (CTimerMapIterator timerIt = GetTimerMap ().begin (); + timerIt != GetTimerMap ().end (); + ) { - GetTimerMap ().GetNextAssoc (pos, strTimerName, timer_item); + timer_item = timerIt->second; + CTimerMapIterator it = timerIt++; // make copy before deleting if (timer_item->bTemporary && !timer_item->bExecutingScript) { delete timer_item; - GetTimerMap ().RemoveKey (strTimerName); + GetTimerMap ().erase (it); iCount++; } } // end of deleting timers @@ -572,19 +588,19 @@ CTimer * timer_item; long CMUSHclientDoc::EnableTimerGroup(LPCTSTR GroupName, BOOL Enabled) { - CString strTimerName; CTimer * timer_item; long iCount = 0; - POSITION pos; // no group name, affect nothing if (strlen (GroupName) == 0) return 0; // count timers - for (pos = GetTimerMap ().GetStartPosition(); pos; ) + for (CTimerMapIterator timerIt = GetTimerMap ().begin (); + timerIt != GetTimerMap ().end (); + timerIt++) { - GetTimerMap ().GetNextAssoc (pos, strTimerName, timer_item); + timer_item = timerIt->second; if (timer_item->strGroup == GroupName) { timer_item->bEnabled = Enabled != 0; @@ -603,48 +619,40 @@ long CMUSHclientDoc::EnableTimerGroup(LPCTSTR GroupName, BOOL Enabled) long CMUSHclientDoc::DeleteTimerGroup(LPCTSTR GroupName) { - CString strTimerName; CTimer * timer_item; - POSITION pos; + int count = 0; // no group name, affect nothing if (strlen (GroupName) == 0) return 0; - vector vToDelete; - - // count timers - for (pos = GetTimerMap ().GetStartPosition(); pos; ) + for (CTimerMapIterator timerIt = GetTimerMap ().begin (); + timerIt != GetTimerMap ().end (); + ) { - GetTimerMap ().GetNextAssoc (pos, strTimerName, timer_item); + CTimerMapIterator it = timerIt++; // make copy before deleting + timer_item = timerIt->second; if (timer_item->strGroup == GroupName) { - // can't if executing a script if (timer_item->bExecutingScript) continue; delete timer_item; - - // remember to delete from timer map - vToDelete.push_back ((LPCTSTR) strTimerName); + GetTimerMap ().erase (it); + count++; } } // end of timers - // now delete from map, do it this way in case deleting whilst looping throws things out - for (vector::const_iterator it = vToDelete.begin (); - it != vToDelete.end (); - it++) - GetTimerMap ().RemoveKey (it->c_str ()); - if (!vToDelete.empty ()) + if (count) { SortTimers (); if (!m_CurrentPlugin) // plugin mods don't really count SetModifiedFlag (TRUE); // document has changed } - return vToDelete.size (); + return count; } // end of DeleteTimerGroup @@ -664,10 +672,10 @@ CTimer * Timer_item; vaResult.vt = VT_EMPTY; - // see if Timer exists, if not return EMPTY - if (!GetTimerMap ().Lookup (strTimerName, Timer_item)) - return vaResult; - + CTimerMapIterator timerIt = GetTimerMap ().find ((LPCTSTR) strTimerName); + if (timerIt == GetTimerMap ().end ()) + return vaResult; + Timer_item = timerIt->second; CString strOptionName = OptionName; @@ -755,8 +763,10 @@ CTimer * Timer_item; // trim spaces from name, make lower-case CheckObjectName (strTimerName, false); - if (!GetTimerMap ().Lookup (strTimerName, Timer_item)) + CTimerMapIterator timerIt = GetTimerMap ().find ((LPCTSTR) strTimerName); + if (timerIt == GetTimerMap ().end ()) return eTimerNotFound; + Timer_item = timerIt->second; CString strOptionName = OptionName; diff --git a/scripting/methods/methods_xml.cpp b/scripting/methods/methods_xml.cpp index ce97ff1a..4ac82309 100644 --- a/scripting/methods/methods_xml.cpp +++ b/scripting/methods/methods_xml.cpp @@ -149,8 +149,10 @@ BSTR CMUSHclientDoc::ExportXML(short Type, LPCTSTR Name) case 2: // timer { CTimer * t; - if (GetTimerMap ().Lookup (strName, t)) + CTimerMapIterator timerIt = GetTimerMap ().find ((LPCTSTR) strName); + if (timerIt != GetTimerMap ().end ()) { + t = timerIt->second; Save_Header_XML (ar, "timers", false); Save_One_Timer_XML (ar, t); Save_Footer_XML (ar, "timers"); diff --git a/scripting/scripting.cpp b/scripting/scripting.cpp index fdbc0752..ac011962 100644 --- a/scripting/scripting.cpp +++ b/scripting/scripting.cpp @@ -204,9 +204,11 @@ bool CMUSHclientDoc::FindAllEntryPoints (void) CTimer * pTimer; CString strTimerName; - for (pos = GetTimerMap ().GetStartPosition (); pos; ) + for (CTimerMapIterator timerIt = GetTimerMap ().begin (); + timerIt != GetTimerMap ().end (); + timerIt++) { - GetTimerMap ().GetNextAssoc (pos, strTimerName, pTimer); + pTimer = timerIt->second; pTimer->dispid = GetProcedureDispid (pTimer->strProcedure, "timer", pTimer->strLabel, diff --git a/timers.cpp b/timers.cpp index 7d73a14e..a54407f1 100644 --- a/timers.cpp +++ b/timers.cpp @@ -9,6 +9,8 @@ #include "mainfrm.h" #include "sendvw.h" +unsigned long CTimer::nNextCreateSequence = 0; + void CMUSHclientDoc::ResetOneTimer (CTimer * timer_item) { CmcDateTime tNow = CmcDateTime::GetTimeNow(); @@ -49,21 +51,17 @@ CmcDateTimeSpan tsOneDay (1, 0, 0, 0); void CMUSHclientDoc::ResetAllTimers (CTimerMap & TimerMap) { - CTimer * pTimer; - CString strTimerName; - - for (POSITION pos = TimerMap.GetStartPosition(); pos; ) + for (CTimerMapIterator timerIt = TimerMap.begin (); + timerIt != TimerMap.end (); + timerIt++) { - TimerMap.GetNextAssoc (pos, strTimerName, pTimer); - ResetOneTimer (pTimer); + ResetOneTimer (timerIt->second); } - } // end of CMUSHclientDoc::ResetAllTimers void CMUSHclientDoc::CheckTimerList (CTimerMap & TimerMap) { CTimer * timer_item; -CString strTimerName; CmcDateTime tNow = CmcDateTime::GetTimeNow(); CmcDateTimeSpan tsOneDay (1, 0, 0, 0); @@ -81,16 +79,15 @@ CmcDateTimeSpan tsOneDay (1, 0, 0, 0); } } - CStringList firedTimersList; - POSITION pos; + list firedTimersList; // iterate through all timers for this document - first build list of them - for (pos = TimerMap.GetStartPosition(); pos; ) + for (CTimerMapIterator timerIt = TimerMap.begin (); + timerIt != TimerMap.end (); + timerIt++) { - - - TimerMap.GetNextAssoc (pos, strTimerName, timer_item); + timer_item = timerIt->second; if (!timer_item->bEnabled) // ignore un-enabled timers continue; @@ -106,7 +103,7 @@ CmcDateTimeSpan tsOneDay (1, 0, 0, 0); if (timer_item->tFireTime > tNow) continue; - firedTimersList.AddTail (strTimerName); // add to list of fired timers + firedTimersList.push_back (timerIt->first); // add to list of fired timers } @@ -114,14 +111,18 @@ CmcDateTimeSpan tsOneDay (1, 0, 0, 0); // now process list, checking timer still exists in case a script deleted one // see: http://www.gammon.com.au/forum/?id=10358 - for (pos = firedTimersList.GetHeadPosition (); pos; ) + for (list::iterator firedIt = firedTimersList.begin (); + firedIt != firedTimersList.end (); + firedIt++) { // get next fired timer from list - strTimerName = firedTimersList.GetNext (pos); + string strTimerName = *firedIt; // check still exists, get pointer if so - if (!TimerMap.Lookup (strTimerName, timer_item)) + CTimerMapIterator timerIt = TimerMap.find (strTimerName); + if (timerIt == TimerMap.end ()) continue; + timer_item = timerIt->second; timer_item->nMatched++; // count timer matches timer_item->tWhenFired = tNow; // when it fired @@ -254,14 +255,16 @@ CmcDateTimeSpan tsOneDay (1, 0, 0, 0); // do this code, in which case the timer has gone. // Just get it again to be sure ... [#430] - if (!TimerMap.Lookup (strTimerName, timer_item)) + timerIt = TimerMap.find (strTimerName); + if (timerIt == TimerMap.end ()) return; + timer_item = timerIt->second; // if one-shot timer, delete from list if (timer_item->bOneShot) { - TimerMap.RemoveKey (strTimerName); + TimerMap.erase (timerIt); delete timer_item; SortTimers (); } @@ -379,25 +382,21 @@ void CMUSHclientDoc::OnGameResetalltimers() void CMUSHclientDoc::OnUpdateGameResetalltimers(CCmdUI* pCmdUI) { DoFixMenus (pCmdUI); // remove accelerators from menus - pCmdUI->Enable (!m_TimerMap.IsEmpty ()); + pCmdUI->Enable (!m_TimerMap.empty ()); } void CMUSHclientDoc::SortTimers (void) { -int i; -CString strTimerName; -CTimer * pTimer; -POSITION pos; - GetTimerRevMap ().clear (); // extract pointers into a simple array - for (i = 0, pos = GetTimerMap ().GetStartPosition(); pos; i++) + for (CTimerMapIterator timerIt = GetTimerMap ().begin (); + timerIt != GetTimerMap ().end (); + timerIt++) { - GetTimerMap ().GetNextAssoc (pos, strTimerName, pTimer); - GetTimerRevMap () [pTimer] = strTimerName; + GetTimerRevMap () [timerIt->second] = timerIt->first; } } // end of CMUSHclientDoc::SortTimers diff --git a/world_debug.cpp b/world_debug.cpp index e77cab1c..5612eed5 100644 --- a/world_debug.cpp +++ b/world_debug.cpp @@ -866,9 +866,13 @@ VARIANT CMUSHclientDoc::Debug(LPCTSTR Command) Note (CFormat ("--> Script sub %s NOT active <--", (LPCTSTR) pAlias->strProcedure)); } // end of aliases - for (pos = p->m_TimerMap.GetStartPosition(), iCount2 = 1; pos; iCount2++) + iCount2 = 1; + for (CTimerMapIterator timerIt = p->m_TimerMap.begin (); + timerIt != p->m_TimerMap.end (); + timerIt++, iCount2++) { - p->m_TimerMap.GetNextAssoc (pos, strName, pTimer); + strName = timerIt->first.c_str (); + pTimer = timerIt->second; Note (TFormat ("Timer %i: %02i:%02i:%04.2f=%s", iCount2, pTimer->iAtHour + pTimer->iEveryHour, @@ -1191,9 +1195,11 @@ VARIANT CMUSHclientDoc::Debug(LPCTSTR Command) nEnabled = 0; // count number of timers fired - for (pos = m_TimerMap.GetStartPosition(); pos; ) + for (CTimerMapIterator timerIt = m_TimerMap.begin (); + timerIt != m_TimerMap.end (); + timerIt++) { - m_TimerMap.GetNextAssoc (pos, strName, pTimer); + pTimer = timerIt->second; nTotal++; nTotalMatches += pTimer->nMatched; @@ -1338,11 +1344,11 @@ VARIANT CMUSHclientDoc::Debug(LPCTSTR Command) // no quick way of finding timers count int nTotalTimers = 0; - for (POSITION timerpos = pPlugin->m_TimerMap.GetStartPosition(); timerpos; nTotalTimers++) + for (CTimerMapIterator timerIt = pPlugin->m_TimerMap.begin (); + timerIt != pPlugin->m_TimerMap.end (); + timerIt++) { - CTimer * pTimer; - CString strName; - pPlugin->m_TimerMap.GetNextAssoc (timerpos, strName, pTimer); + nTotalTimers++; } // end of for loop @@ -1821,8 +1827,6 @@ void CMUSHclientDoc::DebugHelper (const CString strAction, CString strArgument) if (m_iNoteTextColour >= 0 && m_iNoteTextColour < MAX_CUSTOM) enabledFore = ColourToName (m_customtext [m_iNoteTextColour]); - POSITION pos; - //----------------------------------------------------------------------- // triggerlist //----------------------------------------------------------------------- @@ -1969,14 +1973,11 @@ void CMUSHclientDoc::DebugHelper (const CString strAction, CString strArgument) map sortedTimers; // put into map for sorting into firing order - for (pos = GetTimerMap ().GetStartPosition(); pos; ) + for (CTimerMapIterator timerIt = GetTimerMap ().begin (); + timerIt != GetTimerMap ().end (); + timerIt++) { - CTimer * pTimer; - CString strName; - - GetTimerMap ().GetNextAssoc (pos, strName, pTimer); - - sortedTimers [pTimer->tFireTime.GetTime ()] = pTimer; + sortedTimers [timerIt->second->tFireTime.GetTime ()] = timerIt->second; } // end of for loop @@ -2396,14 +2397,15 @@ void CMUSHclientDoc::DebugHelper (const CString strAction, CString strArgument) else if (strAction == "showtimer") { - CTimer * pTimer; - if (!GetTimerMap ().Lookup (strArgument, pTimer)) + CTimerMapIterator timerIt = GetTimerMap ().find ((LPCTSTR) strArgument); + if (timerIt == GetTimerMap ().end ()) { Note (TFormat ("Timer %s does not exist.", (LPCTSTR) strArgument)); return; } // if not found + CTimer * pTimer = timerIt->second; char * p = NULL; try diff --git a/xml/xml_load_world.cpp b/xml/xml_load_world.cpp index 3d0a888d..7a24fada 100644 --- a/xml/xml_load_world.cpp +++ b/xml/xml_load_world.cpp @@ -1783,21 +1783,22 @@ CString strTimerName, // generate a name if it doesn't have one if (strTimerName.IsEmpty () || strTimerName.Left (1) == "*") - strTimerName.Format ("*timer%s", (LPCTSTR) App.GetUniqueString ()); + strTimerName.Format ("*timer%010ld", CTimer::GetNextTimerSequence ()); else strTimerName.MakeLower (); // check for duplicate labels - CTimer * timer_check; - if (GetTimerMap ().Lookup (strTimerName, timer_check)) + CTimerMapIterator timerIt = GetTimerMap ().find ((LPCTSTR) strTimerName); + if (timerIt != GetTimerMap ().end ()) { + CTimer * timer_check = timerIt->second; if (iMask & XML_OVERWRITE) delete timer_check; else ThrowErrorException ("Duplicate timer label \"%s\" ", strTimerName); - } // end of duplciate + } // remember if this was from an include t->bIncluded = (iFlags & LOAD_INCLUDE) != 0; @@ -1814,13 +1815,11 @@ CString strTimerName, // version: 4.81 if (t->strLabel.IsEmpty () && !(iMask & XML_PASTE_DUPLICATE)) { - CTimer * pExistingTimer; - CString strExistingTimerName; - - for (POSITION pos = GetTimerMap ().GetStartPosition(); pos; ) + for (CTimerMapIterator timerIt = GetTimerMap ().begin (); + timerIt != GetTimerMap ().end (); + timerIt++) { - GetTimerMap ().GetNextAssoc (pos, strExistingTimerName, pExistingTimer); - if (*pExistingTimer == *t) + if (*timerIt->second == *t) { delete t; // get rid of duplicate timer return false; // and don't add it @@ -1832,7 +1831,7 @@ CString strTimerName, // now add to our internal timer map t->nUpdateNumber = App.GetUniqueNumber (); // for concurrency checks - GetTimerMap ().SetAt (strTimerName, t); + GetTimerMap () [(LPCTSTR) strTimerName] = t; ResetOneTimer (t); // make sure it is reset diff --git a/xml/xml_save_world.cpp b/xml/xml_save_world.cpp index e9ddf662..5fa68ab7 100644 --- a/xml/xml_save_world.cpp +++ b/xml/xml_save_world.cpp @@ -530,12 +530,11 @@ void CMUSHclientDoc::Save_Timers_XML (CArchive& ar) { Save_Header_XML (ar, "timers"); - for (POSITION pos = m_TimerMap.GetStartPosition(); pos; ) + for (CTimerMapIterator timerIt = m_TimerMap.begin (); + timerIt != m_TimerMap.end (); + timerIt++) { - CString strTimerName; - CTimer * t; - - m_TimerMap.GetNextAssoc (pos, strTimerName, t); + CTimer * t = timerIt->second; if (t->bTemporary || t->bIncluded) // don't save temporary timers continue;