1 | /* -*- c-basic-offset: 2 -*- */ |
2 | /* |
3 | Copyright(C) 2016 Brazil |
4 | |
5 | This library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License version 2.1 as published by the Free Software Foundation. |
8 | |
9 | This library is distributed in the hope that it will be useful, |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | Lesser General Public License for more details. |
13 | |
14 | You should have received a copy of the GNU Lesser General Public |
15 | License along with this library; if not, write to the Free Software |
16 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
17 | */ |
18 | |
19 | #include "../grn_ctx_impl.h" |
20 | #include <string.h> |
21 | |
22 | #ifdef GRN_WITH_MRUBY |
23 | #include <mruby.h> |
24 | #include <mruby/class.h> |
25 | #include <mruby/data.h> |
26 | |
27 | #include "mrb_pointer.h" |
28 | #include "mrb_object.h" |
29 | #include "mrb_converter.h" |
30 | |
31 | static struct mrb_data_type mrb_grn_pointer_type = { |
32 | "Groonga::Pointer" , |
33 | NULL |
34 | }; |
35 | |
36 | static mrb_value |
37 | mrb_grn_pointer_initialize(mrb_state *mrb, mrb_value self) |
38 | { |
39 | mrb_value mrb_pointer_ptr; |
40 | |
41 | mrb_get_args(mrb, "o" , &mrb_pointer_ptr); |
42 | DATA_TYPE(self) = &mrb_grn_pointer_type; |
43 | DATA_PTR(self) = mrb_cptr(mrb_pointer_ptr); |
44 | return self; |
45 | } |
46 | |
47 | static mrb_value |
48 | mrb_grn_pointer_get_value(mrb_state *mrb, mrb_value self) |
49 | { |
50 | grn_obj *pointer; |
51 | |
52 | pointer = DATA_PTR(self); |
53 | if (GRN_BULK_VSIZE(pointer) == 0) { |
54 | return mrb_nil_value(); |
55 | } |
56 | |
57 | return grn_mrb_value_from_grn_obj(mrb, GRN_PTR_VALUE(pointer)); |
58 | } |
59 | |
60 | void |
61 | grn_mrb_pointer_init(grn_ctx *ctx) |
62 | { |
63 | grn_mrb_data *data = &(ctx->impl->mrb); |
64 | mrb_state *mrb = data->state; |
65 | struct RClass *module = data->module; |
66 | struct RClass *klass; |
67 | |
68 | klass = mrb_define_class_under(mrb, module, "Pointer" , mrb->object_class); |
69 | MRB_SET_INSTANCE_TT(klass, MRB_TT_DATA); |
70 | mrb_define_method(mrb, klass, "initialize" , |
71 | mrb_grn_pointer_initialize, MRB_ARGS_REQ(1)); |
72 | mrb_define_method(mrb, klass, "value" , |
73 | mrb_grn_pointer_get_value, MRB_ARGS_NONE()); |
74 | mrb_define_method(mrb, klass, "inspect" , |
75 | grn_mrb_object_inspect, MRB_ARGS_NONE()); |
76 | } |
77 | #endif |
78 | |