Parser Class Reference

#include <c4d_general.h>

Detailed Description

A class to evaluate mathematical expressions.

Note
Has to be created with Alloc() and destroyed with Free(). Use AutoAlloc to automate the allocation and destruction based on scope.

Public Member Functions

Bool Eval (const maxon::String &str, Int32 *error, Float *res, Int32 unit=0, Int32 angletype=0, Int32 basis=10)
 
Bool EvalLong (const maxon::String &str, Int32 *error, Int32 *res, Int32 unit, Int32 basis)
 
Bool AddVar (const maxon::String &str, Float *value, Bool case_sensitive=false)
 
Bool RemoveVar (const maxon::String &s, Bool case_sensitive=false)
 
Bool RemoveAllVars (void)
 
void GetParserData (ParserCache *p)
 
Bool Init (const maxon::String &s, Int32 *error, Int32 unit=0, Int32 angle_unit=0, Int32 base=10)
 
Bool ReEval (Float *result, Int32 *error)
 
Bool Calculate (const ParserCache *pdat, Float *result, Int32 *error)
 
Bool AddVarLong (const maxon::String &str, Int32 *value, Bool case_sensitive=false)
 
Bool ReEvalLong (Int32 *result, Int32 *error)
 
Bool CalculateLong (const ParserCache *pdat, Int32 *result, Int32 *error)
 
Bool Reset (ParserCache *p=nullptr)
 

Static Public Member Functions

static ParserAlloc (void)
 
static void Free (Parser *&pr)
 

Private Member Functions

 Parser ()
 
 ~Parser ()
 

Shader Code Generation

Bool GenerateShaderCode (const maxon::String &resultVariableName, maxon::String *result)
 
Bool GenerateShaderCodeFromCache (const ParserCache *pdat, const maxon::String &resultVariableName, maxon::String *result)
 

Constructor & Destructor Documentation

◆ Parser()

Parser ( )
private

◆ ~Parser()

~Parser ( )
private

Member Function Documentation

◆ Alloc()

static Parser* Alloc ( void  )
static

Allocates a parser. Destroy the allocated parser with Free(). Use AutoAlloc to automate the allocation and destruction based on scope.

Returns
The allocated parser, or nullptr if the allocation failed.

◆ Free()

static void Free ( Parser *&  pr)
static

Destructs parsers allocated with Alloc(). Use AutoAlloc to automate the allocation and destruction based on scope.

Parameters
[in,out]prThe parser to destruct. If the pointer is nullptr nothing happens. The pointer is assigned nullptr afterwards.

◆ Eval()

Bool Eval ( const maxon::String str,
Int32 error,
Float res,
Int32  unit = 0,
Int32  angletype = 0,
Int32  basis = 10 
)

Evaluates a string using the variables added with AddVar() and the built-in expressions supported by Cinema 4D. (See the Formula object in the manual.)

Note
Calls internally Init() and Calculate(), or if the string has not changed ReEval().
Parameters
[in]strThe expression to evaluate.
[out]errorAssigned an error value if failed: NEWPARSERERROR
[out]resAssigned the result of the expression.
[in]unitThe unit used in the expression: UNIT
[in]angletypeThe angle unit used in the expression: ANGLE
[in]basisThe base of the number system used in the expression.
Returns
true if successful, otherwise false.

◆ EvalLong()

Bool EvalLong ( const maxon::String str,
Int32 error,
Int32 res,
Int32  unit,
Int32  basis 
)

Evaluates a string using the integer variables added with AddVarLong() and the built-in expressions supported by Cinema 4D. (See the Formula object in the manual.)

Note
Calls internally Init() and CalculateLong(), or if the string has not changed ReEvalLong().
Parameters
[in]strThe expression to evaluate.
[out]errorAssigned an error values if failed: NEWPARSERERROR
[out]resAssigned the result of the expression.
[in]unitThe unit used in the expression: UNIT
[in]basisThe base of the number system used in the expression.
Returns
true if successful, otherwise false.

◆ AddVar()

Bool AddVar ( const maxon::String str,
Float value,
Bool  case_sensitive = false 
)

Adds a variable to the parser to be evaluated.
Example:

Float vu;
for (i=0; i<1000; i++)
{
vu = i*0.2;
parser->AddVar("u",&vu,true);
parser->Eval("Sin(u)",...);
}
Parameters
[in]strThe name of the variable to add, this is the name that will be used in the expression string.
[in]valueAssigned the evaluated value for the variable.
[in]case_sensitivetrue if the variable string is case sensitive, otherwise false.
Returns
true if successful, otherwise false.

◆ RemoveVar()

Bool RemoveVar ( const maxon::String s,
Bool  case_sensitive = false 
)

Removes a variable from the parser evaluation.

Parameters
[in]sThe name of the variable to be removed.
[in]case_sensitivetrue if the variable string is case sensitive, otherwise false.
Returns
true if successful, otherwise false.

◆ RemoveAllVars()

Bool RemoveAllVars ( void  )

Remove all variables from the parser evaluation.

Returns
true if successful, otherwise false.

