Merge pull request #19 from tripzero/master
[profile/ivi/automotive-message-broker.git] / lib / abstractpropertytype.h
index 610cba2..98e2b11 100644 (file)
 #include <list>
 #include "timestamp.h"
 #include <debugout.h>
-namespace Zone {
-enum Type {
-       None = 0,
-       Driver = 1,
-       FrontMiddle = 1 << 1,
-       Passenger = 1 << 2,
-       LeftRear = 1 << 3,
-       MiddleRear = 1 << 4,
-       RightRear = 1 << 5
+#include <boost/algorithm/string.hpp>
+
+class Zone {
+
+public:
+
+       typedef int Type;
+
+       enum {
+               None = 0,
+               Front = 1,
+               Middle = 1 << 1,
+               Right = 1 << 2,
+               Left = 1 << 3,
+               Rear = 1 << 4,
+               Center = 1 << 5,
+               LeftSide = 1 << 6,
+               RightSide = 1 << 7,
+               FrontSide = 1 << 8,
+               BackSide = 1 << 9
+       };
+
+static const Zone::Type FrontRight;
+static const Zone::Type FrontLeft;
+static const Zone::Type MiddleRight;
+static const Zone::Type MiddleLeft;
+static const Zone::Type RearRight;
+static const Zone::Type RearLeft;
+
+typedef std::list<Zone::Type> ZoneList;
+
 };
-}
 
 class AbstractPropertyType
 {
 public:
-       //AbstractPropertyType(): timestamp(-1), sequence(-1), zone(Zone::None), index(0) {}
 
-       AbstractPropertyType(std::string property): name(property), timestamp(-1), sequence(-1), zone(Zone::None), index(0) {}
+       /*!
+        * \brief The Priority enum describes prority of the property type.
+        */
+       enum Priority
+       {
+               Normal = 0,
+               Low,
+               High,
+               Instant
+       };
+
+       AbstractPropertyType(std::string property): name(property), timestamp(amb::currentTime()), sequence(-1), zone(Zone::None), priority(Normal)
+       {
+               void*(name);
+       }
+
+       virtual ~AbstractPropertyType() { }
 
        virtual std::string toString() const = 0;
 
@@ -61,11 +97,33 @@ public:
 
        virtual AbstractPropertyType* copy() = 0;
 
+       /**
+        * @brief quickCopy is intended as a way to quickly copy the often changing bits from one abstract property to another
+        * It assumes that the properties are almost identical in name, source, and zone.
+        * @param other the property to copy from
+        */
+       virtual void quickCopy(AbstractPropertyType* other)
+       {
+               sequence = other->sequence;
+               mValue = other->anyValue();
+               timestamp = other->timestamp;
+       }
+
        bool operator == (AbstractPropertyType &other)
        {
                std::string one = toString();
                std::string two = other.toString();
-               return one == two;
+               return one == two
+                               && zone == other.zone
+                               && sourceUuid == other.sourceUuid
+                               && name == other.name;
+       }
+
+       bool operator != (AbstractPropertyType &other)
+       {
+               std::string one = toString();
+               std::string two = other.toString();
+               return one != two;
        }
 
        std::string name;
@@ -78,7 +136,13 @@ public:
 
        Zone::Type zone;
 
-       int index;
+       /*!
+        * \brief priority is used to tell the routing engine how to prioritize routing the value to plugins.
+        * setting this value to AbstractPropertyType::Instant will tell the routing engine to immedietly
+        * route the value without any reliance on the mainloop.  Instant priority is NOT thread safe.
+        * Default priority is AbstractPropertyType::Normal.
+        */
+       Priority priority;
 
        void setValue(boost::any val)
        {
@@ -112,10 +176,15 @@ class GVS<int>
 public:
        static const char* signature() { return "i"; }
 
-       static char value(GVariant* v)
+       static int value(GVariant* v)
        {
                return g_variant_get_int32(v);
        }
+
+       static std::string stringize(std::string v)
+       {
+               return v;
+       }
 };
 
 template <>
@@ -128,6 +197,10 @@ public:
        {
                return g_variant_get_double(v);
        }
+       static std::string stringize(std::string v)
+       {
+               return v;
+       }
 };
 
 template <>
@@ -140,6 +213,10 @@ public:
        {
                return g_variant_get_uint16(v);
        }
+       static std::string stringize(std::string v)
+       {
+               return v;
+       }
 };
 
 template <>
@@ -152,6 +229,10 @@ public:
        {
                return g_variant_get_int16(v);
        }
+       static std::string stringize(std::string v)
+       {
+               return v;
+       }
 };
 
 template <>
@@ -164,6 +245,10 @@ public:
        {
                return g_variant_get_byte(v);
        }
+       static std::string stringize(std::string v)
+       {
+               return v;
+       }
 };
 
 template <>
@@ -176,6 +261,10 @@ public:
        {
                return g_variant_get_uint32(v);
        }
+       static std::string stringize(std::string v)
+       {
+               return v;
+       }
 };
 
 template <>
