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>
36 struct ServerDeserialization {
38 template<typename T, typename ...Args>
39 static bool deserialize(GVariant* g, T* arg1, Args... args)
43 GVariantIter* iterator = g_variant_iter_new(g);
44 if (NULL == iterator) {
47 if (!deserializeIterator(iterator, arg1)) {
48 g_variant_iter_free(iterator);
51 if (!deserializeIterator(iterator, args...)) {
52 g_variant_iter_free(iterator);
55 g_variant_iter_free(iterator);
60 static bool deserialize(GVariant* g, T* arg1)
64 GVariantIter* iterator = g_variant_iter_new(g);
65 if (NULL == iterator) {
68 if (!deserializeIterator(iterator, arg1)) {
69 g_variant_iter_free(iterator);
72 g_variant_iter_free(iterator);
76 // deserialization from GVariant tuple iterator
77 template<typename T, typename ...Args>
78 static bool deserializeIterator(GVariantIter* g, T* arg1, Args... args)
82 GVariant* elem = g_variant_iter_next_value(g);
86 if (!deserializeElem(elem, arg1)) {
89 if (!deserializeIterator(g, args...)) {
96 static bool deserializeIterator(GVariantIter* g, T* arg1)
100 GVariant* elem = g_variant_iter_next_value(g);
104 if (!deserializeElem(elem, arg1)) {
107 g_variant_unref(elem);
111 // type specialization
112 static bool deserializeElem(GVariant* parameters, std::string* outStr)
114 const gchar* arg = g_variant_get_string(parameters, NULL);
115 *outStr = std::string(arg);
119 static bool deserializeElem(GVariant* parameters, int* outInt)
121 gint32 arg = g_variant_get_int32(parameters);
126 static bool deserializeElem(GVariant* parameters, unsigned* outInt)
128 guint32 arg = g_variant_get_uint32(parameters);
133 static bool deserializeElem(GVariant* parameters, bool* outInt)
135 gboolean arg = g_variant_get_boolean(parameters);
140 static bool deserializeElem(GVariant* parameters, float* outInt)
142 gdouble arg = g_variant_get_double(parameters);
143 *outInt = static_cast<float>(arg);
147 static bool deserializeElem(GVariant* parameters,
148 std::vector<std::string>* outArray)
152 const gchar** args = g_variant_get_strv(
155 for (i = 0; i < length; ++i) {
156 outArray->push_back(std::string(args[i]));
162 static bool deserializeElem(GVariant* parameters,
163 std::list<std::string>* outArray)
167 const gchar** args = g_variant_get_strv(
170 for (i = 0; i < length; ++i) {
171 outArray->push_back(std::string(args[i]));
181 #endif // DPL_DBUS_DBUS_SERVER_DESERIALIZATION_H_