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

/*
 * Copyright (c) 2011 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>

#include <paludis/package_dep_spec_constraint.hh>

#include <algorithm>
#include <list>
#include <ruby.h>

using namespace paludis;
using namespace paludis::ruby;

namespace
{
    static VALUE c_package_dep_spec_constraint;
    static VALUE c_name_constraint;
    static VALUE c_package_name_part_constraint;
    static VALUE c_category_name_part_constraint;

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

        V(const std::shared_ptr<const PackageDepSpecConstraint> & m) :
            mm(m)
        {
        }

        void visit(const NameConstraint &)
        {
            value = Data_Wrap_Struct(c_name_constraint, 0, &Common<std::shared_ptr<const PackageDepSpecConstraint> >::free,
                    new std::shared_ptr<const PackageDepSpecConstraint>(mm));
        }

        void visit(const CategoryNamePartConstraint &)
        {
            value = Data_Wrap_Struct(c_category_name_part_constraint, 0, &Common<std::shared_ptr<const PackageDepSpecConstraint> >::free,
                    new std::shared_ptr<const PackageDepSpecConstraint>(mm));
        }

        void visit(const PackageNamePartConstraint &)
        {
            value = Data_Wrap_Struct(c_package_name_part_constraint, 0, &Common<std::shared_ptr<const PackageDepSpecConstraint> >::free,
                    new std::shared_ptr<const PackageDepSpecConstraint>(mm));
        }
    };

    /*
     * Document-method: name
     *
     * The name constraint.
     */
    static VALUE
    name_constraint_name(VALUE self)
    {
        std::shared_ptr<const PackageDepSpecConstraint> * ptr;
        Data_Get_Struct(self, std::shared_ptr<const PackageDepSpecConstraint>, ptr);
        return qualified_package_name_to_value((std::static_pointer_cast<const NameConstraint>(*ptr))->name());
    }

    /*
     * Document-method: name_part
     *
     * The name part constraint.
     */
    static VALUE
    package_name_part_constraint_name_part(VALUE self)
    {
        std::shared_ptr<const PackageDepSpecConstraint> * ptr;
        Data_Get_Struct(self, std::shared_ptr<const PackageDepSpecConstraint>, ptr);
        return rb_str_new2(stringify((std::static_pointer_cast<const PackageNamePartConstraint>(*ptr))->name_part()).c_str());
    }

    /*
     * Document-method: name_part
     *
     * The name part constraint.
     */
    static VALUE
    category_name_part_constraint_name_part(VALUE self)
    {
        std::shared_ptr<const PackageDepSpecConstraint> * ptr;
        Data_Get_Struct(self, std::shared_ptr<const PackageDepSpecConstraint>, ptr);
        return rb_str_new2(stringify((std::static_pointer_cast<const CategoryNamePartConstraint>(*ptr))->name_part()).c_str());
    }

    void do_register_package_dep_spec_constraint()
    {
        /*
         * Document-class: Paludis::PackageDepSpecConstraint
         *
         * Represents a constraint in a PackageDepSpec.
         */
        c_package_dep_spec_constraint = rb_define_class_under(paludis_module(), "PackageDepSpecConstraint", rb_cObject);
        rb_funcall(c_package_dep_spec_constraint, rb_intern("private_class_method"), 1, rb_str_new2("new"));

        /*
         * Document-class: Paludis::NameConstraint
         *
         * Represents a cat/pkg name constraint in a PackageDepSpec.
         */
        c_name_constraint = rb_define_class_under(
                paludis_module(), "NameConstraint", c_package_dep_spec_constraint);
        rb_funcall(c_name_constraint, rb_intern("private_class_method"), 1, rb_str_new2("new"));
        rb_define_method(c_package_dep_spec_constraint, "name", RUBY_FUNC_CAST(&name_constraint_name), 0);

        /*
         * Document-class: Paludis::PackageNamePartConstraint
         *
         * Represents a /pkg name constraint in a PackageDepSpec.
         */
        c_package_name_part_constraint = rb_define_class_under(
                paludis_module(), "PackageNamePartConstraint", c_package_dep_spec_constraint);
        rb_funcall(c_package_name_part_constraint, rb_intern("private_class_method"), 1, rb_str_new2("new"));
        rb_define_method(c_package_name_part_constraint, "name_part", RUBY_FUNC_CAST(
                    &package_name_part_constraint_name_part), 0);

        /*
         * Document-class: Paludis::CategoryNamePartConstraint
         *
         * Represents a /pkg name constraint in a PackageDepSpec.
         */
        c_category_name_part_constraint = rb_define_class_under(
                paludis_module(), "CategoryNamePartConstraint", c_package_dep_spec_constraint);
        rb_funcall(c_category_name_part_constraint, rb_intern("private_class_method"), 1, rb_str_new2("new"));
        rb_define_method(c_category_name_part_constraint, "name_part", RUBY_FUNC_CAST(
                    &category_name_part_constraint_name_part), 0);
    }
}

VALUE
paludis::ruby::package_dep_spec_constraint_to_value(const std::shared_ptr<const PackageDepSpecConstraint> & 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_package_dep_spec_constraint PALUDIS_ATTRIBUTE((used))
    (&do_register_package_dep_spec_constraint);