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_server_deserialization.h
18 * @author Tomasz Swierczek (t.swierczek@samsung.com)
20 * @brief Header file for DBus data deserialization from GVariant
23 #ifndef DPL_DBUS_DBUS_SERVER_DESERIALIZATION_H_
24 #define DPL_DBUS_DBUS_SERVER_DESERIALIZATION_H_
31 #include <dpl/assert.h>
35 struct ServerDeserialization {
36 template<typename T, typename ... Args>
37 static bool deserialize(GVariant* g, T* arg1, Args ... args)
41 GVariantIter* iterator = g_variant_iter_new(g);
42 if (NULL == iterator) {
45 if (!deserializeIterator(iterator, arg1)) {
46 g_variant_iter_free(iterator);
49 if (!deserializeIterator(iterator, args ...)) {
50 g_variant_iter_free(iterator);
53 g_variant_iter_free(iterator);
58 static bool deserialize(GVariant* g, T* arg1)
62 GVariantIter* iterator = g_variant_iter_new(g);
63 if (NULL == iterator) {
66 if (!deserializeIterator(iterator, arg1)) {
67 g_variant_iter_free(iterator);
70 g_variant_iter_free(iterator);
74 // deserialization from GVariant tuple iterator
75 template<typename T, typename ... Args>
76 static bool deserializeIterator(GVariantIter* g, T* arg1, Args ... args)
80 GVariant* elem = g_variant_iter_next_value(g);
84 if (!deserializeElem(elem, arg1)) {
87 if (!deserializeIterator(g, args ...)) {
94 static bool deserializeIterator(GVariantIter* g, T* arg1)
98 GVariant* elem = g_variant_iter_next_value(g);
102 if (!deserializeElem(elem, arg1)) {
105 g_variant_unref(elem);
109 // type specialization
110 static bool deserializeElem(GVariant* parameters, std::string* outStr)
112 const gchar* arg = g_variant_get_string(parameters, NULL);
113 *outStr = std::string(arg);
117 static bool deserializeElem(GVariant* parameters, int* outInt)
119 gint32 arg = g_variant_get_int32(parameters);
124 static bool deserializeElem(GVariant* parameters, unsigned* outInt)
126 guint32 arg = g_variant_get_uint32(parameters);
131 static bool deserializeElem(GVariant* parameters, bool* outInt)
133 gboolean arg = g_variant_get_boolean(parameters);
138 static bool deserializeElem(GVariant* parameters, float* outInt)
140 gdouble arg = g_variant_get_double(parameters);
141 *outInt = static_cast<float>(arg);
145 static bool deserializeElem(GVariant* parameters,
146 std::vector<std::string>* outArray)
150 const gchar** args = g_variant_get_strv(
153 for (i = 0; i < length; ++i) {
154 outArray->push_back(std::string(args[i]));
160 static bool deserializeElem(GVariant* parameters,
161 std::list<std::string>* outArray)
165 const gchar** args = g_variant_get_strv(
168 for (i = 0; i < length; ++i) {
169 outArray->push_back(std::string(args[i]));
178 #endif // DPL_DBUS_DBUS_SERVER_DESERIALIZATION_H_