Program: gdcm
Module: $RCSfile: gdcmCommandManager.cxx,v $
Language: C++
- Date: $Date: 2005/11/28 15:20:35 $
- Version: $Revision: 1.1 $
+ Date: $Date: 2007/05/23 14:18:08 $
+ Version: $Revision: 1.5 $
Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
l'Image). All rights reserved. See Doc/License.txt or
// ---------------------------------------------------------------
#include "gdcmCommandManager.h"
#include "gdcmCommand.h"
+#include <typeinfo> // for typeif (needed by __BORLANDC__ v6)
-namespace gdcm
+namespace GDCM_NAME_SPACE
{
+//-----------------------------------------------------------------------------
+CommandManager CommandManager::Instance;
+
//-----------------------------------------------------------------------------
// Constructor / Destructor
/**
*/
CommandManager::~CommandManager ()
{
+ if( this == GetInstance() )
+ InClearCommand();
}
//-----------------------------------------------------------------------------
// Public
-void CommandManager::SetCommand(unsigned int type,Command *command)
+void CommandManager::SetCommand(const Base *object, unsigned int type, Command *command)
+{
+ Instance.InSetCommand(object, type, command);
+}
+
+Command *CommandManager::GetCommand(const Base *object, unsigned int type)
+{
+ return(Instance.InGetCommand(object, type));
+}
+
+bool CommandManager::ExecuteCommand(Base *object, unsigned int type, std::string text)
+{
+ return(Instance.InExecuteCommand(object, type, text));
+}
+
+bool CommandManager::ExecuteCommandConst(const Base *object, unsigned int type, std::string text)
+{
+ return(Instance.InExecuteCommandConst(object,type,text));
+}
+
+const CommandManager *CommandManager::GetInstance()
{
- Command *cmd=CommandList[type];
- if(cmd!=command)
+ return &Instance;
+}
+
+//-----------------------------------------------------------------------------
+// Protected
+void CommandManager::InClearCommand(void)
+{
+ CommandHT::iterator it;
+ for(it=CommandList.begin(); it != CommandList.end(); ++it)
+ {
+ if( it->second )
+ it->second->Delete();
+ }
+}
+
+void CommandManager::InSetCommand(const Base *object, unsigned int type, Command *command)
+{
+ CommandKey key = CommandKey(object, type);
+ Command *cmd = CommandList[key];
+ if( cmd != command )
{
- if(cmd)
+ if( cmd )
cmd->Unregister();
- if(command)
+ if( command )
{
- CommandList[type]=command;
+ CommandList[key] = command;
command->Register();
}
else
- CommandList.erase(type);
+ CommandList.erase(key);
}
}
-Command *CommandManager::GetCommand(unsigned int type) const
+Command *CommandManager::InGetCommand(const Base *object,unsigned int type)
{
+ CommandKey key = CommandKey(object,type);
try
{
- return CommandList[type];
+ return CommandList[key];
}
catch(...)
{
}
}
-bool CommandManager::ExecuteCommand(unsigned int type,std::string text)
+bool CommandManager::InExecuteCommand(Base *object,unsigned int type,std::string text)
{
- Command *cmd = GetCommand(type);
- if(cmd)
+ Command *cmd = GetCommand(object,type);
+ if( cmd )
{
cmd->SetText(text);
- cmd->SetObject(this);
+ cmd->SetObject(object);
cmd->SetType(type);
cmd->Execute();
return true;
return false;
}
-bool CommandManager::ConstExecuteCommand(unsigned int type,std::string text) const
+bool CommandManager::InExecuteCommandConst(const Base *object,unsigned int type,std::string text)
{
- Command *cmd = GetCommand(type);
- if(cmd)
+ Command *cmd = GetCommand(object,type);
+ if( cmd )
{
cmd->SetText(text);
- cmd->SetConstObject(this);
+ cmd->SetConstObject(object);
cmd->SetType(type);
cmd->Execute();
return true;
return false;
}
-//-----------------------------------------------------------------------------
-// Protected
-
//-----------------------------------------------------------------------------
// Private
CommandHT::iterator it;
for(it=CommandList.begin();it!=CommandList.end();++it)
{
- os<<indent<<" "<<Command::GetCommandAsString(it->first)
- <<" : "<<typeid(it->second).name()
- <<" ("<<it->second<<")"<<std::endl;
+ os<<indent<<" "<<typeid(it->first.first).name()<<" ("<<it->first.first<<") - "
+ <<Command::GetCommandAsString(it->first.second)
+ <<" : "<<typeid(it->second).name()<<" ("<<it->second<<")"
+ <<std::endl;
}
}