File: Element.h

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 (511 lines) | stat: -rw-r--r-- 12,186 bytes parent folder | download | duplicates (3)
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
// 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, Karsten-O. Laux and Al Riddoch

#ifndef ATLAS_MESSAGE_ELEMENT_H
#define ATLAS_MESSAGE_ELEMENT_H

#include <Atlas/Exception.h>

#include <string>
#include <map>
#include <vector>

namespace Atlas { namespace Message {

/// An exception class issued when the wrong type is requested in as().
class WrongTypeException : public Atlas::Exception
{
  public:
    WrongTypeException() : Atlas::Exception("Wrong Message::Element type") { }
};

class Element;

typedef long IntType;
typedef double FloatType;
typedef void * PtrType;
typedef std::string StringType;
typedef std::map<std::string, Element> MapType;
typedef std::vector<Element> ListType;

/** Multi-type container
 *
 * FIXME: Document this
 *
 * @author Stefanus Du Toit <sdt@gmx.net>
 *
 * Changes:
 *
 *   2003/04/02   Al Riddcoh <alriddoch@zepler.org>
 *                Add in some assignment operators for efficiency
 *   2002/11/07   Al Riddcoh <alriddoch@zepler.org>
 *                Changed the name to Element as Object is a stupid name
 *                for a class.
 *   2000/08/05   Karsten-O. Laux <klaux@rhrk.uni-kl.de>
 *                Changed the members to pointers which only get created when
 *                really needed. This is a major speedup for passing
 *                Object as parameter or when copying it.
 *                Because copying of unused members is omitted.  
 *                All pointers are stored as a union, so we save memory !
 *                Changed IntType to long and added convinience Constructors
 *                for float, int and bool
 *                


 */
class Element
{
public:
    enum Type {
        TYPE_NONE,
        TYPE_INT,
        TYPE_FLOAT,
        TYPE_PTR,
        TYPE_STRING,
        TYPE_MAP,
        TYPE_LIST
    };

private:
    // These are now legacy typedefs. New code should use the
    // Atlas::Message::*Type versions.
    typedef Atlas::Message::IntType IntType;
    typedef Atlas::Message::FloatType FloatType;
    typedef Atlas::Message::PtrType PtrType;
    typedef Atlas::Message::StringType StringType;
    typedef Atlas::Message::MapType MapType;
    typedef Atlas::Message::ListType ListType;

    /// Clear all values.
    void clear(Type new_type = TYPE_NONE);

public:
    /// Construct an empty object.
    Element()
      : t(TYPE_NONE)
    {
    }

    ///
    ~Element()
    {
        clear();
    }

    /// Copy an existing object.
    Element(const Element& obj);

    /// Set type to int, and value to v.
    Element(int v)
      : t(TYPE_INT), i(v)
    {
    }

    /// Set type to int, and value to v.
    Element(bool v)
      : t(TYPE_INT), i(v)
    {
    }

    /// Set type to int, and value to v.
    Element(IntType v)
      : t(TYPE_INT), i(v)
    {
    }

    /// Set type to double, and value to v.
    Element(float v)
      : t(TYPE_FLOAT), f(v)
    {
    }   

    /// Set type to double, and value to v.
    Element(FloatType v)
      : t(TYPE_FLOAT), f(v)
    {
    }

    /// Set type to PtrType, and value to v.
    Element(PtrType v)
      : t(TYPE_PTR), p(v)
    {
    }

    /// Set type to std::string, and value to v.
    Element(const char* v)
      : t(TYPE_STRING)
    {
      if(v)
        s = new DataType<StringType>(v);
      else
        s = new DataType<StringType>();
    }

    /// Set type to std::string, and value to v.
    Element(const StringType& v)
      : t(TYPE_STRING)
    {
      s = new DataType<StringType>(v);
    }
    /// Set type to MapType, and value to v.
    Element(const MapType& v)
      : t(TYPE_MAP)
    {
      m = new DataType<MapType>(v);
    }
    /// Set type to ListType, and value to v.
    Element(const ListType& v)
      : t(TYPE_LIST)
    {
      l = new DataType<ListType>(v);
    }

