Statistics
| Branch: | Revision:

root / src / sim / cproperties.cc @ fbe00e73

History | View | Annotate | Download (3.93 KB)

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

12
  This file is distributed WITHOUT ANY WARRANTY. See the file
13
  `license' for details on this and other legal matters.
14
*--------------------------------------------------------------*/
15
16
#include <sstream>
17
#include <algorithm>
18
#include "cproperties.h"
19
#include "cproperty.h"
20
#include "stringutil.h"
21
22
USING_NAMESPACE
23
24
25
cProperties::cProperties()
26
{
27
    islocked = false;
28
    refcount = 0;
29
}
30
31
cProperties::~cProperties()
32
{
33
    for (int i = 0; i < (int)propv.size(); i++)
34
        delete propv[i];
35
}
36
37
cProperties& cProperties::operator=(const cProperties& other)
38
{
39
    if (islocked)
40
        throw cRuntimeError(this, eLOCKED);
41
42
    // note: do NOT copy islocked flag
43
44
    // delete existing contents
45
    for (int i = 0; i < (int)propv.size(); i++)
46
        delete propv[i];
47
    propv.clear();
48
49
    // copy properties from other
50
    for (int i = 0; i < (int)other.propv.size(); i++)
51
    {
52
        cProperty *p = other.propv[i]->dup();
53
        propv.push_back(p);
54
    }
55
    return *this;
56
}
57
58
std::string cProperties::info() const
59
{
60
    if (propv.empty())
61
        return "";
62
    std::stringstream out;
63
    //out << "size=" << propv.size();
64
    for (int i = 0; i < (int)propv.size(); i++)
65
        out << (i==0 ? "" : " ") << propv[i]->info();
66
    return out.str();
67
}
68
69
void cProperties::parsimPack(cCommBuffer *buffer)
70
{
71
    // TBD
72
}
73
74
void cProperties::parsimUnpack(cCommBuffer *buffer)
75
{
76
    if (islocked)
77
        throw cRuntimeError(this, eLOCKED);
78
    // TBD
79
}
80
81
cProperty *cProperties::get(int k) const
82
{
83
    if (k < 0 || k >= (int)propv.size())
84
        throw cRuntimeError(this, "property index %d out of range", k);
85
    return propv[k];
86
}
87
88
cProperty *cProperties::get(const char *name, const char *index) const
89
{
90
    for (int i = 0; i < (int)propv.size(); i++)
91
        if (!strcmp(propv[i]->getName(), name) && !opp_strcmp(index, propv[i]->getIndex()))
92
            return propv[i];
93
    return NULL;
94
}
95
96
bool cProperties::getAsBool(const char *name, const char *index) const
97
{
98
    cProperty *prop = get(name, index);
99
    if (!prop)
100
        return false;
101
    const char *value = prop->getValue(cProperty::DEFAULTKEY, 0);
102
    if (!opp_isempty(value) && strcmp(value, "true")!=0 && strcmp(value, "false")!=0)
103
        throw cRuntimeError(this, "@%s property: boolean value expected, got '%s'", name, value);
104
105
    return opp_strcmp(value, "false")==0 ? false : true;
106
}
107
108
void cProperties::add(cProperty *p)
109
{
110
    if (islocked)
111
        throw cRuntimeError(this, eLOCKED);
112
    propv.push_back(p);
113
    p->setOwner(this);
114
}
115
116
void cProperties::remove(int k)
117
{
118
    if (islocked)
119
        throw cRuntimeError(this, eLOCKED);
120
121
    if (k < 0 || k >= (int)propv.size())
122
        throw cRuntimeError(this, "property index %d out of range", k);
123
124
    delete propv[k];
125
    propv.erase(propv.begin()+k);
126
}
127
128
const std::vector<const char *> cProperties::getNames() const
129
{
130
    std::vector<const char *> v;
131
    for (int i = 0; i < (int)propv.size(); i++)
132
    {
133
        const char *s = propv[i]->getName();
134
        if (std::find(v.begin(), v.end(), s) == v.end())
135
            v.push_back(s);
136
    }
137
    return v;
138
}
139
140
std::vector<const char *> cProperties::getIndicesFor(const char *name) const
141
{
142
    std::vector<const char *> v;
143
    for (int i = 0; i < (int)propv.size(); i++)
144
    {
145
        if (!strcmp(propv[i]->getName(), name))
146
        {
147
            const char *s = propv[i]->getIndex();
148
            if (std::find(v.begin(), v.end(), s) == v.end())
149
                v.push_back(s);
150
        }
151
    }
152
    return v;
153
}
154
155
void cProperties::lock()
156
{
157
    islocked = true;
158
    for (int i = 0; i < (int)propv.size(); i++)
159
        propv[i]->lock();
160
}
161