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

/*
 * Copyright (c) 2006, 2007, 2008, 2009, 2010, 2011 Ciaran McCreesh
 * Copyright (c) 2006, 2007, 2008 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
 */

#ifndef PALUDIS_GUARD_RUBY_RUBY_PALUDIS_RUBY_HH
#define PALUDIS_GUARD_RUBY_RUBY_PALUDIS_RUBY_HH 1

#include "config.h"

#include <paludis/util/pimp.hh>
#include <paludis/util/attributes.hh>
#include <paludis/util/exception.hh>
#include <paludis/util/singleton.hh>
#include <paludis/environment.hh>
#include <paludis/environments/no_config/no_config_environment.hh>
#include <paludis/repository.hh>
#include <paludis/contents.hh>
#include <paludis/dep_tag.hh>
#include <paludis/mask-fwd.hh>
#include <paludis/metadata_key.hh>
#include <paludis/selection.hh>
#include <paludis/filter.hh>
#include <paludis/generator.hh>
#include <paludis/filtered_generator.hh>
#include <paludis/choice-fwd.hh>
#include <paludis/dep_label-fwd.hh>
#include <paludis/util/stringify.hh>
#include <paludis/util/hashes.hh>

#ifdef ENABLE_RUBY_QA
#include <paludis/qa.hh>
#endif

#include <ruby.h>

#define RUBY_FUNC_CAST(x) reinterpret_cast<VALUE (*)(...)>(x)
#define RDOC_IS_STUPID(x, y) RUBY_FUNC_CAST((y))
#define FAKE_RDOC_METHOD(x) //

namespace paludis
{

    namespace ruby
    {

#ifdef ENABLE_RUBY_QA
        class RubyQAReporter : public QAReporter
        {
            VALUE* reporter;

            public:
                RubyQAReporter(VALUE*);
                void message(const QAMessage &);
                void status(const std::string &);
        };
#endif

        /* general utilities */

        void exception_to_ruby_exception(const std::exception &) PALUDIS_ATTRIBUTE((noreturn));

        std::string value_case_to_RubyCase(const std::string & s);

        VALUE paludis_module();

        /* constructors */

        VALUE package_database_to_value(std::shared_ptr<PackageDatabase>);
        VALUE repository_to_value(std::shared_ptr<Repository>);
        VALUE version_spec_to_value(const VersionSpec &);
        VALUE package_id_to_value(std::shared_ptr<const PackageID>);
        VALUE dep_tag_to_value(std::shared_ptr<const DepTag>);
        VALUE qualified_package_name_to_value(const QualifiedPackageName &);
        VALUE contents_to_value(std::shared_ptr<const Contents>);
        VALUE repository_mask_info_to_value(std::shared_ptr<const RepositoryMaskInfo>);
        VALUE metadata_key_to_value(std::shared_ptr<const MetadataKey> m);
        VALUE fetch_action_failure_to_value(const FetchActionFailure &);
        VALUE generator_to_value(const Generator &);
        VALUE filter_to_value(const Filter &);
        VALUE filtered_generator_to_value(const FilteredGenerator &);
#ifdef ENABLE_RUBY_QA
        VALUE qa_message_to_value(const QAMessage &);
#endif
        template <typename T_> VALUE dep_tree_to_value(const std::shared_ptr<const T_> &);
        template <typename T_> std::shared_ptr<const T_> value_to_dep_tree(VALUE);
        VALUE package_dep_spec_to_value(const std::shared_ptr<const PackageDepSpec> &);
        VALUE uri_label_to_value(const std::shared_ptr<const URILabel> &);
        VALUE dependencies_label_to_value(const std::shared_ptr<const DependenciesLabel> &);
        VALUE mask_to_value(std::shared_ptr<const Mask>);
        VALUE choices_to_value(const std::shared_ptr<const Choices> & c);
        VALUE choice_to_value(const std::shared_ptr<const Choice> & c);
        VALUE choice_value_to_value(const std::shared_ptr<const ChoiceValue> & c);
        VALUE match_package_options_to_value(const MatchPackageOptions & c);
        VALUE bool_to_value(bool b);

