added property registration system 06/2306/1
authorKevron Rees <kevron_m_rees@linux.intel.com>
Thu, 4 Oct 2012 23:10:45 +0000 (16:10 -0700)
committerKevron Rees <kevron_m_rees@linux.intel.com>
Thu, 4 Oct 2012 23:10:45 +0000 (16:10 -0700)
TODO
lib/abstractpropertytype.h
lib/vehicleproperty.cpp
lib/vehicleproperty.h

diff --git a/TODO b/TODO
index 81855a9..c6db1a1 100644 (file)
--- a/TODO
+++ b/TODO
@@ -1,3 +1,4 @@
+- bluetooth adapter setting in obd2source
 - test for memory leaks in websocket sink plugin
 - per-source property filtering in routing engine
 - historic data "get" interface in routing engine
index 1177346..777cd3b 100644 (file)
@@ -32,6 +32,8 @@ class AbstractPropertyType
 public:
        virtual std::string toString() = 0;
 
+       virtual void fromString(std::string)= 0;
+
        void setValue(boost::any val)
        {
                mValue = val;
@@ -84,6 +86,14 @@ public:
                else throw std::runtime_error("value cannot be empty");
        }
 
+       void fromString(std::string val)
+       {
+               if(!val.empty() && val != "")
+               {
+                       serialize<T>(val);
+               }
+       }
+
        std::string toString()
        {
                std::stringstream stream;
@@ -135,6 +145,11 @@ public:
                return *this;
        }
 
+       void fromString(std::string val)
+       {
+               setValue(val);
+       }
+
 
        std::string toString()
        {
index 37d90ff..aa074a7 100644 (file)
@@ -22,6 +22,8 @@
 
 using namespace std;
 
+std::map<VehicleProperty::Property, VehicleProperty::PropertyTypeFactoryCallback> VehicleProperty::registeredPropertyFactoryMap;
+
 const VehicleProperty::Property VehicleProperty::NoValue = "NoValue";
 const VehicleProperty::Property VehicleProperty::VehicleSpeed = "VehicleSpeed";
 const VehicleProperty::Property VehicleProperty::EngineSpeed = "EngineSpeed";
@@ -69,7 +71,7 @@ std::list<VehicleProperty::Property> VehicleProperty::capabilities()
        mProperties.push_back(SteeringWheelAngle);
        mProperties.push_back(TurnSignal);
        mProperties.push_back(ClutchStatus);
-       mProperties.push_back((EngineOilPressure));
+       mProperties.push_back(EngineOilPressure);
        mProperties.push_back(EngineCoolantTemperature);
        mProperties.push_back(AccelerationX);
        mProperties.push_back(AccelerationY);
@@ -121,6 +123,29 @@ AbstractPropertyType* VehicleProperty::getPropertyTypeForPropertyNameValue(Vehic
        else if(name == TirePressureLeftRear) return new TirePressureType(value);
        else if(name == TirePressureRightRear) return new TirePressureType(value);
 
+       else
+       {
+               if(registeredPropertyFactoryMap.count(name) > 0)
+               {
+                       VehicleProperty::PropertyTypeFactoryCallback cb = registeredPropertyFactoryMap[name];
+                       if ( cb != NULL )
+                       {
+                               AbstractPropertyType* type = cb();
+                               if(type == NULL)
+                                       throw std::runtime_error("Cannot return NULL in a PropertyTypeFactory");
+
+                               type->fromString(value);
+
+                               return type;
+                       }
+
+               }
+       }
 
        return nullptr;
 }
+
+void VehicleProperty::registerProperty(VehicleProperty::Property name, VehicleProperty::PropertyTypeFactoryCallback factory)
+{
+       registeredPropertyFactoryMap[name] = factory;
+}
index c5209c3..1be0af9 100644 (file)
@@ -25,6 +25,7 @@
 #include <list>
 #include <set>
 #include <sstream>
+#include <map>
 
 #include <abstractpropertytype.h>
 
@@ -87,6 +88,7 @@ public:
        VehicleProperty();
 
        typedef std::string Property;
+       typedef std::function<AbstractPropertyType* (void)> PropertyTypeFactoryCallback;
 
        /// Various property types:
 
@@ -213,6 +215,12 @@ public:
          * transfered to the caller.
          */
        static AbstractPropertyType* getPropertyTypeForPropertyNameValue(Property name, std::string value);
+
+       static void registerProperty(Property name, PropertyTypeFactoryCallback factory);
+
+private:
+
+       static std::map<Property, PropertyTypeFactoryCallback> registeredPropertyFactoryMap;
     
 };