]> Creatis software - bbtk.git/blob - samples/SampleInterpreter/bbtkSampleInterpreter.cxx
*** empty log message ***
[bbtk.git] / samples / SampleInterpreter / bbtkSampleInterpreter.cxx
1 //=========================================================================
2 // How to create and use a script defined black box
3 //=========================================================================
4
5 //=========================================================================
6 #include <bbtkInterpreter.h>
7 //=========================================================================
8
9 //=========================================================================
10 int main(int argv, char* argc[])
11 {
12   // To track all ...
13   //  bbtk::MessageManager::SetMessageLevel("all",9);
14   
15   try
16     {
17       // Create an interpreter
18       bbtk::Interpreter::Pointer I = bbtk::Interpreter::New();
19       
20       // We tell the interpreter to throw exceptions on error
21       I->SetThrow(true);
22
23       // Interpret the file supposed to define a box called 'Processing'
24       I->InterpretFile("bbProcessing.bbs");
25       
26       // Create an instance 'p' of the 'Processing' box
27       bbtk::BlackBox::Pointer p 
28         = I->GetExecuter()->GetFactory()->NewBlackBox("Processing","p");
29       
30       // Prompt the user
31       std::cout << "Enter a number : ";
32       double num = 0;
33       std::cin >> num;
34
35       // Set its input 'In' to num
36       p->bbSetInput("In",num);
37
38       // Execute it
39       p->bbExecute();
40       
41       // Print out the output 'Out':
42       // 1) We get the output with the generic bbGetOutput method.
43       // 2) It returns a bbtk::Data which can store any data type
44       // 3) We get the value of the data as a double using get<TYPE>()
45       // (because we know it's a double !)
46       double v = p->bbGetOutput("Out").get<double>();
47       
48       std::cout << num << "+1 = "<<v<<std::endl;
49         
50       //============================================================
51       // Second example : imagine the bbProcessing.bbs script has created
52       // the box "a" of type Processing
53       // Here we simulate it using:
54       I->InterpretLine("new Processing a");
55       // We get a the black box "a" in the workspace:
56       bbtk::BlackBox::Pointer a 
57         = I->GetExecuter()->GetWorkspace()->GetPrototype()->bbGetBlackBox("a");
58       // Then the same as previously...
59       a->bbSetInput("In",num);
60       a->bbExecute();
61       double w = p->bbGetOutput("Out").get<double>();
62       std::cout << "In case you did not understand:"<<std::endl<<num << "+1 = "<<w<<std::endl;
63
64       // That's all !
65    }
66   catch (bbtk::Exception e)
67     {
68       std::cout << "* ERROR : "<<e.GetErrorMessage()<<std::endl;
69       return 1;
70     }
71   return 0; 
72
73
74   // To get the list of bbtk object still allocated after main ends
75   // bbtk::StaticInitTime::PrintObjectListInfo = true;
76   // bbtk::MessageManager::SetMessageLevel("object",1);
77 }
78 //=========================================================================