From 3b252c696dadd24d7b92f299ee2a11a202f4fb4e Mon Sep 17 00:00:00 2001 From: Jon Evans Date: Fri, 2 Oct 2020 16:09:15 -0400 Subject: [PATCH] Make tool init order dependent on registration order Previously, init order depended on the ordering of the tool state map, which is opaque from the point of view of registration. This makes it challenging to make one tool's init depend on another. With this change, registration order defines init order. --- common/tool/tool_manager.cpp | 9 ++++++--- include/tool/tool_manager.h | 4 ++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/common/tool/tool_manager.cpp b/common/tool/tool_manager.cpp index 641da18ebb..ad1a092ee8 100644 --- a/common/tool/tool_manager.cpp +++ b/common/tool/tool_manager.cpp @@ -236,6 +236,8 @@ void TOOL_MANAGER::RegisterTool( TOOL_BASE* aTool ) wxASSERT_MSG( m_toolTypes.find( typeid( *aTool ).name() ) == m_toolTypes.end(), wxT( "Adding two tools of the same type may result in unexpected behaviour.") ); + m_toolOrder.push_back( aTool ); + TOOL_STATE* st = new TOOL_STATE( aTool ); m_toolState[aTool] = st; @@ -550,10 +552,11 @@ void TOOL_MANAGER::ResetTools( TOOL_BASE::RESET_REASON aReason ) void TOOL_MANAGER::InitTools() { - for( auto it = m_toolState.begin(); it != m_toolState.end(); /* iteration in the loop */ ) + for( TOOL_VEC::iterator it = m_toolOrder.begin(); it != m_toolOrder.end(); /* iter inside */ ) { - TOOL_BASE* tool = it->first; - TOOL_STATE* state = it->second; + TOOL_BASE* tool = *it; + wxASSERT( m_toolState.count( tool ) ); + TOOL_STATE* state = m_toolState[tool]; setActiveState( state ); ++it; // keep the iterator valid if the element is going to be erased diff --git a/include/tool/tool_manager.h b/include/tool/tool_manager.h index 34edc0d8e9..a6d552980e 100644 --- a/include/tool/tool_manager.h +++ b/include/tool/tool_manager.h @@ -63,6 +63,7 @@ public: typedef std::map NAME_STATE_MAP; typedef std::map ID_STATE_MAP; typedef std::list ID_LIST; + typedef std::vector TOOL_VEC; /** * Generates a unique ID from for a tool with given name. @@ -525,6 +526,9 @@ private: */ void setActiveState( TOOL_STATE* aState ); + /// List of tools in the order they were registered + TOOL_VEC m_toolOrder; + /// Index of registered tools current states, associated by tools' objects. TOOL_STATE_MAP m_toolState;