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 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27 
28 #include <dbus-c++/types.h>
29 #include <dbus-c++/object.h>
30 #include <dbus/dbus.h>
31 #include <cstdlib>
32 #include <stdarg.h>
33 
34 #include "message_p.h"
35 #include "internalerror.h"
36 
37 namespace DBus {
38 
Variant()39 Variant::Variant()
40   : _msg(CallMessage()) // dummy message used as temporary storage for variant data
41 {
42 }
43 
Variant(MessageIter & it)44 Variant::Variant(MessageIter &it)
45   : _msg(CallMessage())
46 {
47   MessageIter vi = it.recurse();
48   MessageIter mi = _msg.writer();
49   vi.copy_data(mi);
50 }
51 
operator =(const Variant & v)52 Variant &Variant::operator = (const Variant &v)
53 {
54   if (&v != this)
55   {
56     _msg = v._msg;
57   }
58   return *this;
59 }
60 
clear()61 void Variant::clear()
62 {
63   CallMessage empty;
64   _msg = empty;
65 }
66 
signature() const67 const Signature Variant::signature() const
68 {
69   char *sigbuf = reader().signature();
70 
71   Signature signature = sigbuf;
72 
73   free(sigbuf);
74 
75   return signature;
76 }
77 
operator <<(MessageIter & iter,const Variant & val)78 MessageIter &operator << (MessageIter &iter, const Variant &val)
79 {
80   const Signature sig = val.signature();
81 
82   MessageIter rit = val.reader();
83   MessageIter wit = iter.new_variant(sig.c_str());
84 
85   rit.copy_data(wit);
86 
87   iter.close_container(wit);
88 
89   return iter;
90 }
91 
operator >>(MessageIter & iter,Variant & val)92 MessageIter &operator >> (MessageIter &iter, Variant &val)
93 {
94   if (iter.type() != DBUS_TYPE_VARIANT)
95     throw ErrorInvalidArgs("variant type expected");
96 
97   val.clear();
98 
99   MessageIter vit = iter.recurse();
100   MessageIter mit = val.writer();
101 
102   vit.copy_data(mit);
103 
104   return ++iter;
105 }
106 
107 } /* namespace DBus */
108