File: BaseObject.cpp

package info (click to toggle)
atlas-cpp 0.5.98-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,652 kB
  • ctags: 2,284
  • sloc: sh: 8,305; cpp: 6,372; python: 1,471; makefile: 216
file content (326 lines) | stat: -rw-r--r-- 8,922 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// This file may be redistributed and modified only under the terms of
// the GNU Lesser General Public License (See COPYING for details).
// Copyright (C) 2000-2001 Stefanus Du Toit and Aloril

#include <Atlas/Objects/BaseObject.h>

using Atlas::Message::Element;
using Atlas::Message::MapType;

namespace Atlas { namespace Objects {

NoSuchAttrException::~NoSuchAttrException() throw ()
{
}

BaseObjectData::~BaseObjectData()
{
    assert( m_refCount==0 );
}

bool BaseObjectData::instanceOf(int classNo) const
{
    return BASE_OBJECT_NO == classNo;
}

bool BaseObjectData::hasAttr(const std::string& name) const
{
    int flag = getAttrFlag(name);
    if (flag >= 0) {
        return m_attrFlags & flag;
    } else {
        return (m_attributes.find(name) != m_attributes.end());
    }
}

bool BaseObjectData::hasAttrFlag(int flag) const
{
    return m_attrFlags & flag;
}

const Element BaseObjectData::getAttr(const std::string& name) const
    throw (NoSuchAttrException) 
{
    Element attr;
    if (copyAttr(name, attr) != 0) {
        throw NoSuchAttrException(name);
    }
    return attr;
}

int BaseObjectData::copyAttr(const std::string& name, Element & attr) const
{
    MapType::const_iterator I = m_attributes.find(name);
    if (I == m_attributes.end()) {
        return -1;
    };
    attr = I->second;
    return 0;
}

void BaseObjectData::setAttr(const std::string& name, const Element& attr)
{
    m_attributes[name] = attr;
}

void BaseObjectData::removeAttr(const std::string& name)
{
    m_attributes.erase(name);
}

const MapType BaseObjectData::asMessage() const
{
    MapType m;
    addToMessage(m);
    return m;
}

void BaseObjectData::addToMessage(MapType & m) const
{
    typedef MapType::const_iterator Iter;
    for (Iter I = m_attributes.begin(); I != m_attributes.end(); I++) {
        m[I->first] = I->second;
    }
}

void BaseObjectData::sendContents(Bridge & b) const
{
    Message::Encoder e(b);
    typedef MapType::const_iterator Iter;
    for (Iter I = m_attributes.begin(); I != m_attributes.end(); I++) {
        e.mapElementItem((*I).first, (*I).second);
    }
}

int BaseObjectData::getAttrClass(const std::string& name) const
{
    return -1;
}

int BaseObjectData::getAttrFlag(const std::string& name) const
{
    return -1;
}

void BaseObjectData::iterate(int& current_class, std::string& attr) const
{
    // m_attributes is handled separately, and we have no other attributes,
    // so we set the iterator to be at the end of the attributes

    current_class = BASE_OBJECT_NO;
    attr = "";
}

BaseObjectData::iterator::iterator(BaseObjectData& obj, int current_class)
    : m_obj(&obj), m_current_class(current_class), m_val("", *this)
{
    // invalid argument means start at the top
    m_I = (current_class < 0) ? m_obj->m_attributes.begin() : m_obj->m_attributes.end();

    // could have m_attributes.begin() == m_attributes.end(), need to check
    if(m_I != m_obj->m_attributes.end())
        m_val.first = m_I->first;
    else
        m_obj->iterate(m_current_class, m_val.first); // get first named attribute
}

// don't set m_val.second
BaseObjectData::iterator& BaseObjectData::iterator::operator=(const iterator& I)
{
    m_obj = I.m_obj;
    m_current_class = I.m_current_class;
    m_I = I.m_I;
    m_val.first = I.m_val.first;
    return *this;
}

// We go through the attributes in the top class
// first and work our way down, since the virtual iterate() function
// works that way.
BaseObjectData::iterator& BaseObjectData::iterator::operator++() // preincrement
{
    if(!m_obj)
        return *this;

    // Since we start with the top class first,
    // we always want attributes that are not
    // named in a particular class to come before
    // those that are. The attributes in m_attributes
    // are not named in any class, so they come first

    if(m_I != m_obj->m_attributes.end()) {
        ++m_I;
        if(m_I != m_obj->m_attributes.end()) {
            m_val.first = m_I->first;
            return *this;
        }
        // end of m_attributes, clear m_val.first so we can start on the rest
        m_val.first = "";
    }

    // we're through m_attributes, so we go on to the named
    // attributes in the classes

    m_obj->iterate(m_current_class, m_val.first);

    return *this;
}

bool BaseObjectData::iterator::operator==(const iterator& I) const
{
    if(m_obj != I.m_obj)
        return false;
    if(m_obj == 0) // ignore other arguments
        return true;

    if(m_I != I.m_I)
        return false;
    if(m_I != m_obj->m_attributes.end()) // ignore other arguments
        return true;

    assert(m_current_class >= 0); // we must be in the class arguments by now
    return m_current_class == I.m_current_class && m_val.first == I.m_val.first;
}

// FIXME figure out some way to use m_current_class to keep from
// having to call the virtual getAttr()/setAttr() in the toplevel class

BaseObjectData::iterator::PsuedoElement::operator Message::Element() const
{
    return (m_I.m_I != m_I.m_obj->m_attributes.end()) ?
	m_I.m_I->second : m_I.m_obj->getAttr(m_I.m_val.first);
}

const BaseObjectData::iterator::PsuedoElement& BaseObjectData::iterator::PsuedoElement::operator=(const Message::Element& val) const
{
    if(m_I.m_I != m_I.m_obj->m_attributes.end())
        m_I.m_I->second = val;
    else
        m_I.m_obj->setAttr(m_I.m_val.first, val);

    return *this;
}

BaseObjectData::const_iterator::const_iterator(const BaseObjectData& obj,
					       int current_class)
    : m_obj(&obj), m_current_class(current_class), m_val("", *this)
{
    // invalid argument means start at the top
    m_I = (current_class < 0) ? m_obj->m_attributes.begin() : m_obj->m_attributes.end();

    // could have m_attributes.begin() == m_attributes.end(), need to check
    if(m_I != m_obj->m_attributes.end())
        m_val.first = m_I->first;
    else
        m_obj->iterate(m_current_class, m_val.first); // get first named attribute
}

// don't set m_val.second
BaseObjectData::const_iterator& BaseObjectData::const_iterator::operator=(const const_iterator& I)
{
    m_obj = I.m_obj;
    m_current_class = I.m_current_class;
    m_I = I.m_I;
    m_val.first = I.m_val.first;
    return *this;
}

// We go through the attributes in the top class
// first and work our way down, since the virtual iterate() function
// works that way.
BaseObjectData::const_iterator& BaseObjectData::const_iterator::operator++() // preincrement
{
    if(!m_obj)
        return *this;

    // Since we start with the top class first,
    // we always want attributes that are not
    // named in a particular class to come before
    // those that are. The attributes in m_attributes
    // are not named in any class, so they come first

    if(m_I != m_obj->m_attributes.end()) {
        ++m_I;
        if(m_I != m_obj->m_attributes.end()) {
            m_val.first = m_I->first;
            return *this;
        }
        // end of m_attributes, clear m_val.first so we can start on the rest
        m_val.first = "";
    }

    // we're through m_attributes, so we go on to the named
    // attributes in the classes

    m_obj->iterate(m_current_class, m_val.first);

    return *this;
}

bool BaseObjectData::const_iterator::operator==(const const_iterator& I) const
{
    if(m_obj != I.m_obj)
        return false;
    if(m_obj == 0) // ignore other arguments
        return true;

    if(m_I != I.m_I)
        return false;
    if(m_I != m_obj->m_attributes.end()) // ignore other arguments
        return true;

    assert(m_current_class >= 0); // we must be in the class arguments by now
    return m_current_class == I.m_current_class && m_val.first == I.m_val.first;
}

// FIXME figure out some way to use m_current_class to keep from
// having to call the virtual getAttr()/setAttr() in the toplevel class

BaseObjectData::const_iterator::PsuedoElement::operator Message::Element() const
{
    return (m_I.m_I != m_I.m_obj->m_attributes.end()) ?
	m_I.m_I->second : m_I.m_obj->getAttr(m_I.m_val.first);
}

BaseObjectData::iterator BaseObjectData::find(const std::string& name)
{
  iterator I;
  I.m_obj = this;
  I.m_val.first = name;

  I.m_I = m_attributes.find(name);
  if (I.m_I != m_attributes.end()) 
    I.m_current_class = -1;
  else {
    I.m_current_class = getAttrClass(name);
    if(I.m_current_class < 0) { // no such attribute
      I.m_current_class = BASE_OBJECT_NO;
      I.m_val.first = "";
    }
  }

  return I;
}

BaseObjectData::const_iterator BaseObjectData::find(const std::string& name) const
{
  const_iterator I;
  I.m_obj = this;
  I.m_val.first = name;

  I.m_I = m_attributes.find(name);
  if (I.m_I != m_attributes.end()) 
    I.m_current_class = -1;
  else {
    I.m_current_class = getAttrClass(name);
    if(I.m_current_class < 0) { // no such attribute
      I.m_current_class = BASE_OBJECT_NO;
      I.m_val.first = "";
    }
  }

  return I;
}


} } // namespace Atlas::Objects