kspread

bitops.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE project
00002    Copyright (C) 1998-2002 The KSpread Team <koffice-devel@kde.org>
00003    Copyright (C) 2006 Brad Hards <bradh@frogmouth.net>
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; only
00008    version 2 of the License.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018    Boston, MA 02110-1301, USA.
00019 */
00020 
00021 // built-in logical functions
00022 
00023 #include "BitOpsModule.h"
00024 #include "FunctionModuleRegistry.h"
00025 #include "Functions.h"
00026 #include "ValueCalc.h"
00027 #include "ValueConverter.h"
00028 
00029 #include <KGenericFactory>
00030 #include <KLocale>
00031 
00032 using namespace KSpread;
00033 
00034 // prototypes (sorted alphabetically)
00035 Value func_bitand (valVector args, ValueCalc *calc, FuncExtra *);
00036 Value func_bitor (valVector args, ValueCalc *calc, FuncExtra *);
00037 Value func_bitxor (valVector args, ValueCalc *calc, FuncExtra *);
00038 Value func_bitlshift (valVector args, ValueCalc *calc, FuncExtra *);
00039 Value func_bitrshift (valVector args, ValueCalc *calc, FuncExtra *);
00040 
00041 #ifndef KSPREAD_UNIT_TEST // Do not create/export the plugin in unit tests.
00042 K_PLUGIN_FACTORY(BitOpsModulePluginFactory,
00043                  registerPlugin<BitOpsModule>();
00044                 )
00045 K_EXPORT_PLUGIN(BitOpsModulePluginFactory("BitOpsModule"))
00046 #endif
00047 
00048 BitOpsModule::BitOpsModule(QObject* parent, const QVariantList&)
00049     : FunctionModule(parent, "bitops", i18n("Bit Operation Functions"))
00050 {
00051 }
00052 
00053 QString BitOpsModule::descriptionFileName() const
00054 {
00055     return QString("bitops.xml");
00056 }
00057 
00058 void BitOpsModule::registerFunctions()
00059 {
00060   FunctionRepository* repo = FunctionRepository::self();
00061   Function *f;
00062 
00063   f = new Function ("BITAND", func_bitand);
00064   f->setParamCount (2);
00065   repo->add (f);
00066   f = new Function ("BITOR", func_bitor);
00067   f->setParamCount (2);
00068   repo->add (f);
00069   f = new Function ("BITXOR", func_bitxor);
00070   f->setParamCount (2);
00071   repo->add (f);
00072   f = new Function ("BITLSHIFT", func_bitlshift);
00073   f->setParamCount (2);
00074   repo->add (f);
00075   f = new Function ("BITRSHIFT", func_bitrshift);
00076   f->setParamCount (2);
00077   repo->add (f);
00078 }
00079 
00080 void BitOpsModule::removeFunctions()
00081 {
00082     // NOTE: The group name has to match the one in the xml description.
00083     FunctionRepository::self()->remove("Bit Operations");
00084 }
00085 
00086 
00087 // Function: BITAND
00088 Value func_bitand (valVector args, ValueCalc *, FuncExtra *)
00089 {
00090   const quint64 x = args[0].asInteger();
00091   const quint64 y = args[1].asInteger();
00092   return Value( static_cast<qint64>( x & y ) );
00093 }
00094 
00095 // Function: BITOR
00096 Value func_bitor (valVector args, ValueCalc *, FuncExtra *)
00097 {
00098   const quint64 x = args[0].asInteger();
00099   const quint64 y = args[1].asInteger();
00100   return Value( static_cast<qint64>( x | y ) );
00101 }
00102 
00103 // Function: BITXOR
00104 Value func_bitxor (valVector args, ValueCalc *, FuncExtra *)
00105 {
00106   const quint64 x = args[0].asInteger();
00107   const quint64 y = args[1].asInteger();
00108   return Value( static_cast<qint64>( x ^ y ) );
00109 }
00110 
00111 // Function: BITLSHIFT
00112 Value func_bitlshift (valVector args, ValueCalc *, FuncExtra *)
00113 {
00114   const quint64 x = args[0].asInteger();
00115   const int numshift = args[1].asInteger();
00116   if ( numshift == 0 )
00117       return Value( static_cast<qint64>( x ) );
00118   else if ( numshift > 0 )
00119       return Value( static_cast<qint64>( x << numshift ) );
00120   else // negative left shift, becomes right shift
00121       return Value( static_cast<qint64>( x >> ( -1 * numshift ) ) );
00122 }
00123 
00124 // Function: BITRSHIFT
00125 Value func_bitrshift (valVector args, ValueCalc *, FuncExtra *)
00126 {
00127   const quint64 x = args[0].asInteger();
00128   const int numshift = args[1].asInteger();
00129   if ( numshift == 0 )
00130       return Value( static_cast<qint64>( x ) );
00131   else if ( numshift > 0 )
00132       return Value( static_cast<qint64>( x >> numshift ) );
00133   else // negative right shift, becomes left shift
00134       return Value( static_cast<qint64>( x << ( -1 * numshift ) ) );
00135 }
00136 
00137 #include "BitOpsModule.moc"