Statistics
| Branch: | Revision:

root / src / sim / clongparimpl.cc @ fbe00e73

History | View | Annotate | Download (4.11 KB)

1
//==========================================================================
2
//   CLONGPAR.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 "clongparimpl.h"
19
#include "cstringtokenizer.h"
20
#include "cdynamicexpression.h"
21
#include "ccomponent.h"
22

    
23
USING_NAMESPACE
24

    
25

    
26
cLongParImpl::cLongParImpl()
27
{
28
    val = 0;
29
}
30

    
31
cLongParImpl::~cLongParImpl()
32
{
33
    deleteOld();
34
}
35

    
36
void cLongParImpl::operator=(const cLongParImpl& 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 cLongParImpl::parsimPack(cCommBuffer *buffer)
50
{
51
    //TBD
52
}
53

    
54
void cLongParImpl::parsimUnpack(cCommBuffer *buffer)
55
{
56
    //TBD
57
}
58

    
59
void cLongParImpl::setBoolValue(bool b)
60
{
61
    throw cRuntimeError(this, eBADCAST, "bool", "int/long");
62
}
63

    
64
void cLongParImpl::setLongValue(long l)
65
{
66
    deleteOld();
67
    val = l;
68
    flags |= FL_CONTAINSVALUE | FL_ISSET;
69
}
70

    
71
void cLongParImpl::setDoubleValue(double d)
72
{
73
    deleteOld();
74
    val = double_to_long(d);
75
    flags |= FL_CONTAINSVALUE | FL_ISSET;
76
}
77

    
78
void cLongParImpl::setStringValue(const char *s)
79
{
80
    throw cRuntimeError(this, eBADCAST, "string", "int/long");
81
}
82

    
83
void cLongParImpl::setXMLValue(cXMLElement *node)
84
{
85
    throw cRuntimeError(this, eBADCAST, "XML", "int/long");
86
}
87

    
88
void cLongParImpl::setExpression(cExpression *e)
89
{
90
    deleteOld();
91
    expr = e;
92
    flags |= FL_ISEXPR | FL_CONTAINSVALUE | FL_ISSET;
93
}
94

    
95
bool cLongParImpl::boolValue(cComponent *) const
96
{
97
    throw cRuntimeError(this, eBADCAST, "int/long", "bool");
98
}
99

    
100
long cLongParImpl::longValue(cComponent *context) const
101
{
102
    return evaluate(context);
103
}
104

    
105
double cLongParImpl::doubleValue(cComponent *context) const
106
{
107
    return evaluate(context);
108
}
109

    
110
const char *cLongParImpl::stringValue(cComponent *) const
111
{
112
    throw cRuntimeError(this, eBADCAST, "int/long", "string");
113
}
114

    
115
std::string cLongParImpl::stdstringValue(cComponent *) const
116
{
117
    throw cRuntimeError(this, eBADCAST, "int/long", "string");
118
}
119

    
120
cXMLElement *cLongParImpl::xmlValue(cComponent *) const
121
{
122
    throw cRuntimeError(this, eBADCAST, "int/long", "XML");
123
}
124

    
125
cExpression *cLongParImpl::getExpression() const
126
{
127
    return (flags | FL_ISEXPR) ? expr : NULL;
128
}
129

    
130
long cLongParImpl::evaluate(cComponent *context) const
131
{
132
    return (flags & FL_ISEXPR) ? expr->longValue(context, getUnit()) : val;
133
}
134

    
135
void cLongParImpl::deleteOld()
136
{
137
    if (flags & FL_ISEXPR)
138
    {
139
        delete expr;
140
        flags &= ~FL_ISEXPR;
141
    }
142
}
143

    
144
cPar::Type cLongParImpl::getType() const
145
{
146
    return cPar::LONG;
147
}
148

    
149
bool cLongParImpl::isNumeric() const
150
{
151
    return true;
152
}
153

    
154
void cLongParImpl::convertToConst(cComponent *context)
155
{
156
    setLongValue(longValue(context));
157
}
158

    
159
std::string cLongParImpl::str() const
160
{
161
    if (flags & FL_ISEXPR)
162
        return expr->str();
163

    
164
    char buf[32];
165
    sprintf(buf, "%ld", val);
166
    return buf;
167
}
168

    
169
void cLongParImpl::parse(const char *text)
170
{
171
    // try parsing it as an expression
172
    cDynamicExpression *dynexpr = new cDynamicExpression();
173
    try
174
    {
175
        dynexpr->parse(text);
176
    }
177
    catch (std::exception& e)
178
    {
179
        delete dynexpr;
180
        throw;
181
    }
182
    setExpression(dynexpr);
183

    
184
    // simplify if possible: store as constant instead of expression
185
    if (dynexpr->isAConstant())
186
        convertToConst(NULL);
187
}
188

    
189
int cLongParImpl::compare(const cParImpl *other) const
190
{
191
    int ret = cParImpl::compare(other);
192
    if (ret!=0)
193
        return ret;
194

    
195
    const cLongParImpl *other2 = dynamic_cast<const cLongParImpl *>(other);
196
    if (flags & FL_ISEXPR)
197
        return expr->compare(other2->expr);
198
    else
199
        return (val == other2->val) ? 0 : (val < other2->val) ? -1 : 1;
200
}
201