]> Creatis software - bbtk.git/blobdiff - samples/SampleInterpreter/bbtkSampleInterpreter.cxx
*** empty log message ***
[bbtk.git] / samples / SampleInterpreter / bbtkSampleInterpreter.cxx
diff --git a/samples/SampleInterpreter/bbtkSampleInterpreter.cxx b/samples/SampleInterpreter/bbtkSampleInterpreter.cxx
new file mode 100644 (file)
index 0000000..91f4ff0
--- /dev/null
@@ -0,0 +1,62 @@
+//=========================================================================
+// How to create and use a script defined black box
+//=========================================================================
+
+//=========================================================================
+#include <bbtkInterpreter.h>
+//=========================================================================
+
+//=========================================================================
+int main(int argv, char* argc[])
+{
+  // To track all ...
+  //  bbtk::MessageManager::SetMessageLevel("all",9);
+  
+  try
+    {
+      // Create an interpreter
+      bbtk::Interpreter::Pointer I = bbtk::Interpreter::New();
+      
+      // We tell the interpreter to throw exceptions on error
+      I->SetThrow(true);
+
+      // Interpret the file supposed to define a box called 'Processing'
+      I->InterpretFile("bbProcessing.bbs");
+      
+      // Create an instance 'p' of the 'Processing' box
+      bbtk::BlackBox::Pointer p 
+       = I->GetExecuter()->GetFactory()->NewBlackBox("Processing","p");
+      
+      // Prompt the user
+      std::cout << "Enter a number : ";
+      double num = 0;
+      std::cin >> num;
+      // Set its input 'In' to 1
+      p->bbSetInput("In",num);
+
+      // Execute it
+      p->bbExecute();
+      
+      // Print out the output 'Out':
+      // 1) We get the output with the generic bbGetOutput method.
+      // 2) It returns a bbtk::Data which can store any data type
+      // 3) We get the value of the data as a double using get<TYPE>()
+      // (because we know it's a double !)
+      double v = p->bbGetOutput("Out").get<double>();
+      
+      std::cout << num << "+1 = "<<v<<std::endl;
+       
+    }
+  catch (bbtk::Exception e)
+    {
+      std::cout << "* ERROR : "<<e.GetErrorMessage()<<std::endl;
+      return 1;
+    }
+  return 0; 
+
+
+  // To get the list of bbtk object still allocated after main ends
+  // bbtk::StaticInitTime::PrintObjectListInfo = true;
+  // bbtk::MessageManager::SetMessageLevel("object",1);
+}
+//=========================================================================