af0c2ef279706e3913a61d55716f0f6b894ac34c
[platform/framework/web/wrt-commons.git] / modules / dbus / include / dpl / dbus / dbus_signature.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_deserialization.h
18  * @author      Tomasz Swierczek (t.swierczek@samsung.com)
19  * @version     1.0
20  * @brief       Header file for DBus data signatures
21  */
22
23 #ifndef DPL_DBUS_SIGNATURE_H
24 #define DPL_DBUS_SIGNATURE_H
25
26 #include <dbus/dbus.h>
27 #include <string>
28
29 namespace DPL {
30 namespace DBus {
31
32 template<typename T>
33 struct SimpleType;
34
35 template<typename T>
36 struct Signature
37 {
38     static inline const char* value()
39     {
40         static const char signature[] =
41         { (char) SimpleType<T>::value, 0 };
42         return signature;
43     }
44 };
45
46 // signed integer types
47 template<int size>
48 struct __SignedIntegerType;
49
50 template<>
51 struct __SignedIntegerType<1>
52 {
53     static const int value = DBUS_TYPE_INT16;
54 };
55
56 template<>
57 struct __SignedIntegerType<2>
58 {
59     static const int value = DBUS_TYPE_INT16;
60 };
61
62 template<>
63 struct __SignedIntegerType<4>
64 {
65     static const int value = DBUS_TYPE_INT32;
66 };
67
68 template<>
69 struct __SignedIntegerType<8>
70 {
71     static const int value = DBUS_TYPE_INT64;
72 };
73
74 // unsigned integer types
75 template<int size>
76 struct __UnsignedIntegerType;
77
78 template<>
79 struct __UnsignedIntegerType<1>
80 {
81     static const int value = DBUS_TYPE_BYTE;
82 };
83 template<>
84 struct __UnsignedIntegerType<2>
85 {
86     static const int value = DBUS_TYPE_UINT16;
87 };
88 template<>
89 struct __UnsignedIntegerType<4>
90 {
91     static const int value = DBUS_TYPE_UINT32;
92 };
93 template<>
94 struct __UnsignedIntegerType<8>
95 {
96     static const int value = DBUS_TYPE_UINT64;
97 };
98
99 // basic types
100 template<>
101 struct SimpleType<bool>
102 {
103     static const int value = DBUS_TYPE_BOOLEAN;
104 };
105
106 template<>
107 struct SimpleType<char>
108 {
109     static const int value = __UnsignedIntegerType<sizeof(char)>::value;
110 };
111
112 template<>
113 struct SimpleType<signed char>
114 {
115     static const int value = __SignedIntegerType<sizeof(signed char)>::value;
116 };
117
118 template<>
119 struct SimpleType<unsigned char>
120 {
121     static const int value =
122             __UnsignedIntegerType<sizeof(unsigned char)>::value;
123 };
124
125 template<>
126 struct SimpleType<short>
127 {
128     static const int value = __SignedIntegerType<sizeof(short)>::value;
129 };
130
131 template<>
132 struct SimpleType<unsigned short>
133 {
134     static const int value =
135             __UnsignedIntegerType<sizeof(unsigned short)>::value;
136 };
137
138 template<>
139 struct SimpleType<int>
140 {
141     static const int value = __SignedIntegerType<sizeof(int)>::value;
142 };
143
144 template<>
145 struct SimpleType<unsigned int>
146 {
147     static const int value =
148             __UnsignedIntegerType<sizeof(unsigned int)>::value;
149 };
150
151 template<>
152 struct SimpleType<long>
153 {
154     static const int value = __SignedIntegerType<sizeof(long)>::value;
155 };
156
157 template<>
158 struct SimpleType<unsigned long>
159 {
160     static const int value =
161             __UnsignedIntegerType<sizeof(unsigned long)>::value;
162 };
163
164 template<>
165 struct SimpleType<long long>
166 {
167     static const int value = __SignedIntegerType<sizeof(long long)>::value;
168 };
169
170 template<>
171 struct SimpleType<unsigned long long>
172 {
173     static const int value = __UnsignedIntegerType<
174             sizeof(unsigned long long)>::value;
175 };
176
177 template<>
178 struct SimpleType<float>
179 {
180     static const int value = DBUS_TYPE_DOUBLE;
181 };
182
183 template<>
184 struct SimpleType<double>
185 {
186     static const int value = DBUS_TYPE_DOUBLE;
187 };
188
189 template<>
190 struct SimpleType<const char *>
191 {
192     static const int value = DBUS_TYPE_STRING;
193 };
194
195 template<>
196 struct SimpleType<char *>
197 {
198     static const int value = DBUS_TYPE_STRING;
199 };
200
201 template<>
202 struct SimpleType<std::string>
203 {
204     static const int value = DBUS_TYPE_STRING;
205 };
206
207 // STL containers signatures
208
209 // generic array
210 template<typename T>
211 struct ArraySignature
212 {
213     static inline const char* value()
214     {
215         static const std::string signature = std::string(
216                 DBUS_TYPE_ARRAY_AS_STRING) + Signature<T>::value();
217         return signature.c_str();
218     }
219 };
220
221 // std::vector
222 template<typename T>
223 struct Signature<std::vector<T> > : public ArraySignature<T>
224 {
225 };
226
227 // std::list
228 template<typename T>
229 struct Signature<std::list<T> > : public ArraySignature<T>
230 {
231 };
232
233 // std::set
234 template<typename T>
235 struct Signature<std::set<T> > : public ArraySignature<T>
236 {
237 };
238
239 // std::pair
240 template<typename K, typename V>
241 struct Signature<std::pair<K, V> >
242 {
243     static inline const char* value()
244     {
245         static const std::string signature = std::string(
246                 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING)
247                 + Signature<K>::value() + Signature<V>::value()
248                 + DBUS_DICT_ENTRY_END_CHAR_AS_STRING;
249         return signature.c_str();
250     }
251 };
252
253 // std::map
254 template<typename K, typename V>
255 struct Signature<std::map<K, V> > : public ArraySignature<std::pair<K, V> >
256 {
257 };
258
259 } // namespace DBus
260 } // namespace DPL
261
262 #endif // DPL_DBUS_SIGNATURE_H