Groonga
Loading...
Searching...
No Matches
smart_obj.hpp
Go to the documentation of this file.
1/*
2 Copyright(C) 2021 Sutou Kouhei <kou@clear-code.com>
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#pragma once
20
21namespace grn {
22 class SharedObj {
23 public:
25 ctx_(ctx),
26 obj_(obj) {
27 }
28
29 SharedObj(grn_ctx *ctx, const char *name, int name_size) :
30 ctx_(ctx),
31 obj_(grn_ctx_get(ctx_, name, name_size)) {
32 }
33
35 ctx_(ctx),
36 obj_(grn_ctx_at(ctx_, id)) {
37 }
38
39 SharedObj(SharedObj &&shared_obj) :
40 ctx_(shared_obj.ctx_),
41 obj_(shared_obj.obj_) {
42 grn_obj_refer(ctx_, obj_);
43 }
44
46 if (obj_) {
47 grn_obj_unref(ctx_, obj_);
48 }
49 }
50
52 return obj_;
53 }
54
56 grn_obj *obj = obj_;
57 obj_ = nullptr;
58 return obj;
59 }
60
61 void reset(grn_obj *obj) {
62 obj_ = obj;
63 }
64
65 private:
66 grn_ctx *ctx_;
67 grn_obj *obj_;
68 };
69
70 class UniqueObj {
71 public:
73 ctx_(ctx),
74 obj_(obj) {
75 }
76
77 UniqueObj(UniqueObj &&unique_obj) :
78 ctx_(unique_obj.ctx_),
79 obj_(unique_obj.obj_) {
80 unique_obj.obj_ = nullptr;
81 }
82
84 if (obj_) {
85 grn_obj_close(ctx_, obj_);
86 }
87 }
88
90 return obj_;
91 }
92
94 grn_obj *obj = obj_;
95 obj_ = nullptr;
96 return obj;
97 }
98
99 void reset(grn_obj *obj) {
100 obj_ = obj;
101 }
102
103 private:
104 grn_ctx *ctx_;
105 grn_obj *obj_;
106 };
107}
Definition smart_obj.hpp:22
SharedObj(grn_ctx *ctx, grn_obj *obj)
Definition smart_obj.hpp:24
void reset(grn_obj *obj)
Definition smart_obj.hpp:61
SharedObj(grn_ctx *ctx, const char *name, int name_size)
Definition smart_obj.hpp:29
SharedObj(SharedObj &&shared_obj)
Definition smart_obj.hpp:39
grn_obj * get()
Definition smart_obj.hpp:51
SharedObj(grn_ctx *ctx, grn_id id)
Definition smart_obj.hpp:34
grn_obj * release()
Definition smart_obj.hpp:55
~SharedObj()
Definition smart_obj.hpp:45
Definition smart_obj.hpp:70
void reset(grn_obj *obj)
Definition smart_obj.hpp:99
UniqueObj(UniqueObj &&unique_obj)
Definition smart_obj.hpp:77
grn_obj * release()
Definition smart_obj.hpp:93
~UniqueObj()
Definition smart_obj.hpp:83
UniqueObj(grn_ctx *ctx, grn_obj *obj)
Definition smart_obj.hpp:72
grn_obj * get()
Definition smart_obj.hpp:89
GRN_API grn_rc grn_obj_close(grn_ctx *ctx, grn_obj *obj)
Close an object.
uint32_t grn_id
Definition groonga.h:44
GRN_API grn_obj * grn_ctx_at(grn_ctx *ctx, grn_id id)
Retrieve the object corresponding to the specified ID from the context or the database that the conte...
GRN_API grn_obj * grn_ctx_get(grn_ctx *ctx, const char *name, int name_size)
Retrieve the object corresponding to the specified name from the database that the context is using.
GRN_API void grn_obj_unref(grn_ctx *ctx, grn_obj *obj)
GRN_API grn_rc grn_obj_refer(grn_ctx *ctx, grn_obj *obj)
Definition arrow.hpp:25
Definition groonga.h:351
Definition groonga.h:919