Logo Search packages:      
Sourcecode: libqalculate version File versions  Download package

bool Calculator::calculate ( MathStructure mstruct,
string  str,
int  msecs,
const EvaluationOptions eo = default_evaluation_options,
MathStructure parsed_struct = NULL,
MathStructure to_struct = NULL,
bool  make_to_division = true 

Calculates an expression. The expression should be unlocalized first with unlocalizeExpression(). This function starts the calculation in a separate thread and will return when the calculation has started unless a maximum time has been specified. The calculation can then be stopped with abort().

[out] mstruct Math structure to fill with the result.
str Expression.
msecs The maximum time for the calculation in milliseconds. If msecs <= 0 the time will be unlimited.
eo Options for the evaluation and parsing of the expression.
[out] parsed_struct NULL or a math structure to fill with the result of the parsing of the expression.
[out] to_struct NULL or a math structure to fill with unit expression parsed after "to".
make_to_division If true, the expression after "to" will be interpreted as a unit epxression to convert the result to.
true if the calculation was successfully started (and finished if msecs > 0).

Definition at line 2103 of file Calculator.cc.

References abort(), saveState(), and MathStructure::set().

Referenced by RPNStackEnter().

      b_busy = true;
      if(calculate_thread_stopped) {
            pthread_create(&calculate_thread, &calculate_thread_attr, calculate_proc, calculate_pipe_r);
            calculate_thread_stopped = false;
      bool had_msecs = msecs > 0;
      expression_to_calculate = str;
      tmp_evaluationoptions = eo;
      tmp_proc_command = PROC_NO_COMMAND;
      tmp_rpn_mstruct = NULL;
      tmp_parsedstruct = parsed_struct;
      tmp_tostruct = to_struct;
      tmp_maketodivision = make_to_division;
      bool b_parse = true;
      fwrite(&b_parse, sizeof(bool), 1, calculate_pipe_w);
      void *x = (void*) mstruct;
      fwrite(&x, sizeof(void*), 1, calculate_pipe_w);
      struct timespec rtime;
      rtime.tv_sec = 0;
      rtime.tv_nsec = 1000000;
      while(msecs > 0 && b_busy) {
            nanosleep(&rtime, NULL);
            msecs -= 1;
      if(had_msecs && b_busy) {
            return false;
      return true;

Generated by  Doxygen 1.6.0   Back to index