Groonga 3.0.9 Source Code Document
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
mrb_accessor.c
Go to the documentation of this file.
1 /* -*- c-basic-offset: 2 -*- */
2 /*
3  Copyright(C) 2013 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 "../ctx_impl.h"
20 
21 #ifdef GRN_WITH_MRUBY
22 #include <mruby.h>
23 #include <mruby/class.h>
24 #include <mruby/variable.h>
25 #include <mruby/data.h>
26 
27 #include "../db.h"
28 #include "mrb_accessor.h"
29 
30 static struct mrb_data_type mrb_grn_accessor_type = {
31  "Groonga::Accessor",
32  NULL
33 };
34 
36 mrb_grn_accessor_new(mrb_state *mrb, grn_accessor *accessor)
37 {
38  grn_ctx *ctx = (grn_ctx *)mrb->ud;
39  struct RClass *module = ctx->impl->mrb.module;
40  struct RClass *klass;
41  mrb_value mrb_accessor_ptr;
42 
43  mrb_accessor_ptr = mrb_cptr_value(mrb, accessor);
44  klass = mrb_class_ptr(mrb_const_get(mrb, mrb_obj_value(module),
45  mrb_intern(mrb, "Accessor")));
46  return mrb_obj_new(mrb, klass, 1, &mrb_accessor_ptr);
47 }
48 
49 static mrb_value
50 mrb_grn_accessor_initialize(mrb_state *mrb, mrb_value self)
51 {
52  mrb_value mrb_accessor_ptr;
53 
54  mrb_get_args(mrb, "o", &mrb_accessor_ptr);
55  DATA_TYPE(self) = &mrb_grn_accessor_type;
56  DATA_PTR(self) = mrb_cptr(mrb_accessor_ptr);
57  return self;
58 }
59 
60 static mrb_value
61 mrb_grn_accessor_next(mrb_state *mrb, mrb_value self)
62 {
63  grn_accessor *accessor;
64 
65  accessor = DATA_PTR(self);
66  if (!accessor->next) { return mrb_nil_value(); }
67  return mrb_cptr_value(mrb, accessor->next);
68 }
69 
70 void
72 {
73  mrb_state *mrb = ctx->impl->mrb.state;
74  struct RClass *module = ctx->impl->mrb.module;
75  struct RClass *klass;
76 
77  klass = mrb_define_class_under(mrb, module, "Accessor", mrb->object_class);
79  mrb_define_method(mrb, klass, "initialize", mrb_grn_accessor_initialize, MRB_ARGS_REQ(1));
80  mrb_define_method(mrb, klass, "next", mrb_grn_accessor_next, MRB_ARGS_NONE());
81 }
82 #endif