# DBus-C++

## Desktop Communication Bus System

libdbus-c-- Documentation 0.9.0

- Files
- Data Structures
- Globals
- Main Page

types.h
Go to the documentation of this file.
1/*
2 *
3 * D-Bus++ - C++ bindings for D-Bus
4 *
5 * Copyright (C) 2005-2007 Paolo Durante <shackan@gmail.com>
6 *
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 */
23
24
25#ifndef __DBUSXX_TYPES_H
26#define __DBUSXX_TYPES_H
27
28#include <stdint.h>
29#include <string>
30#include <vector>
31#include <map>
32
33#include "api.h"
34#include "util.h"
35#include "message.h"
36#include "error.h"
37
38 namespace DBus
39{
40
41 struct DXXAPI Path : public std::string
42{
43 Path() {}
44 Path(const std::string &s) : std::string(s) {}
45 Path(const char *c) : std::string(c) {}
46 Path &operator = (std::string &s)
47 {
48 std::string::operator = (s);
49 return *this;
50 }
51};
52
53 struct DXXAPI Signature : public std::string
54{
55 Signature() {}
56 Signature(const std::string &s) : std::string(s) {}
57 Signature(const char *c) : std::string(c) {}
58 Signature &operator = (std::string &s)
59 {
60 std::string::operator = (s);
61 return *this;
62 }
63};
64
65 struct DXXAPI Invalid {};
66
67 class DXXAPI Variant
68{
69 public:
70
71 Variant();
72
73 Variant(MessageIter &it);
74
75 Variant &operator = (const Variant &v);
76
77 const Signature signature() const;
78
79 void clear();
80
81 MessageIter reader() const
82 {
83 return _msg.reader();
84 }
85
86 MessageIter writer()
87 {
88 return _msg.writer();
89 }
90
91 template <typename T>
92 operator T() const;
93
94 private:
95
96 Message _msg;
97};
98
99 template <
100 typename T1,
101 typename T2 = Invalid,
102 typename T3 = Invalid,
103 typename T4 = Invalid,
104 typename T5 = Invalid,
105 typename T6 = Invalid,
106 typename T7 = Invalid,
107 typename T8 = Invalid,
108 typename T9 = Invalid,
109 typename T10 = Invalid,
110 typename T11 = Invalid,
111 typename T12 = Invalid,
112 typename T13 = Invalid,
113 typename T14 = Invalid,
114 typename T15 = Invalid,
115 typename T16 = Invalid // nobody needs more than 16
116 >
117 struct Struct
118{
119 T1 _1;
120 T2 _2;
121 T3 _3;
122 T4 _4;
123 T5 _5;
124 T6 _6;
125 T7 _7;
126 T8 _8;
127 T9 _9;
128 T10 _10;
129 T11 _11;
130 T12 _12;
131 T13 _13;
132 T14 _14;
133 T15 _15;
134 T16 _16;
135};
136
137 template<typename K, typename V>
138 inline bool dict_has_key(const std::map<K, V>& map, const K &key)
139{
140 return map.find(key) != map.end();
141}
142
143 template <typename T>
144 struct type
145{
146 static std::string sig()
147 {
148 throw ErrorInvalidArgs("unknown type");
149 return "";
150 }
151};
152
153 template <> struct type<Variant>
154{
155 static std::string sig()
156 {
157 return "v";
158 }
159};
160 template <> struct type<uint8_t>
161{
162 static std::string sig()
163 {
164 return "y";
165 }
166};
167 template <> struct type<bool>
168{
169 static std::string sig()
170 {
171 return "b";
172 }
173};
174 template <> struct type<int16_t>
175{
176 static std::string sig()
177 {
178 return "n";
179 }
180};
181 template <> struct type<uint16_t>
182{
183 static std::string sig()
184 {
185 return "q";
186 }
187};
188 template <> struct type<int32_t>
189{
190 static std::string sig()
191 {
192 return "i";
193 }
194};
195 template <> struct type<uint32_t>
196{
197 static std::string sig()
198 {
199 return "u";
200 }
201};
202 template <> struct type<int64_t>
203{
204 static std::string sig()
205 {
206 return "x";
207 }
208};
209 template <> struct type<uint64_t>
210{
211 static std::string sig()
212 {
213 return "t";
214 }
215};
216 template <> struct type<double>
217{
218 static std::string sig()
219 {
220 return "d";
221 }
222};
223 template <> struct type<std::string>
224{
225 static std::string sig()
226 {
227 return "s";
228 }
229};
230 template <> struct type<Path>
231{
232 static std::string sig()
233 {
234 return "o";
235 }
236};
237 template <> struct type<Signature>
238{
239 static std::string sig()
240 {
241 return "g";
242 }
243};
244 template <> struct type<Invalid>
245{
246 static std::string sig()
247 {
248 return "";
249 }
250};
251
252 template <typename E>
253 struct type< std::vector<E> >
254{
255 static std::string sig()
256 {
257 return "a" + type<E>::sig();
258 }
259};
260
261 template <typename K, typename V>
262 struct type< std::map<K, V> >
263{
264 static std::string sig()
265 {
266 return "a{" + type<K>::sig() + type<V>::sig() + "}";
267 }
268};
269
270 template <
271 typename T1,
272 typename T2,
273 typename T3,
274 typename T4,
275 typename T5,
276 typename T6,
277 typename T7,
278 typename T8,
279 typename T9,
280 typename T10,
281 typename T11,
282 typename T12,
283 typename T13,
284 typename T14,
285 typename T15,
286 typename T16 // nobody needs more than 16
287 >
288 struct type< Struct<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16> >
289{
290 static std::string sig()
291 {
292 return "("
293 + type<T1>::sig()
294 + type<T2>::sig()
295 + type<T3>::sig()
296 + type<T4>::sig()
297 + type<T5>::sig()
298 + type<T6>::sig()
299 + type<T7>::sig()
300 + type<T8>::sig()
301 + type<T9>::sig()
302 + type<T10>::sig()
303 + type<T11>::sig()
304 + type<T12>::sig()
305 + type<T13>::sig()
306 + type<T14>::sig()
307 + type<T15>::sig()
308 + type<T16>::sig()
309 + ")";
310 }
311};
312
313 extern DXXAPI DBus::MessageIter &operator << (DBus::MessageIter &iter, const DBus::Variant &val);
314
315 inline DBus::MessageIter &operator << (DBus::MessageIter &iter, const DBus::Invalid &)
316{
317 return iter;
318}
319
320 inline DBus::MessageIter &operator << (DBus::MessageIter &iter, const uint8_t &val)
321{
322 iter.append_byte(val);
323 return iter;
324}
325
326 inline DBus::MessageIter &operator << (DBus::MessageIter &iter, const bool &val)
327{
328 iter.append_bool(val);
329 return iter;
330}
331
332 inline DBus::MessageIter &operator << (DBus::MessageIter &iter, const int16_t &val)
333{
334 iter.append_int16(val);
335 return iter;
336}
337
338 inline DBus::MessageIter &operator << (DBus::MessageIter &iter, const uint16_t &val)
339{
340 iter.append_uint16(val);
341 return iter;
342}
343
344 inline DBus::MessageIter &operator << (DBus::MessageIter &iter, const int32_t &val)
345{
346 iter.append_int32(val);
347 return iter;
348}
349
350 inline DBus::MessageIter &operator << (DBus::MessageIter &iter, const uint32_t &val)
351{
352 iter.append_uint32(val);
353 return iter;
354}
355
356 inline DBus::MessageIter &operator << (DBus::MessageIter &iter, const int64_t &val)
357{
358 iter.append_int64(val);
359 return iter;
360}
361
362 inline DBus::MessageIter &operator << (DBus::MessageIter &iter, const uint64_t &val)
363{
364 iter.append_uint64(val);
365 return iter;
366}
367
368 inline DBus::MessageIter &operator << (DBus::MessageIter &iter, const double &val)
369{
370 iter.append_double(val);
371 return iter;
372}
373
374 inline DBus::MessageIter &operator << (DBus::MessageIter &iter, const std::string &val)
375{
376 iter.append_string(val.c_str());
377 return iter;
378}
379
380 inline DBus::MessageIter &operator << (DBus::MessageIter &iter, const DBus::Path &val)
381{
382 iter.append_path(val.c_str());
383 return iter;
384}
385
386 inline DBus::MessageIter &operator << (DBus::MessageIter &iter, const DBus::Signature &val)
387{
388 iter.append_signature(val.c_str());
389 return iter;
390}
391
392 template<typename E>
393 inline DBus::MessageIter &operator << (DBus::MessageIter &iter, const std::vector<E>& val)
394{
395 const std::string sig = DBus::type<E>::sig();
396 DBus::MessageIter ait = iter.new_array(sig.c_str());
397
398 typename std::vector<E>::const_iterator vit;
399 for (vit = val.begin(); vit != val.end(); ++vit)
400 {
401 ait << *vit;
402 }
403
404 iter.close_container(ait);
405 return iter;
406}
407
408 template<>
409 inline DBus::MessageIter &operator << (DBus::MessageIter &iter, const std::vector<uint8_t>& val)
410{
411 DBus::MessageIter ait = iter.new_array("y");
412 ait.append_array('y', &val.front(), val.size());
413 iter.close_container(ait);
414 return iter;
415}
416
417 template<typename K, typename V>
418 inline DBus::MessageIter &operator << (DBus::MessageIter &iter, const std::map<K, V>& val)
419{
420 const std::string sig = "{" + DBus::type<K>::sig() + DBus::type<V>::sig() + "}";
421 DBus::MessageIter ait = iter.new_array(sig.c_str());
422
423 typename std::map<K, V>::const_iterator mit;
424 for (mit = val.begin(); mit != val.end(); ++mit)
425 {
426 DBus::MessageIter eit = ait.new_dict_entry();
427
428 eit << mit->first << mit->second;
429
430 ait.close_container(eit);
431 }
432
433 iter.close_container(ait);
434 return iter;
435}
436
437 template <
438 typename T1,
439 typename T2,
440 typename T3,
441 typename T4,
442 typename T5,
443 typename T6,
444 typename T7,
445 typename T8,
446 typename T9,
447 typename T10,
448 typename T11,
449 typename T12,
450 typename T13,
451 typename T14,
452 typename T15,
453 typename T16
454 >
455 inline DBus::MessageIter &operator << (DBus::MessageIter &iter, const DBus::Struct<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16>& val)
456{
457 DBus::MessageIter sit = iter.new_struct();
458
459 sit << val._1 << val._2 << val._3 << val._4
460 << val._5 << val._6 << val._7 << val._8
461 << val._9 << val._10 << val._11 << val._12
462 << val._13 << val._14 << val._15 << val._16;
463
464 iter.close_container(sit);
465
466 return iter;
467}
468
469 extern DXXAPI DBus::MessageIter &operator << (DBus::MessageIter &iter, const DBus::Variant &val);
470
471 inline DBus::MessageIter &operator >> (DBus::MessageIter &iter, DBus::Invalid &)
472{
473 return iter;
474}
475
476 inline DBus::MessageIter &operator >> (DBus::MessageIter &iter, uint8_t &val)
477{
478 val = iter.get_byte();
479 return ++iter;
480}
481
482 inline DBus::MessageIter &operator >> (DBus::MessageIter &iter, bool &val)
483{
484 val = iter.get_bool();
485 return ++iter;
486}
487
488 inline DBus::MessageIter &operator >> (DBus::MessageIter &iter, int16_t &val)
489{
490 val = iter.get_int16();
491 return ++iter;
492}
493
494 inline DBus::MessageIter &operator >> (DBus::MessageIter &iter, uint16_t &val)
495{
496 val = iter.get_uint16();
497 return ++iter;
498}
499
500 inline DBus::MessageIter &operator >> (DBus::MessageIter &iter, int32_t &val)
501{
502 val = iter.get_int32();
503 return ++iter;
504}
505
506 inline DBus::MessageIter &operator >> (DBus::MessageIter &iter, uint32_t &val)
507{
508 val = iter.get_uint32();
509 return ++iter;
510}
511
512 inline DBus::MessageIter &operator >> (DBus::MessageIter &iter, int64_t &val)
513{
514 val = iter.get_int64();
515 return ++iter;
516}
517
518 inline DBus::MessageIter &operator >> (DBus::MessageIter &iter, uint64_t &val)
519{
520 val = iter.get_uint64();
521 return ++iter;
522}
523
524 inline DBus::MessageIter &operator >> (DBus::MessageIter &iter, double &val)
525{
526 val = iter.get_double();
527 return ++iter;
528}
529
530 inline DBus::MessageIter &operator >> (DBus::MessageIter &iter, std::string &val)
531{
532 val = iter.get_string();
533 return ++iter;
534}
535
536 inline DBus::MessageIter &operator >> (DBus::MessageIter &iter, DBus::Path &val)
537{
538 val = iter.get_path();
539 return ++iter;
540}
541
542 inline DBus::MessageIter &operator >> (DBus::MessageIter &iter, DBus::Signature &val)
543{
544 val = iter.get_signature();
545 return ++iter;
546}
547
548 extern DXXAPI DBus::MessageIter &operator >> (DBus::MessageIter &iter, DBus::Variant &val);
549
550 template<typename E>
551 inline DBus::MessageIter &operator >> (DBus::MessageIter &iter, std::vector<E>& val)
552{
553 if (!iter.is_array())
554 throw DBus::ErrorInvalidArgs("array expected");
555
556 DBus::MessageIter ait = iter.recurse();
557
558 while (!ait.at_end())
559 {
560 E elem;
561
562 ait >> elem;
563
564 val.push_back(elem);
565 }
566 return ++iter;
567}
568
569 template<>
570 inline DBus::MessageIter &operator >> (DBus::MessageIter &iter, std::vector<uint8_t>& val)
571{
572 if (!iter.is_array())
573 throw DBus::ErrorInvalidArgs("array expected");
574
575 if (iter.array_type() != 'y')
576 throw DBus::ErrorInvalidArgs("byte-array expected");
577
578 DBus::MessageIter ait = iter.recurse();
579
580 uint8_t *array;
581 size_t length = ait.get_array(&array);
582
583 val.insert(val.end(), array, array + length);
584
585 return ++iter;
586}
587
588 template<typename K, typename V>
589 inline DBus::MessageIter &operator >> (DBus::MessageIter &iter, std::map<K, V>& val)
590{
591 if (!iter.is_dict())
592 throw DBus::ErrorInvalidArgs("dictionary value expected");
593
594 DBus::MessageIter mit = iter.recurse();
595
596 while (!mit.at_end())
597 {
598 K key;
599 V value;
600
601 DBus::MessageIter eit = mit.recurse();
602
603 eit >> key >> value;
604
605 val[key] = value;
606
607 ++mit;
608 }
609
610 return ++iter;
611}
612
613 template <
614 typename T1,
615 typename T2,
616 typename T3,
617 typename T4,
618 typename T5,
619 typename T6,
620 typename T7,
621 typename T8,
622 typename T9,
623 typename T10,
624 typename T11,
625 typename T12,
626 typename T13,
627 typename T14,
628 typename T15,
629 typename T16
630 >
631 inline DBus::MessageIter &operator >> (DBus::MessageIter &iter, DBus::Struct<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16>& val)
632{
633 DBus::MessageIter sit = iter.recurse();
634
635 sit >> val._1 >> val._2 >> val._3 >> val._4
636 >> val._5 >> val._6 >> val._7 >> val._8
637 >> val._9 >> val._10 >> val._11 >> val._12
638 >> val._13 >> val._14 >> val._15 >> val._16;
639
640 return ++iter;
641}
642
643 template <typename T>
644 inline DBus::Variant::operator T() const
645{
646 T cast;
647 DBus::MessageIter ri = _msg.reader();
648 ri >> cast;
649 return cast;
650}
651
652} /* namespace DBus */
653
654#endif//__DBUSXX_TYPES_H
655
api.h
DXXAPI
#define DXXAPI
Definition api.h:36
DBus::MessageIter
Definition message.h:45
DBus::MessageIter::new_struct
MessageIter new_struct()
Definition message.cpp:284
DBus::MessageIter::get_int64
signed long long get_int64()
Definition message.cpp:155
DBus::MessageIter::array_type
int array_type()
Definition message.cpp:244
DBus::MessageIter::get_path
const char * get_path()
Definition message.cpp:208
DBus::MessageIter::append_byte
bool append_byte(unsigned char byte)
Definition message.cpp:82
DBus::MessageIter::append_array
bool append_array(char type, const void *ptr, size_t length)
Definition message.cpp:239
DBus::MessageIter::get_string
const char * get_string()
Definition message.cpp:196
DBus::MessageIter::get_uint16
unsigned short get_uint16()
Definition message.cpp:124
DBus::MessageIter::get_uint64
unsigned long long get_uint64()
Definition message.cpp:167
DBus::MessageIter::is_array
bool is_array()
Definition message.cpp:256
DBus::MessageIter::get_bool
bool get_bool()
Definition message.cpp:100
DBus::MessageIter::get_signature
const char * get_signature()
Definition message.cpp:220
DBus::MessageIter::get_uint32
unsigned int get_uint32()
Definition message.cpp:148
DBus::MessageIter::append_signature
bool append_signature(const char *chars)
Definition message.cpp:215
DBus::MessageIter::new_dict_entry
MessageIter new_dict_entry()
Definition message.cpp:293
DBus::MessageIter::append_double
bool append_double(double d)
Definition message.cpp:186
DBus::MessageIter::get_array
int get_array(void *ptr)
Definition message.cpp:249
DBus::MessageIter::append_uint32
bool append_uint32(unsigned int u)
Definition message.cpp:143
DBus::MessageIter::get_byte
unsigned char get_byte()
Definition message.cpp:87
DBus::MessageIter::append_int16
bool append_int16(signed short i)
Definition message.cpp:107
DBus::MessageIter::append_path
bool append_path(const char *chars)
Definition message.cpp:203
DBus::MessageIter::get_int16
signed short get_int16()
Definition message.cpp:112
DBus::MessageIter::append_uint64
bool append_uint64(unsigned long long i)
Definition message.cpp:174
DBus::MessageIter::append_int32
bool append_int32(signed int i)
Definition message.cpp:131
DBus::MessageIter::append_bool
bool append_bool(bool b)
Definition message.cpp:94
DBus::MessageIter::is_dict
bool is_dict()
Definition message.cpp:261
DBus::MessageIter::append_int64
bool append_int64(signed long long i)
Definition message.cpp:162
DBus::MessageIter::close_container
void close_container(MessageIter &container)
Definition message.cpp:302
DBus::MessageIter::append_string
bool append_string(const char *chars)
Definition message.cpp:191
DBus::MessageIter::recurse
MessageIter recurse()
Definition message.cpp:227
DBus::MessageIter::append_uint16
bool append_uint16(unsigned short u)
Definition message.cpp:119
DBus::MessageIter::get_double
double get_double()
Definition message.cpp:179
DBus::MessageIter::at_end
bool at_end()
Definition message.cpp:46
DBus::MessageIter::get_int32
signed int get_int32()
Definition message.cpp:136
DBus::MessageIter::new_array
MessageIter new_array(const char *sig)
Definition message.cpp:266
DBus::Message
Definition message.h:159
DBus::Variant
Definition types.h:68
DBus::Variant::Variant
Variant()
Definition types.cpp:39
DBus::Variant::reader
MessageIter reader() const
Definition types.h:81
DBus::Variant::clear
void clear()
Definition types.cpp:61
DBus::Variant::_msg
Message _msg
Definition types.h:96
DBus::Variant::writer
MessageIter writer()
Definition types.h:86
DBus::Variant::signature
const Signature signature() const
Definition types.cpp:67
error.h
message.h
DBus
Definition connection_p.h:42
DBus::dict_has_key
bool dict_has_key(const std::map< K, V > &map, const K &key)
Definition types.h:138
DBus::operator<<
MessageIter & operator<<(MessageIter &iter, const Variant &val)
Definition types.cpp:78
DBus::operator>>
MessageIter & operator>>(MessageIter &iter, Variant &val)
Definition types.cpp:92
DBus::ErrorInvalidArgs
Definition error.h:187
DBus::Invalid
Definition types.h:65
DBus::Path
Definition types.h:42
DBus::Path::Path
Path(const char *c)
Definition types.h:45
DBus::Path::Path
Path(const std::string &s)
Definition types.h:44
DBus::Path::Path
Path()
Definition types.h:43
DBus::Signature
Definition types.h:54
DBus::Signature::Signature
Signature(const char *c)
Definition types.h:57
DBus::Signature::Signature
Signature()
Definition types.h:55
DBus::Signature::Signature
Signature(const std::string &s)
Definition types.h:56
DBus::Struct
Definition types.h:118
DBus::Struct::_11
T11 _11
Definition types.h:129
DBus::Struct::_3
T3 _3
Definition types.h:121
DBus::Struct::_13
T13 _13
Definition types.h:131
DBus::Struct::_7
T7 _7
Definition types.h:125
DBus::Struct::_6
T6 _6
Definition types.h:124
DBus::Struct::_14
T14 _14
Definition types.h:132
DBus::Struct::_10
T10 _10
Definition types.h:128
DBus::Struct::_5
T5 _5
Definition types.h:123
DBus::Struct::_8
T8 _8
Definition types.h:126
DBus::Struct::_2
T2 _2
Definition types.h:120
DBus::Struct::_4
T4 _4
Definition types.h:122
DBus::Struct::_16
T16 _16
Definition types.h:134
DBus::Struct::_15
T15 _15
Definition types.h:133
DBus::Struct::_12
T12 _12
Definition types.h:130
DBus::Struct::_1
T1 _1
Definition types.h:119
DBus::Struct::_9
T9 _9
Definition types.h:127
DBus::type< Invalid >::sig
static std::string sig()
Definition types.h:246
DBus::type< Path >::sig
static std::string sig()
Definition types.h:232
DBus::type< Signature >::sig
static std::string sig()
Definition types.h:239
DBus::type< Struct< T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16 > >::sig
static std::string sig()
Definition types.h:290
DBus::type< Variant >::sig
static std::string sig()
Definition types.h:155
DBus::type< bool >::sig
static std::string sig()
Definition types.h:169
DBus::type< double >::sig
static std::string sig()
Definition types.h:218
DBus::type< int16_t >::sig
static std::string sig()
Definition types.h:176
DBus::type< int32_t >::sig
static std::string sig()
Definition types.h:190
DBus::type< int64_t >::sig
static std::string sig()
Definition types.h:204
DBus::type< std::map< K, V > >::sig
static std::string sig()
Definition types.h:264
DBus::type< std::string >::sig
static std::string sig()
Definition types.h:225
DBus::type< std::vector< E > >::sig
static std::string sig()
Definition types.h:255
DBus::type< uint16_t >::sig
static std::string sig()
Definition types.h:183
DBus::type< uint32_t >::sig
static std::string sig()
Definition types.h:197
DBus::type< uint64_t >::sig
static std::string sig()
Definition types.h:211
DBus::type< uint8_t >::sig
static std::string sig()
Definition types.h:162
DBus::type
Definition types.h:145
DBus::type::sig
static std::string sig()
Definition types.h:146
util.h