]> Creatis software - gdcm.git/blob - src/gdcmCommandManager.cxx
Fix mistypings
[gdcm.git] / src / gdcmCommandManager.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmCommandManager.cxx,v $
5   Language:  C++
6   Date:      $Date: 2007/05/23 14:18:08 $
7   Version:   $Revision: 1.5 $
8                                                                                 
9   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10   l'Image). All rights reserved. See Doc/License.txt or
11   http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
12                                                                                 
13      This software is distributed WITHOUT ANY WARRANTY; without even
14      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15      PURPOSE.  See the above copyright notices for more information.
16                                                                                 
17 =========================================================================*/
18 // ---------------------------------------------------------------
19 #include "gdcmCommandManager.h"
20 #include "gdcmCommand.h"
21 #include <typeinfo> // for typeif (needed by __BORLANDC__ v6)
22
23 namespace GDCM_NAME_SPACE 
24 {
25 //-----------------------------------------------------------------------------
26 CommandManager CommandManager::Instance;
27
28 //-----------------------------------------------------------------------------
29 // Constructor / Destructor
30 /**
31  * \brief Constructor used when we want to generate dicom files from scratch
32  */
33 CommandManager::CommandManager()
34 {
35 }
36
37
38 /**
39  * \brief   Canonical destructor.
40  */
41 CommandManager::~CommandManager ()
42 {
43    if( this == GetInstance() )
44       InClearCommand();
45 }
46
47 //-----------------------------------------------------------------------------
48 // Public
49 void CommandManager::SetCommand(const Base *object, unsigned int type, Command *command)
50 {
51    Instance.InSetCommand(object, type, command);
52 }
53
54 Command *CommandManager::GetCommand(const Base *object, unsigned int type)
55 {
56    return(Instance.InGetCommand(object, type));
57 }
58
59 bool CommandManager::ExecuteCommand(Base *object, unsigned int type, std::string text)
60 {
61    return(Instance.InExecuteCommand(object, type, text));
62 }
63
64 bool CommandManager::ExecuteCommandConst(const Base *object, unsigned int type, std::string text)
65 {
66    return(Instance.InExecuteCommandConst(object,type,text));
67 }
68
69 const CommandManager *CommandManager::GetInstance()
70 {
71    return &Instance;
72 }
73
74 //-----------------------------------------------------------------------------
75 // Protected
76 void CommandManager::InClearCommand(void)
77 {
78    CommandHT::iterator it;
79    for(it=CommandList.begin(); it != CommandList.end(); ++it)
80    {
81       if( it->second )
82          it->second->Delete();
83    }
84 }
85
86 void CommandManager::InSetCommand(const Base *object, unsigned int type, Command *command)
87 {
88    CommandKey key = CommandKey(object, type);
89    Command *cmd = CommandList[key];
90    if( cmd != command )
91    {
92       if( cmd )
93          cmd->Unregister();
94       if( command )
95       {
96          CommandList[key] = command;
97          command->Register();
98       }
99       else
100          CommandList.erase(key);
101    }
102 }
103
104 Command *CommandManager::InGetCommand(const Base *object,unsigned int type)
105 {
106    CommandKey key = CommandKey(object,type);
107    try
108    {
109       return CommandList[key];
110    }
111    catch(...)
112    {
113       return NULL;
114    }
115 }
116
117 bool CommandManager::InExecuteCommand(Base *object,unsigned int type,std::string text)
118 {
119    Command *cmd = GetCommand(object,type);
120    if( cmd )
121    {
122       cmd->SetText(text);
123       cmd->SetObject(object);
124       cmd->SetType(type);
125       cmd->Execute();
126       return true;
127    }
128    return false;
129 }
130
131 bool CommandManager::InExecuteCommandConst(const Base *object,unsigned int type,std::string text)
132 {
133    Command *cmd = GetCommand(object,type);
134    if( cmd )
135    {
136       cmd->SetText(text);
137       cmd->SetConstObject(object);
138       cmd->SetType(type);
139       cmd->Execute();
140       return true;
141    }
142    return false;
143 }
144
145 //-----------------------------------------------------------------------------
146 // Private
147
148 //-----------------------------------------------------------------------------
149 // Print
150 void CommandManager::Print(std::ostream &os, std::string const &indent)
151 {
152    os<<indent<<"Command list : \n";
153    CommandHT::iterator it;
154    for(it=CommandList.begin();it!=CommandList.end();++it)
155    {
156       os<<indent<<"   "<<typeid(it->first.first).name()<<" ("<<it->first.first<<") - "
157         <<Command::GetCommandAsString(it->first.second)
158         <<" : "<<typeid(it->second).name()<<" ("<<it->second<<")"
159         <<std::endl;
160    }
161 }
162
163 //-----------------------------------------------------------------------------
164 } // end namespace gdcm