Simplify wxIdRange code by using new XRCID_Assign() function.

Only traverse the id hash once in XRCID_Assign() instead of using
RemoveXRCIDEntry() to delete the old entry (which usually doesn't exist so
this traversed the entire hash) followed by XRCID_Lookup() to create a new one.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@69663 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin 2011-11-04 17:40:49 +00:00
parent 6bb81c8e1a
commit d265ec6b0e

View File

@ -73,6 +73,10 @@ wxDateTime GetXRCFileModTime(const wxString& filename)
} // anonymous namespace
// Assign the given value to the specified entry or add a new value with this
// name.
static void XRCID_Assign(const char *str_id, int value);
class wxXmlResourceDataRecord
{
public:
@ -152,9 +156,6 @@ public:
void FinaliseRanges(const wxXmlNode* node) const;
// Searches for a known IdRange matching 'name', returning its index or -1
int Find(const wxString& rangename) const;
// Removes, if it exists, an entry from the XRCID table. Used in id-ranges
// to replace defunct or statically-initialised entries with current values
static void RemoveXRCIDEntry(const wxString& idstr);
protected:
wxIdRange* FindRangeForItem(const wxXmlNode* node,
@ -1216,13 +1217,10 @@ void wxIdRange::Finalise(const wxXmlNode* node)
// Create the XRCIDs
for (int i=m_start; i <= m_end; ++i)
{
// First clear any pre-existing XRCID
// Necessary for wxXmlResource::Unload() followed by Load()
wxIdRangeManager::RemoveXRCIDEntry(
m_name + wxString::Format("[%i]", i-m_start));
// Ensure that we overwrite any existing value as otherwise
// wxXmlResource::Unload() followed by Load() wouldn't work correctly.
XRCID_Assign(m_name + wxString::Format("[%i]", i-m_start), i);
// Use the second parameter of GetXRCID to force it to take the value i
wxXmlResource::GetXRCID(m_name + wxString::Format("[%i]", i-m_start), i);
wxLogTrace("xrcrange",
"integer = %i %s now returns %i",
i,
@ -1230,10 +1228,8 @@ void wxIdRange::Finalise(const wxXmlNode* node)
XRCID((m_name + wxString::Format("[%i]", i-m_start)).mb_str()));
}
// and these special ones
wxIdRangeManager::RemoveXRCIDEntry(m_name + "[start]");
wxXmlResource::GetXRCID(m_name + "[start]", m_start);
wxIdRangeManager::RemoveXRCIDEntry(m_name + "[end]");
wxXmlResource::GetXRCID(m_name + "[end]", m_end);
XRCID_Assign(m_name + "[start]", m_start);
XRCID_Assign(m_name + "[end]", m_end);
wxLogTrace("xrcrange","%s[start] = %i %s[end] = %i",
m_name.mb_str(),XRCID(wxString(m_name+"[start]").mb_str()),
m_name.mb_str(),XRCID(wxString(m_name+"[end]").mb_str()));
@ -2495,6 +2491,30 @@ static inline unsigned XRCIdHash(const char *str_id)
return index;
}
static void XRCID_Assign(const char *str_id, int value)
{
const unsigned index = XRCIdHash(str_id);
XRCID_record *oldrec = NULL;
for (XRCID_record *rec = XRCID_Records[index]; rec; rec = rec->next)
{
if (wxStrcmp(rec->key, str_id) == 0)
{
rec->id = value;
return;
}
oldrec = rec;
}
XRCID_record **rec_var = (oldrec == NULL) ?
&XRCID_Records[index] : &oldrec->next;
*rec_var = new XRCID_record;
(*rec_var)->key = wxStrdup(str_id);
(*rec_var)->id = value;
(*rec_var)->next = NULL;
}
static int XRCID_Lookup(const char *str_id, int value_if_not_found = wxID_NONE)
{
const unsigned index = XRCIdHash(str_id);
@ -2700,30 +2720,6 @@ wxString wxXmlResource::FindXRCIDById(int numId)
return wxString();
}
/* static */
void wxIdRangeManager::RemoveXRCIDEntry(const wxString& idstr)
{
const char *str_id = idstr.mb_str();
const unsigned index = XRCIdHash(str_id);
XRCID_record **p_previousrec = &XRCID_Records[index];
for (XRCID_record *rec = XRCID_Records[index]; rec; rec = rec->next)
{
if (wxStrcmp(rec->key, str_id) == 0)
{
// Found the item to be removed so delete its record; but first
// remove it from the linked list.
*p_previousrec = rec->next;
free(rec->key);
delete rec;
return;
}
p_previousrec = &rec->next;
}
}
static void CleanXRCID_Record(XRCID_record *rec)
{
if (rec)