Embedding the CfgScript Interpreter in C++


| Introduction | Start | acdkcfgscript | Language | Library | Embedding | CfgScript IDE | Debugging | Templates | Samples | Wish List |

CfgScript can easily be embedded into a C++ application.

Content of this chapter:

   Using acdk::cfgscript::Script
   Using acdk::cfgscript::Props
   ACDK applications make useage of CfgScript

 Using acdk::cfgscript::Script

Embedding the CfgScript into a ACDK application is quite easy:

Two classes are mainly used to control the Script:
 acdk::cfgscript::Props for the internal (and also external) heap/'stack' of the interpreter and the class  acdk::cfgscript::Script for executing a script.

#include <acdk/cfgscript/Script.h>

	::acdk::cfgscript::RScript script = new ::acdk::cfgscript::Script("<mem>");
  ::acdk::cfgscript::RProps props = new ::acdk::cfgscript::Props();
  // pass variable to the interpreter
  props->setObjectVal("sb", new StringBuffer("CfgScript"));
  RString code =
  	"String retString = \"Hallo\" + sb.toString();\n"
  // ScriptReadWriteParent allows the interpreter to write into 
  // the given props
  script->eval(code, &props, ::acdk::cfgscript::ScriptReadWriteParent);
  // get the resulting string
  RString retString = props->getStringVal("retString");

 Using acdk::cfgscript::Props

Much more simple is to use the embedded Props evaluations functions:

#include <acdk/cfgscript/Props.h>
// ...
	::acdk::cfgscript::RProps props = new ::acdk::cfgscript::Props();
  props->setIntVal("i", 42);
  props->setIntVal("j", 1);
  // ${expr} where expr is either a Props key or a CfgScript expression
  RString s = props->eval("${i + j}");
	// !{ script }! 
	// script will be evaluated as CfgScript script. 
	// the !{ script }! will be replaced with the output 
	// written by the script to 'out'
  s = props->eval("!{ out.print(i + j); }!");

Another example:

RProps props = new Props();
props.setIntVal("i", 42);
RString erg = props.eval("$i");
erg->equals("42") == true;

// inline expression
props.eval("!{ if (i < 42) erg = \"isSmaller\" else erg = \"isGreaterOrSame\" }");
props->getStringVal("erg")->equals("isGreaterOrSame") == true;

 ACDK applications make useage of CfgScript

The CfgScript interpreter is embedded in following ACDK applications: