2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @file dbus_deserialization.h
18 * @author Tomasz Swierczek (t.swierczek@samsung.com)
20 * @brief Header file for DBus data deserialization
23 #ifndef DPL_DBUS_DBUS_DESERIALIZATION_H_
24 #define DPL_DBUS_DBUS_DESERIALIZATION_H_
31 #include <dbus/dbus.h>
32 #include <dpl/dbus/dbus_signature.h>
36 struct Deserialization
38 static bool deserializePrimitive(
39 DBusMessageIter* responseIterator,
43 if (dbus_message_iter_get_arg_type(responseIterator) != type) {
46 dbus_message_iter_get_basic(responseIterator, arg);
50 // char* and all integer types + doubles
52 static bool deserialize(DBusMessageIter* responseIterator, T* arg)
54 if (dbus_message_iter_get_arg_type(responseIterator)
55 != SimpleType<T>::value)
59 dbus_message_iter_get_basic(responseIterator, arg);
63 // float case - read as double
64 static bool deserialize(DBusMessageIter* responseIterator, float* arg)
67 if (!deserialize(responseIterator, &d)) {
70 *arg = static_cast<float>(d);
75 static bool deserialize(
76 DBusMessageIter* responseIterator,
80 if (!deserialize(responseIterator, &str)) {
83 *arg = std::string(str);
87 // dbus array deserialization
89 static bool deserializeContainer(
90 DBusMessageIter* responseIterator,
93 if (dbus_message_iter_get_arg_type(responseIterator)
98 DBusMessageIter subIterator;
99 dbus_message_iter_recurse(responseIterator, &subIterator);
100 while (dbus_message_iter_get_arg_type(&subIterator)
101 != DBUS_TYPE_INVALID)
103 arg->push_back(typename T::value_type());
104 if (!deserialize(&subIterator, &arg->back())) {
107 dbus_message_iter_next(&subIterator);
114 static bool deserialize(
115 DBusMessageIter* responseIterator,
118 return deserializeContainer(responseIterator, arg);
123 static bool deserialize(
124 DBusMessageIter* responseIterator,
127 return deserializeContainer(responseIterator, arg);
132 static bool deserialize(
133 DBusMessageIter* responseIterator,
136 if (dbus_message_iter_get_arg_type(responseIterator)
141 DBusMessageIter subIterator;
142 dbus_message_iter_recurse(responseIterator, &subIterator);
143 while (dbus_message_iter_get_arg_type(&subIterator)
144 != DBUS_TYPE_INVALID)
146 typename std::set<T>::value_type element;
147 if (!deserialize(&subIterator, &element)) {
150 arg->insert(element);
151 dbus_message_iter_next(&subIterator);
157 template<typename A, typename B>
158 static bool deserialize(
159 DBusMessageIter* argsIterator,
160 const std::pair<A, B>* arg)
162 if (dbus_message_iter_get_arg_type(argsIterator)
163 != DBUS_TYPE_DICT_ENTRY)
167 DBusMessageIter dictEntryIterator;
168 dbus_message_iter_recurse(argsIterator, &dictEntryIterator);
169 if (!deserialize(dictEntryIterator, &arg->first)) {
172 dbus_message_iter_next(&dictEntryIterator);
173 if (!deserialize(dictEntryIterator, &arg->second)) {
180 template<typename K, typename V>
181 static bool deserialize(
182 DBusMessageIter* responseIterator,
183 const std::map<K, V>* arg)
185 if (dbus_message_iter_get_arg_type(responseIterator)
190 DBusMessageIter subIterator;
191 dbus_message_iter_recurse(responseIterator, &subIterator);
192 while (dbus_message_iter_get_arg_type(&subIterator)
193 != DBUS_TYPE_INVALID)
195 typename std::pair<K, V> element;
196 if (!deserialize(&subIterator, &element)) {
199 arg->insert(element);
200 dbus_message_iter_next(&subIterator);
207 inline bool Deserialization::deserialize<bool>(
208 DBusMessageIter* responseIterator,
212 if (dbus_message_iter_get_arg_type(responseIterator)
213 != SimpleType<bool>::value)
217 dbus_message_iter_get_basic(responseIterator, &value);
218 *arg = static_cast<bool>(value);
224 #endif // DPL_DBUS_DBUS_DESERIALIZATION_H_