bindings/ruby/rblm-message.c
changeset 390 8623ce9ef39d
parent 389 95287bd6f233
child 391 d8b64455b5ee
equal deleted inserted replaced
389:95287bd6f233 390:8623ce9ef39d
     1 #include "rblm.h"
       
     2 #include "rblm-private.h"
       
     3 
       
     4 /* How to handle type, sub_type and root node*/
       
     5 
       
     6 VALUE lm_cMessage;
       
     7 
       
     8 LmMessage *
       
     9 rb_lm_message_from_ruby_object (VALUE obj)
       
    10 {
       
    11 	LmMessage *m;
       
    12 
       
    13 	if (!rb_lm__is_kind_of (obj, lm_cMessage)) {
       
    14 		rb_raise (rb_eTypeError, "not a LmMessage");
       
    15 	}
       
    16 
       
    17 	Data_Get_Struct (obj, LmMessage, m);
       
    18 
       
    19 	return m;
       
    20 }
       
    21 
       
    22 void
       
    23 msg_free (LmMessage *m)
       
    24 {
       
    25 	lm_message_unref (m);
       
    26 }
       
    27 
       
    28 VALUE
       
    29 rb_lm_message_to_ruby_object (LmMessage *m)
       
    30 {
       
    31 	if (m) {
       
    32 		lm_message_ref (m);
       
    33 		return Data_Wrap_Struct (lm_cMessage, NULL,
       
    34 					 msg_free, m);
       
    35 	} else {
       
    36 		return Qnil;
       
    37 	}
       
    38 }
       
    39 
       
    40 VALUE
       
    41 msg_allocate (VALUE klass)
       
    42 {
       
    43 	return Data_Wrap_Struct (klass, NULL, msg_free, NULL);
       
    44 }
       
    45 
       
    46 VALUE
       
    47 msg_initialize (int argc, VALUE *argv, VALUE self)
       
    48 {
       
    49 	LmMessage  *m;
       
    50 	VALUE       to, type, sub_type;
       
    51 	char       *to_str = NULL;
       
    52 
       
    53 	rb_scan_args (argc, argv, "21", &to, &type, &sub_type);
       
    54 
       
    55 	/* To can be nil */
       
    56 	if (!NIL_P (to)) {
       
    57 		if (!rb_respond_to (to, rb_intern ("to_s"))) {
       
    58 			rb_raise (rb_eArgError, "to should respond to to_s");
       
    59 		} else {
       
    60 			VALUE str_val = rb_funcall (to, rb_intern ("to_s"), 0);
       
    61 			to_str = StringValuePtr (str_val);
       
    62 		}
       
    63 	} 
       
    64 
       
    65 	if (NIL_P (sub_type)) {
       
    66 		/* Without sub_type */
       
    67 		m = lm_message_new (to_str, FIX2INT (type));
       
    68 	} else {
       
    69 		m = lm_message_new_with_sub_type (to_str,
       
    70 						  FIX2INT (type),
       
    71 						  FIX2INT (sub_type));
       
    72 	}
       
    73 
       
    74 	DATA_PTR (self) = m;
       
    75 
       
    76 	return self;
       
    77 }
       
    78 
       
    79 VALUE
       
    80 msg_get_type (VALUE self)
       
    81 {
       
    82 	LmMessage *m = rb_lm_message_from_ruby_object (self);
       
    83 
       
    84 	return INT2FIX (lm_message_get_type (m));
       
    85 }
       
    86 
       
    87 VALUE
       
    88 msg_get_sub_type (VALUE self)
       
    89 {
       
    90 	LmMessage *m = rb_lm_message_from_ruby_object (self);
       
    91 
       
    92 	return INT2FIX (lm_message_get_sub_type (m));
       
    93 }
       
    94 
       
    95 VALUE
       
    96 msg_get_root_node (VALUE self)
       
    97 {
       
    98 	LmMessage *m = rb_lm_message_from_ruby_object (self);
       
    99 
       
   100 	return LMMESSAGENODE2RVAL (m->node);
       
   101 }
       
   102 
       
   103 extern void 
       
   104 Init_lm_message (VALUE lm_mLM)
       
   105 {
       
   106 	lm_cMessage = rb_define_class_under (lm_mLM, "Message", rb_cObject);
       
   107 
       
   108 	rb_define_alloc_func (lm_cMessage, msg_allocate);
       
   109 	
       
   110 	rb_define_method (lm_cMessage, "initialize", msg_initialize, -1);
       
   111 	rb_define_method (lm_cMessage, "type", msg_get_type, 0);
       
   112 	rb_define_method (lm_cMessage, "sub_type", msg_get_sub_type, 0);
       
   113 	rb_define_method (lm_cMessage, "root_node", msg_get_root_node, 0);
       
   114 
       
   115 	rb_define_alias (lm_cMessage, "node", "root_node");
       
   116 }
       
   117