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

/*
 * Copyright (c) 2007 Richard Brown
 *
 * 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/mask.hh>
#include <paludis/util/wrapped_forward_iterator.hh>
#include <ruby.h>

using namespace paludis;
using namespace paludis::ruby;

namespace
{
    static VALUE c_mask;
    static VALUE c_user_mask;
    static VALUE c_unaccepted_mask;
    static VALUE c_repository_mask;
    static VALUE c_unsupported_mask;
    static VALUE c_association_mask;
    static VALUE c_overridden_mask;
    static VALUE c_mask_override_reason;

    struct V
    {
        VALUE value;
        std::shared_ptr<const Mask> mm;

        V(std::shared_ptr<const Mask> _m) :
            mm(_m)
        {
        }

        void visit(const UserMask &)
        {
            value = Data_Wrap_Struct(c_user_mask, 0, &Common<std::shared_ptr<const Mask> >::free,
                    new std::shared_ptr<const Mask>(mm));
        }

        void visit(const UnacceptedMask &)
        {
            value = Data_Wrap_Struct(c_unaccepted_mask, 0, &Common<std::shared_ptr<const Mask> >::free,
                    new std::shared_ptr<const Mask>(mm));
        }

        void visit(const UnsupportedMask &)
        {
            value = Data_Wrap_Struct(c_unsupported_mask, 0, &Common<std::shared_ptr<const Mask> >::free,
                    new std::shared_ptr<const Mask>(mm));
        }

        void visit(const RepositoryMask &)
        {
            value = Data_Wrap_Struct(c_repository_mask, 0, &Common<std::shared_ptr<const Mask> >::free,
                    new std::shared_ptr<const Mask>(mm));
        }

        void visit(const AssociationMask &)
        {
            value = Data_Wrap_Struct(c_association_mask, 0, &Common<std::shared_ptr<const Mask> >::free,
                    new std::shared_ptr<const Mask>(mm));
        }
    };

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

    /*
     * call-seq:
     *     key -> String
     *
     * A single character key, which can be used by clients if they need a very compact way of representing a mask.
     */
    VALUE
    mask_key(VALUE self)
    {
        std::shared_ptr<const Mask> * ptr;
        Data_Get_Struct(self, std::shared_ptr<const Mask>, ptr);
        char c = (*ptr)->key();
        const char* c_ptr = &c;
        return rb_str_new2(c_ptr);
    }

    /*
     * Document-method: description
     *
     * call-seq:
     *     description -> String
     *
     * A description of the mask.
     */
    /*
     * Document-method: explanation
     *
     * call-seq:
     *     explanation -> String
     *
     * An explanation of why we are unsupported.
     */
    template <typename T_, const std::string (T_::* m_) () const>
    struct MaskStringKey
    {
        static VALUE
        fetch(VALUE self)
        {
            std::shared_ptr<const Mask> * ptr;
            Data_Get_Struct(self, std::shared_ptr<const Mask>, ptr);
            std::shared_ptr<const T_> cast_ptr(std::static_pointer_cast<const T_>(*ptr));
            return rb_str_new2(((*cast_ptr).*m_)().c_str());
        }
    };

    /*
     * Document-method: unaccepted_key
     *
     * call-seq:
     *     unaccepted_key -> String
     *
     * Fetch the name of the metadata key that is not accepted.
     */
    /*
     * Document-method: mask_key
     *
     * call-seq:
     *     mask_key -> String
     *
     * Fetch the name of a metadata key explaining the mask.
     */
    template <typename T_, const std::string (T_::* m_) () const>
    struct MaskMetadataKey
    {
        static VALUE
        fetch(VALUE self)
        {
            std::shared_ptr<const Mask> * ptr;
            Data_Get_Struct(self, std::shared_ptr<const Mask>, ptr);
            std::shared_ptr<const T_> cast_ptr(std::static_pointer_cast<const T_>(*ptr));
            return rb_str_new2(((*cast_ptr).*m_)().c_str());
        }
    };

    /*
     * call-seq:
     *     associated_package_spec -> PackageDepSpec
     *
     * Fetch the associated package.
     */
    VALUE
    association_mask_associated_package(VALUE self)
    {
        std::shared_ptr<const Mask> * ptr;
        Data_Get_Struct(self, std::shared_ptr<const Mask>, ptr);
        std::shared_ptr<const AssociationMask> cast_ptr(std::static_pointer_cast<const AssociationMask>(*ptr));
        return package_dep_spec_to_value((cast_ptr)->associated_package_spec());
    }

    /*
     * call-seq:
     *     mask -> Mask
     */
    VALUE
    overridden_mask_mask(VALUE self)
    {
        std::shared_ptr<const OverriddenMask> * ptr;
        Data_Get_Struct(self, std::shared_ptr<const OverriddenMask>, ptr);
        return mask_to_value((*ptr)->mask());
    }

    /*
     * call-seq:
     *     override_reason -> MaskOverrideReason
     */
    VALUE
    overridden_mask_override_reason(VALUE self)
    {
        std::shared_ptr<const OverriddenMask> * ptr;
        Data_Get_Struct(self, std::shared_ptr<const OverriddenMask>, ptr);
        return INT2FIX((*ptr)->override_reason());
    }


    void do_register_mask()
    {
        /*
         * Document-class: Paludis::Mask
         *
         * A mask represents one reason why a PackageID is masked (not installable).
         * A basic Mask has:
         *
         * * A single character key, which can be used by clients if they need a very compact way of representing a mask.
         *
         * * A description.
         *
         * Subclasses provide additional information.
         */
        c_mask = rb_define_class_under(paludis_module(), "Mask", rb_cObject);
        rb_funcall(c_mask, rb_intern("private_class_method"), 1, rb_str_new2("new"));
        rb_define_method(c_mask, "key", RUBY_FUNC_CAST(&mask_key), 0);
        rb_define_method(c_mask, "description",
                RUBY_FUNC_CAST((&MaskStringKey<Mask,&Mask::description>::fetch)), 0);
        rb_define_method(c_mask, "initialize", RUBY_FUNC_CAST(&mask_init), -1);

        /*
         * Document-class: Paludis::UserMask
         *
         * A UserMask is a Mask due to user configuration.
         */
        c_user_mask = rb_define_class_under(paludis_module(), "UserMask", c_mask);

        /*
         * Document-class: Paludis::UnacceptedMask
         *
         * An UnacceptedMask is a Mask that signifies that a particular value or combination of values in
         * (for example) a MetadataCollectionKey or MetadataSpecTreeKey is not accepted by user configuration.
         */
        c_unaccepted_mask = rb_define_class_under(paludis_module(), "UnacceptedMask", c_mask);
        rb_define_method(c_unaccepted_mask, "unaccepted_key_name",
                RUBY_FUNC_CAST((&MaskMetadataKey<UnacceptedMask,&UnacceptedMask::unaccepted_key_name>::fetch)), 0);

        /*
         * Document-class: Paludis::RepositoryMask
         *
         * A RepositoryMask is a Mask that signifies that a PackageID has been marked as masked by a Repository.
         */
        c_repository_mask = rb_define_class_under(paludis_module(), "RepositoryMask", c_mask);

        /*
         * Document-class: Paludis::UnsupportedMask
         *
         *  An UnsupportedMask is a Mask that signifies that a PackageID is not supported, for example because
         *  it is broken or because it uses an unrecognised EAPI.
         */
        c_unsupported_mask = rb_define_class_under(paludis_module(), "UnsupportedMask", c_mask);
        rb_define_method(c_unsupported_mask, "explanation",
                RUBY_FUNC_CAST((&MaskStringKey<UnsupportedMask,&UnsupportedMask::explanation>::fetch)), 0);

        /*
         * Document-class: Paludis::AssociationMask
         *
         * An AssociationMask is a Mask that signifies that a PackageID is masked because of its association with
         * another PackageID that is itself masked.
         *
         * This is used by old-style virtuals. If the provider of a virtual is masked then the virtual itself is
         * masked by association.
         */
        c_association_mask = rb_define_class_under(paludis_module(), "AssociationMask", c_mask);
        rb_define_method(c_association_mask, "associated_package_spec", RUBY_FUNC_CAST(&association_mask_associated_package), 0);

        /*
         * Document-class: Paludis::OverriddenMask
         *
         * An OverriddenMask holds a Mask and an explanation of why it has been overridden.
         */
        c_overridden_mask = rb_define_class_under(paludis_module(), "OverriddenMask", rb_cObject);
        rb_define_method(c_overridden_mask, "mask", RUBY_FUNC_CAST(&overridden_mask_mask), 0);
        rb_define_method(c_overridden_mask, "override_reason", RUBY_FUNC_CAST(&overridden_mask_override_reason), 0);

        /*
         * Document-module: Paludis::MaskOverrideReason
         *
         * The reason an OverriddenMask is overridden.
         */
        c_mask_override_reason = rb_define_module_under(paludis_module(), "MaskOverrideReason");
        for (MaskOverrideReason r(static_cast<MaskOverrideReason>(0)), r_end(last_mro) ; r != r_end ;
                r = static_cast<MaskOverrideReason>(static_cast<int>(r) + 1))
            rb_define_const(c_mask_override_reason, value_case_to_RubyCase(stringify(r)).c_str(), INT2FIX(r));

        // cc_enum_special<paludis/mask.hh, MaskOverrideReason, c_mask_override_reason>
    }
}

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

VALUE
paludis::ruby::overridden_mask_to_value(std::shared_ptr<const OverriddenMask> m)
{
    return Data_Wrap_Struct(c_overridden_mask, 0, &Common<std::shared_ptr<const OverriddenMask> >::free,
            new std::shared_ptr<const OverriddenMask>(m));
}

RegisterRubyClass::Register paludis_ruby_register_mask PALUDIS_ATTRIBUTE((used))
    (&do_register_mask);