Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

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: 2005/11/28 16:31:22 $
00007   Version:   $Revision: 1.2 $
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 
00022 namespace gdcm 
00023 {
00024 //-----------------------------------------------------------------------------
00025 CommandManager CommandManager::Instance;
00026 
00027 //-----------------------------------------------------------------------------
00028 // Constructor / Destructor
00032 CommandManager::CommandManager()
00033 {
00034 }
00035 
00036 
00040 CommandManager::~CommandManager ()
00041 {
00042    if( this == GetInstance() )
00043       InClearCommand();
00044 }
00045 
00046 //-----------------------------------------------------------------------------
00047 // Public
00048 void CommandManager::SetCommand(const Base *object,unsigned int type,Command *command)
00049 {
00050    Instance.InSetCommand(object,type,command);
00051 }
00052 
00053 Command *CommandManager::GetCommand(const Base *object,unsigned int type)
00054 {
00055    return(Instance.InGetCommand(object,type));
00056 }
00057 
00058 bool CommandManager::ExecuteCommand(Base *object,unsigned int type,std::string text)
00059 {
00060    return(Instance.InExecuteCommand(object,type,text));
00061 }
00062 
00063 bool CommandManager::ExecuteCommandConst(const Base *object,unsigned int type,std::string text)
00064 {
00065    return(Instance.InExecuteCommandConst(object,type,text));
00066 }
00067 
00068 const CommandManager *CommandManager::GetInstance()
00069 {
00070    return &Instance;
00071 }
00072 
00073 //-----------------------------------------------------------------------------
00074 // Protected
00075 void CommandManager::InClearCommand(void)
00076 {
00077    CommandHT::iterator it;
00078    for(it=CommandList.begin();it!=CommandList.end();++it)
00079    {
00080       if( it->second )
00081          it->second->Delete();
00082    }
00083 }
00084 
00085 void CommandManager::InSetCommand(const Base *object,unsigned int type,Command *command)
00086 {
00087    CommandKey key = CommandKey(object,type);
00088    Command *cmd = CommandList[key];
00089    if( cmd != command )
00090    {
00091       if( cmd )
00092          cmd->Unregister();
00093       if( command )
00094       {
00095          CommandList[key]=command;
00096          command->Register();
00097       }
00098       else
00099          CommandList.erase(key);
00100    }
00101 }
00102 
00103 Command *CommandManager::InGetCommand(const Base *object,unsigned int type)
00104 {
00105    CommandKey key = CommandKey(object,type);
00106    try
00107    {
00108       return CommandList[key];
00109    }
00110    catch(...)
00111    {
00112       return NULL;
00113    }
00114 }
00115 
00116 bool CommandManager::InExecuteCommand(Base *object,unsigned int type,std::string text)
00117 {
00118    Command *cmd = GetCommand(object,type);
00119    if( cmd )
00120    {
00121       cmd->SetText(text);
00122       cmd->SetObject(object);
00123       cmd->SetType(type);
00124       cmd->Execute();
00125       return true;
00126    }
00127    return false;
00128 }
00129 
00130 bool CommandManager::InExecuteCommandConst(const Base *object,unsigned int type,std::string text)
00131 {
00132    Command *cmd = GetCommand(object,type);
00133    if( cmd )
00134    {
00135       cmd->SetText(text);
00136       cmd->SetConstObject(object);
00137       cmd->SetType(type);
00138       cmd->Execute();
00139       return true;
00140    }
00141    return false;
00142 }
00143 
00144 //-----------------------------------------------------------------------------
00145 // Private
00146 
00147 //-----------------------------------------------------------------------------
00148 // Print
00149 void CommandManager::Print(std::ostream &os, std::string const &indent)
00150 {
00151    os<<indent<<"Command list : \n";
00152    CommandHT::iterator it;
00153    for(it=CommandList.begin();it!=CommandList.end();++it)
00154    {
00155       os<<indent<<"   "<<typeid(it->first.first).name()<<" ("<<it->first.first<<") - "
00156         <<Command::GetCommandAsString(it->first.second)
00157         <<" : "<<typeid(it->second).name()<<" ("<<it->second<<")"
00158         <<std::endl;
00159    }
00160 }
00161 
00162 //-----------------------------------------------------------------------------
00163 } // end namespace gdcm

Generated on Fri Jan 20 10:14:23 2006 for gdcm by  doxygen 1.4.4