    /// overload assignment operator !
    Element& operator=(const Element& obj);

    Element& operator=(int v) 
    {
      if (TYPE_INT != t)
      {
        clear(TYPE_INT);
      }
      i = v;
      return *this;
    }

    Element& operator=(bool v) 
    {
      if (TYPE_INT != t)
      {
        clear(TYPE_INT);
      }
      i = v;
      return *this;
    }

    Element& operator=(IntType v) 
    {
      if (TYPE_INT != t)
      {
        clear(TYPE_INT);
      }
      i = v;
      return *this;
    }

    Element& operator=(float v) 
    {
      if (TYPE_FLOAT != t)
      {
        clear(TYPE_FLOAT);
      }
      f = v;
      return *this;
    }

    Element& operator=(FloatType v) 
    {
      if (TYPE_FLOAT != t)
      {
        clear(TYPE_FLOAT);
      }
      f = v;
      return *this;
    }

    Element& operator=(PtrType v) 
    {
      if (TYPE_PTR != t)
      {
        clear(TYPE_PTR);
      }
      p = v;
      return *this;
    }

    Element& operator=(const char * v) 
    {
      if (TYPE_STRING != t || !s->unique())
      {
        clear(TYPE_STRING);
        s = new DataType<StringType>(v);
      } else {
        *s = StringType(v);
      }
      return *this;
    }

    Element& operator=(const StringType & v) 
    {
      if (TYPE_STRING != t || !s->unique())
      {
        clear(TYPE_STRING);
        s = new DataType<StringType>(v);
      } else {
        *s = v;
      }
      return *this;
    }

    Element& operator=(const MapType & v) 
    {
      if (TYPE_MAP != t || !m->unique())
      {
        clear(TYPE_MAP);
        m = new DataType<MapType>(v);
      } else {
        *m = v;
      }
      return *this;
    }

    Element& operator=(const ListType & v) 
    {
      if (TYPE_LIST != t || !l->unique())
      {
        clear(TYPE_LIST);
        l = new DataType<ListType>(v);
      } else {
        *l = v;
      }
      return *this;
    }

    /// Check for equality with another Element.
    bool operator==(const Element& o) const;

#if defined(__GNUC__) && __GNUC__ < 3
    bool operator!=(const Element& o) const
    {
        return !(*this == o);
    }
#endif // defined(__GNUC__) && __GNUC__ < 3
    
    /// Check for inequality with anything we can check equality with
    template<class C>
    bool operator!=(C c) const
    {
        return !(*this == c);
    }

    /// Check for equality with a int.
    bool operator==(IntType v) const
    {
      return (t == TYPE_INT && i == v);
    }

    /// Check for equality with a double.
    bool operator==(FloatType v) const
    {
      return t == TYPE_FLOAT && f == v;
    }

    /// Check for equality with a pointer.
    bool operator==(PtrType v) const
    {
      return t == TYPE_PTR && p == v;
    }

    /// Check for equality with a const char *.
    bool operator==(const char * v) const
    {
      if(t == TYPE_STRING)
        return (*s == v);
      return false;
    }

    /// Check for equality with a std::string.
    bool operator==(const StringType& v) const
    {
      if(t == TYPE_STRING)
        return (*s == v);
      return false;
    }

    /// Check for equality with a MapType.
    bool operator==(const MapType& v) const
    {
      if(t == TYPE_MAP)
        return (*m == v);
      return false;
    }

    /// Check for equality with a ListType.
    bool operator==(const ListType& v) const
    {
      if (t == TYPE_LIST)
        return (*l == v);
      return false;
    }

    /// Get the current type.
    Type getType() const { return t; }
    /// Check whether the current type is nothing.
    bool isNone() const { return (t == TYPE_NONE); }
    /// Check whether the current type is int.
    bool isInt() const { return (t == TYPE_INT); }
    /// Check whether the current type is double.
    bool isFloat() const { return (t == TYPE_FLOAT); }
    /// Check whether the current type is pointer.
    bool isPtr() const { return (t == TYPE_PTR); }
    /// Check whether the current type is numeric.
    bool isNum() const { return ((t == TYPE_FLOAT) || (t == TYPE_INT)); }
    /// Check whether the current type is std::string.
    bool isString() const { return (t == TYPE_STRING); }
    /// Check whether the current type is MapType.
    bool isMap() const { return (t == TYPE_MAP); }
    /// Check whether the current type is ListType.
    bool isList() const { return (t == TYPE_LIST); }

