]> Creatis software - gdcm.git/blob - Testing/TestCommand.cxx
In order to allow to use current version (1.3) of gdcm *and* ITK (that includes
[gdcm.git] / Testing / TestCommand.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: TestCommand.cxx,v $
5   Language:  C++
6   Date:      $Date: 2007/05/23 14:18:06 $
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 #include "gdcmCommand.h"
19 #include "gdcmCallbackCommand.h"
20 #include "gdcmCommandManager.h"
21
22 #include <iostream>
23 #include <typeinfo>
24
25 class CommandTest : public GDCM_NAME_SPACE::Command
26 {
27    gdcmTypeMacro(CommandTest);
28    gdcmNewMacro(CommandTest);
29
30 public:
31    virtual void Execute()
32    {
33       std::cout << "Test class command... for "
34                 << typeid(GetObject()).name()
35                 << " (" << GetObject() << ")" << std::endl
36                 << GetText() << std::endl;
37       Executed = true;
38    }
39
40    bool IsExecuted() {return Executed;}
41
42 private:
43    CommandTest() {Executed = false;}
44
45    bool Executed;
46 };
47
48 static bool fctExecuted = false;
49 void CallbackTest(GDCM_NAME_SPACE::CallbackCommand *cmd)
50 {
51    std::cout << "Test class command... for "
52              << typeid(cmd->GetObject()).name()
53              << " (" << cmd->GetObject() << ")" << std::endl
54              << cmd->GetText() << std::endl;
55
56    fctExecuted = true;
57 }
58
59 int TestCommand(int , char *[]) 
60 {
61    int error=0;
62
63    GDCM_NAME_SPACE::CallbackCommand *cbk = GDCM_NAME_SPACE::CallbackCommand::New();
64    cbk->SetCallback(CallbackTest);
65    GDCM_NAME_SPACE::CommandManager::SetCommand(NULL,1,cbk);
66    cbk->Delete();
67
68    CommandTest *cmd = CommandTest::New();
69    GDCM_NAME_SPACE::CommandManager::SetCommand(NULL,2,cmd);
70    cmd->Delete();
71
72    std::cout << "Test on callback function execution\n";
73    GDCM_NAME_SPACE::CommandManager::ExecuteCommand(NULL,1,"Test on callback function");
74    if(!fctExecuted)
75       std::cout<<"... Failed\n";
76    error+=!fctExecuted;
77    std::cout << std::endl;
78
79    std::cout << "Test on command class execution\n";
80    GDCM_NAME_SPACE::CommandManager::ExecuteCommand(NULL,2,"Test on command class");
81    if(!cmd->IsExecuted())
82       std::cout<<"... Failed\n";
83    error+=!cmd->IsExecuted();
84    std::cout << std::endl;
85
86    std::cout << "Test on unset command execution\n";
87    GDCM_NAME_SPACE::CommandManager::ExecuteCommand(NULL,3,"Test on callback function");
88    std::cout << std::endl;
89
90    return error;
91 }