possibly fixed uninitialized list problem in ListPropertyType
authorKevron Rees <kevron_m_rees@linux.intel.com>
Fri, 8 Feb 2013 21:42:45 +0000 (13:42 -0800)
committerKevron Rees <kevron_m_rees@linux.intel.com>
Fri, 8 Feb 2013 21:42:45 +0000 (13:42 -0800)
lib/abstractpropertytype.cpp
lib/abstractpropertytype.h
lib/debugout.h
lib/vehicleproperty.cpp
lib/vehicleproperty.h
plugins/dbus/dbusinterfacemanager.cpp
plugins/exampleplugin.cpp
plugins/examplesink.cpp
plugins/websocketsink/websocketsinkmanager.cpp

index 42e2a20..03ba0de 100644 (file)
@@ -1,94 +1,2 @@
 #include "abstractpropertytype.h"
 
-
-void ListPropertyType::append(AbstractPropertyType *property)
-{
-       mList.push_back(property->copy());
-}
-
-
-ListPropertyType::ListPropertyType()
-{
-
-}
-
-ListPropertyType::ListPropertyType(AbstractPropertyType *property)
-{
-       append(property);
-}
-
-ListPropertyType::ListPropertyType(ListPropertyType &other)
-{
-       std::list<AbstractPropertyType*> l = other.list();
-       for(auto itr = l.begin(); itr != l.end(); itr++)
-       {
-               append(*itr);
-       }
-}
-
-
-ListPropertyType::~ListPropertyType()
-{
-
-}
-
-
-uint ListPropertyType::count()
-{
-       return mList.size();
-}
-
-
-AbstractPropertyType *ListPropertyType::copy()
-{
-       return new ListPropertyType(*this);
-}
-
-
-std::string ListPropertyType::toString() const
-{
-       std::string str = "[";
-
-       for(auto itr = mList.begin(); itr != mList.end(); itr++)
-       {
-               if(str != "[")
-                       str += ",";
-
-               AbstractPropertyType* t = *itr;
-
-               str += t->toString();
-       }
-
-       str += "]";
-
-       return str;
-}
-
-
-void ListPropertyType::fromString(std::string)
-{
-       /// TODO: try to use VehicleProperty::getPropertyType... here
-}
-
-
-GVariant *ListPropertyType::toVariant()
-{
-       GVariantBuilder params;
-       g_variant_builder_init(&params, G_VARIANT_TYPE_ARRAY);
-
-       for(auto itr = mList.begin(); itr != mList.end(); itr++)
-       {
-               AbstractPropertyType* t = *itr;
-               g_variant_builder_add_value(&params, t->toVariant());
-       }
-
-       GVariant* var =  g_variant_builder_end(&params);
-       g_assert(var);
-       return var;
-}
-
-
-void ListPropertyType::fromVariant(GVariant *v)
-{
-
-}
index 9ed7cf5..df32308 100644 (file)
@@ -22,6 +22,8 @@
 #include <string>
 #include <sstream>
 #include <stdexcept>
+#include <vector>
+#include <iostream>
 #include <boost/any.hpp>
 #include <boost/lexical_cast.hpp>
 #include <boost/utility.hpp>
@@ -362,31 +364,145 @@ private:
        GVariant* mVariant;
 };
 
