]> Creatis software - gdcm.git/blob - gdcmCommandManager.cxx
49adb41807f3d8fc10e566a7f1588b990269be5b
[gdcm.git] / gdcmCommandManager.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmCommandManager.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/11/28 15:20:35 $
7   Version:   $Revision: 1.1 $
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 // Constructor / Destructor
26 /**
27  * \brief Constructor used when we want to generate dicom files from scratch
28  */
29 CommandManager::CommandManager()
30 {
31 }
32
33
34 /**
35  * \brief   Canonical destructor.
36  */
37 CommandManager::~CommandManager ()
38 {
39 }
40
41 //-----------------------------------------------------------------------------
42 // Public
43 void CommandManager::SetCommand(unsigned int type,Command *command)
44 {
45    Command *cmd=CommandList[type];
46    if(cmd!=command)
47    {
48       if(cmd)
49          cmd->Unregister();
50       if(command)
51       {
52          CommandList[type]=command;
53          command->Register();
54       }
55       else
56          CommandList.erase(type);
57    }
58 }
59
60 Command *CommandManager::GetCommand(unsigned int type) const
61 {
62    try
63    {
64       return CommandList[type];
65    }
66    catch(...)
67    {
68       return NULL;
69    }
70 }
71
72 bool CommandManager::ExecuteCommand(unsigned int type,std::string text)
73 {
74    Command *cmd = GetCommand(type);
75    if(cmd)
76    {
77       cmd->SetText(text);
78       cmd->SetObject(this);
79       cmd->SetType(type);
80       cmd->Execute();
81       return true;
82    }
83    return false;
84 }
85
86 bool CommandManager::ConstExecuteCommand(unsigned int type,std::string text) const
87 {
88    Command *cmd = GetCommand(type);
89    if(cmd)
90    {
91       cmd->SetText(text);
92       cmd->SetConstObject(this);
93       cmd->SetType(type);
94       cmd->Execute();
95       return true;
96    }
97    return false;
98 }
99
100 //-----------------------------------------------------------------------------
101 // Protected
102
103 //-----------------------------------------------------------------------------
104 // Private
105
106 //-----------------------------------------------------------------------------
107 // Print
108 void CommandManager::Print(std::ostream &os, std::string const &indent)
109 {
110    os<<indent<<"Command list : \n";
111    CommandHT::iterator it;
112    for(it=CommandList.begin();it!=CommandList.end();++it)
113    {
114       os<<indent<<"   "<<Command::GetCommandAsString(it->first)
115         <<" : "<<typeid(it->second).name()
116         <<" ("<<it->second<<")"<<std::endl;
117    }
118 }
119
120 //-----------------------------------------------------------------------------
121 } // end namespace gdcm