]> Creatis software - gdcm.git/blob - Testing/TestCommand.cxx
* Add Command and CommandManager to have possible callback
[gdcm.git] / Testing / TestCommand.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: TestCommand.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/11/28 15:20:29 $
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 #include "gdcmCommand.h"
19 #include "gdcmCallbackCommand.h"
20 #include "gdcmCommandManager.h"
21
22 #include <iostream>
23 #include <typeinfo.h>
24
25 class CommandTest : public gdcm::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::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 argc, char *argv[]) 
60 {
61    int error=0;
62
63    gdcm::CommandManager *mgr = gdcm::CommandManager::New();
64    CommandTest *cmd = CommandTest::New();
65    gdcm::CallbackCommand *cbk = gdcm::CallbackCommand::New();
66    cbk->SetCallback(CallbackTest);
67
68    mgr->SetCommand(2,cmd);
69    mgr->SetCommand(1,cbk);
70
71    cbk->Delete();
72    cmd->Delete();
73
74    std::cout << "Test on callback function execution\n";
75    mgr->ExecuteCommand(1,"Test on callback function");
76    if(!fctExecuted)
77       std::cout<<"... Failed\n";
78    error+=!fctExecuted;
79    std::cout << std::endl;
80
81    std::cout << "Test on command class execution\n";
82    mgr->ExecuteCommand(2,"Test on command class");
83    if(!cmd->IsExecuted())
84       std::cout<<"... Failed\n";
85    error+=!cmd->IsExecuted();
86    std::cout << std::endl;
87
88    std::cout << "Test on unset command execution\n";
89    mgr->ExecuteCommand(3,"Test on callback function");
90    std::cout << std::endl;
91
92    return error;
93 }