File: data_spec.c

package info (click to toggle)
jruby 1.7.26-1%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 84,572 kB
  • sloc: ruby: 669,910; java: 253,056; xml: 35,152; ansic: 9,187; yacc: 7,267; cpp: 5,244; sh: 1,036; makefile: 345; jsp: 48; tcl: 40
file content (78 lines) | stat: -rw-r--r-- 2,377 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <string.h>

#include "ruby.h"
#include "rubyspec.h"

#ifdef __cplusplus
extern "C" {
#endif

#if defined(HAVE_RDATA) && defined(HAVE_DATA_WRAP_STRUCT)
struct sample_wrapped_struct {
    int foo;
};

VALUE sdaf_alloc_func(VALUE klass) {
    struct sample_wrapped_struct* bar = (struct sample_wrapped_struct *)malloc(sizeof(struct sample_wrapped_struct));
    bar->foo = 42;
    return Data_Wrap_Struct(klass, NULL, NULL, bar);
}

VALUE sdaf_get_struct(VALUE self) {
    struct sample_wrapped_struct* bar;
    Data_Get_Struct(self, struct sample_wrapped_struct, bar);

    return INT2FIX((*bar).foo);
}

VALUE sws_wrap_struct(VALUE self, VALUE val) {
    struct sample_wrapped_struct* bar = (struct sample_wrapped_struct *)malloc(sizeof(struct sample_wrapped_struct));
    bar->foo = FIX2INT(val);
    return Data_Wrap_Struct(rb_cObject, NULL, NULL, bar);
}

VALUE sws_wrap_struct_null(VALUE self, VALUE val) {
    struct sample_wrapped_struct* bar = (struct sample_wrapped_struct *)malloc(sizeof(struct sample_wrapped_struct));
    bar->foo = FIX2INT(val);
    return Data_Wrap_Struct(0, NULL, NULL, bar);
}

VALUE sws_get_struct(VALUE self, VALUE obj) {
    struct sample_wrapped_struct* bar;
    Data_Get_Struct(obj, struct sample_wrapped_struct, bar);

    return INT2FIX((*bar).foo);
}

VALUE sws_get_struct_rdata(VALUE self, VALUE obj) {
  struct sample_wrapped_struct* bar;
  bar = (struct sample_wrapped_struct*) RDATA(obj)->data;
  return INT2FIX(bar->foo);
}

VALUE sws_change_struct(VALUE self, VALUE obj, VALUE new_val) {
  struct sample_wrapped_struct* new_struct = (struct sample_wrapped_struct *)malloc(sizeof(struct sample_wrapped_struct));
  new_struct->foo = FIX2INT(new_val);
  RDATA(obj)->data = new_struct;
  return Qnil;
}

void Init_data_spec() {
  VALUE cls;
  cls = rb_define_class("CApiAllocSpecs", rb_cObject);

  rb_define_alloc_func(cls, sdaf_alloc_func);
  rb_define_method(cls, "wrapped_data", sdaf_get_struct, 0);

  cls = rb_define_class("CApiWrappedStructSpecs", rb_cObject);
  rb_define_method(cls, "wrap_struct", sws_wrap_struct, 1);
  rb_define_method(cls, "wrap_struct_null", sws_wrap_struct_null, 1);
  rb_define_method(cls, "get_struct", sws_get_struct, 1);
  rb_define_method(cls, "get_struct_rdata", sws_get_struct_rdata, 1);
  rb_define_method(cls, "change_struct", sws_change_struct, 2);
#endif
}

#ifdef __cplusplus
}
#endif