Project

General

Profile

Statistics
| Branch: | Revision:

root / src / sim / cboolparimpl.cc @ 3e29b8a0

History | View | Annotate | Download (4.27 KB)

1 01873262 Georg Kunz
//==========================================================================
2
//   CBOOLPAR.CC  - part of
3
//                     OMNeT++/OMNEST
4
//            Discrete System Simulation in C++
5
//
6
//  Author: Andras Varga
7
//
8
//==========================================================================
9
10
/*--------------------------------------------------------------*
11
  Copyright (C) 1992-2008 Andras Varga
12
  Copyright (C) 2006-2008 OpenSim Ltd.
13

14
  This file is distributed WITHOUT ANY WARRANTY. See the file
15
  `license' for details on this and other legal matters.
16
*--------------------------------------------------------------*/
17
18
#include "cboolparimpl.h"
19
#include "cstringtokenizer.h"
20
#include "cdynamicexpression.h"
21
#include "ccomponent.h"
22
23
USING_NAMESPACE
24
25
26
cBoolParImpl::cBoolParImpl()
27
{
28
    val = false;
29
}
30
31
cBoolParImpl::~cBoolParImpl()
32
{
33
    deleteOld();
34
}
35
36
void cBoolParImpl::operator=(const cBoolParImpl& other)
37
{
38
    if (this==&other) return;
39
40
    deleteOld();
41
42
    cParImpl::operator=(other);
43
    if (flags & FL_ISEXPR)
44
        expr = (cExpression *) other.expr->dup();
45
    else
46
        val = other.val;
47
}
48
49
void cBoolParImpl::parsimPack(cCommBuffer *buffer)
50
{
51
    //TBD
52
}
53
54
void cBoolParImpl::parsimUnpack(cCommBuffer *buffer)
55
{
56
    //TBD
57
}
58
59
void cBoolParImpl::setBoolValue(bool b)
60
{
61
    deleteOld();
62
    val = b;
63
    flags |= FL_CONTAINSVALUE | FL_ISSET;
64
}
65
66
void cBoolParImpl::setLongValue(long l)
67
{
68
    throw cRuntimeError(this, eBADCAST, "int/long", "double");
69
}
70
71
void cBoolParImpl::setDoubleValue(double d)
72
{
73
    throw cRuntimeError(this, eBADCAST, "double", "bool");
74
}
75
76
void cBoolParImpl::setStringValue(const char *s)
77
{
78
    throw cRuntimeError(this, eBADCAST, "string", "bool");
79
}
80
81
void cBoolParImpl::setXMLValue(cXMLElement *node)
82
{
83
    throw cRuntimeError(this, eBADCAST, "XML", "bool");
84
}
85
86
void cBoolParImpl::setExpression(cExpression *e)
87
{
88
    deleteOld();
89
    expr = e;
90
    flags |= FL_ISEXPR | FL_CONTAINSVALUE | FL_ISSET;
91
}
92
93
bool cBoolParImpl::boolValue(cComponent *context) const
94
{
95
    return evaluate(context);
96
}
97
98
long cBoolParImpl::longValue(cComponent *) const
99
{
100
    throw cRuntimeError(this, eBADCAST, "bool", "int/long");
101
}
102
103
double cBoolParImpl::doubleValue(cComponent *) const
104
{
105
    throw cRuntimeError(this, eBADCAST, "bool", "double");
106
}
107
108
const char *cBoolParImpl::stringValue(cComponent *) const
109
{
110
    throw cRuntimeError(this, eBADCAST, "bool", "string");
111
}
112
113
std::string cBoolParImpl::stdstringValue(cComponent *) const
114
{
115
    throw cRuntimeError(this, eBADCAST, "bool", "string");
116
}
117
118
cXMLElement *cBoolParImpl::xmlValue(cComponent *) const
119
{
120
    throw cRuntimeError(this, eBADCAST, "bool", "XML");
121
}
122
123
cExpression *cBoolParImpl::getExpression() const
124
{
125
    return (flags | FL_ISEXPR) ? expr : NULL;
126
}
127
128
bool cBoolParImpl::evaluate(cComponent *context) const
129
{
130
    return (flags & FL_ISEXPR) ? expr->boolValue(context) : val;
131
}
132
133
void cBoolParImpl::deleteOld()
134
{
135
    if (flags & FL_ISEXPR)
136
    {
137
        delete expr;
138
        flags &= ~FL_ISEXPR;
139
    }
140
}
141
142
cPar::Type cBoolParImpl::getType() const
143
{
144
    return cPar::BOOL;
145
}
146
147
bool cBoolParImpl::isNumeric() const
148
{
149
    return false;  // because doubleValue() and longValue() throws error
150
}
151
152
void cBoolParImpl::convertToConst(cComponent *context)
153
{
154
    setBoolValue(boolValue(context));
155
}
156
157
std::string cBoolParImpl::str() const
158
{
159
    if (flags & FL_ISEXPR)
160
        return expr->str();
161
    return val ? "true" : "false";
162
}
163
164
void cBoolParImpl::parse(const char *text)
165
{
166
    // shortcut: recognize "true" and "false"
167
    if (strcmp(text, "true")==0 || strcmp(text, "false")==0)
168
    {
169
        setBoolValue(text[0]=='t');
170
        return;
171
    }
172
173
    // try parsing it as an expression
174
    cDynamicExpression *dynexpr = new cDynamicExpression();
175
    try
176
    {
177
        dynexpr->parse(text);
178
    }
179
    catch (std::exception& e)
180
    {
181
        delete dynexpr;
182
        throw;
183
    }
184
    setExpression(dynexpr);
185
186
    // simplify if possible: store as constant instead of expression
187
    if (dynexpr->isAConstant())
188
        convertToConst(NULL);
189
}
190
191
int cBoolParImpl::compare(const cParImpl *other) const
192
{
193
    int ret = cParImpl::compare(other);
194
    if (ret!=0)
195
        return ret;
196
197
    const cBoolParImpl *other2 = dynamic_cast<const cBoolParImpl *>(other);
198
    if (flags & FL_ISEXPR)
199
        return expr->compare(other2->expr);
200
    else
201
        return (val == other2->val) ? 0 : (val < other2->val) ? -1 : 1;
202
}