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);
100 : t(TYPE_FLOAT), f(v)
190 Element& operator=(
long long v)
210 Element& operator=(FloatType v)
230 Element& operator=(
const char * v)
232 if (TYPE_STRING != t || !s->unique())
242 Element& operator=(
const StringType & v)
244 if (TYPE_STRING != t || !s->unique())
254 Element& operator=(StringType && v)
256 if (TYPE_STRING != t || !s->unique())
266 Element& operator=(
const MapType & v)
268 if (TYPE_MAP != t || !m->unique())
278 Element& operator=(MapType && v)
280 if (TYPE_MAP != t || !m->unique())
290 Element& operator=(
const ListType & v)
292 if (TYPE_LIST != t || !l->unique())
302 Element& operator=(ListType && v)
304 if (TYPE_LIST != t || !l->unique())
315 bool operator==(
const Element& o)
const;
321 return !(*
this == c);
327 return (t == TYPE_INT && i == v);
333 return (t == TYPE_INT && i == v);
336 bool operator==(
long long v)
const 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;
603 #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.
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.
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.
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.