SCalc
Public Member Functions | Protected Attributes | List of all members
SCalc::Session Class Reference

A class representing a whole session. More...

#include <session.hh>

Public Member Functions

 Session ()
 
 ~Session ()
 
ParserResulteval (const char *)
 Evaluates a string. More...
 
void eval_and_free (const char *)
 Evaluates a string and frees the result if possible. More...
 
int register_varname (const std::string &str)
 Registers a variable name and returns its number. More...
 
int register_varname (const char *str)
 
const char * varname (int i)
 Returns the name of variable number i.
 
std::vector< std::string > varnames ()
 Returns an array of variable names. More...
 
int nb_vars_defined ()
 Returns the number of currently defined variables.
 
int register_func_def (FuncDef *)
 
int replace_func_def (FuncDef *)
 
int get_func (std::string)
 Get the corresponding function number (or -1 if not defined).
 
int get_func (const char *name)
 Get the corresponding function number (or -1 if not defined).
 
FuncDefget_func_def (std::string)
 Get the function definition of the given name.
 
FuncDefget_func_def (const char *name)
 Overloaded function for convenience.
 
int nb_args_func (std::string)
 Returns the number of arguments of a function.
 
int nb_args_func (const char *name)
 Overloaded for convenience.
 
int nb_funcs ()
 The number of defined functions.
 
std::vector< std::string > func_names ()
 The function names.
 
Expressionconstant (double value)
 
Variables with 'default values'

A set of functions dealing with 'default values' of variables, that is variables which were assigned using the "a = 2" syntax, or manually using set_var().

int set_var (int, double)
 
int set_var (const char *var, double val)
 
int set_var (const std::string &var, double val)
 
int unset_var (int)
 
int unset_var (std::string varname)
 Unset a variable of the given name.
 
int unset_var (const char *varname)
 
int nb_vars_set ()
 Returns the number of variables that have a value.
 
std::set< int > vars_set ()
 
void fill_default (double *)
 
int evaluable (Expression *expr)
 

Protected Attributes

std::vector< std::string > variables
 The variables defined.
 
std::map< std::string, int > variables_numbers
 The cross reference to the variables.
 
std::map< int, double > values
 
std::map< std::string, int > functions_numbers
 
std::vector< FuncDef * > functions
 The function vector:
 

Detailed Description

A class representing a whole session.

The very class that keeps tracks of one session, such as declared function and variables. This is your main entry point to SCalc. Typical example:

SCalc::ParserResult r = sess.eval("1 + 1");
cout << "1 + 1 = " << r->to_expression()->evaluate() << endl;

The main function you'll be interested in is eval() to evaluate a string. It returns a ParserResult object which you can further interrogate and use.

Each Session holds its own set of variables and function definitions. It is probably a very bad idea to try to share function definitions across several different Session objects.

Constructor & Destructor Documentation

◆ Session()

SCalc::Session::Session ( )

Creates a new Session object and register common mathematical functions.

◆ ~Session()

SCalc::Session::~Session ( )

Destroys a Session object and frees associated function definitions.

Member Function Documentation

◆ constant()

Expression* SCalc::Session::constant ( double  value)

Creates a new Expression representing a simple constant value. Typical use:

// Multiply expr by two
Expression * newExpression = Expression::mul(session.constant(2),expr);

Referenced by nb_funcs().

◆ eval()

ParserResult* SCalc::Session::eval ( const char *  )

Evaluates a string.

eval() returns a ParserResult object, which needs to be checked and cast into the right object, using ParserResult::is_func_def(), ParserResult::is_expression() or ParserResult::is_syntax_error().

You are responsible for freeing the object if you don't need it but you must check that is it not in use (such as function definitions) using ParserResult::can_delete():

SCalc::ParserResult * r = sess.eval(some_string);
// do something here
if(r->can_delete())
delete r;

◆ eval_and_free()

void SCalc::Session::eval_and_free ( const char *  )

Evaluates a string and frees the result if possible.

This function evaluates a string and frees the result if that is possible. Of course, as the function returns void, it is not currently possible to tell fi anything went fine, so you might as well not want to use it. It is used by the test suite to setup functions.

◆ evaluable()

int SCalc::Session::evaluable ( Expression expr)

Tells if we have enough default variables to evaluate this expression without any other context.

Referenced by SCalc::Expression::evaluable(), and nb_vars_set().

◆ fill_default()

void SCalc::Session::fill_default ( double *  )

Fills the target array with the default values. Be careful: the argument has to have room for at least nb_vars_defined !

Referenced by nb_vars_set().

◆ register_func_def()

int SCalc::Session::register_func_def ( FuncDef )

Registers a function. Fails if a function is already registered under that name, or if the function is anonymous. All the registered functions are deleted when the Session is destroyed.

Referenced by nb_vars_set().

◆ register_varname()

int SCalc::Session::register_varname ( const std::string &  str)

Registers a variable name and returns its number.

Registers a new variable name, and returns its index in the array returned by varnames(). If the variable already exists, just return its number: no second registration is done.

Referenced by nb_vars_defined().

◆ replace_func_def()

int SCalc::Session::replace_func_def ( FuncDef )

Replaces a function definition. The former occupant of the place is simply deleted. We will need to ensure at some point that no trailing references are left. If the previous definition doesn't exist, simply calls register_func_def().

Referenced by nb_vars_set().

◆ set_var()

int SCalc::Session::set_var ( int  ,
double   
)

Sets the (default) value of a variable

Todo:
There should be a way to get the value of a variable.

Referenced by nb_vars_defined().

◆ unset_var()

int SCalc::Session::unset_var ( int  )

Unset a given variable. Returns 0 if the variable wasn't previously set.

Referenced by nb_vars_defined().

◆ varnames()

std::vector<std::string> SCalc::Session::varnames ( )
inline

Returns an array of variable names.

varnames() returns an array of variable names, in the position expected by SCalc::Expression::evaluate().

See also
register_varname(const char*)

References variables.

◆ vars_set()

std::set<int> SCalc::Session::vars_set ( )

Returns the set of the numbers of the variables which have a default value.

Referenced by nb_vars_set().

Member Data Documentation

◆ functions_numbers

std::map<std::string, int> SCalc::Session::functions_numbers
protected

The currently defined functions. It works this way: The integer is the rank of the corresponding FuncDef pointe in the functions vector. This is done so that user defined functions can override to some extends the ones that were previously defined...

◆ values

std::map<int, double> SCalc::Session::values
protected

(default) values of variables, that is values that were assigned using a "a = 1.2" syntax.


The documentation for this class was generated from the following file: