muparser - Fast Math Parser Library

Version 2.2.5

Defining parser variables

Custom variables can be defined either explicit in the code by using the DefineVar function or implicit by the parser. Implicit declaration will call a variable factory function provided by the user. The parser is never the owner of its variables. So you must take care of their destruction in case of dynamic allocation. The general idea is to bind every parser variable to a C++ variable. For this reason, you have to make sure the C++ variable stays valid as long as you process a formula that needs it. Only variables of type double are supported.

Explicitely defining variables

Explicitely in this context means you have to do add the variables manually it in your application code. So you must know in advance which variables you intend to use. If this is not the case have a look at the section on Implicit creation of new variables.
warning Defining new Variables will reset the parser bytecode. Do not use this function just for changing the values of variables! It would dramatically reduce the parser performance! Once the parser knows the address of the variable there is no need to explicitely call a function for changing the value. Since the parser knows the address it knows the value too so simply change the C++ variable in your code directly!

[DLL interface]

The first parameter is a valid parser handle, the second the variable name and the third a pointer to the associated C++ variable.
double fVal=0;
mupDefineVar(hParser, "a", &fVal);  
See also: example2/example2.c.

[Parser class interface]

The first parameter is the variable name and the second a pointer to the associated C++ variable.
double fVal=0;
parser.DefineVar("a", &fVal);
See also: example1/example1.cpp; src/muParserTest.cpp.

Implicit creation of new variables

Implicit declaration of new variables is only possible by setting a factory function. Implicit creation means that everytime the parser finds an unknown token at a position where a variable could be located it creates a new variable with that name automatically. The necessary factory function must be of type:
double* (*facfun_type)(const char*, void*)
The first argument to a factory function is the name of the variable found by the parser. The second is a pointer to user defined data. This pointer can be used to provide a pointer to a class that implements the actual factory. By doing this it is possible to use custom factory classes depending on the variable name.
warning Be aware of name conflicts! Please notice that recognizing the name of an undefined variable is the last step during parser token detection. If the potential variable name starts with identifiers that could be interpreted as a function or operator it will be detected as such most likely resulting in an syntax error.
The following code is an example of a factory function. The example does not use dynamic allocation for the new variables although this would be possible too. But when using dynamic allocation you must keep track of the variables allocated implicitely in order to free them later on.
double* AddVariable(const char *a_szName, void *pUserData)
  static double afValBuf[100];  
  static int iVal = -1;          


  std::cout << "Generating new variable \"" 
            << a_szName << "\" (slots left: " 
            << 99-iVal << ")" << endl;
  // You can also use custom factory classes like for instance:
  // MyFactory *pFactory = (MyFactory*)pUserData;
  // pFactory->CreateNewVariable(a_szName);
  afValBuf[iVal] = 0;

  if (iVal>=99)
    throw mu::ParserError("Variable buffer overflow.");
    return &afValBuf[iVal];
See also: example1/example1.cpp. In order to add a variable factory use the SetVarFactory functions. The first parameter is a pointer to the static factory function, the second parameter is optional and represents a pointer to user defined data. Without a variable factory each undefined variable will cause an undefined token error. Factory functions can be used to query the values of newly created variables directly from a database. If you emit errors from a factory function be sure to throw an exception of type ParserBase::exception_type all other exceptions will be caught internally and result in an internal error.

[DLL interface]

mupSetVarFactory(hParser, AddVariable, pUserData);
See also: example2/example2.c.

[Parser class interface]

parser.SetVarFactory(AddVariable, pUserData);
See also: example1/example1.cpp.

Querying parser variables

Keeping track of all variables can be a difficult task. For simplification the parser allows the user to query the variables defined in the parser. There are two different sets of variables that can be accessed:

Since the usage of the necessary commands is similar the following example shows querying the parser variables only.

[DLL interface]

For querying the variables used in the expression exchange mupGetVarNum(...) with mupGetExprVarNum(...) and mupGetVar(...) with mupGetExprVar(...) in the following example. Due to the use of an temporary internal static buffer for storing the variable name in the DLL version this DLL-function is not thread safe.
// Get the number of variables
int iNumVar = mupGetVarNum(a_hParser);

// Query the variables  
for (int i=0; i < iNumVar; ++i)
  const char_type *szName = 0;
  double *pVar = 0;
  mupGetVar(a_hParser, i, &szName, &pVar);
  std::cout << "Name: " << szName << "   Address: [0x" << pVar << "]\n";
See also: example2/example2.c.

[Parser class interface]

For querying the expression variables exchange parser.GetVar() with parser.GetUsedVar() in the following example.
// Get the map with the variables
mu::Parser::varmap_type variables = parser.GetVar();
cout << "Number: " << (int)variables.size() << "\n";

// Get the number of variables 
mu::Parser::varmap_type::const_iterator item = variables.begin();

// Query the variables
for (; item!=variables.end(); ++item)
  cout << "Name: " << item->first << " Address: [0x" << item->second << "]\n";
See also: example1/example1.cpp.
back      next

You might also like: