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
00022 namespace gdcm
00023 {
00024
00025 CommandManager CommandManager::Instance;
00026
00027
00028
00032 CommandManager::CommandManager()
00033 {
00034 }
00035
00036
00040 CommandManager::~CommandManager ()
00041 {
00042 if( this == GetInstance() )
00043 InClearCommand();
00044 }
00045
00046
00047
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
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
00146
00147
00148
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 }