Cleaning up of the bindings for better checking
authorMikael Hallendal <micke@imendio.com>
Wed, 09 Apr 2008 22:06:42 +0200
changeset 363 6d53af6c3227
parent 362 586b54d0f21e
child 364 71e6639a924d
Cleaning up of the bindings for better checking Added rb_lm__is_kind_of which is taken from rcairo and use that to better check for correct object types.
bindings/ruby/rblm-private.c
bindings/ruby/rblm-private.h
bindings/ruby/rlm-connection.c
bindings/ruby/rlm-message.c
bindings/ruby/rloudmouth.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bindings/ruby/rblm-private.c	Wed Apr 09 22:06:42 2008 +0200
@@ -0,0 +1,8 @@
+#include "rblm-private.h"
+
+gboolean
+rb_lm__is_kind_of (VALUE object, VALUE klass)
+{
+	return RVAL2GBOOL (rb_obj_is_kind_of (object, klass));
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bindings/ruby/rblm-private.h	Wed Apr 09 22:06:42 2008 +0200
@@ -0,0 +1,14 @@
+
+#ifndef __RBLM_PRIVATE_H__
+#define __RBLM_PRIVATE_H__
+
+#include <glib.h>
+#include <ruby.h>
+
+#define GBOOL2RVAL(x) (x == TRUE ? Qtrue : Qfalse)
+#define RVAL2GBOOL(x) RTEST(x)
+
+gboolean   rb_lm__is_kind_of (VALUE object, VALUE klass);
+
+#endif /* __RBLM_PRIVATE_H__ */
+
--- a/bindings/ruby/rlm-connection.c	Wed Apr 09 20:01:36 2008 +0200
+++ b/bindings/ruby/rlm-connection.c	Wed Apr 09 22:06:42 2008 +0200
@@ -1,7 +1,23 @@
 #include "rloudmouth.h"
 
+VALUE lm_cConnection;
+
 VALUE conn_set_server (VALUE self, VALUE server);
 
+static LmConnection *
+rb_lm_connection_from_ruby_object (VALUE obj)
+{
+	LmConnection *conn;
+
+	if (!rb_lm__is_kind_of (obj, lm_cConnection)) {
+		rb_raise (rb_eTypeError, "not a LmConnection");
+	}
+
+	Data_Get_Struct (obj, LmConnection, conn);
+
+	return conn;
+}
+
 void
 conn_mark (LmConnection *self)
 {
@@ -45,11 +61,9 @@
 VALUE
 conn_open (int argc, VALUE *argv, VALUE self)
 {
-	LmConnection *conn;
+	LmConnection *conn = rb_lm_connection_from_ruby_object (self);
 	VALUE         func;
 
-	Data_Get_Struct (self, LmConnection, conn);
-
 	rb_scan_args (argc, argv, "0&", &func);
 	if (NIL_P (func)) {
 		func = rb_block_proc ();
@@ -62,9 +76,7 @@
 VALUE
 conn_close (VALUE self)
 {
-	LmConnection *conn;
-
-	Data_Get_Struct (self, LmConnection, conn);
+	LmConnection *conn = rb_lm_connection_from_ruby_object (self);
 
 	return GBOOL2RVAL (lm_connection_close (conn, NULL));
 }
@@ -79,11 +91,9 @@
 VALUE
 conn_auth (int argc, VALUE *argv, VALUE self)
 {
-	LmConnection *conn;
+	LmConnection *conn = rb_lm_connection_from_ruby_object (self);
 	VALUE         name, password, resource, func; 
 
-	Data_Get_Struct (self, LmConnection, conn);
-
 	rb_scan_args (argc, argv, "21&", &name, &password, &resource, &func);
 	if (NIL_P (func)) {
 		func = rb_block_proc ();
@@ -101,9 +111,7 @@
 VALUE
 conn_set_keep_alive_rate (VALUE self, VALUE rate)
 {
-	LmConnection *conn;
-
-	Data_Get_Struct (self, LmConnection, conn);
+	LmConnection *conn = rb_lm_connection_from_ruby_object (self);
 
 	lm_connection_set_keep_alive_rate (conn, NUM2UINT (rate));
 
@@ -120,29 +128,23 @@
 VALUE
 conn_is_open (VALUE self)
 {
-	LmConnection *conn;
+	LmConnection *conn = rb_lm_connection_from_ruby_object (self);
 
-	Data_Get_Struct (self, LmConnection, conn);
-	
 	return GBOOL2RVAL (lm_connection_is_open (conn));
 }
 
 VALUE
 conn_is_authenticated (VALUE self)
 {
-	LmConnection *conn;
+	LmConnection *conn = rb_lm_connection_from_ruby_object (self);
 
-	Data_Get_Struct (self, LmConnection, conn);
-	
 	return GBOOL2RVAL (lm_connection_is_authenticated (conn));
 }
 
 VALUE
 conn_get_server (VALUE self)
 {
-	LmConnection *conn;
-
-	Data_Get_Struct (self, LmConnection, conn);
+	LmConnection *conn = rb_lm_connection_from_ruby_object (self);
 
 	return rb_str_new2 (lm_connection_get_server (conn));
 }
@@ -150,9 +152,7 @@
 VALUE
 conn_set_server (VALUE self, VALUE server)
 {
-	LmConnection *conn;
-
-	Data_Get_Struct (self, LmConnection, conn);
+	LmConnection *conn = rb_lm_connection_from_ruby_object (self);
 
 	if (!rb_respond_to (server, rb_intern ("to_s"))) {
 		rb_raise (rb_eArgError, "server should respond to to_s");
@@ -167,9 +167,7 @@
 VALUE
 conn_get_jid (VALUE self)
 {
-	LmConnection *conn;
-
-	Data_Get_Struct (self, LmConnection, conn);
+	LmConnection *conn = rb_lm_connection_from_ruby_object (self);
 
 	return rb_str_new2 (lm_connection_get_jid (conn));
 }
@@ -177,9 +175,7 @@
 VALUE
 conn_set_jid (VALUE self, VALUE jid)
 {
-	LmConnection *conn;
-
-	Data_Get_Struct (self, LmConnection, conn);
+	LmConnection *conn = rb_lm_connection_from_ruby_object (self);
 
 	if (!rb_respond_to (jid, rb_intern ("to_s"))) {
 		rb_raise (rb_eArgError, "jid should respond to to_s");
@@ -194,9 +190,7 @@
 VALUE
 conn_get_port (VALUE self)
 {
-	LmConnection *conn;
-
-	Data_Get_Struct (self, LmConnection, conn);
+	LmConnection *conn = rb_lm_connection_from_ruby_object (self);
 
 	return UINT2NUM (lm_connection_get_port (conn));
 }
@@ -204,9 +198,7 @@
 VALUE
 conn_set_port (VALUE self, VALUE port)
 {
-	LmConnection *conn;
-
-	Data_Get_Struct (self, LmConnection, conn);
+	LmConnection *conn = rb_lm_connection_from_ruby_object (self);
 
 	lm_connection_set_port (conn, NUM2UINT (port));
 
@@ -216,9 +208,7 @@
 VALUE
 conn_get_state (VALUE self)
 {
-	LmConnection *conn;
-
-	Data_Get_Struct (self, LmConnection, conn);
+	LmConnection *conn = rb_lm_connection_from_ruby_object (self);
 
 	/* TODO: FIXME */
 	return Qnil;
@@ -227,8 +217,6 @@
 void
 Init_lm_connection (VALUE lm_mLM)
 {
-	VALUE lm_cConnection;
-	
 	lm_cConnection = rb_define_class_under (lm_mLM, "Connection", 
 						rb_cObject);
 
--- a/bindings/ruby/rlm-message.c	Wed Apr 09 20:01:36 2008 +0200
+++ b/bindings/ruby/rlm-message.c	Wed Apr 09 22:06:42 2008 +0200
@@ -1,46 +1,43 @@
 #include "rloudmouth.h"
+#include "rblm-private.h"
 
 /* How to handle type, sub_type and root node*/
 
-typedef struct {
-	LmMessage *message;
-} MsgWrapper;
+VALUE lm_cMessage;
+
+static LmMessage *
+rb_lm_message_from_ruby_object (VALUE obj)
+{
+	LmMessage *m;
 
-void
-msg_wrapper_mark (MsgWrapper *wrapper)
-{
-	/* Do nothing here */
+	if (!rb_lm__is_kind_of (obj, lm_cMessage)) {
+		rb_raise (rb_eTypeError, "not a LmMessage");
+	}
+
+	Data_Get_Struct (obj, LmMessage, m);
+
+	return m;
 }
 
 void
-msg_wrapper_free (MsgWrapper *wrapper)
+msg_free (LmMessage *m)
 {
-	if (wrapper->message) {
-		lm_message_unref (wrapper->message);
-	}
-
-	g_slice_free (MsgWrapper, wrapper);
+	lm_message_unref (m);
 }
 
 VALUE
 msg_allocate (VALUE klass)
 {
-	MsgWrapper *wrapper;
-
-	wrapper = g_slice_new0 (MsgWrapper);
-
-	Data_Wrap_Struct (klass, msg_wrapper_mark, msg_wrapper_free, wrapper);
+	return Data_Wrap_Struct (klass, NULL, msg_free, NULL);
 }
 
 VALUE
 msg_initialize (int argc, VALUE *argv, VALUE self)
 {
-	MsgWrapper *wrapper;
+	LmMessage  *m;
 	VALUE       to, type, sub_type;
 	char       *to_str = NULL;
 
-	Data_Get_Struct (self, MsgWrapper, wrapper);
-
 	rb_scan_args (argc, argv, "21", &to, &type, &sub_type);
 
 	/* To can be nil */
@@ -55,35 +52,32 @@
 
 	if (NIL_P (sub_type)) {
 		/* Without sub_type */
-		wrapper->message = lm_message_new (to_str, NUM2INT (type));
+		m = lm_message_new (to_str, NUM2INT (type));
 	} else {
-		wrapper->message = 
-			lm_message_new_with_sub_type (to_str,
-						      NUM2INT (type),
-						      NUM2INT (sub_type));
+		m = lm_message_new_with_sub_type (to_str,
+						  NUM2INT (type),
+						  NUM2INT (sub_type));
 	}
 
+	DATA_PTR (self) = m;
+
 	return self;
 }
 
 VALUE
 msg_get_type (VALUE self)
 {
-	MsgWrapper *wrapper;
+	LmMessage *m = rb_lm_message_from_ruby_object (self);
 
-	Data_Get_Struct (self, MsgWrapper, wrapper);
-
-	return INT2NUM (lm_message_get_type (wrapper->message));
+	return INT2NUM (lm_message_get_type (m));
 }
 
 VALUE
 msg_get_sub_type (VALUE self)
 {
-	MsgWrapper *wrapper;
+	LmMessage *m = rb_lm_message_from_ruby_object (self);
 
-	Data_Get_Struct (self, MsgWrapper, wrapper);
-
-	return INT2NUM (lm_message_get_sub_type (wrapper->message));
+	return INT2NUM (lm_message_get_sub_type (m));
 }
 
 VALUE
@@ -93,21 +87,9 @@
 	return Qnil;
 }
 
-LmMessage *
-rlm_message_from_value (VALUE self)
-{
-	MsgWrapper *wrapper;
-
-	Data_Get_Struct (self, MsgWrapper, wrapper);
-
-	return wrapper->message;
-}
-
 extern void 
 Init_lm_message (VALUE lm_mLM)
 {
-	VALUE lm_cMessage;
-
 	lm_cMessage = rb_define_class_under (lm_mLM, "Message", rb_cObject);
 
 	rb_define_alloc_func (lm_cMessage, msg_allocate);
--- a/bindings/ruby/rloudmouth.h	Wed Apr 09 20:01:36 2008 +0200
+++ b/bindings/ruby/rloudmouth.h	Wed Apr 09 22:06:42 2008 +0200
@@ -5,8 +5,6 @@
 #include <loudmouth/loudmouth.h> 
 #include <glib.h>
 
-#define GBOOL2RVAL(x) (x == TRUE ? Qtrue : Qfalse)
-
 extern void Init_loudmouth          (void);
 extern void Init_lm_connection      (VALUE lm_mLM);
 extern void Init_lm_message         (VALUE lm_mLM);