aboutsummaryrefslogtreecommitdiff
path: root/ruby/package_dep_spec_constraint.cc
blob: 4cfd42f67f1a407662bf188bc2e7b1becc0430eb (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
/* 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;

    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));
        }
    };

    /*
     * 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());
    }

    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);
    }
}

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);