@@ -188,6 +277,10 @@ public:
        {
                return g_variant_get_int64(v);
        }
+       static std::string stringize(std::string v)
+       {
+               return v;
+       }
 };
 
 template <>
@@ -198,7 +291,11 @@ public:
 
        static uint64_t value(GVariant* v)
        {
-               g_variant_get_uint64(v);
+               return g_variant_get_uint64(v);
+       }
+       static std::string stringize(std::string v)
+       {
+               return v;
        }
 };
 
@@ -212,6 +309,14 @@ public:
        {
                return g_variant_get_boolean(v);
        }
+       static std::string stringize(std::string v)
+       {
+               if(v == "0" || v == "1")
+                       return v;
+
+               boost::algorithm::to_lower(v);
+               return v == "true" ? "1":"0";
+       }
 };
 
 
@@ -230,8 +335,9 @@ public:
                timestamp = other.timestamp;
                sequence = other.sequence;
                sourceUuid = other.sourceUuid;
-               index = other.index;
                name = other.name;
+               zone = other.zone;
+
        }
 
        BasicPropertyType & operator = (BasicPropertyType const & other)
@@ -240,8 +346,8 @@ public:
                timestamp = other.timestamp;
                sequence = other.sequence;
                sourceUuid = other.sourceUuid;
-               index = other.index;
                name = other.name;
+               zone = other.zone;
 
                return *this;
        }
@@ -278,7 +384,13 @@ public:
                else setValue(T());
        }
 
-       BasicPropertyType(std::string val)
+       BasicPropertyType(std::string propertyName)
+               :AbstractPropertyType(propertyName)
+       {
+               mValue = T();
+       }
+
+       /*BasicPropertyType(std::string val)
                :AbstractPropertyType("")
        {
                if(!val.empty() && val != "")
@@ -286,7 +398,7 @@ public:
                        serialize<T>(val);
                }
                else setValue(T());
-       }
+       }*/
 
        AbstractPropertyType* copy()
        {
@@ -338,7 +450,7 @@ private:
        template <class N>
        void serialize(std::string  val,  typename std::enable_if<!std::is_enum<N>::value, N>::type* = 0)
        {
-               std::stringstream stream(val);
+               std::stringstream stream(GVS<T>::stringize(val));
                N someTemp;
                stream>>someTemp;
                setValue(someTemp);
@@ -382,7 +494,7 @@ public:
        StringPropertyType(std::string propertyName)
                :AbstractPropertyType(propertyName)
        {
-
+               setValue(std::string());
        }
 
        StringPropertyType(std::string propertyName, std::string val)
@@ -398,8 +510,8 @@ public:
                timestamp = other.timestamp;
                sequence = other.sequence;
                sourceUuid = other.sourceUuid;
-               index = other.index;
                name = other.name;
+               zone = other.zone;
        }
 
        StringPropertyType & operator = (StringPropertyType const & other)
@@ -408,8 +520,8 @@ public:
                timestamp = other.timestamp;
                sequence = other.sequence;
                sourceUuid = other.sourceUuid;
-               index = other.index;
                name = other.name;
+               zone = other.zone;
 
                return *this;
        }
@@ -472,16 +584,13 @@ public:
                timestamp = other.timestamp;
                sequence = other.sequence;
                sourceUuid = other.sourceUuid;
-               index = other.index;
                name = other.name;
+               zone = other.zone;
        }
 
        ~ListPropertyType()
        {
-               for(auto itr = mList.begin(); itr != mList.end(); itr++)
-               {
-                       delete *itr;
-               }
+               clear();
        }
 
        /** append - appends a property to the list
@@ -536,6 +645,8 @@ public:
 
        void fromString(std::string str )
        {
+               clear();
+
                if(!str.length())
                        return;
 
@@ -553,10 +664,8 @@ public:
                std::string element;
                while(std::getline(f,element,','))
                {
-                       T *foo = new T(element);
-                       append (foo);
-
-                       delete foo;
+                       T foo("", element);
+                       append (&foo);
                }
        }
 
@@ -573,7 +682,7 @@ public:
                        GVariant *var = t->toVariant();
                        GVariant *newvar = g_variant_new("v",var);
                        g_variant_builder_add_value(&params, newvar);
-                       
+
                }
 
                GVariant* var =  g_variant_builder_end(&params);
@@ -584,6 +693,8 @@ public:
 
        void fromVariant(GVariant* v)
        {
+               clear();
+
                /// TODO: fill this in
                gsize dictsize = g_variant_n_children(v);
                for (int i=0;i<dictsize;i++)
@@ -599,6 +710,16 @@ public:
        std::list<AbstractPropertyType*> list() { return mList; }
 
 private:
+
+       void clear()
+       {
+               for(auto itr = mList.begin(); itr != mList.end(); itr++)
+               {
+                       delete *itr;
+               }
+               mList.clear();
+       }
+
        void appendPriv(AbstractPropertyType* i)
        {
                mList.push_back(i);