1 | /* -*- c-basic-offset: 2 -*- */ |
2 | /* |
3 | Copyright(C) 2017 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 | |
21 | #ifdef GRN_WITH_MRUBY |
22 | #include <mruby.h> |
23 | #include <mruby/class.h> |
24 | #include <mruby/data.h> |
25 | #include <mruby/array.h> |
26 | |
27 | #include "mrb_ctx.h" |
28 | #include "mrb_window_definition.h" |
29 | |
30 | static void |
31 | mrb_grn_window_definition_free(mrb_state *mrb, void *data) |
32 | { |
33 | grn_window_definition *definition = data; |
34 | |
35 | if (!definition) { |
36 | return; |
37 | } |
38 | |
39 | if (definition->sort_keys) { |
40 | mrb_free(mrb, definition->sort_keys); |
41 | } |
42 | if (definition->group_keys) { |
43 | mrb_free(mrb, definition->group_keys); |
44 | } |
45 | mrb_free(mrb, definition); |
46 | } |
47 | |
48 | static struct mrb_data_type mrb_grn_window_definition_type = { |
49 | "Groonga::WindowDefinition" , |
50 | mrb_grn_window_definition_free |
51 | }; |
52 | |
53 | static mrb_value |
54 | mrb_grn_window_definition_initialize(mrb_state *mrb, mrb_value self) |
55 | { |
56 | grn_window_definition *result; |
57 | |
58 | DATA_TYPE(self) = &mrb_grn_window_definition_type; |
59 | |
60 | result = mrb_calloc(mrb, 1, sizeof(grn_window_definition)); |
61 | DATA_PTR(self) = result; |
62 | |
63 | return self; |
64 | } |
65 | |
66 | |
67 | static mrb_value |
68 | mrb_grn_window_definition_close(mrb_state *mrb, mrb_value self) |
69 | { |
70 | grn_window_definition *definition; |
71 | |
72 | definition = DATA_PTR(self); |
73 | if (definition) { |
74 | mrb_grn_window_definition_free(mrb, definition); |
75 | DATA_PTR(self) = NULL; |
76 | } |
77 | |
78 | return mrb_nil_value(); |
79 | } |
80 | |
81 | static mrb_value |
82 | mrb_grn_window_definition_set_sort_keys(mrb_state *mrb, mrb_value self) |
83 | { |
84 | grn_window_definition *definition; |
85 | mrb_value mrb_keys; |
86 | |
87 | definition = DATA_PTR(self); |
88 | mrb_get_args(mrb, "A!" , &mrb_keys); |
89 | |
90 | if (definition->sort_keys) { |
91 | mrb_free(mrb, definition->sort_keys); |
92 | } |
93 | |
94 | if (mrb_nil_p(mrb_keys)) { |
95 | definition->sort_keys = NULL; |
96 | definition->n_sort_keys = 0; |
97 | } else { |
98 | mrb_int i, n; |
99 | n = RARRAY_LEN(mrb_keys); |
100 | definition->sort_keys = mrb_calloc(mrb, n, sizeof(grn_table_sort_key)); |
101 | for (i = 0; i < n; i++) { |
102 | grn_table_sort_key *sort_key = DATA_PTR(RARRAY_PTR(mrb_keys)[i]); |
103 | definition->sort_keys[i] = *sort_key; |
104 | } |
105 | definition->n_sort_keys = n; |
106 | } |
107 | |
108 | return mrb_nil_value(); |
109 | } |
110 | |
111 | static mrb_value |
112 | mrb_grn_window_definition_set_group_keys(mrb_state *mrb, mrb_value self) |
113 | { |
114 | grn_window_definition *definition; |
115 | mrb_value mrb_keys; |
116 | |
117 | definition = DATA_PTR(self); |
118 | mrb_get_args(mrb, "A!" , &mrb_keys); |
119 | |
120 | if (definition->group_keys) { |
121 | mrb_free(mrb, definition->group_keys); |
122 | } |
123 | |
124 | if (mrb_nil_p(mrb_keys)) { |
125 | definition->group_keys = NULL; |
126 | definition->n_group_keys = 0; |
127 | } else { |
128 | mrb_int i, n; |
129 | n = RARRAY_LEN(mrb_keys); |
130 | definition->group_keys = mrb_calloc(mrb, n, sizeof(grn_table_sort_key)); |
131 | for (i = 0; i < n; i++) { |
132 | grn_table_sort_key *group_key = DATA_PTR(RARRAY_PTR(mrb_keys)[i]); |
133 | definition->group_keys[i] = *group_key; |
134 | } |
135 | definition->n_group_keys = n; |
136 | } |
137 | |
138 | return mrb_nil_value(); |
139 | } |
140 | |
141 | void |
142 | grn_mrb_window_definition_init(grn_ctx *ctx) |
143 | { |
144 | grn_mrb_data *data = &(ctx->impl->mrb); |
145 | mrb_state *mrb = data->state; |
146 | struct RClass *module = data->module; |
147 | struct RClass *klass; |
148 | |
149 | klass = mrb_define_class_under(mrb, module, "WindowDefinition" , |
150 | mrb->object_class); |
151 | MRB_SET_INSTANCE_TT(klass, MRB_TT_DATA); |
152 | |
153 | mrb_define_method(mrb, klass, "initialize" , |
154 | mrb_grn_window_definition_initialize, MRB_ARGS_NONE()); |
155 | |
156 | mrb_define_method(mrb, klass, "close" , |
157 | mrb_grn_window_definition_close, MRB_ARGS_NONE()); |
158 | |
159 | mrb_define_method(mrb, klass, "sort_keys=" , |
160 | mrb_grn_window_definition_set_sort_keys, MRB_ARGS_REQ(1)); |
161 | mrb_define_method(mrb, klass, "group_keys=" , |
162 | mrb_grn_window_definition_set_group_keys, MRB_ARGS_REQ(1)); |
163 | } |
164 | #endif |
165 | |