From e4ec886a6cbe45f5d1bc8ebd3c530efddc9c9ab7 Mon Sep 17 00:00:00 2001 From: Maciej Suminski Date: Wed, 9 Jul 2014 11:59:24 +0200 Subject: [PATCH] Added an alternative way to reach tools in the Tool Framework. --- common/tool/tool_manager.cpp | 10 +++++++--- include/tool/tool_manager.h | 27 +++++++++++++++++++++++---- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/common/tool/tool_manager.cpp b/common/tool/tool_manager.cpp index abfd66b566..8f052d57fa 100644 --- a/common/tool/tool_manager.cpp +++ b/common/tool/tool_manager.cpp @@ -109,7 +109,7 @@ TOOL_MANAGER::TOOL_MANAGER() : TOOL_MANAGER::~TOOL_MANAGER() { - std::map::iterator it, it_end; + boost::unordered_map::iterator it, it_end; for( it = m_toolState.begin(), it_end = m_toolState.end(); it != it_end; ++it ) { @@ -129,6 +129,8 @@ void TOOL_MANAGER::RegisterTool( TOOL_BASE* aTool ) wxT( "Adding two tools with the same name may result in unexpected behaviour.") ); wxASSERT_MSG( m_toolIdIndex.find( aTool->GetId() ) == m_toolIdIndex.end(), wxT( "Adding two tools with the same ID may result in unexpected behaviour.") ); + wxASSERT_MSG( m_toolTypes.find( typeid( *aTool ).name() ) == m_toolTypes.end(), + wxT( "Adding two tools of the same type may result in unexpected behaviour.") ); TOOL_STATE* st = new TOOL_STATE; @@ -141,6 +143,7 @@ void TOOL_MANAGER::RegisterTool( TOOL_BASE* aTool ) m_toolState[aTool] = st; m_toolNameIndex[aTool->GetName()] = st; m_toolIdIndex[aTool->GetId()] = st; + m_toolTypes[typeid( *aTool ).name()] = st->theTool; aTool->m_toolMgr = this; @@ -155,6 +158,7 @@ void TOOL_MANAGER::RegisterTool( TOOL_BASE* aTool ) m_toolState.erase( aTool ); m_toolNameIndex.erase( aTool->GetName() ); m_toolIdIndex.erase( aTool->GetId() ); + m_toolTypes.erase( typeid( *aTool ).name() ); delete st; delete aTool; @@ -272,7 +276,7 @@ bool TOOL_MANAGER::runTool( TOOL_BASE* aTool ) TOOL_BASE* TOOL_MANAGER::FindTool( int aId ) const { - std::map::const_iterator it = m_toolIdIndex.find( aId ); + boost::unordered_map::const_iterator it = m_toolIdIndex.find( aId ); if( it != m_toolIdIndex.end() ) return it->second->theTool; @@ -283,7 +287,7 @@ TOOL_BASE* TOOL_MANAGER::FindTool( int aId ) const TOOL_BASE* TOOL_MANAGER::FindTool( const std::string& aName ) const { - std::map::const_iterator it = m_toolNameIndex.find( aName ); + boost::unordered_map::const_iterator it = m_toolNameIndex.find( aName ); if( it != m_toolNameIndex.end() ) return it->second->theTool; diff --git a/include/tool/tool_manager.h b/include/tool/tool_manager.h index c8861c7ff1..62f7e42e8a 100644 --- a/include/tool/tool_manager.h +++ b/include/tool/tool_manager.h @@ -26,8 +26,9 @@ #ifndef __TOOL_MANAGER_H #define __TOOL_MANAGER_H -#include #include +#include +#include #include @@ -135,6 +136,21 @@ public: */ TOOL_BASE* FindTool( const std::string& aName ) const; + /* + * Function GetTool() + * Returns the tool of given type or NULL if there is no such tool registered. + */ + template + T* GetTool() + { + boost::unordered_map::iterator tool = m_toolTypes.find( typeid( T ).name() ); + + if( tool != m_toolTypes.end() ) + return static_cast( tool->second ); + + return NULL; + } + /** * Function ResetTools() * Resets all tools (i.e. calls their Reset() method). @@ -344,13 +360,16 @@ private: bool isActive( TOOL_BASE* aTool ); /// Index of registered tools current states, associated by tools' objects. - std::map m_toolState; + boost::unordered_map m_toolState; /// Index of the registered tools current states, associated by tools' names. - std::map m_toolNameIndex; + boost::unordered_map m_toolNameIndex; + + /// Index of the registered tools to easily lookup by their type. + boost::unordered_map m_toolTypes; /// Index of the registered tools current states, associated by tools' ID numbers. - std::map m_toolIdIndex; + boost::unordered_map m_toolIdIndex; /// Stack of the active tools std::deque m_activeTools;