From 5905d50e1aee6f1d09b5392862762f0f2660125d Mon Sep 17 00:00:00 2001 From: Tristan Hume Date: Fri, 1 May 2015 16:59:40 -0400 Subject: [PATCH] Switch to structs for thin wrappers. --- dub.json | 2 +- source/app.d | 4 ++-- source/ddbus/conv.d | 2 +- source/ddbus/router.d | 4 ++-- source/ddbus/simple.d | 4 ++-- source/ddbus/thin.d | 43 ++++++++++++++++++++++++++++++------------- 6 files changed, 38 insertions(+), 21 deletions(-) diff --git a/dub.json b/dub.json index 808df83..6615adb 100644 --- a/dub.json +++ b/dub.json @@ -5,7 +5,7 @@ "copyright": "Copyright © 2015, Tristan Hume", "license": "MIT", "authors": ["Tristan Hume"], - "lflags": ["-ldbus-1"], + "lflags": ["-ldbus-1"], "dependencies": { "dunit": "~>1.0.10" } diff --git a/source/app.d b/source/app.d index 71808ac..d3d5cd0 100644 --- a/source/app.d +++ b/source/app.d @@ -6,11 +6,11 @@ void main() { Connection conn = connectToBus(); for(int i = 0; i < 50; i++) { - Message msg = new Message("ca.thume.transience","/ca/thume/transience/screensurface", + Message msg = Message("ca.thume.transience","/ca/thume/transience/screensurface", "ca.thume.transience.screensurface","testDot"); conn.sendBlocking(msg); } - Message msg2 = new Message("ca.thume.transience","/ca/thume/transience/screensurface", + Message msg2 = Message("ca.thume.transience","/ca/thume/transience/screensurface", "ca.thume.transience.screensurface","testPing"); Message res = conn.sendWithReplyBlocking(msg2,3000); int result = res.read!int(); diff --git a/source/ddbus/conv.d b/source/ddbus/conv.d index 72cd5be..80893ad 100644 --- a/source/ddbus/conv.d +++ b/source/ddbus/conv.d @@ -76,7 +76,7 @@ void readIterTuple(Tup)(DBusMessageIter *iter, ref Tup tuple) if(isTuple!Tup && unittest { import dunit.toolkit; import ddbus.thin; - Message msg = new Message("org.example.wow","/wut","org.test.iface","meth"); + Message msg = Message("org.example.wow","/wut","org.test.iface","meth"); bool[] emptyB; auto args = tuple(5,true,"wow",[6,5],tuple(6.2,4,[["lol"]],emptyB)); msg.build(args.expand); diff --git a/source/ddbus/router.d b/source/ddbus/router.d index 63d6637..c873112 100644 --- a/source/ddbus/router.d +++ b/source/ddbus/router.d @@ -72,8 +72,8 @@ class MessageRouter { unittest { import dunit.toolkit; - auto msg = new Message("org.example.test", "/test","org.example.testing","testMethod"); - auto patt= new MessagePattern(msg); + auto msg = Message("org.example.test", "/test","org.example.testing","testMethod"); + auto patt= MessagePattern(msg); patt.assertEqual(patt); patt.sender.assertNull(); patt.path.assertEqual("/test"); diff --git a/source/ddbus/simple.d b/source/ddbus/simple.d index 7f6eb3d..71ec8cf 100644 --- a/source/ddbus/simple.d +++ b/source/ddbus/simple.d @@ -14,14 +14,14 @@ class PathIface { } Ret call(Ret, Args...)(string meth, Args args) if(allCanDBus!Args && canDBus!Ret) { - Message msg = new Message(dbus_message_new_method_call(dest,path,iface,meth.toStringz())); + Message msg = Message(dbus_message_new_method_call(dest,path,iface,meth.toStringz())); msg.build(args); Message ret = conn.sendWithReplyBlocking(msg); return ret.read!Ret(); } Message opDispatch(string meth, Args...)(Args args) { - Message msg = new Message(dbus_message_new_method_call(dest,path,iface,meth.toStringz())); + Message msg = Message(dbus_message_new_method_call(dest,path,iface,meth.toStringz())); msg.build(args); return conn.sendWithReplyBlocking(msg); } diff --git a/source/ddbus/thin.d b/source/ddbus/thin.d index f4802d0..4118927 100644 --- a/source/ddbus/thin.d +++ b/source/ddbus/thin.d @@ -1,3 +1,4 @@ +/// Thin OO wrapper around DBus types module ddbus.thin; import ddbus.c_lib; @@ -30,7 +31,9 @@ enum MessageType { Call, Return, Error, Signal } -class Message { +struct Message { + DBusMessage *msg; + this(string dest, string path, string iface, string method) { msg = dbus_message_new_method_call(dest.toStringz(), path.toStringz(), iface.toStringz(), method.toStringz()); } @@ -39,6 +42,10 @@ class Message { msg = m; } + this(this) { + dbus_message_ref(msg); + } + ~this() { dbus_message_unref(msg); } @@ -71,7 +78,7 @@ class Message { } Message createReturn() { - return new Message(dbus_message_new_method_return(msg)); + return Message(dbus_message_new_method_return(msg)); } MessageType type() { @@ -109,22 +116,32 @@ class Message { assert(cStr != null); return cStr.fromStringz().assumeUnique(); } - - DBusMessage *msg; } unittest { import dunit.toolkit; - auto msg = new Message("org.example.test", "/test","org.example.testing","testMethod"); + auto msg = Message("org.example.test", "/test","org.example.testing","testMethod"); msg.path().assertEqual("/test"); } -class Connection { +struct Connection { DBusConnection *conn; this(DBusConnection *connection) { conn = connection; } + this(this) { + dbus_connection_ref(conn); + } + + ~this() { + dbus_connection_unref(conn); + } + + void close() { + dbus_connection_close(conn); + } + void send(Message msg) { dbus_connection_send(conn,msg.msg, null); } @@ -135,20 +152,20 @@ class Connection { } Message sendWithReplyBlocking(Message msg, int timeout = 100) { + DBusMessage *dbusMsg = msg.msg; + dbus_message_ref(dbusMsg); DBusMessage *reply = wrapErrors((err) { - return dbus_connection_send_with_reply_and_block(conn,msg.msg,timeout,err); + auto ret = dbus_connection_send_with_reply_and_block(conn,dbusMsg,timeout,err); + dbus_message_unref(dbusMsg); + return ret; }); - return new Message(reply); - } - - ~this() { - dbus_connection_unref(conn); + return Message(reply); } } Connection connectToBus(DBusBusType bus = DBusBusType.DBUS_BUS_SESSION) { DBusConnection *conn = wrapErrors((err) { return dbus_bus_get(bus,err); }); - return new Connection(conn); + return Connection(conn); } unittest {