gdcmCommandManager.cxx

Go to the documentation of this file.
00001 /*=========================================================================
00002                                                                                 
00003   Program:   gdcm
00004   Module:    $RCSfile: gdcmCommandManager.cxx,v $
00005   Language:  C++
00006   Date:      $Date: 2007/05/23 14:18:08 $
00007   Version:   $Revision: 1.5 $
00008                                                                                 
00009   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
00010   l'Image). All rights reserved. See Doc/License.txt or
00011   http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
00012                                                                                 
00013      This software is distributed WITHOUT ANY WARRANTY; without even
00014      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00015      PURPOSE.  See the above copyright notices for more information.
00016                                                                                 
00017 =========================================================================*/
00018 // ---------------------------------------------------------------
00019 #include "gdcmCommandManager.h"
00020 #include "gdcmCommand.h"
00021 #include <typeinfo> // for typeif (needed by __BORLANDC__ v6)
00022 
00023 namespace GDCM_NAME_SPACE 
00024 {
00025 //-----------------------------------------------------------------------------
00026 CommandManager CommandManager::Instance;
00027 
00028 //-----------------------------------------------------------------------------
00029 // Constructor / Destructor
00033 CommandManager::CommandManager()
00034 {
00035 }
00036 
00037 
00041 CommandManager::~CommandManager ()
00042 {
00043    if( this == GetInstance() )
00044       InClearCommand();
00045 }
00046 
00047 //-----------------------------------------------------------------------------
00048 // Public
00049 void CommandManager::SetCommand(const Base *object, unsigned int type, Command *command)
00050 {
00051    Instance.InSetCommand(object, type, command);
00052 }
00053 
00054 Command *CommandManager::GetCommand(const Base *object, unsigned int type)
00055 {
00056    return(Instance.InGetCommand(object, type));
00057 }
00058 
00059 bool CommandManager::ExecuteCommand(Base *object, unsigned int type, std::string text)
00060 {
00061    return(Instance.InExecuteCommand(object, type, text));
00062 }
00063 
00064 bool CommandManager::ExecuteCommandConst(const Base *object, unsigned int type, std::string text)
00065 {
00066    return(Instance.InExecuteCommandConst(object,type,text));
00067 }
00068 
00069 const CommandManager *CommandManager::GetInstance()
00070 {
00071    return &Instance;
00072 }
00073 
00074 //-----------------------------------------------------------------------------
00075 // Protected
00076 void CommandManager::InClearCommand(void)
00077 {
00078    CommandHT::iterator it;
00079    for(it=CommandList.begin(); it != CommandList.end(); ++it)
00080    {
00081       if( it->second )
00082          it->second->Delete();
00083    }
00084 }
00085 
00086 void CommandManager::InSetCommand(const Base *object, unsigned int type, Command *command)
00087 {
00088    CommandKey key = CommandKey(object, type);
00089    Command *cmd = CommandList[key];
00090    if( cmd != command )
00091    {
00092       if( cmd )
00093          cmd->Unregister();
00094       if( command )
00095       {
00096          CommandList[key] = command;
00097          command->Register();
00098       }
00099       else
00100          CommandList.erase(key);
00101    }
00102 }
00103 
00104 Command *CommandManager::InGetCommand(const Base *object,unsigned int type)
00105 {
00106    CommandKey key = CommandKey(object,type);
00107    try
00108    {
00109       return CommandList[key];
00110    }
00111    catch(...)
00112    {
00113       return NULL;
00114    }
00115 }
00116 
00117 bool CommandManager::InExecuteCommand(Base *object,unsigned int type,std::string text)
00118 {
00119    Command *cmd = GetCommand(object,type);
00120    if( cmd )
00121    {
00122       cmd->SetText(text);
00123       cmd->SetObject(object);
00124       cmd->SetType(type);
00125       cmd->Execute();
00126       return true;
00127    }
00128    return false;
00129 }
00130 
00131 bool CommandManager::InExecuteCommandConst(const Base *object,unsigned int type,std::string text)
00132 {
00133    Command *cmd = GetCommand(object,type);
00134    if( cmd )
00135    {
00136       cmd->SetText(text);
00137       cmd->SetConstObject(object);
00138       cmd->SetType(type);
00139       cmd->Execute();
00140       return true;
00141    }
00142    return false;
00143 }
00144 
00145 //-----------------------------------------------------------------------------
00146 // Private
00147 
00148 //-----------------------------------------------------------------------------
00149 // Print
00150 void CommandManager::Print(std::ostream &os, std::string const &indent)
00151 {
00152    os<<indent<<"Command list : \n";
00153    CommandHT::iterator it;
00154    for(it=CommandList.begin();it!=CommandList.end();++it)
00155    {
00156       os<<indent<<"   "<<typeid(it->first.first).name()<<" ("<<it->first.first<<") - "
00157         <<Command::GetCommandAsString(it->first.second)
00158         <<" : "<<typeid(it->second).name()<<" ("<<it->second<<")"
00159         <<std::endl;
00160    }
00161 }
00162 
00163 //-----------------------------------------------------------------------------
00164 } // end namespace gdcm

Generated on Fri Aug 24 12:53:08 2007 for gdcm by  doxygen 1.4.6