00001 /*========================================================================= 00002 00003 Program: Medical Imaging & Interaction Toolkit 00004 Language: C++ 00005 Date: $Date$ 00006 Version: $Revision: 14081 $ 00007 00008 Copyright (c) German Cancer Research Center, Division of Medical and 00009 Biological Informatics. All rights reserved. 00010 See MITKCopyright.txt or https://www.mitk.org/copyright.html for details. 00011 00012 This software is distributed WITHOUT ANY WARRANTY; without even 00013 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 00014 PURPOSE. See the above copyright notices for more information. 00015 00016 =========================================================================*/ 00017 00018 00019 #include "mitkDelegateManager.h" 00020 00021 mitk::DelegateManager::DelegateManager() 00022 { 00023 } 00024 mitk::DelegateManager* mitk::DelegateManager::GetInstance() 00025 { 00026 static mitk::DelegateManager _DelegateManager; 00027 return &_DelegateManager; 00028 } 00029 00030 void mitk::DelegateManager::SetCommand( const std::string& _CommandID, mitk::MessageAbstractDelegate<>* _Command) 00031 { 00032 MessageAbstractDelegate<>* _ExistingCommand = this->GetCommand(_CommandID); 00033 // delete existing MessageAbstractDelegate for id, but only if the existing MessageAbstractDelegate is not the same as the 00034 // MessageAbstractDelegate parameter 00035 if(_ExistingCommand && _ExistingCommand != _Command) 00036 { 00037 m_CommandMap.erase(_CommandID); 00038 delete _ExistingCommand; 00039 _ExistingCommand = 0; 00040 } 00041 m_CommandMap[_CommandID] = _Command; 00042 } 00043 00044 bool mitk::DelegateManager::RemoveCommand( const std::string& _CommandID ) 00045 { 00046 MessageAbstractDelegate<>* _ExistingCommand = this->GetCommand(_CommandID); 00047 if(_ExistingCommand) 00048 { 00049 m_CommandMap.erase(_CommandID); 00050 delete _ExistingCommand; 00051 _ExistingCommand = 0; 00052 return true; 00053 } 00054 return false; 00055 } 00056 00057 mitk::MessageAbstractDelegate<>* mitk::DelegateManager::GetCommand( const std::string& _CommandID ) const 00058 { 00059 std::map<std::string, MessageAbstractDelegate<>*>::const_iterator it = m_CommandMap.find(_CommandID); 00060 return (it == m_CommandMap.end()) ? 0: it->second; 00061 } 00062 00063 mitk::DelegateManager::~DelegateManager() 00064 { 00065 // delete all commands 00066 for(std::map<std::string, MessageAbstractDelegate<>*>::iterator it=m_CommandMap.begin(); it!=m_CommandMap.end() 00067 ; it++) 00068 { 00069 delete it->second; 00070 it->second = 0; 00071 } 00072 00073 m_CommandMap.clear(); 00074 }