]> 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       // Set its input 'In' to 1
35       p->bbSetInput("In",num);
36
37       // Execute it
38       p->bbExecute();
39       
40       // Print out the output 'Out':
41       // 1) We get the output with the generic bbGetOutput method.
42       // 2) It returns a bbtk::Data which can store any data type
43       // 3) We get the value of the data as a double using get<TYPE>()
44       // (because we know it's a double !)
45       double v = p->bbGetOutput("Out").get<double>();
46       
47       std::cout << num << "+1 = "<<v<<std::endl;
48         
49     }
50   catch (bbtk::Exception e)
51     {
52       std::cout << "* ERROR : "<<e.GetErrorMessage()<<std::endl;
53       return 1;
54     }
55   return 0; 
56
57
58   // To get the list of bbtk object still allocated after main ends
59   // bbtk::StaticInitTime::PrintObjectListInfo = true;
60   // bbtk::MessageManager::SetMessageLevel("object",1);
61 }
62 //=========================================================================