File: Operation.cpp

package info (click to toggle)
taskjuggler 2.3.0-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 8,764 kB
  • ctags: 3,700
  • sloc: cpp: 36,852; sh: 12,761; xml: 5,541; perl: 5,207; makefile: 269; python: 258; lisp: 67
file content (182 lines) | stat: -rw-r--r-- 4,161 bytes parent folder | download
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
177
178
179
180
181
182
/*
 * Operation.cpp - TaskJuggler
 *
 * Copyright (c) 2001, 2002, 2003, 2004 by Chris Schlaeger <cs@kde.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * $Id: Operation.cpp 1259 2006-01-31 12:04:00Z cs $
 */

#include "Operation.h"
#include "ExpressionTree.h"
#include "Utility.h"
#include "ExpressionFunctionTable.h"

Operation::Operation(const Operation& op)
{
    opt = op.opt;
    value = op.value;
    name = op.name;
    ops = 0;
    opsCount = op.opsCount;
    valid = FALSE;
    if (opsCount > 0)
    {
        ops = new Operation*[opsCount];
        for (int i = 0; i < opsCount; ++i)
            ops[i] = new Operation(*op.ops[i]);
    }
}

Operation::~Operation()
{
    for (int i = 0 ; i < opsCount; ++i)
        delete ops[i];
    delete [] ops;
}

long
Operation::evalAsInt(ExpressionTree* et) const
{
    switch (opt)
    {
    case Const:
        return value;
    case Variable:
    case Id:
        return et->resolve(name);
    case Function:
        return evalFunction(et);
    case Date:
        return value;
    case String:
        return name.toLong();
    case Not:
        return !ops[0]->evalAsInt(et);
    case And:
        return ops[0]->evalAsInt(et) && ops[1]->evalAsInt(et);
    case Or:
        return ops[0]->evalAsInt(et) || ops[1]->evalAsInt(et);
    case Greater:
        return ops[0]->evalAsInt(et) > ops[1]->evalAsInt(et);
    case Smaller:
        return ops[0]->evalAsInt(et) < ops[1]->evalAsInt(et);
    case Equal:
        return ops[0]->evalAsInt(et) == ops[1]->evalAsInt(et);
    case GreaterOrEqual:
        return ops[0]->evalAsInt(et) >= ops[1]->evalAsInt(et);
    case SmallerOrEqual:
        return ops[0]->evalAsInt(et) <= ops[1]->evalAsInt(et);
    default:
        qFatal("Operation::evalAsInt: "
               "Unknown opType %d (name: %s)", opt, name.ascii());
        return 0;
    }
}

time_t
Operation::evalAsTime(ExpressionTree* et) const
{
    switch(opt)
    {
    case Const:
    case Date:
        return value;
    case String:
        return date2time(name);
    case Variable:
    case Id:
        return et->resolve(name);
    case Function:
        return evalFunction(et);
    default:
        qFatal("Operation::evalAsTime: "
               "Unknown opType %d (name: %s)", opt, name.ascii());
        return 0;
    }
}

QString
Operation::evalAsString(ExpressionTree* et) const
{
    switch(opt)
    {
    case Const:
        return QString("%1").arg(value);
    case Function:
        return evalFunctionAsString(et);
    case Date:
        return time2date(value);
    case Id:
        return name;
    case String:
        return name;
    default:
        qFatal("Operation::evalAsString: "
               "Unknown opType %d (name: %s)", opt, name.ascii());
        return QString::null;
    }
}

long
Operation::evalFunction(ExpressionTree* et) const
{
    if (EFT.getFunction(name))
    {
        return EFT.getFunction(name)->longCall(et, ops);
    }
    else
        qFatal("Unknown function %s", name.data());

    return 0;
}

QString
Operation::evalFunctionAsString(ExpressionTree* ) const
{
    // There are no functions yet that return a string.
    return QString::null;
}

QString
Operation::debugString()
{
    QString res;
    switch(opt)
    {
        case Const:
            res.sprintf("Const:%ld", value);
            break;
        case Variable:
            res.sprintf("Variable:%s", name.latin1());
            break;
        case Function:
            res.sprintf("Function:%s", name.latin1());
            break;
        case Id:
            res.sprintf("Id:%s", name.latin1());
            break;
        case Date:
            res.sprintf("Date:%s", name.latin1());
            break;
        case String:
            res = name;
            break;
        case Not:
            res = "Not";
            break;
        case And:
            res = "And";
            break;
        case Or:
            res = "Or";
            break;
        default:
            res = "Unknown";
    }
    return res;
}