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().
PtrType asPtr() const
Retrieve the current value as a pointer.
FloatType asNum() const
Retrieve the current value as a number.
Element(const MapType &v)
Set type to MapType, and value to v.
bool operator==(long long v) const
Check for equality with a int.
bool isString() const
Check whether the current type is std::string.
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.
bool operator==(PtrType v) const
Check for equality with a pointer.
const std::string & asString() const
Retrieve the current value as a const std::string reference.
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 isPtr() const
Check whether the current type is pointer.
MapType & asMap()
Retrieve the current value as a non-const MapType reference.
bool isNum() const
Check whether the current type is numeric.
bool operator==(const char *v) const
Check for equality with a const char *.
Element(bool v)
Set type to int, and value to v.
IntType asInt() const
Retrieve the current value as a int.
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.
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.
FloatType asFloat() const
Retrieve the current value as a double.
Type getType() const
Get the current type.
bool operator!=(C c) const
Check for inequality with anything we can check equality with.
bool isNone() const
Check whether the current type is nothing.
Element(long long v)
Set type to int, and value to v.
bool operator==(const MapType &v) const
Check for equality with a MapType.
const ListType & asList() const
Retrieve the current value as a const ListType reference.
bool isFloat() const
Check whether the current type is double.
bool operator==(const StringType &v) const
Check for equality with a std::string.
bool isInt() const
Check whether the current type is int.
bool operator==(long v) const
Check for equality with a int.
Element(const ListType &v)
Set type to ListType, and value to v.
bool operator==(FloatType v) const
Check for equality with a double.
bool operator==(int v) const
Check for equality with a int.
bool isMap() const
Check whether the current type is MapType.
bool operator==(const ListType &v) const
Check for equality with a ListType.
Element(PtrType v)
Set type to PtrType, and value to v.
const MapType & asMap() const
Retrieve the current value as a const MapType reference.
ListType & asList()
Retrieve the current value as a non-const ListType reference.
bool isList() const
Check whether the current type is ListType.