Initialize Tizen 2.3
[framework/web/wrt-commons.git] / modules_mobile / dbus / include / dpl / dbus / dbus_serialization.h
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /*
17  * @file        dbus_serialization.h
18  * @author      Tomasz Swierczek (t.swierczek@samsung.com)
19  * @version     1.0
20  * @brief       Header file for DBus data derialization
21  */
22
23 #ifndef DPL_DBUS_DBUS_SERIALIZATION_H_
24 #define DPL_DBUS_DBUS_SERIALIZATION_H_
25
26 #include <map>
27 #include <vector>
28 #include <list>
29 #include <set>
30 #include <string>
31 #include <dbus/dbus.h>
32 #include <dpl/foreach.h>
33 #include <dpl/dbus/dbus_signature.h>
34
35 namespace DPL {
36 namespace DBus {
37 struct Serialization
38 {
39     // std::string
40     static bool serialize(
41         DBusMessageIter* argsIterator,
42         const std::string& str)
43     {
44         return serialize(argsIterator, str.c_str());
45     }
46
47     // float case - send as double
48     static bool serialize(DBusMessageIter* argsIterator, const float& arg)
49     {
50         const double d = static_cast<double>(arg);
51         return serialize(argsIterator, d);
52     }
53
54     // char* and all integer types + doubles
55     template<typename T>
56     static bool serialize(DBusMessageIter* argsIterator, const T& arg)
57     {
58         return dbus_message_iter_append_basic(argsIterator,
59                                               SimpleType<T>::value,
60                                               &arg);
61     }
62
63     // dbus array serialization
64     template<typename T>
65     static bool serializeContainer(
66         DBusMessageIter* argsIterator,
67         const T& arg)
68     {
69         typename T::const_iterator containerIt;
70         DBusMessageIter subIterator;
71         if (!dbus_message_iter_open_container(argsIterator, DBUS_TYPE_ARRAY,
72                                               Signature<typename T::value_type>
73                                                   ::value(), &subIterator))
74         {
75             return false;
76         }
77         FOREACH(containerIt, arg) {
78             if (!serialize(&subIterator, *containerIt)) {
79                 return false;
80             }
81         }
82         return dbus_message_iter_close_container(argsIterator, &subIterator);
83     }
84
85     // std::vector
86     template<typename T>
87     static bool serialize(
88         DBusMessageIter* argsIterator,
89         const std::vector<T> &arg)
90     {
91         return serializeContainer(argsIterator, arg);
92     }
93
94     // std::list
95     template<typename T>
96     static bool serialize(
97         DBusMessageIter* argsIterator,
98         const std::list<T> &arg)
99     {
100         return serializeContainer(argsIterator, arg);
101     }
102
103     // std::set
104     template<typename T>
105     static bool serialize(
106         DBusMessageIter* argsIterator,
107         const std::set<T> &arg)
108     {
109         return serializeContainer(argsIterator, arg);
110     }
111
112     // std::pair
113     template<typename A, typename B>
114     static bool serialize(
115         DBusMessageIter* argsIterator,
116         const std::pair<A, B> &arg)
117     {
118         DBusMessageIter dictEntryIterator;
119         if (!dbus_message_iter_open_container(argsIterator,
120                                               DBUS_TYPE_DICT_ENTRY, NULL,
121                                               &dictEntryIterator))
122         {
123             return false;
124         }
125         if (!serialize(dictEntryIterator, arg.first)) {
126             return false;
127         }
128         if (!serialize(dictEntryIterator, arg.second)) {
129             return false;
130         }
131         return dbus_message_iter_close_container(argsIterator,
132                                                  &dictEntryIterator);
133     }
134
135     // std::map
136     template<typename K, typename V>
137     static bool serialize(
138         DBusMessageIter* argsIterator,
139         const std::map<K, V> &arg)
140     {
141         return serializeContainer(argsIterator, arg);
142     }
143 };
144
145 // char* and all integer types + doubles
146 template<>
147 inline bool Serialization::serialize<bool>(DBusMessageIter* argsIterator,
148                                            const bool& arg)
149 {
150     unsigned int value = static_cast<unsigned int>(arg);
151     return dbus_message_iter_append_basic(argsIterator,
152                                           SimpleType<bool>::value,
153                                           &value);
154 }
155 } // namespace DBus
156 } // namespace DPL
157
158 #endif // DPL_DBUS_DBUS_SERIALIZATION_H_