◆ GetParserData()

void GetParserData ( ParserCache p)

Retrieves the cached parser data.

Parameters
[out]pAssigned the parser cache. The caller owns the pointed parser cache.
This is used for instance by the Calculate() method. It makes possible to use multiple caches with one parser.

◆ Init()

Bool Init ( const maxon::String s,
Int32 error,
Int32  unit = 0,
Int32  angle_unit = 0,
Int32  base = 10 
)

Initializes the parser with an expression string.

Parameters
[in]sThe expression string for the initialization.
[out]errorAssigned an error value if failed: NEWPARSERERROR
[in]unitThe unit used in the expression: UNIT
[in]angle_unitThe angle unit used in the expression: ANGLE
[in]baseThe base of the number system used in the expression.
Returns
true if successful, otherwise false.

◆ ReEval()

Bool ReEval ( Float result,
Int32 error 
)

Re-evaluates the expression.

Warning
The expression string must not change prior to using this method.
Parameters
[out]resultAssigned the result of the expression.
[out]errorAssigned an error values if failed: NEWPARSERERROR
Returns
true if successful, otherwise false.

◆ Calculate()

Bool Calculate ( const ParserCache pdat,
Float result,
Int32 error 
)

Calculates the result for an expression in an existing parser cache.

See also
GetParserData()
Parameters
[in]pdatA parser cache.
[out]resultAssigned the result of the expression.
[out]errorAssigned an error values if failed: NEWPARSERERROR
Returns
true if successful, otherwise false.

◆ AddVarLong()

Bool AddVarLong ( const maxon::String str,
Int32 value,
Bool  case_sensitive = false 
)

Adds an integer variable to the parser to be evaluated. Example:

for (i=0; i<1000; i++)
{
parser->AddVar("i",&i,true);
parser->EvalLong("2*i",...);
}
Parameters
[in]strThe name of the variable to add, this is the name that will be used in the string.
[in]valueAssigned the evaluated value for the variable.
[in]case_sensitivetrue if the variable string is case sensitive, otherwise false.
Returns
true if successful, otherwise false.

◆ ReEvalLong()

Bool ReEvalLong ( Int32 result,
Int32 error 
)

Re-evaluates the expression.

Warning
The expression string must not change prior to using this method.
Parameters
[out]resultAssigned the result of the expression.
[out]errorAssigned an error values if failed: NEWPARSERERROR
Returns
true if successful, otherwise false.

◆ CalculateLong()

Bool CalculateLong ( const ParserCache pdat,
Int32 result,
Int32 error 
)

Calculates the result for an expression in an existing parser cache.

See also
GetParserData()
Parameters
[in]pdatA parser cache.
[out]resultAssigned the result of the expression.
[out]errorAssigned an error values if failed: NEWPARSERERROR
Returns
true if successful, otherwise false.

◆ Reset()

Bool Reset ( ParserCache p = nullptr)

Resets certain functions used by the parser (Random etc.).
This is necessary in animated situations, otherwise results will not be consistent for the same frame.

Parameters
[in]pThe parser cache to reset.
Returns
true if successful, otherwise false.

◆ GenerateShaderCode()

Bool GenerateShaderCode ( const maxon::String resultVariableName,
maxon::String result 
)

Returns an OpenGL Shading Language representation of the parsed expression.

Since
R17.032
Warning
Init() must be called before using this function, as it uses internally-cached data. Knowledge of the OpenGL Shading Language is required for correct use of this function.
Note
The generated OpenGL shader code assigns the result of the expression to a float variable with the given name (it must be a valid GLSL identifier).
Each expression variable name is converted to upper case before being inserted into the shader code.
Variable names with spaces are not supported and must be valid GLSL identifiers.
Declarations of these expression variables (such as those defined with AddVar()) must be added by the caller to the start of the code string and initialized with default values.
It is advisable to add braces around these declarations and the generated code so as to avoid naming conflicts.
Parameters
[in]resultVariableNameThe name of the variable in the shade code to which the result will be assigned.
[out]resultThe generated shader code.
Returns
true if successful, otherwise false.

◆ GenerateShaderCodeFromCache()

Bool GenerateShaderCodeFromCache ( const ParserCache pdat,
const maxon::String resultVariableName,
maxon::String result 
)

Returns an OpenGL Shading Language representation of the parsed expression.

Since
R17.032
Warning
Knowledge of the OpenGL Shading Language is required for correct use of this function.
Note
The generated OpenGL shader code assigns the result of the expression to a float variable with the given name (it must be a valid GLSL identifier).
Each expression variable name is converted to upper case before being inserted into the shader code.
Variable names with spaces are not supported and must be valid GLSL identifiers.
Declarations of these expression variables (such as those defined with AddVar()) must be added by the caller to the start of the code string and initialized with default values.
It is advisable to add braces around these declarations and the generated code so as to avoid naming conflicts.
Parameters
[in]pdatA parser cache.
[in]resultVariableNameThe name of the variable in the shade code to which the result will be assigned.
[out]resultThe generated shader code.
Returns
true if successful, otherwise false.