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 module groonga_d.smart_obj;
19 
20 
21 private static import groonga_d.groonga;
22 
23 extern (C++, grn) {
24 /+
25 	class SharedObj
26 	{
27 	public:
28 		this(groonga_d.groonga.grn_ctx* ctx, groonga_d.groonga.grn_obj* obj)
29 		{
30 			this.ctx_ = ctx;
31 			this.obj_ = obj;
32 		}
33 
34 		this(groonga_d.groonga.grn_ctx* ctx, const (char)* name, int name_size)
35 		{
36 			this.ctx_ = ctx;
37 			this.obj_ = groonga_d.groonga.grn_ctx_get(this.ctx_, name, name_size);
38 		}
39 
40 		this(groonga_d.groonga.grn_ctx* ctx, groonga_d.groonga.grn_id id)
41 		{
42 			this.ctx_ = ctx;
43 			this.obj_ = groonga_d.groonga.grn_ctx_at(this.ctx_, id);
44 		}
45 
46 		this(SharedObj &&shared_obj)
47 		{
48 			this.ctx_ = shared_obj.ctx_;
49 			this.obj_(shared_obj.obj_);
50 			groonga_d.groonga.grn_obj_refer(this.ctx_, this.obj_);
51 		}
52 
53 		~this()
54 		{
55 			if (this.obj_) {
56 				groonga_d.groonga.grn_obj_unref(this.ctx_, this.obj_);
57 			}
58 		}
59 
60 		groonga_d.groonga.grn_obj* get()
61 		{
62 			return this.obj_;
63 		}
64 
65 		groonga_d.groonga.grn_obj* release()
66 		{
67 			groonga_d.groonga.grn_obj* obj = this.obj_;
68 			this.obj_ = null;
69 
70 			return obj;
71 		}
72 
73 		void reset(groonga_d.groonga.grn_obj* obj)
74 		{
75 			this.obj_ = obj;
76 		}
77 
78 	private:
79 		groonga_d.groonga.grn_ctx* ctx_;
80 		groonga_d.groonga.grn_obj* obj_;
81 	}
82 
83 	class UniqueObj
84 	{
85 	public:
86 		this(groonga_d.groonga.grn_ctx* ctx, groonga_d.groonga.grn_obj* obj)
87 		{
88 			this.ctx_ = ctx;
89 			this.obj_ = obj;
90 		}
91 
92 		this(UniqueObj &&unique_obj)
93 		{
94 			this.ctx_ = unique_obj.ctx_;
95 			this.obj_ = unique_obj.obj_;
96 			unique_obj.obj_ = null;
97 		}
98 
99 		~this()
100 		{
101 			if (this.obj_) {
102 				groonga_d.groonga.grn_obj_close(this.ctx_, this.obj_);
103 			}
104 		}
105 
106 		groonga_d.groonga.grn_obj* get()
107 		{
108 			return this.obj_;
109 		}
110 
111 		groonga_d.groonga.grn_obj* release()
112 		{
113 			groonga_d.groonga.grn_obj* obj = this.obj_;
114 			this.obj_ = null;
115 
116 			return obj;
117 		}
118 
119 		void reset(groonga_d.groonga.grn_obj* obj)
120 		{
121 			this.obj_ = obj;
122 		}
123 
124 	private:
125 		groonga_d.groonga.grn_ctx* ctx_;
126 		groonga_d.groonga.grn_obj* obj_;
127 	}
128 +/
129 }