CpgExpression

CpgExpression — Mathematical expression evaluation

Synopsis

#define             CPG_EXPRESSION_CONST                (obj)
struct              CpgExpression;
struct              CpgExpressionClass;
typedef             CpgPropertyForward;
CpgExpression *     cpg_expression_new                  (const gchar *expression);
CpgExpression *     cpg_expression_copy                 (CpgExpression *expression);
const GSList *      cpg_expression_get_dependencies     (CpgExpression *expression);
const gchar *       cpg_expression_get_as_string        (CpgExpression *expression);
gboolean            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);
void                cpg_expression_reset_variadic       (CpgExpression *expression);
gboolean            cpg_expression_equal                (CpgExpression *expression,
                                                         CpgExpression *other);
void                cpg_expression_set_from_string      (CpgExpression *expression,
                                                         const gchar *value);
void                cpg_expression_reset_cache          (CpgExpression *expression);
gboolean            cpg_expression_get_has_cache        (CpgExpression *expression);
void                cpg_expression_set_has_cache        (CpgExpression *expression,
                                                         gboolean cache);
const GSList *      cpg_expression_get_instructions     (CpgExpression *expression);
void                cpg_expression_set_instructions     (CpgExpression *expression,
                                                         const GSList *instructions);
const GSList *      cpg_expression_get_operators        (CpgExpression *expression);
gboolean            cpg_expression_get_once             (CpgExpression *expression);
void                cpg_expression_set_once             (CpgExpression *expression,
                                                         gboolean instant);
gint                cpg_expression_get_error_at         (CpgExpression *expression);
                    CpgExpressionPrivate;

Object Hierarchy

  GObject
   +----GInitiallyUnowned
         +----CpgExpression

Implemented Interfaces

CpgExpression implements CpgModifiable.

Properties

  "expression"               gchar*                : Read / Write / Construct
  "has-cache"                gboolean              : Read / Write / Construct
  "value"                    gdouble               : Read / Write

Description

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.

Details

CPG_EXPRESSION_CONST()

#define CPG_EXPRESSION_CONST(obj)       (G_TYPE_CHECK_INSTANCE_CAST ((obj), CPG_TYPE_EXPRESSION, CpgExpression const))


struct CpgExpression

struct CpgExpression;


struct CpgExpressionClass

struct CpgExpressionClass {
};


CpgPropertyForward

typedef CPG_FORWARD_DECL (CpgProperty) CpgPropertyForward;


cpg_expression_new ()

CpgExpression *     cpg_expression_new                  (const gchar *expression);

Create a new CpgExpression containing the expression expression

expression :

an expression

Returns :

a new CpgExpression

cpg_expression_copy ()

CpgExpression *     cpg_expression_copy                 (CpgExpression *expression);

Create a copy of a CpgExpression.

expression :

A CpgExpression

Returns :

A CpgExpression. [transfer full]

cpg_expression_get_dependencies ()

const 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

expression :

a CpgExpression

Returns :

a list of CpgProperty. [element-type CpgProperty][transfer container]

cpg_expression_get_as_string ()

const gchar *       cpg_expression_get_as_string        (CpgExpression *expression);

Get the string representation of the expression

expression :

a CpgExpression

Returns :

the string representation of the expression

cpg_expression_compile ()

gboolean            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

expression :

a CpgExpression

context :

the evaluation context

error :

a GError

Returns :

TRUE if the expression compiled successfully, FALSE otherwise

cpg_expression_evaluate ()

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

expression :

a CpgExpression

Returns :

the result of evaluating the expression

cpg_expression_set_value ()

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

expression :

a CpgExpression

value :

a value

cpg_expression_reset ()

void                cpg_expression_reset                (CpgExpression *expression);

Resets all the expression flags (cache, instant)

expression :

a CpgExpression

cpg_expression_reset_variadic ()

void                cpg_expression_reset_variadic       (CpgExpression *expression);

Reset the cache of the variadic functions (such as rand()). You normally do not need to call this function directly.

expression :

A CpgExpression

cpg_expression_equal ()

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

expression :

a CpgExpression

other :

a CpgExpression

Returns :

TRUE if the expressions are equal, FALSE otherwise

cpg_expression_set_from_string ()

void                cpg_expression_set_from_string      (CpgExpression *expression,
                                                         const gchar *value);

Set a new expression for expression

expression :

a CpgExpression

value :

the value

cpg_expression_reset_cache ()

void                cpg_expression_reset_cache          (CpgExpression *expression);

Resets the possibly cached result of the value

expression :

a CpgExpression

cpg_expression_get_has_cache ()

gboolean            cpg_expression_get_has_cache        (CpgExpression *expression);


cpg_expression_set_has_cache ()

void                cpg_expression_set_has_cache        (CpgExpression *expression,
                                                         gboolean cache);


cpg_expression_get_instructions ()

const GSList *      cpg_expression_get_instructions     (CpgExpression *expression);

Get list of CpgInstruction. The list is owned by expression and should not be freed or modified

expression :

a CpgExpression

Returns :

list of CpgInstruction. [element-type CpgInstruction][transfer none]

cpg_expression_set_instructions ()

void                cpg_expression_set_instructions     (CpgExpression *expression,
                                                         const 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.

expression :

A CpgExpression

instructions :

A GSList of CpgInstruction. [element-type CpgInstruction][transfer full]

cpg_expression_get_operators ()

const GSList *      cpg_expression_get_operators        (CpgExpression *expression);


cpg_expression_get_once ()

gboolean            cpg_expression_get_once             (CpgExpression *expression);

Get whether the expression is only evaluated once.

expression :

A CpgExpression

Returns :

TRUE if the expression is only evaluated once, FALSE otherwise.

cpg_expression_set_once ()

void                cpg_expression_set_once             (CpgExpression *expression,
                                                         gboolean instant);

When an expression is "once", its value will not change.

expression :

A CpgExpression

instant :

Whether the expression should be constant

cpg_expression_get_error_at ()

gint                cpg_expression_get_error_at         (CpgExpression *expression);

Get the character position in the expression at which an error occurred while compiling the expression

expression :

A CpgExpression

Returns :

the character position at which an error occurred

CpgExpressionPrivate

typedef struct _CpgExpressionPrivate CpgExpressionPrivate;

Property Details

The "expression" property

  "expression"               gchar*                : Read / Write / Construct

Expression.

Default value: NULL


The "has-cache" property

  "has-cache"                gboolean              : Read / Write / Construct

Has cache.

Default value: TRUE


The "value" property

  "value"                    gdouble               : Read / Write

Value.

Default value: 0