+template <class T>
 class ListPropertyType: public AbstractPropertyType
 {
 public:
 
-       ListPropertyType();
-       ListPropertyType(AbstractPropertyType *property);
-       ListPropertyType(ListPropertyType & other);
-       ~ListPropertyType();
+       ListPropertyType():initialized(false){}
+       ListPropertyType(AbstractPropertyType *property):initialized(false)
+       {
+               appendPriv(property->copy());
+       }
+
+       ListPropertyType(ListPropertyType & other)
+               :initialized(false)
+       {
+               std::list<AbstractPropertyType*> l = other.list();
+               for(auto itr = l.begin(); itr != l.end(); itr++)
+               {
+                       append(*itr);
+               }
+       }
+
+       ~ListPropertyType()
+       {
+               for(auto itr = mList.begin(); itr != mList.end(); itr++)
+               {
+                       delete *itr;
+               }
+       }
+
+       /** append - appends a property to the list
+        * @arg property - property to be appended.  Will be copied and owned by ListPropertyType.
+        * You are responsible for freeing property after append is called.
+        **/
+       void append(AbstractPropertyType* property)
+       {
+               if(!initialized)
+               {
+                       for(auto itr = mList.begin(); itr != mList.end(); itr++)
+                       {
+                               AbstractPropertyType *p = *itr;
+                               delete p;
+                       }
+                       mList.clear();
+                       initialized = true;
+               }
 
-       void append(AbstractPropertyType* property);
+               appendPriv(property->copy());
+       }
+
+       uint count()
+       {
+               return mList.size();
+       }
+
+       AbstractPropertyType* copy()
+       {
+               return new ListPropertyType(*this);
+       }
 
-       uint count();
+       std::string toString() const
+       {
+               std::string str = "[";
+
+               for(auto itr = mList.begin(); itr != mList.end(); itr++)
+               {
+                       if(str != "[")
+                               str += ",";
 
-       AbstractPropertyType* copy();
+                       AbstractPropertyType* t = *itr;
 
-       std::string toString() const;
-       void fromString(std::string );
+                       str += t->toString();
+               }
+
+               str += "]";
 
+               return str;
+       }
 
-       GVariant* toVariant();
-       void fromVariant(GVariant* v);
+
+       void fromString(std::string str )
+       {
+               if(!str.length())
+                       return;
+
+               if(str[0] != '[' && str[str.length()-1] != ']')
+               {
+                       return;
+               }
+
+               str = str.substr(1,str.length() - 2);
+
+               std::vector<std::string> elements;
+
+               std::istringstream f(str);
+
+               std::string element;
+               while(std::getline(f,element,','))
+               {
+                       T *foo = new T(element);
+                       append (foo);
+
+                       delete foo;
+               }
+       }
+
+
+       GVariant* toVariant()
+       {
+
+               GVariantBuilder params;
+               g_variant_builder_init(&params, G_VARIANT_TYPE_ARRAY);
+
+               for(auto itr = mList.begin(); itr != mList.end(); itr++)
+               {
+                       AbstractPropertyType* t = *itr;
+                       g_variant_builder_add_value(&params, t->toVariant());
+               }
+
+               GVariant* var =  g_variant_builder_end(&params);
+               g_assert(var);
+               return var;
+
+       }
+
+       void fromVariant(GVariant* v)
+       {
+
+       }
 
        std::list<AbstractPropertyType*> list() { return mList; }
 
 private:
+       void appendPriv(AbstractPropertyType* i)
+       {
+               mList.push_back(i);
+       }
+
+       bool initialized;
+
        std::list<AbstractPropertyType*> mList;
 };
 
index 6fbc690..b25f1a3 100644 (file)
@@ -104,7 +104,7 @@ private:
                f.precision(15);
                f<<time;
 
-               while(f.str().length() < 15)
+               while(f.str().length() <= 15)
                {
                        f<<" ";
                }
index ae1d864..7dbd30e 100644 (file)
@@ -42,6 +42,7 @@ const VehicleProperty::Property VehicleProperty::TransmissionGearPosition = "Tra
 const VehicleProperty::Property VehicleProperty::TransmissionMode = "TransmissionMode";
 const VehicleProperty::Property VehicleProperty::ThrottlePosition = "ThrottlePosition";
 const VehicleProperty::Property VehicleProperty::WheelBrake = "WheelBrake";
+const VehicleProperty::Property VehicleProperty::WheelBrakePressure = "WheelBrakePressure";
 const VehicleProperty::Property VehicleProperty::SteeringWheelAngle = "SteeringWheelAngle";
 const VehicleProperty::Property VehicleProperty::TurnSignal = "TurnSignal";
 const VehicleProperty::Property VehicleProperty::ClutchStatus = "ClutchStatus";
@@ -121,6 +122,7 @@ VehicleProperty::VehicleProperty()
        REGISTERPROPERTY(TransmissionMode,Transmission::Normal);
        registerPropertyPriv(ThrottlePosition, [](){ return new ThrottlePositionType(0); });
        registerPropertyPriv(WheelBrake, [](){ return new WheelBrakeType(false); });
+       REGISTERPROPERTY(WheelBrakePressure,0);
        registerPropertyPriv(SteeringWheelAngle, [](){ return new SteeringWheelAngleType(0); });
        registerPropertyPriv(TurnSignal, [](){ return new TurnSignalType(TurnSignals::Off); });
        registerPropertyPriv(ClutchStatus, [](){ return new ClutchStatusType(false); });
index 672d07a..5d6fab2 100644 (file)
@@ -204,6 +204,9 @@ public:
        static const Property WheelBrake;
        typedef BasicPropertyType<bool> WheelBrakeType;
 
+       static const Property WheelBrakePressure;
+       typedef BasicPropertyType<uint16_t> WheelBrakePressureType;
+
        /**< Steering wheel angle (0-359) */
        static const Property SteeringWheelAngle;
        typedef BasicPropertyType<uint16_t> SteeringWheelAngleType;
@@ -369,7 +372,7 @@ public:
        typedef BasicPropertyType<Vehicle::Type> VehicleTypeType;
 
        static const Property DoorsPerRow;
-       typedef ListPropertyType DoorsPerRowType;
+       typedef ListPropertyType<BasicPropertyType<uint16_t> > DoorsPerRowType;
 
        static const Property TransmissionGearType;
        typedef BasicPropertyType<Transmission::Type> TransmissionGearTypeType;
