My C++ wrapper for D-BUS

David Eriksson twogood at users.sourceforge.net
Fri Sep 16 02:26:30 PDT 2005


On Fri, 2005-09-16 at 08:32 +0200, Murray Cumming wrote:
> On Tue, 2005-08-09 at 14:06 +0200, David Eriksson wrote:
> > Hi,
> > 
> > As Paul Durante were also doing his own C++ bindings for D-BUS instead
> > of using the Freedesktop-sponsored C++ bindings [1], I might just as
> > well confess I was hit my by the NIH syndrome [2] and also wrote a C++
> > wrapper for the D-BUS API. It's not finished yet but it contains enough
> > for my needs at the moment.
> 
> It would be nice if people could get together and work on one finished
> binding. 

Yes that is a good idea, but my code is more of a wrapper than a
binding. By "wrapper" I mean a close to 1:1 mapping to C function calls,
while a "binding" is better to take advantage of language features and
allows me to do more with less code. 

A wrapper could eventually serve as a foundation for a binding.

For example, my wrapper requires code like the one I show below for each
type of D-BUS object and method. A clever binding could probably do much
of this for me with some templates and stuff, but this is enough for my
needs at the moment. That's something I might look at in the future but
time for the project is scarce and parts less related to D-BUS need to
be addressed first.


DBusHandlerResult HandshakeHandler::HandleMessage(/*{{{*/
    Handshake_ptr handshake,
    dbus::Message_ptr message)
{
  MIDASYNC_TRACER();

  const string member = message->Member();

  if (member == "GetProperties")
    GetProperties(handshake, message);
  else if (member == "GetProperty")
    GetProperty(handshake, message);
  else if (member == "ProvidePassword")
    ProvidePassword(handshake, message);
  else if (member == "ReplacePartnership")
    ReplacePartnership(handshake, message);
  else if (member == "BeMyGuest")
    BeMyGuest(handshake, message);
  else
  {
    trace << "Call to unknown method " <<
      message->Interface() << "::" << member <<
      "{" << message->Path() << "}" << endtrace;
    throw DbusUnknownMethod(message->Get());
  }

  return DBUS_HANDLER_RESULT_HANDLED;
}/*}}}*/

void HandshakeHandler::BeMyGuest(/*{{{*/
    Handshake_ptr handshake, 
    dbus::Message_ptr message)
{
  MIDASYNC_TRACER();

  bool success = handshake->BeMyGuest();

  dbus::Message_ptr reply( dbus::Message::NewMethodReturn(message) );

  DBusMessageIter iter;
  reply->IterInit(iter);

  dbus_message_iter_append_boolean(&iter, success);

  if (!mConnection->Send(reply))
    throw runtime_error("dbus_connection_send failed");
}/*}}}*/



> Personally I'd really like a pure C++ API that doesn't depend
> on glibmm or Qt (though libsigc++ would be OK if needed).

See below...

> That needs a little cleverness to allow integration with whatever main
> loop is used by the application. I started a second (also unfinished,
> due to time) dbus C++ API, as a wrapper of the dbus-glib API, in order
> to avoid that mainloop-integration problem.

Sounds perfectly reasonable.

> [snip]
> 
> > - Any C++ users who for various reasons are not using Boost [3] might
> > not like my bindings because I depend on boost::shared_ptr.
> 
> boost is not meant to be a stable API and does not allow parallel
> installation when they break API. It's not something that should be used
> in other APIs that should be stable.

My experience with the parts of boost I use is that the API is stable
and the code is of high quality, but I assume they don't make any
guarantees.

> shared_ptrs are easy to implement -
> I think I put one in the dbus-cpp code.

Yes, that's right, you did. From a general adoption perspective I can
see that it may be good to avoid dependencies on other libraries. (The
"pure C++ API" above.)

>From my personal perspective I would never dream of re-implementing
something I can get from boost, and when I use glibmm to get a main
loop, libsigc++ comes natural for signals.

(Yes, it is somewhat contradictory to write my own D-BUS wrapper if I'm
so keen on using already available code.)

By the way, don't forget that the upcoming standardized shared_ptr is
available in gcc 4 as std::tr1:shared_ptr. I haven't compared it with
boost::shared_ptr but I don't think there is much difference.


To summarize: We have different objectives for our implementations and
as I hardly have time for the project I use my D-BUS wrappers in, I will
probably not be of much use... :-(

-- 
Regards,
               -\- David Eriksson -/-

        SynCE - http://synce.sourceforge.net
      ScummVM - http://scummvm.sourceforge.net
     Desquirr - http://desquirr.sourceforge.net



More information about the dbus mailing list