aboutsummaryrefslogtreecommitdiff
path: root/ruby/generator.cc
blob: 1c8f7ee3a4b1fb693fbfd915a5e36e8ee5772c9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2008 Ciaran McCreesh
 *
 * This file is part of the Paludis package manager. Paludis is free software;
 * you can redistribute it and/or modify it under the terms of the GNU General
 * Public License version 2, as published by the Free Software Foundation.
 *
 * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <paludis_ruby.hh>

using namespace paludis;
using namespace paludis::ruby;

#define RUBY_FUNC_CAST(x) reinterpret_cast<VALUE (*)(...)>(x)

namespace
{
    static VALUE c_generator_module;
    static VALUE c_generator;
    static VALUE c_generator_all;
    static VALUE c_generator_matches;
    static VALUE c_generator_package;
    static VALUE c_generator_in_repository;
    static VALUE c_generator_from_repository;
    static VALUE c_generator_category;
    static VALUE c_generator_intersection;

    VALUE
    generator_init(int, VALUE *, VALUE self)
    {
        return self;
    }

    VALUE
    generator_all_new(VALUE self)
    {
        Generator * ptr(0);
        try
        {
            ptr = new generator::All();
            VALUE data(Data_Wrap_Struct(self, 0, &Common<Generator>::free, ptr));
            rb_obj_call_init(data, 0, &self);
            return data;
        }
        catch (const std::exception & e)
        {
            delete ptr;
            exception_to_ruby_exception(e);
        }
    }

    /*
     * Document-method: &
     *
     * call-seq:
     *     &(other_generator) -> Generator
     *
     * Combine with another Generator using a set intersection.
     */
    VALUE
    generator_ampersand(VALUE self, VALUE other)
    {
        Generator * ptr(0);
        try
        {
            Generator g1(value_to_generator(self));
            Generator g2(value_to_generator(other));
            return generator_to_value(g1 & g2);
        }
        catch (const std::exception & e)
        {
            delete ptr;
            exception_to_ruby_exception(e);
        }
    }

    /*
     * Document-method: |
     *
     * call-seq:
     *     |(filter) -> FilteredGenerator
     *
     * Combine with a Filter to produce a FilteredGenerator.
     */
    VALUE
    generator_bar(VALUE self, VALUE other)
    {
        Generator * ptr(0);
        try
        {
            Generator g1(value_to_generator(self));
            Filter f1(value_to_filter(other));
            return filtered_generator_to_value(g1 | f1);
        }
        catch (const std::exception & e)
        {
            delete ptr;
            exception_to_ruby_exception(e);
        }
    }

    VALUE
    generator_matches_new(VALUE self, VALUE spec_v)
    {
        Generator * ptr(0);
        try
        {
            std::tr1::shared_ptr<const PackageDepSpec> spec(value_to_package_dep_spec(spec_v));
            ptr = new generator::Matches(*spec);
            VALUE data(Data_Wrap_Struct(self, 0, &Common<Generator>::free, ptr));
            rb_obj_call_init(data, 1, &spec_v);
            return data;
        }
        catch (const std::exception & e)
        {
            delete ptr;
            exception_to_ruby_exception(e);
        }
    }

    VALUE
    generator_category_new(VALUE self, VALUE name_v)
    {
        Generator * ptr(0);
        try
        {
            CategoryNamePart name(StringValuePtr(name_v));
            ptr = new generator::Category(name);
            VALUE data(Data_Wrap_Struct(self, 0, &Common<Generator>::free, ptr));
            rb_obj_call_init(data, 1, &name_v);
            return data;
        }
        catch (const std::exception & e)
        {
            delete ptr;
            exception_to_ruby_exception(e);
        }
    }

    VALUE
    generator_package_new(VALUE self, VALUE name_v)
    {
        Generator * ptr(0);
        try
        {
            QualifiedPackageName name(value_to_qualified_package_name(name_v));
            ptr = new generator::Package(name);
            VALUE data(Data_Wrap_Struct(self, 0, &Common<Generator>::free, ptr));
            rb_obj_call_init(data, 1, &name_v);
            return data;
        }
        catch (const std::exception & e)
        {
            delete ptr;
            exception_to_ruby_exception(e);
        }
    }

    VALUE
    generator_in_repository_new(VALUE self, VALUE name_v)
    {
        Generator * ptr(0);
        try
        {
            RepositoryName name(StringValuePtr(name_v));
            ptr = new generator::InRepository(name);
            VALUE data(Data_Wrap_Struct(self, 0, &Common<Generator>::free, ptr));
            rb_obj_call_init(data, 1, &name_v);
            return data;
        }
        catch (const std::exception & e)
        {
            delete ptr;
            exception_to_ruby_exception(e);
        }
    }

