7 #ifndef ATLAS_MESSAGE_ELEMENT_H
8 #define ATLAS_MESSAGE_ELEMENT_H
10 #include <Atlas/Exception.h>
11 #include <Atlas/float.h>
18 namespace Atlas {
namespace Message {
29 typedef std::int64_t IntType;
30 typedef double FloatType;
31 typedef void * PtrType;
32 typedef std::string StringType;
33 typedef std::map<std::string, Element> MapType;
34 typedef std::vector<Element> ListType;
55 void clear(Type new_type = TYPE_NONE);
101 : t(TYPE_FLOAT), f(v)
233 if (TYPE_STRING != t || !s->unique())
236 s =
new DataType<StringType>(std::string(v));
245 if (TYPE_STRING != t || !s->unique())
248 s =
new DataType<StringType>(v);
257 if (TYPE_STRING != t || !s->unique())
260 s =
new DataType<StringType>(v);
269 if (TYPE_MAP != t || !m->unique())
272 m =
new DataType<MapType>(v);
281 if (TYPE_MAP != t || !m->unique())
284 m =
new DataType<MapType>(std::move(v));
293 if (TYPE_LIST != t || !l->unique())
296 l =
new DataType<ListType>(v);
305 if (TYPE_LIST != t || !l->unique())
308 l =
new DataType<ListType>(std::move(v));
322 return !(*
this == c);
328 return (t == TYPE_INT && i == v);
334 return (t == TYPE_INT && i == v);
339 return (t == TYPE_INT && i == v);
344 return t == TYPE_FLOAT && Equal(f, v);
350 return t == TYPE_PTR && p == v;
388 bool isNone()
const {
return (t == TYPE_NONE); }
390 bool isInt()
const {
return (t == TYPE_INT); }
392 bool isFloat()
const {
return (t == TYPE_FLOAT); }
394 bool isPtr()
const {
return (t == TYPE_PTR); }
396 bool isNum()
const {
return ((t == TYPE_FLOAT) || (t == TYPE_INT)); }
398 bool isString()
const {
return (t == TYPE_STRING); }
400 bool isMap()
const {
return (t == TYPE_MAP); }
402 bool isList()
const {
return (t == TYPE_LIST); }
407 if (t == TYPE_INT)
return i;
417 if (t == TYPE_FLOAT)
return f;
420 FloatType Float()
const
427 if (t == TYPE_PTR)
return p;
437 if (t == TYPE_FLOAT)
return f;
438 if (t == TYPE_INT)
return FloatType(i);
444 if (t == TYPE_STRING)
return *s;
450 if (t == TYPE_STRING)
return *(s = s->makeUnique());
453 const StringType& String()
const
459 return *(s = s->makeUnique());
477 if (t == TYPE_MAP)
return *m;
483 if (t == TYPE_MAP)
return *(m = m->makeUnique());
486 const MapType& Map()
const
492 return *(m = m->makeUnique());
510 if (t == TYPE_LIST)
return *l;
516 if (t == TYPE_LIST)
return *(l = l->makeUnique());
519 const ListType& List()
const
525 return *(l = l->makeUnique());
540 static const char * typeName(Type);
548 DataType() : _refcount(1), _data(
nullptr) {}
550 explicit DataType(
const C& c) : _refcount(1), _data(c) {}
551 explicit DataType(C&& c) : _refcount(1), _data(std::move(c)) {}
554 DataType& operator=(
const C& c) {_data = c;
return *
this;}
555 DataType& operator=(
const C&& c) {_data = std::move(c);
return *
this;}
558 bool operator==(
const C& c)
const {
return _data == c;}
560 void ref() {++_refcount;}
561 void unref() {
if(--_refcount == 0)
delete this;}
563 bool unique()
const {
return _refcount == 1;}
572 operator C&() {
return _data;}
580 return std::move(_data);
586 unsigned long _refcount;
595 DataType<StringType>* s;
596 DataType<MapType>* m;
597 DataType<ListType>* l;
bool operator==(FloatType v) const
Check for equality with a double.
ListType & asList()
Retrieve the current value as a non-const ListType reference.
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.
const std::string & asString() const
Retrieve the current value as a const std::string reference.
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.
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.
Element(int v)
Set type to int, and value to v.
bool isPtr() const
Check whether the current type is pointer.
const MapType & asMap() const
Retrieve the current value as a const MapType reference.
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.
Element(PtrType v)
Set type to PtrType, and value to v.
MapType & asMap()
Retrieve the current value as a non-const MapType reference.
bool operator==(const MapType &v) const
Check for equality with a MapType.
std::string & asString()
Retrieve the current value as a non-const std::string reference.
bool operator==(const ListType &v) const
Check for equality with a ListType.
StringType && moveString()
bool operator==(PtrType v) const
Check for equality with a pointer.
Element & operator=(const Element &obj)
overload assignment operator !
const ListType & asList() const
Retrieve the current value as a const ListType reference.
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.
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().