7#ifndef ATLAS_MESSAGE_ELEMENT_H
8#define ATLAS_MESSAGE_ELEMENT_H
10#include <Atlas/Exception.h>
11#include <Atlas/float.h>
17namespace Atlas {
namespace Message {
28typedef std::int64_t IntType;
29typedef double FloatType;
30typedef void * PtrType;
31typedef std::string StringType;
32typedef std::map<std::string, Element> MapType;
33typedef std::vector<Element> ListType;
54 void clear(Type new_type = TYPE_NONE);
100 : t(TYPE_FLOAT), f(v)
232 if (TYPE_STRING != t || !s->unique())
235 s =
new DataType<StringType>(std::string(v));
244 if (TYPE_STRING != t || !s->unique())
247 s =
new DataType<StringType>(v);
256 if (TYPE_STRING != t || !s->unique())
259 s =
new DataType<StringType>(v);
268 if (TYPE_MAP != t || !m->unique())
271 m =
new DataType<MapType>(v);
280 if (TYPE_MAP != t || !m->unique())
283 m =
new DataType<MapType>(std::move(v));
292 if (TYPE_LIST != t || !l->unique())
295 l =
new DataType<ListType>(v);
304 if (TYPE_LIST != t || !l->unique())
307 l =
new DataType<ListType>(std::move(v));
321 return !(*
this == c);
327 return (t == TYPE_INT && i == v);
333 return (t == TYPE_INT && i == v);
338 return (t == TYPE_INT && i == v);
343 return t == TYPE_FLOAT && Equal(f, v);
349 return t == TYPE_PTR && p == v;
387 bool isNone()
const {
return (t == TYPE_NONE); }
389 bool isInt()
const {
return (t == TYPE_INT); }
391 bool isFloat()
const {
return (t == TYPE_FLOAT); }
393 bool isPtr()
const {
return (t == TYPE_PTR); }
395 bool isNum()
const {
return ((t == TYPE_FLOAT) || (t == TYPE_INT)); }
397 bool isString()
const {
return (t == TYPE_STRING); }
399 bool isMap()
const {
return (t == TYPE_MAP); }
401 bool isList()
const {
return (t == TYPE_LIST); }
406 if (t == TYPE_INT)
return i;
416 if (t == TYPE_FLOAT)
return f;
419 FloatType Float()
const
426 if (t == TYPE_PTR)
return p;
436 if (t == TYPE_FLOAT)
return f;
437 if (t == TYPE_INT)
return FloatType(i);
443 if (t == TYPE_STRING)
return *s;
449 if (t == TYPE_STRING)
return *(s = s->makeUnique());
452 const StringType& String()
const
458 return *(s = s->makeUnique());
476 if (t == TYPE_MAP)
return *m;
482 if (t == TYPE_MAP)
return *(m = m->makeUnique());
485 const MapType& Map()
const
491 return *(m = m->makeUnique());
509 if (t == TYPE_LIST)
return *l;
515 if (t == TYPE_LIST)
return *(l = l->makeUnique());
518 const ListType& List()
const
524 return *(l = l->makeUnique());
539 static const char * typeName(Type);
547 DataType() : _refcount(1), _data(
nullptr) {}
549 explicit DataType(
const C& c) : _refcount(1), _data(c) {}
550 explicit DataType(C&& c) : _refcount(1), _data(std::move(c)) {}
553 DataType& operator=(
const C& c) {_data = c;
return *
this;}
554 DataType& operator=(
const C&& c) {_data = std::move(c);
return *
this;}
557 bool operator==(
const C& c)
const {
return _data == c;}
559 void ref() {++_refcount;}
560 void unref() {
if(--_refcount == 0)
delete this;}
562 bool unique()
const {
return _refcount == 1;}
571 operator C&() {
return _data;}
579 return std::move(_data);
585 unsigned long _refcount;
594 DataType<StringType>* s;
595 DataType<MapType>* m;
596 DataType<ListType>* l;
ListType & asList()
Retrieve the current value as a non-const ListType reference.
bool operator==(FloatType v) const
Check for equality with a double.
Element(const StringType &v)
Set type to std::string, and value to v.
bool operator==(const StringType &v) const
Check for equality with a std::string.
Element(FloatType v)
Set type to double, and value to v.
bool isString() const
Check whether the current type is std::string.
Element(bool v)
Set type to int, and value to v.
bool operator==(const Element &o) const
Check for equality with another Element.
bool isNum() const
Check whether the current type is numeric.
StringType && moveString()
Element(ListType &&v)
Set type to ListType, and move v.
PtrType asPtr() const
Retrieve the current value as a pointer.
FloatType asFloat() const
Retrieve the current value as a double.
FloatType asNum() const
Retrieve the current value as a number.
Element(const char *v)
Set type to std::string, and value to v.
bool isFloat() const
Check whether the current type is double.
std::string & asString()
Retrieve the current value as a non-const std::string reference.
Element(int v)
Set type to int, and value to v.
bool isPtr() const
Check whether the current type is pointer.
bool operator==(int v) const
Check for equality with a int.
Element(StringType &&v)
Set type to std::string, and move v.
Type getType() const
Get the current type.
const ListType & asList() const
Retrieve the current value as a const ListType reference.
Element(PtrType v)
Set type to PtrType, and value to v.
const MapType & asMap() const
Retrieve the current value as a const MapType reference.
bool operator==(const MapType &v) const
Check for equality with a MapType.
bool operator==(const ListType &v) const
Check for equality with a ListType.
bool operator==(PtrType v) const
Check for equality with a pointer.
MapType & asMap()
Retrieve the current value as a non-const MapType reference.
Element & operator=(const Element &obj)
overload assignment operator !
Element()
Construct an empty object.
bool isMap() const
Check whether the current type is MapType.
bool operator!=(C c) const
Check for inequality with anything we can check equality with.
Element(const ListType &v)
Set type to ListType, and value to v.
const std::string & asString() const
Retrieve the current value as a const std::string reference.
bool isList() const
Check whether the current type is ListType.
bool operator==(const char *v) const
Check for equality with a const char *.
Element(MapType &&v)
Set type to MapType, and move v.
IntType asInt() const
Retrieve the current value as a int.
Element(const MapType &v)
Set type to MapType, and value to v.
bool operator==(long v) const
Check for equality with a int.
bool isNone() const
Check whether the current type is nothing.
bool isInt() const
Check whether the current type is int.
An exception class issued when the wrong type is requested in as().