Atlas
0.7.0
Networking protocol for the Worldforge system.
|
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)
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;
603 #endif // ATLAS_MESSAGE_ELEMENT_H
bool operator==(const ListType &v) const
Check for equality with a ListType.
bool operator==(const Element &o) const
Check for equality with another Element.
Element(const StringType &v)
Set type to std::string, and value to v.
Type getType() const
Get the current type.
std::string & asString()
Retrieve the current value as a non-const std::string reference.
const MapType & asMap() const
Retrieve the current value as a const MapType reference.
Element(ListType &&v)
Set type to ListType, and move v.
PtrType asPtr() const
Retrieve the current value as a pointer.
bool isString() const
Check whether the current type is std::string.
Element(MapType &&v)
Set type to MapType, and move v.
bool operator==(int v) const
Check for equality with a int.
StringType && moveString()
const std::string & asString() const
Retrieve the current value as a const std::string reference.
Element(const char *v)
Set type to std::string, and value to v.
bool operator==(const MapType &v) const
Check for equality with a MapType.
Element()
Construct an empty object.
Element(FloatType v)
Set type to double, and value to v.
IntType asInt() const
Retrieve the current value as a int.
Element(PtrType v)
Set type to PtrType, and value to v.
bool isMap() const
Check whether the current type is MapType.
bool isPtr() const
Check whether the current type is pointer.
Element(const MapType &v)
Set type to MapType, and value to v.
bool operator==(const StringType &v) const
Check for equality with a std::string.
bool isNum() const
Check whether the current type is numeric.
Element(StringType &&v)
Set type to std::string, and move v.
Element(const ListType &v)
Set type to ListType, and value to v.
bool isInt() const
Check whether the current type is int.
ListType & asList()
Retrieve the current value as a non-const ListType reference.
bool operator!=(C c) const
Check for inequality with anything we can check equality with.
bool isFloat() const
Check whether the current type is double.
bool operator==(const char *v) const
Check for equality with a const char *.
bool operator==(PtrType v) const
Check for equality with a pointer.
bool isNone() const
Check whether the current type is nothing.
Element(int v)
Set type to int, and value to v.
An exception class issued when the wrong type is requested in as().
MapType & asMap()
Retrieve the current value as a non-const MapType reference.
FloatType asNum() const
Retrieve the current value as a number.
bool operator==(FloatType v) const
Check for equality with a double.
FloatType asFloat() const
Retrieve the current value as a double.
Element & operator=(const Element &obj)
overload assignment operator !
bool isList() const
Check whether the current type is ListType.
bool operator==(long v) const
Check for equality with a int.
const ListType & asList() const
Retrieve the current value as a const ListType reference.
Element(bool v)
Set type to int, and value to v.