ttcalc/src/parsermanager.h

195 lines
5.3 KiB
C
Raw Normal View History

/*
* This file is a part of TTCalc - a mathematical calculator
* and is distributed under the (new) BSD licence.
* Author: Tomasz Sowa <t.sowa@slimaczek.pl>
*/
/*
* Copyright (c) 2006-2007, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name Tomasz Sowa nor the names of contributors to this
* project may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef headerfilecalculation
#define headerfilecalculation
/*!
\file parsermanager.h
\brief object of type ParserManager we're using during calculating
*/
#include "resource.h"
#include <ttmath/ttmathparser.h>
#include "programresources.h"
#include <windows.h>
/*!
\brief object of type ParserManager we're using during calculating
In our program we're using three kind of precisions. First is the smallest
, and the third is the biggest. Because precision is established during
compilation (templates) we need three different objects. ParserManager
helps us to maintain these objects.
*/
class ParserManager
{
public:
ParserManager();
ParserManager(const ParserManager & p);
ParserManager & operator=(const ParserManager & p);
~ParserManager();
/*!
the main method which call parserX.Parse(...)
*/
ttmath::ErrorCode Parse();
/*
it returns the last error code which was during parsing
*/
ttmath::ErrorCode GetLastCode();
/*!
we call this method directly after when we have left
WaitForCalculatingAndBlockForStop() method from the ThreadController
only in this method we can read variables which can be changed
by the first thread
*/
void MakeCopyOfVariables();
/*!
it prepares our three parsers to work, it should be called only once
*/
void Init();
/*!
this method prints result
(a correct value if was or an error instead)
*/
void PrintResult();
private:
ttmath::Parser<ttmath::Big<1,3> > parser1;
ttmath::Parser<ttmath::Big<2,6> > parser2;
ttmath::Parser<ttmath::Big<3,9> > parser3;
ttmath::Objects variables, functions;
const unsigned int buffer_len;
char * buffer;
int base_input, base_output;
bool always_scientific;
int when_scientific;
int rounding;
int precision;
Languages::Country country;
ttmath::ErrorCode code;
template<class ValueType>
void PrintResult(ttmath::Parser<ValueType> & matparser)
{
std::string result, part;
unsigned int i = 0;
for(i=0 ; i<matparser.stack.size() ; ++i)
{
try
{
if( matparser.stack[i].value.ToString(part,base_output, always_scientific, when_scientific, rounding) )
{
// we shouldn't have this error in the new version of ToStrign(...)
// (it could be if were using very big mantissa and exponent
// in the Big type)
//
// look at this: part.erase()
//
// we're using it because it's something wrong on gcc
// when we're compiling with -O3 optimization flag
//
// gcc version 3.4.2 (mingw-special) with -O3
// program tell us if there was an error here (from ToString())
// but in fact it shouldn't have been here
// I don't know where is the problem (I think in optimization,
// with -O1 and -O2 program works well and visual express works perfectly
// as well)
part.erase();
// we can omit this part.erase() and use operator+= instead of
// operator= below, for example:
// part += GetPrgRes()->GetLanguages()->GuiMessage(country, Languages::overflow_during_printing);
// will be working correctly on gcc with -O3
// this error doesn't appear always, it can be seen, for example,
// if we use Big<1,3> type and we give '2^32' for calculating
// I didn't check this error on a new version of gcc
part = GetPrgRes()->GetLanguages()->GuiMessage(country, Languages::overflow_during_printing);
}
}
catch(...)
{
part.erase();
part = GetPrgRes()->GetLanguages()->ErrorMessage(country, ttmath::err_internal_error);
}
result += part;
if( i < matparser.stack.size()-1 )
result += " ";
}
SetDlgItemText(GetPrgRes()->GetMainWindow(),IDC_OUTPUT_EDIT,result.c_str());
}
};
#endif