    /// Retrieve the current value as a int.
    IntType asInt() const throw (WrongTypeException)
    {
        if (t == TYPE_INT) return i;
        throw WrongTypeException();
    }
    IntType Int() const
    {
        return i;
    }
    /// Retrieve the current value as a double.
    FloatType asFloat() const throw (WrongTypeException)
    {
        if (t == TYPE_FLOAT) return f;
        throw WrongTypeException();
    }
    FloatType Float() const
    {
        return f;
    }
    /// Retrieve the current value as a pointer.
    PtrType asPtr() const throw (WrongTypeException)
    {
        if (t == TYPE_PTR) return p;
        throw WrongTypeException();
    }
    PtrType Ptr() const
    {
        return p;
    }
    /// Retrieve the current value as a number.
    FloatType asNum() const throw (WrongTypeException)
    {
        if (t == TYPE_FLOAT) return f;
        if (t == TYPE_INT) return FloatType(i);
        throw WrongTypeException();
    }
    /// Retrieve the current value as a const std::string reference.
    const std::string& asString() const throw (WrongTypeException)
    {
        if (t == TYPE_STRING) return *s;
        throw WrongTypeException();
    }
    /// Retrieve the current value as a non-const std::string reference.
    std::string& asString() throw (WrongTypeException)
    {
        if (t == TYPE_STRING) return *(s = s->makeUnique());
        throw WrongTypeException();
    }
    const StringType& String() const
    {
        return *s;
    }
    StringType& String()
    {
        return *(s = s->makeUnique());
    }
    /// Retrieve the current value as a const MapType reference.
    const MapType& asMap() const throw (WrongTypeException)
    {
        if (t == TYPE_MAP) return *m;
        throw WrongTypeException();
    }
    /// Retrieve the current value as a non-const MapType reference.
    MapType& asMap() throw (WrongTypeException)
    {
        if (t == TYPE_MAP) return *(m = m->makeUnique());
        throw WrongTypeException();
    }
    const MapType& Map() const
    {
        return *m;
    }
    MapType& Map()
    {
        return *(m = m->makeUnique());
    }
    /// Retrieve the current value as a const ListType reference.
    const ListType& asList() const throw (WrongTypeException)
    {
        if (t == TYPE_LIST) return *l;
        throw WrongTypeException();
    }
    /// Retrieve the current value as a non-const ListType reference.
    ListType& asList() throw (WrongTypeException)
    {
        if (t == TYPE_LIST) return *(l = l->makeUnique());
        throw WrongTypeException();
    }
    const ListType& List() const
    {
        return *l;
    }
    ListType& List()
    {
        return *(l = l->makeUnique());
    }

    static const char * typeName(Type);

protected:

    template<class C>
    class DataType
    {
    public:
        DataType() : _refcount(1) {}
        DataType(const C& c) : _refcount(1), _data(c) {}

        DataType& operator=(const C& c) {_data = c; return *this;}

        bool operator==(const C& c) const {return _data == c;}

        void ref() {++_refcount;}
        void unref() {if(--_refcount == 0) delete this;}

        bool unique() const {return _refcount == 1;}
        DataType* makeUnique()
        {
           if(unique())
               return this;
           unref(); // _refcount > 1, so this is fine
           return new DataType(_data);
        }

        operator C&() {return _data;}
//        operator const C&() const {return _data;}

    private:
        DataType(const DataType&); // unimplemented
        DataType& operator=(const DataType&); // unimplemented

        unsigned long _refcount;
        C _data;
    };

    Type t;
    union {
      IntType i;
      FloatType f;
      void* p;
      DataType<StringType>* s;
      DataType<MapType>* m;
      DataType<ListType>* l;
    };
};

} } // namespace Atlas::Message


#endif // ATLAS_MESSAGE_ELEMENT_H