Project

General

Profile

Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (4.29 KB)

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

    
24
USING_NAMESPACE
25

    
26

    
27
cXMLParImpl::cXMLParImpl()
28
{
29
    val = NULL;
30
}
31

    
32
cXMLParImpl::~cXMLParImpl()
33
{
34
    deleteOld();
35
}
36

    
37
void cXMLParImpl::operator=(const cXMLParImpl& other)
38
{
39
    if (this==&other) return;
40

    
41
    deleteOld();
42

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

    
50
std::string cXMLParImpl::detailedInfo() const
51
{
52
    return (flags & FL_ISEXPR) ? "<expression>" : val==NULL ? "NULL" : val->detailedInfo();
53
}
54

    
55
void cXMLParImpl::parsimPack(cCommBuffer *buffer)
56
{
57
    //TBD
58
}
59

    
60
void cXMLParImpl::parsimUnpack(cCommBuffer *buffer)
61
{
62
    //TBD
63
}
64

    
65
void cXMLParImpl::setBoolValue(bool b)
66
{
67
    throw cRuntimeError(this, eBADCAST, "bool", "XML");
68
}
69

    
70
void cXMLParImpl::setLongValue(long l)
71
{
72
    throw cRuntimeError(this, eBADCAST, "int/long", "XML");
73
}
74

    
75
void cXMLParImpl::setDoubleValue(double d)
76
{
77
    throw cRuntimeError(this, eBADCAST, "double", "XML");
78
}
79

    
80
void cXMLParImpl::setStringValue(const char *s)
81
{
82
    throw cRuntimeError(this, eBADCAST, "string", "XML");
83
}
84

    
85
void cXMLParImpl::setXMLValue(cXMLElement *node)
86
{
87
    deleteOld();
88
    val = node;
89
    flags |= FL_CONTAINSVALUE | FL_ISSET;
90
}
91

    
92
void cXMLParImpl::setExpression(cExpression *e)
93
{
94
    deleteOld();
95
    expr = e;
96
    flags |= FL_ISEXPR | FL_CONTAINSVALUE | FL_ISSET;
97
}
98

    
99
bool cXMLParImpl::boolValue(cComponent *) const
100
{
101
    throw cRuntimeError(this, eBADCAST, "XML", "bool");
102
}
103

    
104
long cXMLParImpl::longValue(cComponent *) const
105
{
106
    throw cRuntimeError(this, eBADCAST, "XML", "int/long");
107
}
108

    
109
double cXMLParImpl::doubleValue(cComponent *) const
110
{
111
    throw cRuntimeError(this, eBADCAST, "XML", "double");
112
}
113

    
114
const char *cXMLParImpl::stringValue(cComponent *) const
115
{
116
    throw cRuntimeError(this, eBADCAST, "XML", "string");
117
}
118

    
119
std::string cXMLParImpl::stdstringValue(cComponent *) const
120
{
121
    throw cRuntimeError(this, eBADCAST, "XML", "string");
122
}
123

    
124
cXMLElement *cXMLParImpl::xmlValue(cComponent *context) const
125
{
126
    return evaluate(context);
127
}
128

    
129
cExpression *cXMLParImpl::getExpression() const
130
{
131
    return (flags | FL_ISEXPR) ? expr : NULL;
132
}
133

    
134
cXMLElement *cXMLParImpl::evaluate(cComponent *context) const
135
{
136
    return (flags & FL_ISEXPR) ? expr->xmlValue(context) : val;
137
}
138

    
139
void cXMLParImpl::deleteOld()
140
{
141
    if (flags & FL_ISEXPR)
142
    {
143
        delete expr;
144
        flags &= ~FL_ISEXPR;
145
    }
146
}
147

    
148
cPar::Type cXMLParImpl::getType() const
149
{
150
    return cPar::XML;
151
}
152

    
153
bool cXMLParImpl::isNumeric() const
154
{
155
    return false;
156
}
157

    
158
void cXMLParImpl::convertToConst(cComponent *context)
159
{
160
    setXMLValue(xmlValue(context));
161
}
162

    
163
std::string cXMLParImpl::str() const
164
{
165
    if (flags & FL_ISEXPR)
166
        return expr->str();
167

    
168
    if (val)
169
        return std::string("<")+val->getTagName()+"> from "+val->getSourceLocation();
170
    else
171
        return std::string("NULL");
172
}
173

    
174
void cXMLParImpl::parse(const char *text)
175
{
176
    // try parsing it as an expression
177
    cDynamicExpression *dynexpr = new cDynamicExpression();
178
    try
179
    {
180
        dynexpr->parse(text);
181
    }
182
    catch (std::exception& e)
183
    {
184
        delete dynexpr;
185
        throw;
186
    }
187
    setExpression(dynexpr);
188

    
189
    // simplify if possible: store as constant instead of expression
190
    if (dynexpr->isAConstant())
191
        convertToConst(NULL);
192
}
193

    
194
int cXMLParImpl::compare(const cParImpl *other) const
195
{
196
    int ret = cParImpl::compare(other);
197
    if (ret!=0)
198
        return ret;
199

    
200
    const cXMLParImpl *other2 = dynamic_cast<const cXMLParImpl *>(other);
201
    if (flags & FL_ISEXPR)
202
        return expr->compare(other2->expr);
203
    else
204
        return (val == other2->val) ? 0 : (val < other2->val) ? -1 : 1;
205
}
206