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 {
28 typedef std::int64_t IntType;
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;
54 void clear(Type new_type = TYPE_NONE);
101 : t(TYPE_FLOAT), f(v)
107 : t(TYPE_FLOAT), f(v)
207 Element& operator=(
long long v)
227 Element& operator=(FloatType v)
247 Element& operator=(
const char * v)
249 if (TYPE_STRING != t || !s->unique())
259 Element& operator=(
const StringType & v)
261 if (TYPE_STRING != t || !s->unique())
271 Element& operator=(StringType && v)
273 if (TYPE_STRING != t || !s->unique())
283 Element& operator=(
const MapType & v)
285 if (TYPE_MAP != t || !m->unique())
295 Element& operator=(MapType && v)
297 if (TYPE_MAP != t || !m->unique())
307 Element& operator=(
const ListType & v)
309 if (TYPE_LIST != t || !l->unique())
319 Element& operator=(ListType && v)
321 if (TYPE_LIST != t || !l->unique())
332 bool operator==(
const Element& o)
const;
334 #if defined(__GNUC__) && __GNUC__ < 3 335 bool operator!=(
const Element& o)
const 337 return !(*
this == o);
339 #endif // defined(__GNUC__) && __GNUC__ < 3 345 return !(*
this == c);
351 return (t == TYPE_INT && i == v);
357 return (t == TYPE_INT && i == v);
363 return (t == TYPE_INT && i == v);
369 return t == TYPE_FLOAT && Equal(f, v);
375 return t == TYPE_PTR && p == v;
413 bool isNone()
const {
return (t == TYPE_NONE); }
415 bool isInt()
const {
return (t == TYPE_INT); }
417 bool isFloat()
const {
return (t == TYPE_FLOAT); }
419 bool isPtr()
const {
return (t == TYPE_PTR); }
421 bool isNum()
const {
return ((t == TYPE_FLOAT) || (t == TYPE_INT)); }
423 bool isString()
const {
return (t == TYPE_STRING); }
425 bool isMap()
const {
return (t == TYPE_MAP); }
427 bool isList()
const {
return (t == TYPE_LIST); }
432 if (t == TYPE_INT)
return i;
442 if (t == TYPE_FLOAT)
return f;
445 FloatType Float()
const 452 if (t == TYPE_PTR)
return p;
462 if (t == TYPE_FLOAT)
return f;
463 if (t == TYPE_INT)
return FloatType(i);
469 if (t == TYPE_STRING)
return *s;
475 if (t == TYPE_STRING)
return *(s = s->makeUnique());
478 const StringType& String()
const 484 return *(s = s->makeUnique());
502 if (t == TYPE_MAP)
return *m;
508 if (t == TYPE_MAP)
return *(m = m->makeUnique());
511 const MapType& Map()
const 517 return *(m = m->makeUnique());
535 if (t == TYPE_LIST)
return *l;
541 if (t == TYPE_LIST)
return *(l = l->makeUnique());
544 const ListType& List()
const 550 return *(l = l->makeUnique());
565 static const char * typeName(Type);
573 DataType() : _refcount(1), _data(
nullptr) {}
575 explicit DataType(
const C& c) : _refcount(1), _data(c) {}
576 explicit DataType(C&& c) : _refcount(1), _data(std::move(c)) {}
579 DataType& operator=(
const C& c) {_data = c;
return *
this;}
580 DataType& operator=(
const C&& c) {_data = std::move(c);
return *
this;}
583 bool operator==(
const C& c)
const {
return _data == c;}
585 void ref() {++_refcount;}
586 void unref() {
if(--_refcount == 0)
delete this;}
588 bool unique()
const {
return _refcount == 1;}
597 operator C&() {
return _data;}
605 return std::move(_data);
611 unsigned long _refcount;
629 #endif // ATLAS_MESSAGE_ELEMENT_H An exception class issued when the wrong type is requested in as().
bool operator!=(C c) const
Check for inequality with anything we can check equality with.
FloatType asFloat() const
Retrieve the current value as a double.
Element(const MapType &v)
Set type to MapType, and value to v.
bool isPtr() const
Check whether the current type is pointer.
Element(const StringType &v)
Set type to std::string, and value to v.
Element(const char *v)
Set type to std::string, and value to v.
Element(MapType &&v)
Set type to MapType, and move v.
Element(float v)
Set type to double, and value to v.
StringType && moveString()
Element()
Construct an empty object.
bool isNone() const
Check whether the current type is nothing.
bool operator==(long v) const
Check for equality with a int.
MapType & asMap()
Retrieve the current value as a non-const MapType reference.
bool isList() const
Check whether the current type is ListType.
Element(bool v)
Set type to int, and value to v.
bool isNum() const
Check whether the current type is numeric.
bool operator==(const ListType &v) const
Check for equality with a ListType.
bool operator==(PtrType v) const
Check for equality with a pointer.
std::string & asString()
Retrieve the current value as a non-const std::string reference.
Element(FloatType v)
Set type to double, and value to v.
bool operator==(FloatType v) const
Check for equality with a double.
Element(ListType &&v)
Set type to ListType, and move v.
Element(StringType &&v)
Set type to std::string, and move v.
Element(int v)
Set type to int, and value to v.
bool isMap() const
Check whether the current type is MapType.
Element(long long v)
Set type to int, and value to v.
const std::string & asString() const
Retrieve the current value as a const std::string reference.
bool operator==(const MapType &v) const
Check for equality with a MapType.
IntType asInt() const
Retrieve the current value as a int.
const ListType & asList() const
Retrieve the current value as a const ListType reference.
bool operator==(const char *v) const
Check for equality with a const char *.
bool operator==(const StringType &v) const
Check for equality with a std::string.
PtrType asPtr() const
Retrieve the current value as a pointer.
bool operator==(long long v) const
Check for equality with a int.
FloatType asNum() const
Retrieve the current value as a number.
Element(const ListType &v)
Set type to ListType, and value to v.
bool operator==(int v) const
Check for equality with a int.
const MapType & asMap() const
Retrieve the current value as a const MapType reference.
bool isInt() const
Check whether the current type is int.
bool isString() const
Check whether the current type is std::string.
Element(PtrType v)
Set type to PtrType, and value to v.
Type getType() const
Get the current type.
bool isFloat() const
Check whether the current type is double.
ListType & asList()
Retrieve the current value as a non-const ListType reference.