Duck-typify message handling.
authorTilman Sauerbeck <tilman@code-monkey.de>
Thu, 28 Apr 2005 15:38:47 +0000 (15:38 +0000)
committerTilman Sauerbeck <tilman@code-monkey.de>
Thu, 28 Apr 2005 15:38:47 +0000 (15:38 +0000)
src/rb_edje.c
src/rb_edje_main.c
src/rb_messages.c [deleted file]
src/rb_messages.h [deleted file]

index 02ff4ea2f254abec8e9e88531bc5fe6e8a98c5b7..1202b795d20bc953232823ef3f4e6bc57b81d2c7 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id: rb_edje.c 328 2005-04-28 07:44:46Z tilman $
+ * $Id: rb_edje.c 330 2005-04-28 15:38:47Z tilman $
  *
  * Copyright (C) 2004 Tilman Sauerbeck (tilman at code-monkey de)
  *
@@ -28,9 +28,9 @@
 #include "rb_edje.h"
 #include "rb_edje_main.h"
 #include "rb_part.h"
-#include "rb_messages.h"
 
 VALUE cEdje;
+static VALUE cMsg, eEdjeError;
 
 static void c_mark (RbEdje *e)
 {
@@ -412,23 +412,59 @@ static VALUE c_data_get (VALUE self, VALUE key)
 
 static VALUE c_send_message (VALUE self, VALUE msg)
 {
-       VALUE m;
+       Edje_Message_String s;
+       Edje_Message_Int i;
+       Edje_Message_Float f;
        Edje_Message_Type type;
-       int id;
+       void *data;
+       VALUE v, tmp;
 
        GET_OBJ (self, RbEdje, e);
 
        CHECK_CLASS (msg, cMsg);
 
-       type = NUM2INT (rb_iv_get (msg, "@type"));
-       id = NUM2INT (rb_iv_get (msg, "@id"));
-       m = rb_funcall (msg, rb_intern ("serialize"), 0, NULL);
+       v = rb_iv_get (msg, "@value");
 
-       edje_object_message_send (e->real.real, type, id, (void *) m);
+       if (NIL_P (v)) {
+               rb_raise (eEdjeError, "value must not be nil");
+               return Qnil;
+       }
+
+       if (!NIL_P (tmp = rb_check_string_type (v))) {
+               type = EDJE_MESSAGE_STRING;
+               s.str = StringValuePtr (tmp);
+               data = &s;
+       } else if (rb_obj_is_kind_of (v, rb_cFixnum)) {
+               type = EDJE_MESSAGE_INT;
+               i.val = FIX2INT (v);
+               data = &i;
+       } else if (rb_obj_is_kind_of (v, rb_cFloat)) {
+               type = EDJE_MESSAGE_FLOAT;
+               f.val = NUM2DBL (v);
+               data = &v;
+       /* } else if (!NIL_P (tmp = rb_check_array_type (v))) { */
+       } else {
+               rb_raise (eEdjeError, "unsupported value");
+       }
+
+       edje_object_message_send (e->real.real, type,
+                                 NUM2INT (rb_iv_get (msg, "@id")), data);
 
        return Qnil;
 }
 
+static VALUE c_msg_init (int argc, VALUE *argv, VALUE self)
+{
+       VALUE val;
+
+       rb_scan_args (argc, argv, "01", &val);
+
+       rb_iv_set (self, "@id", UINT2NUM (0));
+       rb_iv_set (self, "@value", val);
+
+       return self;
+}
+
 void Init_Edje (void)
 {
        cEdje = rb_define_class_under (mEdje, "Edje", cEvasObject);
@@ -450,4 +486,14 @@ void Init_Edje (void)
        rb_define_method (cEdje, "animation=", c_animation_set, 1);
        rb_define_method (cEdje, "data", c_data_get, 1);
        rb_define_method (cEdje, "send_message", c_send_message, 1);
+
+       cMsg = rb_define_class_under (mEdje, "Message", rb_cObject);
+
+       rb_define_method (cMsg, "initialize", c_msg_init, -1);
+
+       rb_define_attr (cMsg, "id", 1, 1);
+       rb_define_attr (cMsg, "value", 1, 1);
+
+       eEdjeError = rb_define_class_under (mEdje, "EdjeError",
+                                          rb_eStandardError);
 }
index e00a2491e5ab9330dc9b7f66e4fc7500a3f033d3..d11d4565121cf206f3f1a460d18782173f0cd372 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id: rb_edje_main.c 287 2005-03-15 18:08:01Z tilman $
+ * $Id: rb_edje_main.c 330 2005-04-28 15:38:47Z tilman $
  *
  * Copyright (C) 2004 Tilman Sauerbeck (tilman at code-monkey de)
  *
@@ -25,7 +25,6 @@
 #include "rb_edje_main.h"
 #include "rb_edje.h"
 #include "rb_part.h"
-#include "rb_messages.h"
 
 /*
  * call-seq:
@@ -114,6 +113,5 @@ void Init_edje (void)
 
        Init_Edje ();
        Init_Part ();
-       Init_Messages ();
 }
 
diff --git a/src/rb_messages.c b/src/rb_messages.c
deleted file mode 100644 (file)
index a40227e..0000000
+++ /dev/null
@@ -1,137 +0,0 @@
-/*
- * $Id: rb_messages.c 329 2005-04-28 10:41:17Z tilman $
- *
- * Copyright (C) 2004 Tilman Sauerbeck (tilman at code-monkey de)
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
- */
-
-#include <ruby.h>
-
-#include <Edje.h>
-
-#define __RB_MESSAGES_C
-#include "rb_edje_main.h"
-#include "rb_edje.h"
-#include "rb_messages.h"
-
-#define DEF_CONST(mod, prefix, name) \
-       rb_define_const ((mod), #name, \
-                        INT2FIX (prefix##name));
-
-VALUE cMsg;
-static VALUE cStrMsg, cIntMsg, cFloatMsg;
-
-static VALUE c_msg_init (VALUE self)
-{
-       rb_iv_set (self, "@id", UINT2NUM (0));
-
-       return self;
-}
-
-static VALUE c_str_msg_init (VALUE self, VALUE str)
-{
-       Check_Type (str, T_STRING);
-
-       rb_call_super (0, NULL);
-
-       rb_iv_set (self, "@type", UINT2NUM (EDJE_MESSAGE_STRING));
-       rb_iv_set (self, "@value", str);
-
-       return self;
-}
-
-static VALUE c_str_msg_serialize (VALUE self)
-{
-       static volatile Edje_Message_String ret;
-       VALUE s;
-
-       s = rb_iv_get (self, "@value");
-       ret.str = StringValuePtr (s);
-
-       return (VALUE) &ret;
-}
-
-static VALUE c_int_msg_init (VALUE self, VALUE val)
-{
-       Check_Type (val, T_FIXNUM);
-
-       rb_call_super (0, NULL);
-
-       rb_iv_set (self, "@type", UINT2NUM (EDJE_MESSAGE_INT));
-       rb_iv_set (self, "@value", val);
-
-       return self;
-}
-
-static VALUE c_int_msg_serialize (VALUE self)
-{
-       static volatile Edje_Message_Int ret;
-       VALUE i;
-
-       i = rb_iv_get (self, "@value");
-       ret.val = NUM2INT (i);
-
-       return (VALUE) &ret;
-}
-
-static VALUE c_float_msg_init (VALUE self, VALUE val)
-{
-       Check_Type (val, T_FLOAT);
-
-       rb_call_super (0, NULL);
-
-       rb_iv_set (self, "@type", UINT2NUM (EDJE_MESSAGE_FLOAT));
-       rb_iv_set (self, "@value", val);
-
-       return self;
-}
-
-static VALUE c_float_msg_serialize (VALUE self)
-{
-       static volatile Edje_Message_Float ret;
-       VALUE f;
-
-       f = rb_iv_get (self, "@value");
-       ret.val = NUM2DBL (f);
-
-       return (VALUE) &ret;
-}
-
-void Init_Messages (void)
-{
-       cMsg = rb_define_class_under (mEdje, "Message", rb_cObject);
-
-       rb_define_method (cMsg, "initialize", c_msg_init, 0);
-
-       rb_define_attr (cMsg, "type", 1, 0);
-       rb_define_attr (cMsg, "id", 1, 1);
-       rb_define_attr (cMsg, "value", 1, 1);
-
-       /* StringMessage */
-       cStrMsg = rb_define_class_under (mEdje, "StringMessage", cMsg);
-       rb_define_method (cStrMsg, "initialize", c_str_msg_init, 1);
-       rb_define_method (cStrMsg, "serialize", c_str_msg_serialize, 0);
-
-       /* IntMessage */
-       cIntMsg = rb_define_class_under (mEdje, "IntMessage", cMsg);
-       rb_define_method (cIntMsg, "initialize", c_int_msg_init, 1);
-       rb_define_method (cIntMsg, "serialize", c_int_msg_serialize, 0);
-
-       /* FloatMessage */
-       cFloatMsg = rb_define_class_under (mEdje, "FloatMessage", cMsg);
-       rb_define_method (cFloatMsg, "initialize", c_float_msg_init, 1);
-       rb_define_method (cFloatMsg, "serialize", c_float_msg_serialize, 0);
-}
diff --git a/src/rb_messages.h b/src/rb_messages.h
deleted file mode 100644 (file)
index 37be521..0000000
+++ /dev/null
@@ -1,30 +0,0 @@
-/*
- * $Id: rb_messages.h 329 2005-04-28 10:41:17Z tilman $
- *
- * Copyright (C) 2004 Tilman Sauerbeck (tilman at code-monkey de)
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
- */
-
-#ifndef __RB_MESSAGES_H
-#define __RB_MESSAGES_H
-
-void Init_Messages (void);
-
-#ifndef __RB_MESSAGES_C
-extern VALUE cMsg;
-#endif
-
-#endif