Initialize Tizen 2.3
[framework/web/wrt-commons.git] / modules_wearable / 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 struct ServerSerialization {
32     template<typename ... Args>
33     static GVariant* serialize(Args ... args)
34     {
35         GVariantBuilder* builder = g_variant_builder_new(G_VARIANT_TYPE_TUPLE);
36         if (NULL == builder) {
37             return NULL;
38         }
39         serializeBuilder(builder, args ...);
40         return g_variant_builder_end(builder);
41     }
42
43     // serialization on GVariantBuilder
44     template<typename T, typename ... Args>
45     static void serializeBuilder(GVariantBuilder* builder,
46                                  const T& arg,
47                                  Args ... args)
48     {
49         serializeElem(builder, arg);
50         serializeBuilder(builder, args ...);
51     }
52
53     template<typename T>
54     static void serializeBuilder(GVariantBuilder* builder, const T& arg)
55     {
56         serializeElem(builder, arg);
57     }
58
59     // type specialization
60     static void serializeElem(GVariantBuilder* builder, int arg)
61     {
62         g_variant_builder_add_value(builder, g_variant_new_int32(arg));
63     }
64
65     static void serializeElem(GVariantBuilder* builder, unsigned arg)
66     {
67         g_variant_builder_add_value(builder, g_variant_new_uint32(arg));
68     }
69
70     static void serializeElem(GVariantBuilder* builder, bool arg)
71     {
72         g_variant_builder_add_value(builder, g_variant_new_boolean(arg));
73     }
74
75     static void serializeElem(GVariantBuilder* builder, float arg)
76     {
77         gdouble d = static_cast<gdouble>(arg);
78         g_variant_builder_add_value(builder, g_variant_new_double(d));
79     }
80
81     static void serializeElem(GVariantBuilder* builder, const char* arg)
82     {
83         g_variant_builder_add_value(builder, g_variant_new_string(arg));
84     }
85
86     static void serializeElem(GVariantBuilder* builder,
87                               const std::string& arg)
88     {
89         g_variant_builder_add_value(builder, g_variant_new_string(arg.c_str()));
90     }
91 };
92 } // namespace DBus
93 } // namespace DPL
94
95 #endif // DPL_DBUS_DBUS_SERVER_SERIALIZATION_H_