From 0b8411a7e5f35431b43b7f614662abf92aba07e9 Mon Sep 17 00:00:00 2001 From: Kevron Rees Date: Thu, 4 Oct 2012 16:10:45 -0700 Subject: [PATCH] added property registration system --- TODO | 1 + lib/abstractpropertytype.h | 15 +++++++++++++++ lib/vehicleproperty.cpp | 27 ++++++++++++++++++++++++++- lib/vehicleproperty.h | 8 ++++++++ 4 files changed, 50 insertions(+), 1 deletion(-) diff --git a/TODO b/TODO index 81855a9..c6db1a1 100644 --- 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 diff --git a/lib/abstractpropertytype.h b/lib/abstractpropertytype.h index 1177346..777cd3b 100644 --- a/lib/abstractpropertytype.h +++ b/lib/abstractpropertytype.h @@ -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(val); + } + } + std::string toString() { std::stringstream stream; @@ -135,6 +145,11 @@ public: return *this; } + void fromString(std::string val) + { + setValue(val); + } + std::string toString() { diff --git a/lib/vehicleproperty.cpp b/lib/vehicleproperty.cpp index 37d90ff..aa074a7 100644 --- a/lib/vehicleproperty.cpp +++ b/lib/vehicleproperty.cpp @@ -22,6 +22,8 @@ using namespace std; +std::map 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::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; +} diff --git a/lib/vehicleproperty.h b/lib/vehicleproperty.h index c5209c3..1be0af9 100644 --- a/lib/vehicleproperty.h +++ b/lib/vehicleproperty.h @@ -25,6 +25,7 @@ #include #include #include +#include #include @@ -87,6 +88,7 @@ public: VehicleProperty(); typedef std::string Property; + typedef std::function 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 registeredPropertyFactoryMap; }; -- 2.7.4