kspread Library API Documentation

functions.cc

00001 /* This file is part of the KDE project
00002    Copyright (C) 2003,2004 Ariya Hidayat <ariya@kde.org>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00017    Boston, MA 02111-1307, USA.
00018 */
00019 
00020 #include "formula.h"
00021 #include "functions.h"
00022 #include "valueconverter.h"
00023 
00024 #include <qdict.h>
00025 #include <qvaluevector.h>
00026 
00027 #include <kstaticdeleter.h>
00028 
00029 #include <math.h>
00030 
00031 namespace KSpread
00032 {
00033 
00034 class Function::Private
00035 {
00036 public:
00037   QString name;
00038   FunctionPtr ptr;
00039 };
00040 
00041 class FunctionRepository::Private
00042 {
00043 public:
00044   QDict<Function> functions;
00045 };
00046 
00047 } // namespace KSpread
00048 
00049 
00050 using namespace KSpread;
00051 
00052 Function::Function( const QString& name, FunctionPtr ptr )
00053 {
00054   d = new Private;
00055   d->name = name;
00056   d->ptr = ptr;
00057 }
00058 
00059 Function::~Function()
00060 {
00061   delete d;
00062 }
00063 
00064 QString Function::name() const
00065 {
00066   return d->name;
00067 }
00068 
00069 KSpreadValue Function::exec( const Formula* formula, QValueVector<KSpreadValue> args )
00070 {
00071   if( !d->ptr ) return KSpreadValue::errorVALUE();
00072   return (*d->ptr)( formula, args );
00073 }
00074 
00075 static KStaticDeleter<FunctionRepository> fr_sd;
00076 FunctionRepository* FunctionRepository::s_self = 0;
00077 
00078 FunctionRepository* FunctionRepository::self()
00079 {
00080   if( !s_self )
00081     fr_sd.setObject( s_self, new FunctionRepository() );
00082     
00083   return s_self;
00084 }
00085 
00086 KSpreadValue function_sin( const Formula* formula, QValueVector<KSpreadValue> args )
00087 {
00088 /*
00089   (Tomas:)
00090   Commented out as we have no locale available. The way used to handle
00091   locale pointers needs to be changed, we now have three zillions pointers
00092   all pointing to the same object...
00093   Also, we don't have the converter ready ... That should be done by the
00094   repository, before giving the call to us
00095   
00096   KSpreadValue result;
00097   if( args.count() != 1 )
00098     return KSpreadValue::errorVALUE();
00099     
00100   KSpreadValue angle = converter()->asFloat( args[0] );
00101   if( angle.isError() )
00102     return KSpreadValue::errorVALUE();
00103   
00104   return KSpreadValue( sin( angle.asFloat() ) );
00105 */
00106   return KSpreadValue::errorVALUE();
00107 }
00108 
00109 FunctionRepository::FunctionRepository()
00110 {
00111   d = new Private;
00112   
00113   d->functions.setAutoDelete( true );
00114   
00115   // registerMathFunction
00116   
00117   add( new Function( "SIN", function_sin ) );
00118   
00119   // load desc/help from XML file
00120 }
00121 
00122 FunctionRepository::~FunctionRepository()
00123 {
00124   delete d;
00125 }
00126 
00127 void FunctionRepository::add( Function* function )
00128 {
00129   if( !function ) return;
00130   d->functions.insert( function->name().upper(), function );
00131 }
00132 
00133 Function* FunctionRepository::function( const QString& name )
00134 {
00135   return d->functions.find( name.upper() );
00136 }
KDE Logo
This file is part of the documentation for kspread Library Version 1.4.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Mon Feb 13 09:42:44 2006 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003