1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
/* This file is part of the KDE project
Copyright (C) 2003,2004 Ariya Hidayat <ariya@kde.org>
Copyright (C) 2005 Tomas Mecir <mecirt@gmail.com>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; only
version 2 of the License.
This library 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
// Local
#include "Function.h"
#include "Value.h"
using namespace Calligra::Sheets;
class Q_DECL_HIDDEN Function::Private
{
public:
QString name;
QString alternateName;
FunctionPtr ptr;
int paramMin, paramMax;
bool acceptArray;
bool ne; // need FunctionExtra* when called ?
};
Function::Function(const QString& name, FunctionPtr ptr)
: d(new Private)
{
d->name = name;
d->ptr = ptr;
d->acceptArray = false;
d->paramMin = 1;
d->paramMax = 1;
d->ne = false;
}
Function::~Function()
{
delete d;
}
QString Function::name() const
{
return d->name;
}
QString Function::alternateName() const
{
return d->alternateName;
}
void Function::setAlternateName(const QString &name)
{
d->alternateName = name;
}
void Function::setParamCount(int min, int max)
{
d->paramMin = min;
d->paramMax = (max == 0) ? min : max;
}
bool Function::paramCountOkay(int count)
{
// less than needed
if (count < d->paramMin) return false;
// no upper limit
if (d->paramMax == -1) return true;
// more than needed
if (count > d->paramMax) return false;
// okay otherwise
return true;
}
void Function::setAcceptArray(bool accept)
{
d->acceptArray = accept;
}
bool Function::needsExtra()
{
return d->ne;
}
void Function::setNeedsExtra(bool extra)
{
d->ne = extra;
}
Value Function::exec(valVector args, ValueCalc *calc, FuncExtra *extra)
{
// check number of parameters
if (!paramCountOkay(args.count()))
return Value::errorVALUE();
if (extra)
extra->function = this;
// do we need to perform array expansion ?
bool mustExpandArray = false;
if (!d->acceptArray)
for (int i = 0; i < args.count(); ++i) {
if (args[i].isArray())
mustExpandArray = true;
}
if (!d->ptr) return Value::errorVALUE();
// perform the actual array expansion if need be
if (mustExpandArray) {
// compute number of rows/cols of the result
int rows = 0;
int cols = 0;
for (int i = 0; i < args.count(); ++i) {
int x = 1;
if (extra) x = extra->ranges[i].rows();
if (x > rows) rows = x;
if (extra) x = extra->ranges[i].columns();
if (x > cols) cols = x;
}
// allocate the resulting array
Value res(Value::Array);
// perform the actual computation for each element of the array
for (int row = 0; row < rows; ++row)
for (int col = 0; col < cols; ++col) {
// fill in the parameter vector
valVector vals(args.count());
FuncExtra extra2 = *extra;
for (int i = 0; i < args.count(); ++i) {
int r = extra->ranges[i].rows();
int c = extra->ranges[i].columns();
vals[i] = args[i].isArray() ?
args[i].element(col % c, row % r) : args[i];
// adjust the FuncExtra structure to refer to the correct cells
extra2.ranges[i].col1 += col;
extra2.ranges[i].row1 += row;
extra2.ranges[i].col2 = extra2.ranges[i].col1;
extra2.ranges[i].row2 = extra2.ranges[i].row1;
}
// execute the function on each element
res.setElement(col, row, exec(vals, calc, &extra2));
}
return res;
} else
// call the function
return (*d->ptr)(args, calc, extra);
}
FunctionCaller::FunctionCaller(FunctionPtr ptr, const valVector &args, ValueCalc *calc, FuncExtra *extra)
: m_ptr(ptr), m_args(args), m_calc(calc), m_extra(extra)
{
}
Value FunctionCaller::exec()
{
return (*m_ptr)(m_args, m_calc, m_extra);
}
Value FunctionCaller::exec(const valVector &args)
{
return (*m_ptr)(args, m_calc, m_extra);
}
|