reverted varianttype
[profile/ivi/automotive-message-broker.git] / plugins / common / varianttype.h
1 /*
2 Copyright (C) 2012 Intel Corporation
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17 */
18
19 #ifndef VARIANTTYPE_H_
20 #define VARIANTTYPE_H_
21
22 #include <string>
23 #include <functional>
24 #include <boost/any.hpp>
25 #include <glib.h>
26
27 #include "debugout.h"
28 #include "abstractpropertytype.h"
29 #include "abstractroutingengine.h"
30 #include "vehicleproperty.h"
31
32 class AbstractDBusInterface;
33
34 using namespace std;
35
36 typedef function<void (boost::any)> SetterFunc;
37
38 class VariantType
39 {
40
41 public:
42
43         enum Access {
44                 Read,
45                 Write,
46                 ReadWrite
47         };
48
49         VariantType(AbstractRoutingEngine* re, VehicleProperty::Property ambPropertyName, std::string propertyName,  Access access);
50         virtual ~VariantType();
51
52         bool operator == (VariantType & other)
53         {
54                 return (other.name() == name()
55                                 && other.ambPropertyName() == ambPropertyName()
56                                 && other.sourceFilter() == sourceFilter()
57                                 && other.zoneFilter() == zoneFilter());
58         }
59
60         virtual void setSetterFunction(SetterFunc setterFunc)
61         {
62                 mSetterFunc = setterFunc;
63         }
64
65         virtual const string signature()
66         {
67                 GVariant* var = toVariant();
68                 if(!var) return "";
69
70                 const string s = g_variant_get_type_string(var);
71                 g_variant_unref(var);
72                 return s;
73         }
74
75         virtual string name()
76         {
77                 return mPropertyName;
78         }
79
80         virtual VehicleProperty::Property ambPropertyName()
81         {
82                 return mAmbPropertyName;
83         }
84
85         virtual Access access()
86         {
87                 return mAccess;
88         }
89
90         void setSourceFilter(std::string filter) { mSourceFilter = filter; }
91         void setZoneFilter(Zone::Type zone)
92         {
93                 if(mValue)
94                         mValue->zone = zone;
95
96                 mZoneFilter = zone;
97         }
98
99         std::string sourceFilter() { return mSourceFilter; }
100         Zone::Type zoneFilter() { return mZoneFilter; }
101
102         virtual GVariant* toVariant();
103         virtual void fromVariant(GVariant *value);
104
105         double timestamp()
106         {
107                 if(mValue)
108                         return mValue->timestamp;
109                 return 0;
110         }
111
112         int sequence()
113         {
114                 if(mValue)
115                         return mValue->sequence;
116                 return 0;
117         }
118
119         virtual void setValue(AbstractPropertyType* val)
120         {
121                 if(!val)
122                         return;
123
124                 if(!mValue)
125                 {
126                         mValue = val->copy();
127                 }
128                 else
129                 {
130                         mValue->quickCopy(val);
131                 }
132
133                 if(mUpdateFrequency == 0)
134                 {
135                         PropertyInfo info = routingEngine->getPropertyInfo(mValue->name, mValue->sourceUuid);
136
137                         if(info.isValid())
138                                 mUpdateFrequency = info.updateFrequency();
139                         else
140                                 mUpdateFrequency = -1;
141                 }
142         }
143
144         virtual void updateValue(AbstractPropertyType* val)
145         {
146                 setValue(val);
147         }
148
149         int updateFrequency()
150         {
151                 return mUpdateFrequency;
152         }
153
154         AbstractPropertyType* value()
155         {
156                 return mValue;
157         }
158
159         void initialize();
160
161 protected:
162
163         std::string mSourceFilter;
164         Zone::Type mZoneFilter;
165         int mUpdateFrequency;
166         AbstractRoutingEngine* routingEngine;
167         string mPropertyName;
168         VehicleProperty::Property mAmbPropertyName;
169         SetterFunc mSetterFunc;
170         Access mAccess;
171         AbstractPropertyType* mValue;
172         AbstractDBusInterface* mInterface;
173
174 private:
175         bool mInitialized;
176 };
177
178 #endif // ABSTRACTPROPERTY_H