Debian Codegraph @

lib/mrb/mrb_hash_table.c

raw symbols SCIP
1 /*
2 Copyright(C) 2014-2015 Brazil
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
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/string.h>
26
27 #include "mrb_ctx.h"
28 #include "mrb_hash_table.h"
29 #include "mrb_options.h"
30
31 static struct mrb_data_type mrb_grn_hash_table_type = {
32 "Groonga::HashTable",
33 NULL
34 };
35
36 static mrb_value
37 mrb_grn_hash_table_class_create(mrb_state *mrb, mrb_value klass)
38 {
39 grn_ctx *ctx = (grn_ctx *)mrb->ud;
40 mrb_value mrb_options = mrb_nil_value();
41 const char *name = NULL;
42 unsigned int name_size = 0;
43 const char *path = NULL;
44 grn_obj_flags flags = GRN_OBJ_TABLE_HASH_KEY;
45 grn_obj *key_type = NULL;
46 grn_obj *value_type = NULL;
47 grn_obj *table;
48
49 mrb_get_args(mrb, "|H", &mrb_options);
50
51 if (!mrb_nil_p(mrb_options)) {
52 mrb_value mrb_name;
53 mrb_value mrb_flags;
54 mrb_value mrb_key_type;
55 mrb_value mrb_value_type;
56
57 mrb_name = grn_mrb_options_get_lit(mrb, mrb_options, "name");
58 if (!mrb_nil_p(mrb_name)) {
59 name = RSTRING_PTR(mrb_name);
60 name_size = RSTRING_LEN(mrb_name);
61 }
62
63 mrb_flags = grn_mrb_options_get_lit(mrb, mrb_options, "flags");
64 if (!mrb_nil_p(mrb_flags)) {
65 flags |= mrb_integer(mrb_flags);
66 }
67
68 mrb_key_type = grn_mrb_options_get_lit(mrb, mrb_options, "key_type");
69 if (!mrb_nil_p(mrb_key_type)) {
70 key_type = DATA_PTR(mrb_key_type);
71 }
72
73 mrb_value_type = grn_mrb_options_get_lit(mrb, mrb_options, "value_type");
74 if (!mrb_nil_p(mrb_value_type)) {
75 key_type = DATA_PTR(mrb_value_type);
76 }
77 }
78
79 table = grn_table_create(ctx, name, name_size, path, flags,
80 key_type, value_type);
81 grn_mrb_ctx_check(mrb);
82
83 return mrb_funcall(mrb, klass, "new", 1, mrb_cptr_value(mrb, table));
84 }
85
86 static mrb_value
87 mrb_grn_hash_table_initialize(mrb_state *mrb, mrb_value self)
88 {
89 mrb_value mrb_hash_table_ptr;
90
91 mrb_get_args(mrb, "o", &mrb_hash_table_ptr);
92 DATA_TYPE(self) = &mrb_grn_hash_table_type;
93 DATA_PTR(self) = mrb_cptr(mrb_hash_table_ptr);
94 return self;
95 }
96
97 void
98 grn_mrb_hash_table_init(grn_ctx *ctx)
99 {
100 grn_mrb_data *data = &(ctx->impl->mrb);
101 mrb_state *mrb = data->state;
102 struct RClass *module = data->module;
103 struct RClass *table_with_key_class;
104 struct RClass *klass;
105
106 table_with_key_class = mrb_class_get_under(mrb, module, "TableWithKey");
107 klass = mrb_define_class_under(mrb, module, "HashTable", table_with_key_class);
108 MRB_SET_INSTANCE_TT(klass, MRB_TT_DATA);
109
110 mrb_define_class_method(mrb, klass, "create",
111 mrb_grn_hash_table_class_create,
112 MRB_ARGS_OPT(1));
113
114 mrb_define_method(mrb, klass, "initialize",
115 mrb_grn_hash_table_initialize, MRB_ARGS_REQ(1));
116 }
117 #endif