types.cpp

Go to the documentation of this file.
00001 /*
00002  *
00003  *  D-Bus++ - C++ bindings for D-Bus
00004  *
00005  *  Copyright (C) 2005-2007  Paolo Durante <shackan@gmail.com>
00006  *
00007  *
00008  *  This library is free software; you can redistribute it and/or
00009  *  modify it under the terms of the GNU Lesser General Public
00010  *  License as published by the Free Software Foundation; either
00011  *  version 2.1 of the License, or (at your option) any later version.
00012  *
00013  *  This library is distributed in the hope that it will be useful,
00014  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  *  Lesser General Public License for more details.
00017  *
00018  *  You should have received a copy of the GNU Lesser General Public
00019  *  License along with this library; if not, write to the Free Software
00020  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021  *
00022  */
00023 
00024 #ifdef HAVE_CONFIG_H
00025 #include <config.h>
00026 #endif
00027 
00028 #include <dbus-c++/types.h>
00029 #include <dbus-c++/object.h>
00030 #include <dbus/dbus.h>
00031 #include <cstdlib>
00032 #include <stdarg.h>
00033 
00034 #include "message_p.h"
00035 #include "internalerror.h"
00036 
00037 using namespace DBus;
00038 
00039 Variant::Variant()
00040   : _msg(CallMessage()) // dummy message used as temporary storage for variant data
00041 {
00042 }
00043 
00044 Variant::Variant(MessageIter &it)
00045   : _msg(CallMessage())
00046 {
00047   MessageIter vi = it.recurse();
00048   MessageIter mi = _msg.writer();
00049   vi.copy_data(mi);
00050 }
00051 
00052 Variant &Variant::operator = (const Variant &v)
00053 {
00054   if (&v != this)
00055   {
00056     _msg = v._msg;
00057   }
00058   return *this;
00059 }
00060 
00061 void Variant::clear()
00062 {
00063   CallMessage empty;
00064   _msg = empty;
00065 }
00066 
00067 const Signature Variant::signature() const
00068 {
00069   char *sigbuf = reader().signature();
00070 
00071   Signature signature = sigbuf;
00072 
00073   free(sigbuf);
00074 
00075   return signature;
00076 }
00077 
00078 MessageIter &operator << (MessageIter &iter, const Variant &val)
00079 {
00080   const Signature sig = val.signature();
00081 
00082   MessageIter rit = val.reader();
00083   MessageIter wit = iter.new_variant(sig.c_str());
00084 
00085   rit.copy_data(wit);
00086 
00087   iter.close_container(wit);
00088 
00089   return iter;
00090 }
00091 
00092 MessageIter &operator >> (MessageIter &iter, Variant &val)
00093 {
00094   if (iter.type() != DBUS_TYPE_VARIANT)
00095     throw ErrorInvalidArgs("variant type expected");
00096 
00097   val.clear();
00098 
00099   MessageIter vit = iter.recurse();
00100   MessageIter mit = val.writer();
00101 
00102   vit.copy_data(mit);
00103 
00104   return ++iter;
00105 }
00106