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

/*
 * Copyright (c) 2007 Richard Brown <rbrown@gentoo.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/find_unused_packages_task.hh>
#include <libwrapiter/libwrapiter_forward_iterator.hh>
#include <libwrapiter/libwrapiter_output_iterator.hh>
#include <paludis/util/sequence-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_find_unused_packages_task;

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

    VALUE
    find_unused_packages_task_new(int argc, VALUE *argv, VALUE self)
    {
        FindUnusedPackagesTask * ptr(0);
        try
        {
            if (2 == argc)
            {
                ptr = new FindUnusedPackagesTask(value_to_environment(argv[0]).get(),
                    value_to_repository(argv[1]).get());
            }
            else
            {
                rb_raise(rb_eArgError, "FindUnusedPackagesTask expects two arguments, but got %d",argc);
            }
            VALUE tdata(Data_Wrap_Struct(self, 0, &Common<FindUnusedPackagesTask>::free, ptr));
            rb_obj_call_init(tdata, argc, argv);
            return tdata;
        }
        catch (const std::exception & e)
        {
            delete ptr;
            exception_to_ruby_exception(e);
        }
    }

    VALUE find_unused_packages_task_execute(VALUE self, VALUE qpn)
    {
        try
        {
            FindUnusedPackagesTask * ptr;
            Data_Get_Struct(self, FindUnusedPackagesTask, ptr);
            tr1::shared_ptr<const PackageIDSequence> c(ptr->execute(value_to_qualified_package_name(qpn)));
            VALUE result(rb_ary_new());
            for (PackageIDSequence::ConstIterator i(c->begin()), i_end(c->end()) ; i != i_end ; ++i)
                rb_ary_push(result, package_id_to_value(*i));
            return result;
        }
        catch (const std::exception & e)
        {
            exception_to_ruby_exception(e);
        }
    }

    void do_register_find_unused_packages_task()
    {
        /* Document-class: Paludis::QA::Message
         *
         * A QA message
         */
        c_find_unused_packages_task = rb_define_class_under(paludis_module(),
                "FindUnusedPackagesTask", rb_cObject);
        rb_define_singleton_method(c_find_unused_packages_task,
                "new", RUBY_FUNC_CAST(&find_unused_packages_task_new),-1);
        rb_define_method(c_find_unused_packages_task,
                "initialize", RUBY_FUNC_CAST(&find_unused_packages_task_init),-1);
        rb_define_method(c_find_unused_packages_task,
                "execute", RUBY_FUNC_CAST(&find_unused_packages_task_execute),1);
    }
}

RegisterRubyClass::Register paludis_ruby_register_find_unused_packages_task PALUDIS_ATTRIBUTE((used))
    (&do_register_find_unused_packages_task);