Update User Agent String
[framework/web/wrt-commons.git] / modules / dbus / include / dpl / dbus / dbus_server_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_server_serialization.h
18  * @author      Tomasz Swierczek (t.swierczek@samsung.com)
19  * @version     1.0
20  * @brief       Header file for DBus data serialization to GVariant
21  */
22
23 #ifndef DPL_DBUS_DBUS_SERVER_SERIALIZATION_H_
24 #define DPL_DBUS_DBUS_SERVER_SERIALIZATION_H_
25
26 #include <string>
27 #include <gio/gio.h>
28
29 namespace DPL {
30 namespace DBus {
31
32 struct ServerSerialization {
33
34     template<typename ...Args>
35     static GVariant* serialize(Args... args)
36     {
37         GVariantBuilder* builder = g_variant_builder_new(G_VARIANT_TYPE_TUPLE);
38         if (NULL == builder) {
39             return NULL;
40         }
41         serializeBuilder(builder, args ...);
42         return g_variant_builder_end(builder);
43     }
44
45     // serialization on GVariantBuilder
46     template<typename T, typename ...Args>
47     static void serializeBuilder(GVariantBuilder* builder,
48                                  const T& arg,
49                                  Args... args)
50     {
51         serializeElem(builder, arg);
52         serializeBuilder(builder, args ...);
53     }
54
55     template<typename T>
56     static void serializeBuilder(GVariantBuilder* builder, const T& arg)
57     {
58         serializeElem(builder, arg);
59     }
60
61     // type specialization
62     static void serializeElem(GVariantBuilder* builder, int arg)
63     {
64         g_variant_builder_add_value(builder, g_variant_new_int32(arg));
65     }
66
67     static void serializeElem(GVariantBuilder* builder, unsigned arg)
68     {
69         g_variant_builder_add_value(builder, g_variant_new_uint32(arg));
70     }
71
72     static void serializeElem(GVariantBuilder* builder, bool arg)
73     {
74         g_variant_builder_add_value(builder, g_variant_new_boolean(arg));
75     }
76
77     static void serializeElem(GVariantBuilder* builder, float arg)
78     {
79         gdouble d = static_cast<gdouble>(arg);
80         g_variant_builder_add_value(builder, g_variant_new_double(d));
81     }
82
83     static void serializeElem(GVariantBuilder* builder, const char* arg)
84     {
85         g_variant_builder_add_value(builder, g_variant_new_string(arg));
86     }
87
88     static void serializeElem(GVariantBuilder* builder,
89                                  const std::string& arg)
90     {
91         g_variant_builder_add_value(builder, g_variant_new_string(arg.c_str()));
92     }
93 };
94
95 } // namespace DBus
96 } // namespace DPL
97
98 #endif // DPL_DBUS_DBUS_SERVER_SERIALIZATION_H_