]> Creatis software - gdcm.git/blob - src/gdcmCommandManager.cxx
* Improvement #2 : the CommandManager is now a static class so,
[gdcm.git] / src / gdcmCommandManager.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmCommandManager.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/11/28 16:31:22 $
7   Version:   $Revision: 1.2 $
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
22 namespace gdcm 
23 {
24 //-----------------------------------------------------------------------------
25 CommandManager CommandManager::Instance;
26
27 //-----------------------------------------------------------------------------
28 // Constructor / Destructor
29 /**
30  * \brief Constructor used when we want to generate dicom files from scratch
31  */
32 CommandManager::CommandManager()
33 {
34 }
35
36
37 /**
38  * \brief   Canonical destructor.
39  */
40 CommandManager::~CommandManager ()
41 {
42    if( this == GetInstance() )
43       InClearCommand();
44 }
45
46 //-----------------------------------------------------------------------------
47 // Public
48 void CommandManager::SetCommand(const Base *object,unsigned int type,Command *command)
49 {
50    Instance.InSetCommand(object,type,command);
51 }
52
53 Command *CommandManager::GetCommand(const Base *object,unsigned int type)
54 {
55    return(Instance.InGetCommand(object,type));
56 }
57
58 bool CommandManager::ExecuteCommand(Base *object,unsigned int type,std::string text)
59 {
60    return(Instance.InExecuteCommand(object,type,text));
61 }
62
63 bool CommandManager::ExecuteCommandConst(const Base *object,unsigned int type,std::string text)
64 {
65    return(Instance.InExecuteCommandConst(object,type,text));
66 }
67
68 const CommandManager *CommandManager::GetInstance()
69 {
70    return &Instance;
71 }
72
73 //-----------------------------------------------------------------------------
74 // Protected
75 void CommandManager::InClearCommand(void)
76 {
77    CommandHT::iterator it;
78    for(it=CommandList.begin();it!=CommandList.end();++it)
79    {
80       if( it->second )
81          it->second->Delete();
82    }
83 }
84
85 void CommandManager::InSetCommand(const Base *object,unsigned int type,Command *command)
86 {
87    CommandKey key = CommandKey(object,type);
88    Command *cmd = CommandList[key];
89    if( cmd != command )
90    {
91       if( cmd )
92          cmd->Unregister();
93       if( command )
94       {
95          CommandList[key]=command;
96          command->Register();
97       }
98       else
99          CommandList.erase(key);
100    }
101 }
102
103 Command *CommandManager::InGetCommand(const Base *object,unsigned int type)
104 {
105    CommandKey key = CommandKey(object,type);
106    try
107    {
108       return CommandList[key];
109    }
110    catch(...)
111    {
112       return NULL;
113    }
114 }
115
116 bool CommandManager::InExecuteCommand(Base *object,unsigned int type,std::string text)
117 {
118    Command *cmd = GetCommand(object,type);
119    if( cmd )
120    {
121       cmd->SetText(text);
122       cmd->SetObject(object);
123       cmd->SetType(type);
124       cmd->Execute();
125       return true;
126    }
127    return false;
128 }
129
130 bool CommandManager::InExecuteCommandConst(const Base *object,unsigned int type,std::string text)
131 {
132    Command *cmd = GetCommand(object,type);
133    if( cmd )
134    {
135       cmd->SetText(text);
136       cmd->SetConstObject(object);
137       cmd->SetType(type);
138       cmd->Execute();
139       return true;
140    }
141    return false;
142 }
143
144 //-----------------------------------------------------------------------------
145 // Private
146
147 //-----------------------------------------------------------------------------
148 // Print
149 void CommandManager::Print(std::ostream &os, std::string const &indent)
150 {
151    os<<indent<<"Command list : \n";
152    CommandHT::iterator it;
153    for(it=CommandList.begin();it!=CommandList.end();++it)
154    {
155       os<<indent<<"   "<<typeid(it->first.first).name()<<" ("<<it->first.first<<") - "
156         <<Command::GetCommandAsString(it->first.second)
157         <<" : "<<typeid(it->second).name()<<" ("<<it->second<<")"
158         <<std::endl;
159    }
160 }
161
162 //-----------------------------------------------------------------------------
163 } // end namespace gdcm