00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "gdcmCommandManager.h"
00020 #include "gdcmCommand.h"
00021 #include <typeinfo>
00022
00023 namespace GDCM_NAME_SPACE
00024 {
00025
00026 CommandManager CommandManager::Instance;
00027
00028
00029
00033 CommandManager::CommandManager()
00034 {
00035 }
00036
00037
00041 CommandManager::~CommandManager ()
00042 {
00043 if( this == GetInstance() )
00044 InClearCommand();
00045 }
00046
00047
00048
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
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
00147
00148
00149
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 }