aboutsummaryrefslogtreecommitdiff
path: root/ruby/dep_tag.cc
blob: 3ff8912964477f61a746866673b3045ec9fc801d (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
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2007 Richard Brown <rbrown@gentoo.org>
 * Copyright (c) 2007 David Leverton <levertond@googlemail.com>
 *
 * 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>
#include <paludis/dep_spec.hh>
#include <paludis/dep_tag.hh>
#include <paludis/util/visitor-impl.hh>
#include <ruby.h>

using namespace paludis;
using namespace paludis::ruby;

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

namespace
{
    static VALUE c_dep_tag;
    static VALUE c_dependency_dep_tag;
    static VALUE c_glsa_dep_tag;
    static VALUE c_general_set_dep_tag;
    static VALUE c_target_dep_tag;
    struct V :
        ConstVisitor<DepTagVisitorTypes>
    {
        VALUE value;
        tr1::shared_ptr<const DepTag> mm;

        V(tr1::shared_ptr<const DepTag> _m) :
            mm(_m)
        {
        }

        void visit(const DependencyDepTag &)
        {
            value = Data_Wrap_Struct(c_dependency_dep_tag, 0, &Common<tr1::shared_ptr<const DependencyDepTag> >::free,
                    new tr1::shared_ptr<const DependencyDepTag>(tr1::static_pointer_cast<const DependencyDepTag>(mm)));
        }

        void visit(const GLSADepTag &)
        {
            value = Data_Wrap_Struct(c_glsa_dep_tag, 0, &Common<tr1::shared_ptr<const GLSADepTag> >::free,
                    new tr1::shared_ptr<const GLSADepTag>(tr1::static_pointer_cast<const GLSADepTag>(mm)));
        }

        void visit(const GeneralSetDepTag &)
        {
            value = Data_Wrap_Struct(c_general_set_dep_tag, 0, &Common<tr1::shared_ptr<const GeneralSetDepTag> >::free,
                    new tr1::shared_ptr<const GeneralSetDepTag>(tr1::static_pointer_cast<const GeneralSetDepTag>(mm)));
        }

        void visit(const TargetDepTag &)
        {
            value = Data_Wrap_Struct(c_target_dep_tag, 0, &Common<tr1::shared_ptr<const TargetDepTag> >::free,
                    new tr1::shared_ptr<const TargetDepTag>(tr1::static_pointer_cast<const TargetDepTag>(mm)));
        }
    };


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

    VALUE
    dependency_dep_tag_new(int argc, VALUE * argv, VALUE self)
    {
        if (2 != argc)
            rb_raise(rb_eArgError, "DependencyDepTag expects two arguments, but got %d", argc);

        tr1::shared_ptr<const DependencyDepTag> * ptr(0);
        try
        {
            ptr = new tr1::shared_ptr<const DependencyDepTag>(
                new DependencyDepTag(value_to_package_id(argv[0]),
                    *value_to_package_dep_spec(argv[1]),
                    // XXX make this an argument, once the new
                    // visitors are Rubified
                    tr1::shared_ptr<ConstTreeSequence<DependencySpecTree, AllDepSpec> >(
                        new ConstTreeSequence<DependencySpecTree, AllDepSpec>(
                            tr1::shared_ptr<AllDepSpec>(new AllDepSpec)))));
            VALUE tdata(Data_Wrap_Struct(self, 0, &Common<tr1::shared_ptr<const DependencyDepTag> >::free, ptr));
            rb_obj_call_init(tdata, argc, argv);
            return tdata;
        }
        catch (const std::exception & e)
        {
            delete ptr;
            exception_to_ruby_exception(e);
        }
    }


    VALUE
    glsa_dep_tag_new(int argc, VALUE * argv, VALUE self)
    {
        if (2 != argc)
            rb_raise(rb_eArgError, "GLSADepTag expects two arguments, but got %d",argc);

        tr1::shared_ptr<const GLSADepTag> * ptr(0);
        try
        {
            ptr = new tr1::shared_ptr<const GLSADepTag>(new GLSADepTag(StringValuePtr(argv[0]), StringValuePtr(argv[1])));
            VALUE tdata(Data_Wrap_Struct(self, 0, &Common<tr1::shared_ptr<const GLSADepTag> >::free, ptr));
            rb_obj_call_init(tdata, argc, argv);
            return tdata;
        }
        catch (const std::exception & e)
        {
            delete ptr;
            exception_to_ruby_exception(e);
        }
    }

    VALUE
    general_set_dep_tag_new(int argc, VALUE * argv, VALUE self)
    {
        if (2 != argc)
            rb_raise(rb_eArgError, "GeneralSetDepTag expects two arguments, but got %d",argc);

        tr1::shared_ptr<const GeneralSetDepTag> * ptr(0);
        try
        {
            ptr = new tr1::shared_ptr<const GeneralSetDepTag>(new GeneralSetDepTag(SetName(StringValuePtr(argv[0])), StringValuePtr(argv[1])));
            VALUE tdata(Data_Wrap_Struct(self, 0, &Common<tr1::shared_ptr<const GeneralSetDepTag> >::free, ptr));
            rb_obj_call_init(tdata, argc, argv);
            return tdata;
        }
        catch (const std::exception & e)
        {
            delete ptr;
            exception_to_ruby_exception(e);
        }
    }

    VALUE
    target_dep_tag_new(VALUE self)
    {
        tr1::shared_ptr<const TargetDepTag> * ptr(0);
        try
        {
            ptr = new tr1::shared_ptr<const TargetDepTag>(new TargetDepTag);
            VALUE tdata(Data_Wrap_Struct(self, 0, &Common<tr1::shared_ptr<const TargetDepTag> >::free, ptr));
            rb_obj_call_init(tdata, 0, 0);
            return tdata;
        }
        catch (const std::exception & e)
        {
            delete ptr;
            exception_to_ruby_exception(e);
        }
    }

    /*
     * Document-method: short_text
     *
     * call-seq:
     *     short_text -> String
     *
     * Fetch our short text (for example, 'GLSA-1234') that is
     * displayed with the dep list entry.
     */
    /*
     * Document-method: category
     *
     * call-seq:
     *     category -> String
     *
     * Fetch our DepTagCategory's tag.
     */
    /*
     * Document-method: glsa_title
     *
     * call-seq:
     *     glsa_title -> String
     *
     * Fetch our GLSA title (for example, 'Yet another PHP remote access hole').
     */
    /*
     * Document-method: source
     *
     * call-seq:
     *     source -> String
     *
     * From which repository or environment did we originate?
     */
    template <typename T_, std::string (T_::* m_) () const>
    struct DepTagThings
    {
        static VALUE
        fetch(VALUE self)
        {
            tr1::shared_ptr<const T_> * ptr;
            Data_Get_Struct(self, tr1::shared_ptr<const T_>, ptr);
            return rb_str_new2((((**ptr).*m_)()).c_str());
        }
    };

    void do_register_dep_tag()
    {
        /*
         * Document-class: Paludis::DepTag
         *
         * A DepTag can be associated with a PackageDepSpec, and is transferred onto any associated DepListEntry instances.
         *
         * It is used for tagging dep list entries visually, for example to indicate an associated GLSA.
         */
        c_dep_tag = rb_define_class_under(paludis_module(), "DepTag", rb_cObject);
        rb_funcall(c_dep_tag, rb_intern("private_class_method"), 1, rb_str_new2("new"));
        rb_define_method(c_dep_tag, "initialize", RUBY_FUNC_CAST(&dep_tag_init_1), -1);
        rb_define_method(c_dep_tag, "short_text", RUBY_FUNC_CAST((&DepTagThings<DepTag,&DepTag::short_text>::fetch)), 0);
        rb_define_method(c_dep_tag, "category", RUBY_FUNC_CAST((&DepTagThings<DepTag,&DepTag::category>::fetch)), 0);

        /*
         * Document-class: Paludis::DependencyDepTag
         *
         * DepTag subclass for dependencies.
         */
        c_dependency_dep_tag = rb_define_class_under(paludis_module(), "DependencyDepTag", c_dep_tag);
        rb_define_singleton_method(c_dependency_dep_tag, "new", RUBY_FUNC_CAST(&dependency_dep_tag_new), -1);

        /*
         * Document-class: Paludis::GLSADepTag
         *
         * DepTag subclass for GLSAs.
         */
        c_glsa_dep_tag = rb_define_class_under(paludis_module(), "GLSADepTag", c_dep_tag);
        rb_define_singleton_method(c_glsa_dep_tag, "new", RUBY_FUNC_CAST(&glsa_dep_tag_new), -1);
        rb_define_method(c_glsa_dep_tag, "glsa_title", RUBY_FUNC_CAST((&DepTagThings<GLSADepTag,
                        &GLSADepTag::glsa_title>::fetch)), 0);

        /*
         * Document-class: Paludis::GeneralSetDepTag
         *
         * DepTag subclass for general sets.
         */
        c_general_set_dep_tag = rb_define_class_under(paludis_module(), "GeneralSetDepTag", c_dep_tag);
        rb_define_singleton_method(c_general_set_dep_tag, "new", RUBY_FUNC_CAST(&general_set_dep_tag_new), -1);
        rb_define_method(c_general_set_dep_tag, "source", RUBY_FUNC_CAST((&DepTagThings<GeneralSetDepTag,
                        &GeneralSetDepTag::source>::fetch)), 0);

        /*
         * Document-class: Paludis::TargetDepTag
         *
         * DepTag subclass for general sets.
         */
        c_target_dep_tag = rb_define_class_under(paludis_module(), "TargetDepTag", c_dep_tag);
        rb_define_singleton_method(c_target_dep_tag, "new", RUBY_FUNC_CAST(&target_dep_tag_new), 0);
    }
}

VALUE
paludis::ruby::dep_tag_to_value(tr1::shared_ptr<const DepTag> m)
{
    try
    {
        V v(m);
        m->accept(v);
        return v.value;
    }
    catch (const std::exception & e)
    {
        exception_to_ruby_exception(e);
    }
}

RegisterRubyClass::Register paludis_ruby_register_dep_tag PALUDIS_ATTRIBUTE((used))
    (&do_register_dep_tag);