reverted varianttype
[profile/ivi/automotive-message-broker.git] / plugins / common / dbusexport.hpp
1 #include <string>
2 #include <unordered_map>
3 #include <unordered_set>
4 #include <listplusplus.h>
5 #include <abstractsink.h>
6 #include <abstractroutingengine.h>
7
8 namespace amb
9 {
10
11 std::unordered_map<std::string, std::unordered_set<Zone::Type>> getUniqueSourcesList(AbstractRoutingEngine *re, PropertyList implementedProperties)
12 {
13         std::unordered_map<std::string, std::unordered_set<Zone::Type>> uniqueSourcesList;
14
15         for(auto property : implementedProperties)
16         {
17                 std::vector<std::string> sources = re->sourcesForProperty(property);
18
19                 for(auto source : sources)
20                 {
21                         PropertyInfo info = re->getPropertyInfo(property, source);
22
23                         std::unordered_set<Zone::Type> uniqueZoneList;
24
25                         if(uniqueSourcesList.count(source))
26                         {
27                                 uniqueZoneList = uniqueSourcesList[source];
28                         }
29
30                         Zone::ZoneList zoneList = info.zones();
31
32                         if(!zoneList.size())
33                         {
34                                 uniqueZoneList.emplace(Zone::None);
35                         }
36
37                         for(auto zoneItr : zoneList)
38                         {
39                                 uniqueZoneList.emplace(zoneItr);
40                         }
41
42                         uniqueSourcesList[source] = uniqueZoneList;
43                 }
44         }
45
46         return uniqueSourcesList;
47 }
48
49 template <typename T>
50 void exportProperty(VehicleProperty::Property prop, AbstractRoutingEngine *re, GDBusConnection *connection)
51 {
52         T* t = new T(prop, re, connection);
53
54         prop = t->objectName();
55
56         /// check if we need more than one instance:
57
58         PropertyList implementedProperties = t->wantsProperties();
59
60         std::unordered_map<std::string, std::unordered_set<Zone::Type> > uniqueSourcesList = getUniqueSourcesList(re, implementedProperties);
61
62         delete t;
63
64         PropertyList supported = re->supported();
65
66         for(auto itr : uniqueSourcesList)
67         {
68                 std::unordered_set<Zone::Type> zones = itr.second;
69
70                 std::string source = itr.first;
71
72                 std::string objectPath = "/" + source;
73
74                 boost::algorithm::erase_all(objectPath, "-");
75
76                 for(auto zone : zones)
77                 {
78                         T* t = new T(prop, re, connection);
79                         std::stringstream fullobjectPath;
80                         fullobjectPath<< objectPath << "/" << zone << "/" <<t->objectName();
81                         t->setObjectPath(fullobjectPath.str());
82                         t->setSourceFilter(source);
83                         t->setZoneFilter(zone);
84                         t->supportedChanged(supported);
85                 }
86
87         }
88 }
89
90 template <typename T>
91 void exportProperty(AbstractRoutingEngine *re, GDBusConnection *connection)
92 {
93         exportProperty<T>("", re, connection);
94 }
95
96 }