Version 2.2.5
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.
![]() |
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! |
double fVal=0; mupDefineVar(hParser, "a", &fVal);See also: example2/example2.c.
double fVal=0; parser.DefineVar("a", &fVal);See also: example1/example1.cpp; src/muParserTest.cpp.
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.
![]() |
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. |
double* AddVariable(const char *a_szName, void *pUserData) { static double afValBuf[100]; static int iVal = -1; iVal++; 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."); else 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.
mupSetVarFactory(hParser, AddVariable, pUserData);
See also: example2/example2.c.
parser.SetVarFactory(AddVariable, pUserData);
See also: example1/example1.cpp.
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.
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.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.
You might also like: