7 #ifndef ATLAS_MESSAGE_ELEMENT_H
8 #define ATLAS_MESSAGE_ELEMENT_H
10 #include <Atlas/Exception.h>
11 #include <Atlas/float.h>
17 namespace Atlas {
namespace Message {
29 typedef double FloatType;
30 typedef void * PtrType;
31 typedef std::string StringType;
32 typedef std::map<std::string, Element> MapType;
33 typedef std::vector<Element> ListType;
76 typedef Atlas::Message::IntType IntType;
77 typedef Atlas::Message::FloatType FloatType;
78 typedef Atlas::Message::PtrType PtrType;
79 typedef Atlas::Message::StringType StringType;
80 typedef Atlas::Message::MapType MapType;
81 typedef Atlas::Message::ListType ListType;
84 void clear(Type new_type = TYPE_NONE);
122 : t(TYPE_FLOAT), f(v)
128 : t(TYPE_FLOAT), f(v)
232 if (TYPE_STRING != t || !s->unique())
235 s =
new DataType<StringType>(v);
244 if (TYPE_STRING != t || !s->unique())
247 s =
new DataType<StringType>(v);
256 if (TYPE_MAP != t || !m->unique())
259 m =
new DataType<MapType>(v);
268 if (TYPE_LIST != t || !l->unique())
271 l =
new DataType<ListType>(v);
281 #if defined(__GNUC__) && __GNUC__ < 3
284 return !(*
this == o);
286 #endif // defined(__GNUC__) && __GNUC__ < 3
292 return !(*
this == c);
298 return (t == TYPE_INT && i == v);
304 return t == TYPE_FLOAT && Equal(f, v);
310 return t == TYPE_PTR && p == v;
348 bool isNone()
const {
return (t == TYPE_NONE); }
350 bool isInt()
const {
return (t == TYPE_INT); }
352 bool isFloat()
const {
return (t == TYPE_FLOAT); }
354 bool isPtr()
const {
return (t == TYPE_PTR); }
356 bool isNum()
const {
return ((t == TYPE_FLOAT) || (t == TYPE_INT)); }
358 bool isString()
const {
return (t == TYPE_STRING); }
360 bool isMap()
const {
return (t == TYPE_MAP); }
362 bool isList()
const {
return (t == TYPE_LIST); }
367 if (t == TYPE_INT)
return i;
377 if (t == TYPE_FLOAT)
return f;
380 FloatType Float()
const
387 if (t == TYPE_PTR)
return p;
397 if (t == TYPE_FLOAT)
return f;
398 if (t == TYPE_INT)
return FloatType(i);
404 if (t == TYPE_STRING)
return *s;
410 if (t == TYPE_STRING)
return *(s = s->makeUnique());
413 const StringType& String()
const
419 return *(s = s->makeUnique());
424 if (t == TYPE_MAP)
return *m;
430 if (t == TYPE_MAP)
return *(m = m->makeUnique());
433 const MapType& Map()
const
439 return *(m = m->makeUnique());
444 if (t == TYPE_LIST)
return *l;
450 if (t == TYPE_LIST)
return *(l = l->makeUnique());
453 const ListType& List()
const
459 return *(l = l->makeUnique());
462 static const char * typeName(Type);
470 DataType() : _refcount(1), _data(0) {}
471 DataType(
const C& c) : _refcount(1), _data(c) {}
473 DataType& operator=(
const C& c) {_data = c;
return *
this;}
475 bool operator==(
const C& c)
const {
return _data == c;}
477 void ref() {++_refcount;}
478 void unref() {
if(--_refcount == 0)
delete this;}
480 bool unique()
const {
return _refcount == 1;}
489 operator C&() {
return _data;}
496 unsigned long _refcount;
514 #endif // ATLAS_MESSAGE_ELEMENT_H
Element(IntType v)
Set type to int, and value to v.
Definition: Element.h:115
bool operator==(PtrType v) const
Check for equality with a pointer.
Definition: Element.h:308
bool isMap() const
Check whether the current type is MapType.
Definition: Element.h:360
const ListType & asList() const
Retrieve the current value as a const ListType reference.
Definition: Element.h:442
bool operator==(IntType v) const
Check for equality with a int.
Definition: Element.h:296
bool operator==(FloatType v) const
Check for equality with a double.
Definition: Element.h:302
Element & operator=(const Element &obj)
overload assignment operator !
bool operator==(const ListType &v) const
Check for equality with a ListType.
Definition: Element.h:338
const MapType & asMap() const
Retrieve the current value as a const MapType reference.
Definition: Element.h:422
Element(const MapType &v)
Set type to MapType, and value to v.
Definition: Element.h:155
Multi-type container.
Definition: Element.h:61
bool isPtr() const
Check whether the current type is pointer.
Definition: Element.h:354
std::string & asString()
Retrieve the current value as a non-const std::string reference.
Definition: Element.h:408
Type getType() const
Get the current type.
Definition: Element.h:346
PtrType asPtr() const
Retrieve the current value as a pointer.
Definition: Element.h:385
bool isFloat() const
Check whether the current type is double.
Definition: Element.h:352
bool operator==(const MapType &v) const
Check for equality with a MapType.
Definition: Element.h:330
Base class for all exceptions thrown by Atlas-C++.
Definition: Exception.h:18
bool operator==(const Element &o) const
Check for equality with another Element.
MapType & asMap()
Retrieve the current value as a non-const MapType reference.
Definition: Element.h:428
Element(const char *v)
Set type to std::string, and value to v.
Definition: Element.h:139
Element(const Element &obj)
Copy an existing object.
Element(float v)
Set type to double, and value to v.
Definition: Element.h:121
Element(const ListType &v)
Set type to ListType, and value to v.
Definition: Element.h:161
bool isList() const
Check whether the current type is ListType.
Definition: Element.h:362
Element(const StringType &v)
Set type to std::string, and value to v.
Definition: Element.h:149
IntType asInt() const
Retrieve the current value as a int.
Definition: Element.h:365
FloatType asFloat() const
Retrieve the current value as a double.
Definition: Element.h:375
bool isNum() const
Check whether the current type is numeric.
Definition: Element.h:356
Element(FloatType v)
Set type to double, and value to v.
Definition: Element.h:127
bool isNone() const
Check whether the current type is nothing.
Definition: Element.h:348
Element(int v)
Set type to int, and value to v.
Definition: Element.h:103
bool operator==(const StringType &v) const
Check for equality with a std::string.
Definition: Element.h:322
ListType & asList()
Retrieve the current value as a non-const ListType reference.
Definition: Element.h:448
The Atlas namespace.
Definition: Bridge.h:20
Element(PtrType v)
Set type to PtrType, and value to v.
Definition: Element.h:133
Element()
Construct an empty object.
Definition: Element.h:88
An exception class issued when the wrong type is requested in as().
Definition: Element.h:21
Element(bool v)
Set type to int, and value to v.
Definition: Element.h:109
FloatType asNum() const
Retrieve the current value as a number.
Definition: Element.h:395
const std::string & asString() const
Retrieve the current value as a const std::string reference.
Definition: Element.h:402
bool operator==(const char *v) const
Check for equality with a const char *.
Definition: Element.h:314
bool operator!=(C c) const
Check for inequality with anything we can check equality with.
Definition: Element.h:290
bool isInt() const
Check whether the current type is int.
Definition: Element.h:350
bool isString() const
Check whether the current type is std::string.
Definition: Element.h:358
Copyright 2000-2004 the respective authors.
This document can be licensed under the terms of the GNU Free Documentation
License or the GNU General Public License and may be freely distributed under
the terms given by one of these licenses.