    VALUE
    generator_from_repository_new(VALUE self, VALUE name_v)
    {
        Generator * ptr(0);
        try
        {
            RepositoryName name(StringValuePtr(name_v));
            ptr = new generator::FromRepository(name);
            VALUE data(Data_Wrap_Struct(self, 0, &Common<Generator>::free, ptr));
            rb_obj_call_init(data, 1, &name_v);
            return data;
        }
        catch (const std::exception & e)
        {
            delete ptr;
            exception_to_ruby_exception(e);
        }
    }

    VALUE
    generator_intersection_new(VALUE self, VALUE f1_v, VALUE f2_v)
    {
        Generator * ptr(0);
        try
        {
            Generator f1(value_to_generator(f1_v));
            Generator f2(value_to_generator(f2_v));
            ptr = new generator::Intersection(f1, f2);
            VALUE data(Data_Wrap_Struct(self, 0, &Common<Generator>::free, ptr));
            rb_obj_call_init(data, 2, &f1_v);
            return data;
        }
        catch (const std::exception & e)
        {
            delete ptr;
            exception_to_ruby_exception(e);
        }
    }

    void do_register_generator()
    {
        /*
         * Document-module: Paludis::Generator
         *
         * Collection of classes that produce results for an
         * Environment selection.
         */
        c_generator_module = rb_define_module_under(paludis_module(), "Generator");

        /*
         * Document-class: Paludis::Generator::Generator
         *
         * Generator for an Environment selection.
         */
        c_generator = rb_define_class_under(c_generator_module, "Generator", rb_cObject);
        rb_funcall(c_generator, rb_intern("private_class_method"), 1, rb_str_new2("new"));
        rb_define_method(c_generator, "initialize", RUBY_FUNC_CAST(&generator_init), -1);
        rb_define_method(c_generator, "to_s", RUBY_FUNC_CAST(&Common<Generator>::to_s), 0);
        rb_define_method(c_generator, "&", RUBY_FUNC_CAST(&generator_ampersand), 1);
        rb_define_method(c_generator, "|", RUBY_FUNC_CAST(&generator_bar), 1);

        /*
         * Document-class: Paludis::Generator::All
         *
         * Generate all packages.
         */
        c_generator_all = rb_define_class_under(c_generator_module, "All", c_generator);
        rb_define_singleton_method(c_generator_all, "new", RUBY_FUNC_CAST(&generator_all_new), 0);

        /*
         * Document-class: Paludis::Generator::Matches
         *
         * Generate matching packages.
         */
        c_generator_matches = rb_define_class_under(c_generator_module, "Matches", c_generator);
        rb_define_singleton_method(c_generator_matches, "new", RUBY_FUNC_CAST(&generator_matches_new), 1);

        /*
         * Document-class: Paludis::Generator::Intersection
         *
         * Generate packages from the intersection of two other Generator instances.
         */
        c_generator_intersection = rb_define_class_under(c_generator_module, "Intersection", c_generator);
        rb_define_singleton_method(c_generator_intersection, "new", RUBY_FUNC_CAST(&generator_intersection_new), 2);

        /*
         * Document-class: Paludis::Generator::Package
         *
         * Generate all named packages.
         */
        c_generator_package = rb_define_class_under(c_generator_module, "Package", c_generator);
        rb_define_singleton_method(c_generator_package, "new", RUBY_FUNC_CAST(&generator_package_new), 1);

        /*
         * Document-class: Paludis::Generator::Category
         *
         * Generate all packages in a given category.
         */
        c_generator_category = rb_define_class_under(c_generator_module, "Category", c_generator);
        rb_define_singleton_method(c_generator_category, "new", RUBY_FUNC_CAST(&generator_category_new), 1);

        /*
         * Document-class: Paludis::Generator::InRepository
         *
         * Generate all packages in a given repository.
         */
        c_generator_in_repository = rb_define_class_under(c_generator_module, "InRepository", c_generator);
        rb_define_singleton_method(c_generator_in_repository, "new", RUBY_FUNC_CAST(&generator_in_repository_new), 1);

        /*
         * Document-class: Paludis::Generator::FromRepository
         *
         * Generate all packages originally from a given repository.
         */
        c_generator_from_repository = rb_define_class_under(c_generator_module, "FromRepository", c_generator);
        rb_define_singleton_method(c_generator_from_repository, "new", RUBY_FUNC_CAST(&generator_from_repository_new), 1);
    }
}

Generator
paludis::ruby::value_to_generator(VALUE v)
{
    if (rb_obj_is_kind_of(v, c_generator))
    {
        Generator * f_ptr;
        Data_Get_Struct(v, Generator, f_ptr);
        return *f_ptr;
    }
    else
        rb_raise(rb_eTypeError, "Can't convert %s into Generator", rb_obj_classname(v));
}

VALUE
paludis::ruby::generator_to_value(const Generator & v)
{
    Generator * vv(new Generator(v));
    return Data_Wrap_Struct(c_generator, 0, &Common<Generator>::free, vv);
}

RegisterRubyClass::Register paludis_ruby_register_generator PALUDIS_ATTRIBUTE((used))
    (&do_register_generator);