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

/*
 * Copyright (c) 2006 Ciaran McCreesh <ciaranm@ciaranm.org>
 *
 * 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/version_spec.hh>
#include <ruby.h>

using namespace paludis;
using namespace paludis::ruby;

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

namespace
{
    static VALUE c_qualified_package_name;

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

    /*
     * call-seq:
     *     QualifiedPackageName.new(category_and_package_string) -> QualifiedPackageName
     *     QualifiedPackageName.new(category_name, package_name) -> QualifiedPackageName
     *
     * Creates a new QualifiedPackageName.
     */
    VALUE
    qualified_package_name_new(int argc, VALUE *argv, VALUE self)
    {
        QualifiedPackageName * ptr(0);
        try
        {
            if (1 == argc)
            {
                ptr = new QualifiedPackageName(StringValuePtr(argv[0]));
            }
            else if (2 == argc)
            {
                ptr = new QualifiedPackageName(CategoryNamePart(StringValuePtr(argv[0])), PackageNamePart(StringValuePtr(argv[1])));
            }
            else
            {
                rb_raise(rb_eArgError, "QualifiedPackageName expects one or two arguments, but got %d",argc);
            }
            VALUE tdata(Data_Wrap_Struct(self, 0, &Common<QualifiedPackageName>::free, ptr));
            rb_obj_call_init(tdata,argc, argv);
            return tdata;
        }
        catch (const std::exception & e)
        {
            delete ptr;
            exception_to_ruby_exception(e);
        }
    }

    /*
     * Document-method: category
     *
     * call-seq:
     *     category -> String
     *
     * Fetch the category part of the QualifiedPackageName.
     */
    /*
     * Document-method: category=
     *
     * call-seq:
     *     category=
     *
     * Set the category.
     */
    /*
     * Document-method: package
     *
     * call-seq:
     *     package -> String
     *
     * Fetch the package name part of the QualifiedPackageName.
     */
    /*
     * Document-method: package=
     *
     * call-seq:
     *     package=
     *
     * Set the package name.
     */
    template <typename T_, T_ QualifiedPackageName::* m_>
    struct QPNMember
    {
        static VALUE
        fetch(VALUE self)
        {
            QualifiedPackageName * p;
            Data_Get_Struct(self, QualifiedPackageName, p);
            return rb_str_new2(stringify(p->*m_).c_str());
        }

        static VALUE
        set(VALUE self, VALUE str)
        {
            try
            {
                QualifiedPackageName * p;
                Data_Get_Struct(self, QualifiedPackageName, p);
                p->*m_ =  T_ ((StringValuePtr(str)));
                return self;
            }
            catch (const std::exception & e)
            {
                exception_to_ruby_exception(e);
            }
        }
    };

    VALUE
    qualified_package_name_compare(VALUE left, VALUE right)
    {
        try
        {
            QualifiedPackageName l = value_to_qualified_package_name(left);
            QualifiedPackageName r = value_to_qualified_package_name(right);
            return INT2FIX(paludis::compare(l,r));
        }
        catch (const std::exception & e)
        {
            exception_to_ruby_exception(e);
        }
    }

    void do_register_qualified_package_name()
    {
        /*
         * Document-class: Paludis::QualifiedPackageName
         *
         * Represents a category plus package name. Includes
         * Comparable[http://www.ruby-doc.org/core/classes/Comparable.html]
         */
        c_qualified_package_name = rb_define_class_under(paludis_module(), "QualifiedPackageName", rb_cObject);
        rb_define_singleton_method(c_qualified_package_name, "new", RUBY_FUNC_CAST(&qualified_package_name_new), -1);
        rb_define_method(c_qualified_package_name, "initialize", RUBY_FUNC_CAST(&qualified_package_name_init), -1);
        rb_define_method(c_qualified_package_name, "<=>", RUBY_FUNC_CAST(qualified_package_name_compare), 1);
        rb_include_module(c_qualified_package_name, rb_mComparable);
        rb_define_method(c_qualified_package_name, "to_s", RUBY_FUNC_CAST(&Common<QualifiedPackageName>::to_s), 0);
        rb_define_alias(c_qualified_package_name, "to_str", "to_s");
        rb_define_method(c_qualified_package_name, "category",
                RUBY_FUNC_CAST((&QPNMember<CategoryNamePart, &QualifiedPackageName::category>::fetch)), 0);
        rb_define_method(c_qualified_package_name, "category=",
                RUBY_FUNC_CAST((&QPNMember<CategoryNamePart, &QualifiedPackageName::category>::set)), 1);
        rb_define_method(c_qualified_package_name, "package",
                RUBY_FUNC_CAST((&QPNMember<PackageNamePart, &QualifiedPackageName::package>::fetch)), 0);
        rb_define_method(c_qualified_package_name, "package=",
                RUBY_FUNC_CAST((&QPNMember<PackageNamePart, &QualifiedPackageName::package>::set)), 1);
    }
}

QualifiedPackageName
paludis::ruby::value_to_qualified_package_name(VALUE v)
{
    if (T_STRING == TYPE(v))
        return QualifiedPackageName(StringValuePtr(v));
    else if (rb_obj_is_kind_of(v, c_qualified_package_name))
    {
        QualifiedPackageName * v_ptr;
        Data_Get_Struct(v, QualifiedPackageName, v_ptr);
        return *v_ptr;
    }
    else
    {
        rb_raise(rb_eTypeError, "Can't convert %s into QualifiedPackageName", rb_obj_classname(v));
    }
}

VALUE
paludis::ruby::qualified_package_name_to_value(const QualifiedPackageName & qpn)
{
    QualifiedPackageName * qpn2(new QualifiedPackageName(qpn));
    return Data_Wrap_Struct(c_qualified_package_name, 0, &Common<QualifiedPackageName>::free, qpn2);
}

RegisterRubyClass::Register paludis_ruby_register_qualified_package_name PALUDIS_ATTRIBUTE((used))
    (&do_register_qualified_package_name);