Use standard containers in wxHtmlParser

Replace wx synonyms for std::unordered_map<> and unordered_set<> defined
using macros with the standard classes themselves and also replace a
wxVector<> used for a stack with std::stack<>.

Also store std::unique_ptr<> rather than raw pointers in the various
containers that own the pointers to make the code more clear and robust.
This commit is contained in:
Vadim Zeitlin 2023-04-17 20:57:33 +01:00
parent 1ac57fbcc4
commit 93ae99cba5
2 changed files with 15 additions and 21 deletions

View file

@ -14,11 +14,14 @@
#include "wx/html/htmltag.h"
#include "wx/filesys.h"
#include "wx/hashmap.h"
#include "wx/hashset.h"
#include "wx/vector.h"
#include "wx/fontenc.h"
#include <memory>
#include <stack>
#include <unordered_map>
#include <unordered_set>
class WXDLLIMPEXP_FWD_BASE wxMBConv;
class WXDLLIMPEXP_FWD_HTML wxHtmlParser;
class WXDLLIMPEXP_FWD_HTML wxHtmlTagHandler;
@ -27,13 +30,7 @@ class WXDLLIMPEXP_FWD_HTML wxHtmlEntitiesParser;
class wxHtmlTextPieces;
class wxHtmlParserState;
WX_DECLARE_HASH_SET_WITH_DECL_PTR(wxHtmlTagHandler*,
wxPointerHash, wxPointerEqual,
wxHtmlTagHandlersSet,
class WXDLLIMPEXP_HTML);
WX_DECLARE_STRING_HASH_MAP_WITH_DECL(wxHtmlTagHandler*,
wxHtmlTagHandlersHash,
class WXDLLIMPEXP_HTML);
using wxHtmlTagHandlersHash = std::unordered_map<wxString, wxHtmlTagHandler*>;
enum wxHtmlURLType
@ -185,9 +182,8 @@ protected:
// (see wxHtmlWinParser for details about filling it)
// m_HandlersHash is for random access based on knowledge of tag name (BR, P, etc.)
// it may (and often does) contain more references to one object
// m_HandlersList is list of all handlers and it is guaranteed to contain
// only one reference to each handler instance.
wxHtmlTagHandlersSet m_HandlersSet;
// m_HandlersSet is set of all handlers and owns its elements.
std::unordered_set<std::unique_ptr<wxHtmlTagHandler>> m_HandlersSet;
wxHtmlTagHandlersHash m_HandlersHash;
wxDECLARE_NO_COPY_CLASS(wxHtmlParser);
@ -195,7 +191,7 @@ protected:
// class for opening files (file system)
wxFileSystem *m_FS;
// handlers stack used by PushTagHandler and PopTagHandler
wxVector<wxHtmlTagHandlersHash*> m_HandlersStack;
std::stack<std::unique_ptr<wxHtmlTagHandlersHash>> m_HandlersStack;
// entity parse
wxHtmlEntitiesParser *m_entitiesParser;

View file

@ -86,8 +86,6 @@ wxHtmlParser::~wxHtmlParser()
while (RestoreState()) {}
DestroyDOMTree();
WX_CLEAR_ARRAY(m_HandlersStack);
WX_CLEAR_HASH_SET(wxHtmlTagHandlersSet, m_HandlersSet);
delete m_entitiesParser;
delete m_Source;
}
@ -331,7 +329,7 @@ void wxHtmlParser::AddTagHandler(wxHtmlTagHandler *handler)
while (tokenizer.HasMoreTokens())
m_HandlersHash[tokenizer.GetNextToken()] = handler;
m_HandlersSet.insert(handler);
m_HandlersSet.insert(std::unique_ptr<wxHtmlTagHandler>(handler));
handler->SetParser(this);
}
@ -341,7 +339,9 @@ void wxHtmlParser::PushTagHandler(wxHtmlTagHandler *handler, const wxString& tag
wxStringTokenizer tokenizer(tags, wxT(", "));
wxString key;
m_HandlersStack.push_back(new wxHtmlTagHandlersHash(m_HandlersHash));
m_HandlersStack.push(std::unique_ptr<wxHtmlTagHandlersHash>(
new wxHtmlTagHandlersHash(m_HandlersHash))
);
while (tokenizer.HasMoreTokens())
{
@ -355,10 +355,8 @@ void wxHtmlParser::PopTagHandler()
wxCHECK_RET( !m_HandlersStack.empty(),
"attempt to remove HTML tag handler from empty stack" );
wxHtmlTagHandlersHash *prev = m_HandlersStack.back();
m_HandlersStack.pop_back();
m_HandlersHash = *prev;
delete prev;
m_HandlersHash = *m_HandlersStack.top();
m_HandlersStack.pop();
}
void wxHtmlParser::SetSourceAndSaveState(const wxString& src)