Updated MSpec source to 46e80081.
[rbx.git] / spec / subtend / ext / subtend_wrapped_struct.c
blob39279b3932330ae1a5c328fcba0c490d4b2a28de
1 #include <ruby.h>
2 #include <string.h>
4 struct sample_wrapped_struct {
5 int foo;
6 };
8 VALUE sws_wrap_struct(VALUE self, VALUE val) {
9 struct sample_wrapped_struct* bar = (struct sample_wrapped_struct *)malloc(sizeof(struct sample_wrapped_struct));
10 bar->foo = FIX2INT(val);
11 return Data_Wrap_Struct(rb_cObject, NULL, NULL, bar);
14 VALUE sws_get_struct(VALUE self, VALUE obj) {
15 struct sample_wrapped_struct* bar;
16 Data_Get_Struct(obj, struct sample_wrapped_struct, bar);
18 return INT2FIX((*bar).foo);
21 VALUE sws_get_struct_rdata(VALUE self, VALUE obj) {
22 struct sample_wrapped_struct* bar;
23 bar = (struct sample_wrapped_struct*) RDATA(obj)->data;
24 return INT2FIX(bar->foo);
27 VALUE sws_change_struct(VALUE self, VALUE obj, VALUE new_val) {
28 struct sample_wrapped_struct* new_struct = (struct sample_wrapped_struct *)malloc(sizeof(struct sample_wrapped_struct));
29 new_struct->foo = FIX2INT(new_val);
30 RDATA(obj)->data = new_struct;
31 return Qnil;
34 void Init_subtend_wrapped_struct() {
35 VALUE cls;
36 cls = rb_define_class("SubtendWrappedStruct", rb_cObject);
37 rb_define_method(cls, "wrap_struct", sws_wrap_struct, 1);
38 rb_define_method(cls, "get_struct", sws_get_struct, 1);
39 rb_define_method(cls, "get_struct_rdata", sws_get_struct_rdata, 1);
40 rb_define_method(cls, "change_struct", sws_change_struct, 2);