Source: kalamaris/vartype.h
|
|
|
|
/* vartype.h -
This file is part of Kalamaris
Copyright (C) 2000 Antonio Larrosa Jimenez
Kalamaris' homepage : http://www.arrakis.es/~rlarrosa/kalamaris.html
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
Send comments and bug fixes to Antonio Larrosa <larrosa@kde.org>
***************************************************************************/
#include "garbage.h"
#ifndef _VARTYPE_H
#define _VARTYPE_H
class QString;
#define T_USE_DOUBLE
//#define T_USE_LONGDOUBLE
//#define T_USE_ARBITRARYLENGTH
#if defined(T_USE_DOUBLE)
#define T_DOUBLE double
#define T_IS_BUILTIN
#elif defined(T_USE_LONGDOUBLE)
#define T_DOUBLE long double
#define T_IS_BUILTIN
//#elif defined(T_USE_ARBITRARYLENGTH)
//#include <gmp.h>
//#define T_TYPE mpf_t
#else
#error "No data types defined for T !!!\n"
"Please uncomment one definition in the header of vartype.h\n"
#endif
#define CT_TYPEREF const T_DOUBLE &
class T
{
protected:
T_DOUBLE v;
static unsigned int m_decimalNumbers;
public:
T( const QString &s);
T( const T_DOUBLE g);
T( const int g);
T( const unsigned int g);
/**
* Copy constructor
*/
T( const T &x);
virtual T *copy(void) const;
virtual inline int operator==( const T &v1 );
virtual inline int operator==( CT_TYPEREF v1 );
virtual inline int operator!=( const T &v1 );
virtual inline int operator!=( CT_TYPEREF v1 );
virtual inline int operator<( const T &v1 );
virtual inline int operator<( CT_TYPEREF v1 );
virtual inline int operator>( const T &v1 );
virtual inline int operator>( CT_TYPEREF v1 );
virtual inline int operator>=( const T &v1 );
virtual inline int operator>=( CT_TYPEREF v1 );
virtual inline int operator<=( const T &v1 );
virtual inline int operator<=( CT_TYPEREF v1 );
virtual T &operator=( const T &x );
virtual T &operator=( CT_TYPEREF x );
virtual T &operator++( );
virtual T &operator++( int );
virtual T &operator--( );
virtual T &operator--( int );
virtual T &operator+=( const T &x );
virtual T &operator+=( const unsigned int i );
virtual T &operator+=( const int i );
virtual T &operator-=( const T &x );
virtual T &operator-=( const unsigned int i );
virtual T &operator-=( const int i );
virtual T &operator*=( const T &x );
virtual T &operator*=( const unsigned int i );
virtual T &operator*=( const int i );
virtual T &operator/=( const T &x );
virtual T &operator/=( const unsigned int i );
virtual T &operator/=( const int i );
virtual T &abs( void );
virtual T &sqrt( const double &idx=2.0 );
virtual T &sin( void );
virtual T &cos( void );
virtual T &tan( void );
virtual T &exp( void );
virtual T &log( void );
virtual T &log( const T &base );
/**
* These are operators that return a pointer to T instead of a reference,
* so that instead of adding the objects to the garbage collection, you can
* take care about them.
*/
static T *add(const T * m1, const T * m2);
static T *add(const T * m1, const unsigned int i);
static T *add(const T * m1, const int i);
static T *sub(const T * m1, const T * m2);
static T *sub(const T * m1, const unsigned int i);
static T *sub(const T * m1, const int i);
static T *mul(const T * m1, const T * m2);
static T *mul(const T * m1, const unsigned int i);
static T *mul(const T * m1, const int i);
static T *div(const T * m1, const T * m2);
static T *div(const T * m1, const unsigned int i);
static T *div(const T * m1, const int i);
static T *pow(const T * m1, const T * m2);
virtual operator const double () const;
inline CT_TYPEREF value() const { return v; };
virtual QString string(void) const;
virtual const char *isA() const { return "T"; };
/**
* XXX The next method is optimized as there's only T, TArbitraryPrecision
* and TMatrix data types. It should be changed when more types are added
*/
virtual bool isA(const char *str) const { return strlen(str)==1; };
static void setPrintDecimalNumbers(unsigned int i);
static unsigned int decimalNumbers(void) { return T::m_decimalNumbers; };
/**
* Returns a pointer to an object of the type the user preferred to use.
* This means that whenever you want to use a T object, instead of doing:
* {
* T a(anyparam);
* ...
* }
*
* You must do :
* {
* T &a=*T::newT(anyparam);
* ...
* T::deleteT(a);
* }
*
* This may looks as a showstopper, but it's needed in order to have much
* more powerful data types.
*/
static T *newT(const QString &s);
static T *newT(const double g);
static T *newT(const int g);
static T *newT(const unsigned int g);
static T *newT(const T &x);
/**
* This is a convenience method that work like the newT ones, but which
* inserts the object in the garbage collector and returns a reference to it
* This way, you can use:
*
* T &a=*T::newT(2.0); // a=2
* T &b=*T::newT(T::newTG(4.0)*a); // b=8
*
* Note that you don't have to call deleteT for these objects, you just
* have to call Garbage::collect() when they're no longer in use.
*/
static T &newTG(const double g);
static T &newTG(const int g);
static T &newTG(const unsigned int g);
enum TDataTypes { T_ , TArbitraryPrecision_ };
static void deleteT(T &x);
static void setDataType( TDataTypes t );
protected:
static TDataTypes TDataType;
};
// Operators
/**
* Operators
*
* These operators return a reference to an object which is in the garbage
* collection.
*/
/*inline T &operator+( const T &v1, const T &v2 );
inline T &operator-( const T &v1, const T &v2 );
inline T &operator*( const T &v1, const T &v2 );
inline T &operator/( const T &v1, const T &v2 );
*/
class Garbage;
inline T &operator+( const T &v1, const T &v2 )
{
T *tmp=T::add(&v1,&v2);
Garbage::throwT(tmp);
return *tmp;
}
inline T &operator-( const T &v1, const T &v2 )
{
T *tmp=T::sub(&v1,&v2);
Garbage::throwT(tmp);
return *tmp;
}
inline T &operator*( const T &v1, const T &v2 )
{
T *tmp=T::mul(&v1,&v2);
Garbage::throwT(tmp);
return *tmp;
}
inline T &operator/( const T &v1, const T &v2 )
{
T *tmp=T::div(&v1,&v2);
Garbage::throwT(tmp);
return *tmp;
}
inline T &operator+( const T &v1, unsigned int i )
{
T *tmp=T::add(&v1,i);
Garbage::throwT(tmp);
return *tmp;
}
inline T &operator-( const T &v1, unsigned int i )
{
T *tmp=T::sub(&v1,i);
Garbage::throwT(tmp);
return *tmp;
}
inline T &operator*( const T &v1, unsigned int i)
{
T *tmp=T::mul(&v1,i);
Garbage::throwT(tmp);
return *tmp;
}
inline T &operator/( const T &v1, unsigned int i )
{
T *tmp=T::div(&v1,i);
Garbage::throwT(tmp);
return *tmp;
}
inline T &operator+( const T &v1, int i )
{
T *tmp=T::add(&v1,i);
Garbage::throwT(tmp);
return *tmp;
}
inline T &operator-( const T &v1, int i )
{
T *tmp=T::sub(&v1,i);
Garbage::throwT(tmp);
return *tmp;
}
inline T &operator*( const T &v1, int i)
{
T *tmp=T::mul(&v1,i);
Garbage::throwT(tmp);
return *tmp;
}
inline T &operator/( const T &v1, int i )
{
T *tmp=T::div(&v1,i);
Garbage::throwT(tmp);
return *tmp;
}
#endif
Generated by: antlarr@terminus on Mon Jun 5 03:54:02 2000, using kdoc 2.0a22. |