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 void clear(Type new_type = TYPE_NONE);
118 : t(TYPE_FLOAT), f(v)
124 : t(TYPE_FLOAT), f(v)
233 Element& operator=(FloatType v)
253 Element& operator=(
const char * v)
255 if (TYPE_STRING != t || !s->unique())
265 Element& operator=(
const StringType & v)
267 if (TYPE_STRING != t || !s->unique())
277 Element& operator=(StringType && v)
279 if (TYPE_STRING != t || !s->unique())
289 Element& operator=(
const MapType & v)
291 if (TYPE_MAP != t || !m->unique())
301 Element& operator=(MapType && v)
303 if (TYPE_MAP != t || !m->unique())
313 Element& operator=(
const ListType & v)
315 if (TYPE_LIST != t || !l->unique())
325 Element& operator=(ListType && v)
327 if (TYPE_LIST != t || !l->unique())
338 bool operator==(
const Element& o)
const;
340 #if defined(__GNUC__) && __GNUC__ < 3 341 bool operator!=(
const Element& o)
const 343 return !(*
this == o);
345 #endif // defined(__GNUC__) && __GNUC__ < 3 351 return !(*
this == c);
357 return (t == TYPE_INT && i == v);
363 return t == TYPE_FLOAT && Equal(f, v);
369 return t == TYPE_PTR && p == v;
407 bool isNone()
const {
return (t == TYPE_NONE); }
409 bool isInt()
const {
return (t == TYPE_INT); }
411 bool isFloat()
const {
return (t == TYPE_FLOAT); }
413 bool isPtr()
const {
return (t == TYPE_PTR); }
415 bool isNum()
const {
return ((t == TYPE_FLOAT) || (t == TYPE_INT)); }
417 bool isString()
const {
return (t == TYPE_STRING); }
419 bool isMap()
const {
return (t == TYPE_MAP); }
421 bool isList()
const {
return (t == TYPE_LIST); }
426 if (t == TYPE_INT)
return i;
436 if (t == TYPE_FLOAT)
return f;
439 FloatType Float()
const 446 if (t == TYPE_PTR)
return p;
456 if (t == TYPE_FLOAT)
return f;
457 if (t == TYPE_INT)
return FloatType(i);
463 if (t == TYPE_STRING)
return *s;
469 if (t == TYPE_STRING)
return *(s = s->makeUnique());
472 const StringType& String()
const 478 return *(s = s->makeUnique());
490 return std::move(s->move());
496 if (t == TYPE_MAP)
return *m;
502 if (t == TYPE_MAP)
return *(m = m->makeUnique());
505 const MapType& Map()
const 511 return *(m = m->makeUnique());
523 return std::move(m->move());
529 if (t == TYPE_LIST)
return *l;
535 if (t == TYPE_LIST)
return *(l = l->makeUnique());
538 const ListType& List()
const 544 return *(l = l->makeUnique());
556 return std::move(l->move());
559 static const char * typeName(Type);
567 DataType() : _refcount(1), _data(0) {}
569 explicit DataType(
const C& c) : _refcount(1), _data(c) {}
570 explicit DataType(C&& c) : _refcount(1), _data(std::move(c)) {}
573 DataType& operator=(
const C& c) {_data = c;
return *
this;}
574 DataType& operator=(
const C&& c) {_data = std::move(c);
return *
this;}
577 bool operator==(
const C& c)
const {
return _data == c;}
579 void ref() {++_refcount;}
580 void unref() {
if(--_refcount == 0)
delete this;}
582 bool unique()
const {
return _refcount == 1;}
591 operator C&() {
return _data;}
599 return std::move(_data);
605 unsigned long _refcount;
623 #endif // ATLAS_MESSAGE_ELEMENT_H ListType moveList()
Moves the list out of the Element.
Definition: Element.h:553
MapType & asMap()
Retrieve the current value as a non-const MapType reference.
Definition: Element.h:500
Element(const ListType &v)
Set type to ListType, and value to v.
Definition: Element.h:171
FloatType asFloat() const
Retrieve the current value as a double.
Definition: Element.h:434
const MapType & asMap() const
Retrieve the current value as a const MapType reference.
Definition: Element.h:494
bool operator==(FloatType v) const
Check for equality with a double.
Definition: Element.h:361
bool operator!=(C c) const
Check for inequality with anything we can check equality with.
Definition: Element.h:349
bool operator==(PtrType v) const
Check for equality with a pointer.
Definition: Element.h:367
bool operator==(IntType v) const
Check for equality with a int.
Definition: Element.h:355
bool operator==(const char *v) const
Check for equality with a const char *.
Definition: Element.h:373
bool isNum() const
Check whether the current type is numeric.
Definition: Element.h:415
Element(const MapType &v)
Set type to MapType, and value to v.
Definition: Element.h:158
Element(IntType v)
Set type to int, and value to v.
Definition: Element.h:111
Element(MapType &&v)
Set type to MapType, and move v.
Definition: Element.h:164
const std::string & asString() const
Retrieve the current value as a const std::string reference.
Definition: Element.h:461
C move()
Moves the data out of the container.
Definition: Element.h:598
Element(const StringType &v)
Set type to std::string, and value to v.
Definition: Element.h:145
Multi-type container.
Definition: Element.h:60
Element()
Construct an empty object.
Definition: Element.h:80
MapType moveMap()
Moves the map out of the Element.
Definition: Element.h:520
bool isFloat() const
Check whether the current type is double.
Definition: Element.h:411
IntType asInt() const
Retrieve the current value as a int.
Definition: Element.h:424
bool operator==(const StringType &v) const
Check for equality with a std::string.
Definition: Element.h:381
The Atlas namespace.
Definition: Bridge.h:20
StringType moveString()
Moves the string out of the Element.
Definition: Element.h:487
bool isPtr() const
Check whether the current type is pointer.
Definition: Element.h:413
bool isNone() const
Check whether the current type is nothing.
Definition: Element.h:407
Element(bool v)
Set type to int, and value to v.
Definition: Element.h:105
const ListType & asList() const
Retrieve the current value as a const ListType reference.
Definition: Element.h:527
FloatType asNum() const
Retrieve the current value as a number.
Definition: Element.h:454
Element(FloatType v)
Set type to double, and value to v.
Definition: Element.h:123
bool operator==(const ListType &v) const
Check for equality with a ListType.
Definition: Element.h:397
Element(StringType &&v)
Set type to std::string, and move v.
Definition: Element.h:151
An exception class issued when the wrong type is requested in as().
Definition: Element.h:20
bool isList() const
Check whether the current type is ListType.
Definition: Element.h:421
bool isMap() const
Check whether the current type is MapType.
Definition: Element.h:419
ListType & asList()
Retrieve the current value as a non-const ListType reference.
Definition: Element.h:533
bool operator==(const MapType &v) const
Check for equality with a MapType.
Definition: Element.h:389
Element(PtrType v)
Set type to PtrType, and value to v.
Definition: Element.h:129
Element(float v)
Set type to double, and value to v.
Definition: Element.h:117
bool isInt() const
Check whether the current type is int.
Definition: Element.h:409
Type getType() const
Get the current type.
Definition: Element.h:405
Element(ListType &&v)
Set type to ListType, and move v.
Definition: Element.h:177
Element(int v)
Set type to int, and value to v.
Definition: Element.h:99
Base class for all exceptions thrown by Atlas-C++.
Definition: Exception.h:17
bool isString() const
Check whether the current type is std::string.
Definition: Element.h:417
Element(const char *v)
Set type to std::string, and value to v.
Definition: Element.h:135
PtrType asPtr() const
Retrieve the current value as a pointer.
Definition: Element.h:444
std::string & asString()
Retrieve the current value as a non-const std::string reference.
Definition: Element.h:467