index 40a2a08..e84182f 100644 (file)
@@ -55,7 +55,12 @@ on_bus_acquired (GDBusConnection *connection, const gchar *name, gpointer user_d
        ConstructProperty(EngineOilProperty);
        ConstructProperty(ExteriorBrightnessProperty);
        ConstructProperty(VehicleId);
+       ConstructProperty(TransmissionInfoProperty);
+       ConstructProperty(VehicleTypeProperty);
+       ConstructProperty(FuelInfoProperty);
+       ConstructProperty(SizeProperty);
        ConstructProperty(DoorsProperty);
+       ConstructProperty(WheelInformationProperty);
 
        PropertyList list = VehicleProperty::customProperties();
 
index 7c24b7d..1742eb3 100644 (file)
@@ -76,54 +76,79 @@ void ExampleSourcePlugin::getPropertyAsync(AsyncPropertyReply *reply)
        {
                VehicleProperty::VehicleSpeedType temp(velocity);
                reply->value = &temp;
+               reply->success = true;
                reply->completed(reply);
        }
        else if(reply->property == VehicleProperty::EngineSpeed)
        {
                VehicleProperty::EngineSpeedType temp(engineSpeed);
                reply->value = &temp;
+               reply->success = true;
                reply->completed(reply);
        }
        else if(reply->property == VehicleProperty::AccelerationX)
        {
                VehicleProperty::AccelerationType temp(accelerationX);
                reply->value = &temp;
+               reply->success = true;
                reply->completed(reply);
        }
        else if(reply->property == VehicleProperty::TransmissionShiftPosition)
        {
                VehicleProperty::TransmissionShiftPositionType temp(transmissionShiftPostion);
                reply->value = &temp;
+               reply->success = true;
                reply->completed(reply);
        }
        else if(reply->property == VehicleProperty::SteeringWheelAngle)
        {
                VehicleProperty::SteeringWheelAngleType temp(steeringWheelAngle);
                reply->value = &temp;
+               reply->success = true;
                reply->completed(reply);
        }
        else if(reply->property == VehicleProperty::VIN)
        {
                VehicleProperty::VINType temp("ABC00000000000000");
                reply->value = &temp;
+               reply->success = true;
                reply->completed(reply);
        }
        else if(reply->property == VehicleProperty::WMI)
        {
                VehicleProperty::WMIType temp("abc");
                reply->value = &temp;
+               reply->success = true;
                reply->completed(reply);
        }
        else if(reply->property == VehicleProperty::BatteryVoltage)
        {
                VehicleProperty::BatteryVoltageType temp(12.6);
                reply->value = &temp;
+               reply->success = true;
                reply->completed(reply);
        }
        else if(reply->property == VehicleProperty::ExteriorBrightness)
        {
                VehicleProperty::ExteriorBrightnessType temp(1000);
                reply->value = &temp;
+               reply->success = true;
+               reply->completed(reply);
+       }
+       else if(reply->property == VehicleProperty::DoorsPerRow)
+       {
+               VehicleProperty::DoorsPerRowType temp;
+
+               BasicPropertyType<uint16_t> row1(2);
+               BasicPropertyType<uint16_t> row2(2);
+               BasicPropertyType<uint16_t> row3(1);
+
+               temp.append(&row1);
+               temp.append(&row2);
+               temp.append(&row3);
+
+               reply->value = &temp;
+               reply->success = true;
                reply->completed(reply);
        }
 }
@@ -158,6 +183,7 @@ PropertyList ExampleSourcePlugin::supported()
        props.push_back(VehicleProperty::BatteryVoltage);
        props.push_back(VehicleProperty::MachineGunTurretStatus);
        props.push_back(VehicleProperty::ExteriorBrightness);
+       props.push_back(VehicleProperty::DoorsPerRow);
        
        return props;
 }
index ce9449e..d980941 100644 (file)
@@ -77,6 +77,15 @@ void ExampleSink::supportedChanged(PropertyList supportedProperties)
 
        routingEngine->getPropertyAsync(batteryVoltageRequest);
 
+       AsyncPropertyRequest doorsPerRowRequest;
+       doorsPerRowRequest.property = VehicleProperty::DoorsPerRow;
+       doorsPerRowRequest.completed = [](AsyncPropertyReply* reply)
+       {
+               DebugOut(1)<<"Doors per row: "<<reply->value->toString()<<endl; delete reply;
+       };
+
+       routingEngine->getPropertyAsync(doorsPerRowRequest);
+
        auto getRangedCb = [](gpointer data)
        {
                AbstractRoutingEngine* routingEngine = (AbstractRoutingEngine*)data;
index 85f4c9f..7fbe297 100644 (file)
@@ -120,7 +120,7 @@ void WebSocketSinkManager::addSingleShotSink(libwebsocket* socket, VehicleProper
        }
        velocityRequest.completed = [socket,id,property](AsyncPropertyReply* reply)
        {
-               printf("Got property:%s\n",reply->value->toString().c_str());
+               DebugOut()<<"Got property: "<<reply->value->toString().c_str()<<endl;
                //uint16_t velocity = boost::any_cast<uint16_t>(reply->value);
                stringstream s;
                s.precision(15);