Updated comments and fixed tabbing
[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 class VariantType
35 {
36
37 public:
38
39         enum Access {
40                 Read,
41                 Write,
42                 ReadWrite
43         };
44
45         VariantType(AbstractRoutingEngine* re, VehicleProperty::Property ambPropertyName, std::string propertyName,  Access access);
46         virtual ~VariantType();
47
48         bool operator == (VariantType & other)
49         {
50                 return (other.name() == name()
51                                 && other.ambPropertyName() == ambPropertyName()
52                                 && other.sourceFilter() == sourceFilter()
53                                 && other.zoneFilter() == zoneFilter());
54         }
55
56         virtual const string signature()
57         {
58                 GVariant* var = toVariant();
59                 if(!var) return "";
60
61                 const string s = g_variant_get_type_string(var);
62                 g_variant_unref(var);
63                 return s;
64         }
65
66         virtual string name()
67         {
68                 return mPropertyName;
69         }
70
71         virtual VehicleProperty::Property ambPropertyName()
72         {
73                 return mAmbPropertyName;
74         }
75
76         virtual Access access()
77         {
78                 return mAccess;
79         }
80
81         void setSourceFilter(std::string filter) { mSourceFilter = filter; }
82         void setZoneFilter(Zone::Type zone)
83         {
84                 if(mValue)
85                         mValue->zone = zone;
86
87                 mZoneFilter = zone;
88         }
89
90         std::string sourceFilter() { return mSourceFilter; }
91         Zone::Type zoneFilter() { return mZoneFilter; }
92
93         virtual GVariant* toVariant();
94         virtual void fromVariant(GVariant *value, std::function<void (bool, AsyncPropertyReply::Error)> callback);
95
96         double timestamp()
97         {
98                 if(mValue)
99                         return mValue->timestamp;
100                 return 0;
101         }
102
103         int sequence()
104         {
105                 if(mValue)
106                         return mValue->sequence;
107                 return 0;
108         }
109
110         virtual void setValue(AbstractPropertyType* val)
111         {
112                 if(!val)
113                         return;
114
115                 if(!mValue)
116                 {
117                         mValue = val->copy();
118                 }
119                 else
120                 {
121                         mValue->quickCopy(val);
122                 }
123
124                 if(mUpdateFrequency == 0)
125                 {
126                         PropertyInfo info = routingEngine->getPropertyInfo(mValue->name, mValue->sourceUuid);
127
128                         if(info.isValid())
129                                 mUpdateFrequency = info.updateFrequency();
130                         else
131                                 mUpdateFrequency = -1;
132                 }
133         }
134
135         virtual void updateValue(AbstractPropertyType* val)
136         {
137                 setValue(val);
138         }
139
140         int updateFrequency()
141         {
142                 return mUpdateFrequency;
143         }
144
145         AbstractPropertyType* value()
146         {
147                 return mValue;
148         }
149
150         void initialize();
151
152 protected:
153
154         std::string mSourceFilter;
155         Zone::Type mZoneFilter;
156         int mUpdateFrequency;
157         AbstractRoutingEngine* routingEngine;
158         string mPropertyName;
159         VehicleProperty::Property mAmbPropertyName;
160         Access mAccess;
161         AbstractPropertyType* mValue;
162         AbstractDBusInterface* mInterface;
163
164 private:
165         bool mInitialized;
166 };
167
168 #endif // ABSTRACTPROPERTY_H