        VersionSpec value_to_version_spec(VALUE v);
        std::shared_ptr<const PackageID> value_to_package_id(VALUE, bool nil_ok = false);
        std::shared_ptr<const PackageDepSpec> value_to_package_dep_spec(VALUE v);
        std::shared_ptr<const DependenciesLabelsDepSpec> value_to_dependencies_labels_dep_spec(VALUE v);
        std::shared_ptr<const DepSpec> value_to_dep_spec(VALUE v);
        std::shared_ptr<const DepTag> value_to_dep_tag(VALUE v);
        QualifiedPackageName value_to_qualified_package_name(VALUE v);
        std::shared_ptr<Environment> value_to_environment(VALUE v);
        std::shared_ptr<NoConfigEnvironment> value_to_no_config_environment(VALUE v);
        std::shared_ptr<Repository> value_to_repository(VALUE);
        std::shared_ptr<const SupportsActionTestBase> value_to_supports_action_test_base(VALUE v);
        std::shared_ptr<Action> value_to_action(VALUE v);
        std::shared_ptr<const Choices> value_to_choices(VALUE v);
        std::shared_ptr<const Choice> value_to_choice(VALUE v);
        std::shared_ptr<const ChoiceValue> value_to_choice_value(VALUE v);
        std::shared_ptr<const DependenciesLabel> value_to_dependencies_label(VALUE v);
        MatchPackageOptions value_to_match_package_options(VALUE v);
        bool value_to_bool(VALUE v);

        Filter value_to_filter(VALUE v);
        Selection value_to_selection(VALUE v);
        FilteredGenerator value_to_filtered_generator(VALUE v);
        Generator value_to_generator(VALUE v);

        VALUE * install_action_value_ptr();
        VALUE * fetch_action_value_ptr();
        VALUE * info_action_value_ptr();
        VALUE * config_action_value_ptr();
        VALUE * uninstall_action_value_ptr();
        VALUE * pretend_action_value_ptr();
        VALUE * pretend_fetch_action_value_ptr();
        VALUE * dependencies_labels_dep_spec_value_ptr();

#ifdef ENABLE_RUBY_QA
        QACheckProperties value_to_qa_check_properties(VALUE);
#endif

        /* registration */

        class RegisterRubyClass :
            public Singleton<RegisterRubyClass>,
            private Pimp<RegisterRubyClass>
        {
            friend class Singleton<RegisterRubyClass>;

            private:
                RegisterRubyClass();
                ~RegisterRubyClass();

            public:
                class Register;
                friend class Register;

                void execute() const;
        };

        class RegisterRubyClass::Register
        {
            public:
                Register(void (* func)());
        };

        template <typename T_>
        struct Common
        {
            static void free(void * p)
            {
                delete static_cast<T_ *>(p);
            }

            static VALUE compare(VALUE left, VALUE right)
            {
                T_ * left_ptr, * right_ptr;
                Data_Get_Struct(left, T_, left_ptr);
                Data_Get_Struct(right, T_, right_ptr);
                if (*left_ptr < *right_ptr)
                    return INT2FIX(-1);
                if (*left_ptr > *right_ptr)
                    return INT2FIX(1);
                return INT2FIX(0);
            }

            static VALUE equal(VALUE left, VALUE right)
            {
                T_ * left_ptr, * right_ptr;
                Data_Get_Struct(left, T_, left_ptr);
                Data_Get_Struct(right, T_, right_ptr);
                return (*left_ptr == *right_ptr) ? Qtrue : Qfalse;
            }

            static VALUE equal_via_ptr(VALUE left, VALUE right)
            {
                T_ * left_ptr, * right_ptr;
                Data_Get_Struct(left, T_, left_ptr);
                Data_Get_Struct(right, T_, right_ptr);
                return (**left_ptr == **right_ptr) ? Qtrue : Qfalse;
            }

            static VALUE to_s(VALUE self)
            {
                T_ * self_ptr;
                Data_Get_Struct(self, T_, self_ptr);
                return rb_str_new2(stringify(*self_ptr).c_str());

            }

            static VALUE hash(VALUE self)
            {
                T_ * self_ptr;
                Data_Get_Struct(self, T_, self_ptr);
                return INT2FIX(Hash<T_>()(*self_ptr));
            }

            static VALUE hash_via_ptr(VALUE self)
            {
                T_ * self_ptr;
                Data_Get_Struct(self, T_, self_ptr);
                return INT2FIX(Hash<typename std::remove_const<typename T_::element_type>::type>()(**self_ptr));
            }


            static VALUE to_s_via_ptr(VALUE self)
            {
                T_ * self_ptr;
                Data_Get_Struct(self, T_, self_ptr);

                return rb_str_new2(stringify(**self_ptr).c_str());
            }
        };

        void init();
    }
}

#endif