![]() |
![]() |
![]() |
cpg-network Reference Manual | ![]() |
---|---|---|---|---|
Top | Description |
#include <cpg-network/cpg-expression.h> CpgInstruction; enum CpgInstructionCode; enum CpgInstructionBinding; CpgInstructionProperty; CpgExpression; CpgExpression * cpg_expression_new (gchar const *expression); GSList * cpg_expression_get_dependencies (CpgExpression *expression); const gchar * cpg_expression_get_as_string (CpgExpression *expression); gint cpg_expression_compile (CpgExpression *expression, CpgCompileContext *context, GError **error); gdouble cpg_expression_evaluate (CpgExpression *expression); void cpg_expression_set_value (CpgExpression *expression, gdouble value); void cpg_expression_reset (CpgExpression *expression); gboolean cpg_expression_equal (CpgExpression *expression, CpgExpression *other); void cpg_expression_set_from_string (CpgExpression *expression, gchar const *value); void cpg_expression_reset_cache (CpgExpression *expression); GSList * cpg_expression_get_instructions (CpgExpression *expression); gboolean cpg_expression_set_instructions (CpgExpression *expression, GSList *instructions); CpgInstruction * cpg_instruction_function_new (guint id, gchar const *name, gint arguments, gboolean variable); CpgInstruction * cpg_instruction_custom_function_new (CpgFunction *function, gint arguments); CpgInstruction * cpg_instruction_number_new (gdouble value); CpgInstruction * cpg_instruction_operator_new (guint id, gchar const *name, gint arguments); CpgInstruction * cpg_instruction_property_new (CpgProperty *property, CpgInstructionBinding binding); CpgInstruction * cpg_instruction_copy (CpgInstruction *instruction); void cpg_instruction_free (CpgInstruction *instruction); gchar * cpg_instruction_to_string (CpgInstruction *instruction);
A CpgExpression contains a mathematical expression. The expression in string format can be compiled and evaluated. At the compilation phase, a list of CpgObject is provided as a context in which variables are mapped to CpgProperty in this context.
typedef enum { CPG_INSTRUCTION_TYPE_NONE, CPG_INSTRUCTION_TYPE_FUNCTION, CPG_INSTRUCTION_TYPE_NUMBER, CPG_INSTRUCTION_TYPE_OPERATOR, CPG_INSTRUCTION_TYPE_PROPERTY, CPG_INSTRUCTION_TYPE_CUSTOM_FUNCTION } CpgInstructionCode;
Enum used to indicate instruction type
typedef enum { CPG_INSTRUCTION_BINDING_NONE = 0, CPG_INSTRUCTION_BINDING_FROM, CPG_INSTRUCTION_BINDING_TO, } CpgInstructionBinding;
typedef struct { CpgInstruction parent; CPG_FORWARD_DECL (CpgProperty) *property; CpgInstructionBinding binding; } CpgInstructionProperty;
CpgExpression * cpg_expression_new (gchar const *expression);
Create a new CpgExpression containing the expression expression
|
an expression |
Returns : |
a new CpgExpression |
GSList * cpg_expression_get_dependencies (CpgExpression *expression);
Get a list of CpgProperty on which the expression depends. The list is owned
by expression
and should not be freed or modified
|
a CpgExpression |
Returns : |
a list of CpgProperty |
const gchar * cpg_expression_get_as_string (CpgExpression *expression);
Get the string representation of the expression
|
a CpgExpression |
Returns : |
the string representation of the expression |
gint cpg_expression_compile (CpgExpression *expression, CpgCompileContext *context, GError **error);
Compile the expression. The context is a list of CpgObject from which
properties can be looked up (such as global constants, or from/to objects).
If there were any errors during compilation, error
will be set accordingly
|
a CpgExpression |
|
the evaluation context |
|
a GError |
Returns : |
TRUE if the expression compiled successfully, FALSE otherwise
|
gdouble cpg_expression_evaluate (CpgExpression *expression);
Get the result of evaluating the expression. If the expression is not yet compiled, 0.0 is returned. The result of the evaluation is cached in the expression. Make sure to call cpg_expression_reset_cache to clear the cache if needed
|
a CpgExpression |
Returns : |
the result of evaluating the expression |
void cpg_expression_set_value (CpgExpression *expression, gdouble value);
Sets the cached/instant value of an expression. If the expression is reset, this value will no longer be used and the expression will be evaluated as normal
|
a CpgExpression |
|
a value |
void cpg_expression_reset (CpgExpression *expression);
Resets all the expression flags (cache, instant)
|
a CpgExpression |
gboolean cpg_expression_equal (CpgExpression *expression, CpgExpression *other);
Get whether two expressions are equal. If the expressions are compiled, they are evaluated for equality by means of their instructions. Otherwise the comparison is done on their string representations
|
a CpgExpression |
|
a CpgExpression |
Returns : |
TRUE if the expressions are equal, FALSE otherwise
|
void cpg_expression_set_from_string (CpgExpression *expression, gchar const *value);
Set a new expression for expression
|
a CpgExpression |
|
the value |
void cpg_expression_reset_cache (CpgExpression *expression);
Resets the possibly cached result of the value
|
a CpgExpression |
GSList * cpg_expression_get_instructions (CpgExpression *expression);
Get list of CpgInstruction. The list is owned by expression
and should
not be freed or modified
|
a CpgExpression |
Returns : |
list of CpgInstruction |
gboolean cpg_expression_set_instructions (CpgExpression *expression, GSList *instructions);
Set the instructions used to evaluate the expression. You should never have to use this function. It's main purpose is for optimization of expressions in cpgrawc.
|
A CpgExpression |
|
A GSList of CpgInstruction |
Returns : |
TRUE if the new instruction set is valid, FALSE otherwise
|
CpgInstruction * cpg_instruction_function_new (guint id, gchar const *name, gint arguments, gboolean variable);
Creates a new function call instruction. The id id
of the instruction
specifies which function the instruction represents (see
CpgMathFunctionType). If variable
is TRUE
, the instruction expects the
first value on the stack, when the instruction is executed, to be the number
of arguments to process.
|
function id |
|
function name |
|
the number of arguments this function takes |
|
if the number of arguments is variable for this function |
Returns : |
the new CpgInstruction |
CpgInstruction * cpg_instruction_custom_function_new (CpgFunction *function, gint arguments);
Creates a new custom function call instruction. If the number of arguments
(arguments
) is not equal to the number of arguments function
takes, the
instruction expects the first value on the stack, when the instruction is
executed, to be the number of arguments to process.
|
A CpgFunction |
|
The number of arguments this function takes |
Returns : |
A CpgInstruction |
CpgInstruction * cpg_instruction_number_new (gdouble value);
Creates a new number instruction. This is the most basic instruction, which simply pushes a number onto the stack when executed.
|
the numeric value |
Returns : |
the new CpgInstruction |
CpgInstruction * cpg_instruction_operator_new (guint id, gchar const *name, gint arguments);
Creates a new operator instruction. The id id
of the instruction
specifies which operator the instruction represents (see
CpgMathOperatorType).
|
operator id |
|
operator name |
|
the number of arguments this operator takes |
Returns : |
the new CpgInstruction |
CpgInstruction * cpg_instruction_property_new (CpgProperty *property, CpgInstructionBinding binding);
Creates a new property call instruction. When the instruction is executed, the property value expression is evaluated and its return value is pushed on the stack.
|
a CpgProperty |
|
the property binding |
Returns : |
the new CpgInstruction |
CpgInstruction * cpg_instruction_copy (CpgInstruction *instruction);
Create a copy of a CpgInstruction
|
a CpgInstruction |
Returns : |
a new CpgInstruction |
void cpg_instruction_free (CpgInstruction *instruction);
Free previously created CpgInstruction object
|
a CpgInstruction |
gchar * cpg_instruction_to_string (CpgInstruction *instruction);
Get the string representation of an instruction.
|
A CpgInstruction |
Returns : |
The string representation of the instruction. |