File indexing completed on 2024-05-12 16:35:27

0001 /* This file is part of the KDE project
0002    Copyright (C) 1998-2002 The KSpread Team <calligra-devel@kde.org>
0003    Copyright (C) 2006 Brad Hards <bradh@frogmouth.net>
0004 
0005    This library is free software; you can redistribute it and/or
0006    modify it under the terms of the GNU Library General Public
0007    License as published by the Free Software Foundation; only
0008    version 2 of the License.
0009 
0010    This library is distributed in the hope that it will be useful,
0011    but WITHOUT ANY WARRANTY; without even the implied warranty of
0012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013    Library General Public License for more details.
0014 
0015    You should have received a copy of the GNU Library General Public License
0016    along with this library; see the file COPYING.LIB.  If not, write to
0017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018    Boston, MA 02110-1301, USA.
0019 */
0020 
0021 // built-in logical functions
0022 
0023 #include "BitOpsModule.h"
0024 
0025 #include "Function.h"
0026 #include "FunctionModuleRegistry.h"
0027 #include "ValueCalc.h"
0028 #include "ValueConverter.h"
0029 
0030 using namespace Calligra::Sheets;
0031 
0032 // prototypes (sorted alphabetically)
0033 Value func_bitand(valVector args, ValueCalc *calc, FuncExtra *);
0034 Value func_bitor(valVector args, ValueCalc *calc, FuncExtra *);
0035 Value func_bitxor(valVector args, ValueCalc *calc, FuncExtra *);
0036 Value func_bitlshift(valVector args, ValueCalc *calc, FuncExtra *);
0037 Value func_bitrshift(valVector args, ValueCalc *calc, FuncExtra *);
0038 
0039 
0040 CALLIGRA_SHEETS_EXPORT_FUNCTION_MODULE("kspreadbitopsmodule.json", BitOpsModule)
0041 
0042 
0043 BitOpsModule::BitOpsModule(QObject* parent, const QVariantList&)
0044         : FunctionModule(parent)
0045 {
0046     Function *f;
0047 
0048     f = new Function("BITAND", func_bitand);
0049     f->setParamCount(2);
0050     add(f);
0051     f = new Function("BITOR", func_bitor);
0052     f->setParamCount(2);
0053     add(f);
0054     f = new Function("BITXOR", func_bitxor);
0055     f->setParamCount(2);
0056     add(f);
0057     f = new Function("BITLSHIFT", func_bitlshift);
0058     f->setParamCount(2);
0059     add(f);
0060     f = new Function("BITRSHIFT", func_bitrshift);
0061     f->setParamCount(2);
0062     add(f);
0063 }
0064 
0065 QString BitOpsModule::descriptionFileName() const
0066 {
0067     return QString("bitops.xml");
0068 }
0069 
0070 
0071 // Function: BITAND
0072 Value func_bitand(valVector args, ValueCalc *, FuncExtra *)
0073 {
0074     const quint64 x = args[0].asInteger();
0075     const quint64 y = args[1].asInteger();
0076     return Value(static_cast<qint64>(x & y));
0077 }
0078 
0079 // Function: BITOR
0080 Value func_bitor(valVector args, ValueCalc *, FuncExtra *)
0081 {
0082     const quint64 x = args[0].asInteger();
0083     const quint64 y = args[1].asInteger();
0084     return Value(static_cast<qint64>(x | y));
0085 }
0086 
0087 // Function: BITXOR
0088 Value func_bitxor(valVector args, ValueCalc *, FuncExtra *)
0089 {
0090     const quint64 x = args[0].asInteger();
0091     const quint64 y = args[1].asInteger();
0092     return Value(static_cast<qint64>(x ^ y));
0093 }
0094 
0095 // Function: BITLSHIFT
0096 Value func_bitlshift(valVector args, ValueCalc *, FuncExtra *)
0097 {
0098     const quint64 x = args[0].asInteger();
0099     const int numshift = args[1].asInteger();
0100     if (numshift == 0)
0101         return Value(static_cast<qint64>(x));
0102     else if (numshift > 0)
0103         return Value(static_cast<qint64>(x << numshift));
0104     else // negative left shift, becomes right shift
0105         return Value(static_cast<qint64>(x >>(-1 * numshift)));
0106 }
0107 
0108 // Function: BITRSHIFT
0109 Value func_bitrshift(valVector args, ValueCalc *, FuncExtra *)
0110 {
0111     const quint64 x = args[0].asInteger();
0112     const int numshift = args[1].asInteger();
0113     if (numshift == 0)
0114         return Value(static_cast<qint64>(x));
0115     else if (numshift > 0)
0116         return Value(static_cast<qint64>(x >> numshift));
0117     else // negative right shift, becomes left shift
0118         return Value(static_cast<qint64>(x << (-1 * numshift)));
0119 }
0120 
0121 #include "bitops.moc"