Statistics
| Branch: | Revision:

root / src / sim / cstringparimpl.cc @ 08285dff

History | View | Annotate | Download (4.43 KB)

1
//==========================================================================
2
//   CSTRINGPAR.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 "cstringparimpl.h"
19
#include "cdynamicexpression.h"
20
#include "ccomponent.h"
21
#include "stringutil.h"
22

    
23
USING_NAMESPACE
24

    
25

    
26
cStringParImpl::cStringParImpl()
27
{
28
}
29

    
30
cStringParImpl::~cStringParImpl()
31
{
32
    deleteOld();
33
}
34

    
35
void cStringParImpl::operator=(const cStringParImpl& other)
36
{
37
    if (this==&other) return;
38

    
39
    deleteOld();
40

    
41
    cParImpl::operator=(other);
42
    if (flags & FL_ISEXPR)
43
        expr = (cExpression *) other.expr->dup();
44
    else
45
        val = other.val;
46
}
47

    
48
void cStringParImpl::parsimPack(cCommBuffer *buffer)
49
{
50
    //TBD
51
}
52

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

    
58
void cStringParImpl::setBoolValue(bool b)
59
{
60
    throw cRuntimeError(this, eBADCAST, "bool", "string");
61
}
62

    
63
void cStringParImpl::setLongValue(long l)
64
{
65
    throw cRuntimeError(this, eBADCAST, "int/long", "string");
66
}
67

    
68
void cStringParImpl::setDoubleValue(double d)
69
{
70
    throw cRuntimeError(this, eBADCAST, "double", "string");
71
}
72

    
73
void cStringParImpl::setStringValue(const char *s)
74
{
75
    deleteOld();
76
    val = (s ? s : "");
77
    flags |= FL_CONTAINSVALUE | FL_ISSET;
78
}
79

    
80
void cStringParImpl::setXMLValue(cXMLElement *node)
81
{
82
    throw cRuntimeError(this, eBADCAST, "XML", "string");
83
}
84

    
85
void cStringParImpl::setExpression(cExpression *e)
86
{
87
    deleteOld();
88
    expr = e;
89
    flags |= FL_ISEXPR | FL_CONTAINSVALUE | FL_ISSET;
90
}
91

    
92
bool cStringParImpl::boolValue(cComponent *context) const
93
{
94
    throw cRuntimeError(this, eBADCAST, "string", "bool");
95
}
96

    
97
long cStringParImpl::longValue(cComponent *) const
98
{
99
    throw cRuntimeError(this, eBADCAST, "string", "int/long");
100
}
101

    
102
double cStringParImpl::doubleValue(cComponent *) const
103
{
104
    throw cRuntimeError(this, eBADCAST, "string", "double");
105
}
106

    
107
const char *cStringParImpl::stringValue(cComponent *context) const
108
{
109
    if (flags & FL_ISEXPR)
110
        throw cRuntimeError(this, "stringValue() and conversion to `const char *' cannot be invoked "
111
                                  "on parameters declared `volatile string' in NED -- use stdstringValue() "
112
                                  "or conversion to `std::string' instead.");
113
    return val.c_str();
114
}
115

    
116
std::string cStringParImpl::stdstringValue(cComponent *context) const
117
{
118
    return evaluate(context);
119
}
120

    
121
cXMLElement *cStringParImpl::xmlValue(cComponent *) const
122
{
123
    throw cRuntimeError(this, eBADCAST, "string", "XML");
124
}
125

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

    
131
std::string cStringParImpl::evaluate(cComponent *context) const
132
{
133
    return (flags & FL_ISEXPR) ? expr->stringValue(context) : val;
134
}
135

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

    
145
cPar::Type cStringParImpl::getType() const
146
{
147
    return cPar::STRING;
148
}
149

    
150
bool cStringParImpl::isNumeric() const
151
{
152
    return false;
153
}
154

    
155
void cStringParImpl::convertToConst(cComponent *context)
156
{
157
    setStringValue(stdstringValue(context).c_str());
158
}
159

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

    
165
    return opp_quotestr(val.c_str());
166
}
167

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

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

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

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