aboutsummaryrefslogtreecommitdiff
path: root/paludis/util/kc.hh
blob: ad4f1b074d273a3beca7f86e1a37c945c6f86a1f (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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

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

#ifndef PALUDIS_GUARD_PALUDIS_UTIL_KC_HH
#define PALUDIS_GUARD_PALUDIS_UTIL_KC_HH 1

#include <paludis/util/kc-fwd.hh>

namespace paludis
{
    namespace kc
    {
        template <typename Key_, typename Type_>
        struct Field
        {
            typedef Type_ ConstructorType;
            typedef void DefaultConstructorValueType;

            typedef Key_ NamedKeyFirstParamType;
            typedef Type_ NamedKeySecondParamType;
        };

        template <unsigned id_>
        struct NoField
        {
            typedef NoField<id_> ConstructorType;
            typedef NoField<id_> DefaultConstructorValueType;

            typedef NoField<id_> NamedKeyFirstParamType;
            typedef NoField<id_> NamedKeySecondParamType;
        };

        template <unsigned id_>
        struct Key
        {
        };

        template <bool b_, typename T_>
        struct NamedField;

        template <typename T_>
        struct NamedField<false, T_>
        {
        };

        template <unsigned id_, typename T_>
        struct NamedField<true, Field<Key<id_>, T_> >
        {
            const T_ & value;

            NamedField(const T_ & v) :
                value(v)
            {
            }
        };

        template <typename T_>
        struct Part;

        template <unsigned id_>
        struct Part<NoField<id_> >
        {
            public:
                void operator[] (const NoField<id_> &);

                Part(const NoField<id_> &)
                {
                }

                Part(const NamedField<false, NoField<id_> > &)
                {
                }
        };

        template <unsigned id_, typename Type_>
        class Part<Field<Key<id_>, Type_> >
        {
            private:
                Type_ _value;

            public:
                Type_ & operator[] (const Key<id_> &)
                {
                    return _value;
                }

                const Type_ & operator[] (const Key<id_> &) const
                {
                    return _value;
                }

                Part(const Type_ & v) :
                    _value(v)
                {
                }

                Part(const NamedField<true, Field<Key<id_>, Type_> > & v) :
                    _value(v.value)
                {
                }
        };

        template <
            typename T1_,
            typename T2_,
            typename T3_,
            typename T4_,
            typename T5_
            > class KeyedClass :
                public Part<T1_>,
                public Part<T2_>,
                public Part<T3_>,
                public Part<T4_>,
                public Part<T5_>
        {
            public:
                typedef KeyedClass BaseType;

                KeyedClass
                (
                        const typename T1_::ConstructorType & v1 = typename T1_::DefaultConstructorValueType(),
                        const typename T2_::ConstructorType & v2 = typename T2_::DefaultConstructorValueType(),
                        const typename T3_::ConstructorType & v3 = typename T3_::DefaultConstructorValueType(),
                        const typename T4_::ConstructorType & v4 = typename T4_::DefaultConstructorValueType(),
                        const typename T5_::ConstructorType & v5 = typename T5_::DefaultConstructorValueType()
                  ) :
                    Part<T1_>(v1),
                    Part<T2_>(v2),
                    Part<T3_>(v3),
                    Part<T4_>(v4),
                    Part<T5_>(v5)
                {
                }

                KeyedClass(const KeyedClass & other) :
                    Part<T1_>(other),
                    Part<T2_>(other),
                    Part<T3_>(other),
                    Part<T4_>(other),
                    Part<T5_>(other)
                {
                }

                using Part<T1_>::operator[];
                using Part<T2_>::operator[];
                using Part<T3_>::operator[];
                using Part<T4_>::operator[];
                using Part<T5_>::operator[];

                template <
                    bool b1_,
                    bool b2_,
                    bool b3_,
                    bool b4_,
                    bool b5_
                    >
                struct Named
                {
                    NamedField<b1_, T1_> v1;
                    NamedField<b2_, T2_> v2;
                    NamedField<b3_, T3_> v3;
                    NamedField<b4_, T4_> v4;
                    NamedField<b5_, T5_> v5;

                    Named()
                    {
                    }

                    Named(
                            NamedField<b1_, T1_> p1,
                            NamedField<b2_, T2_> p2,
                            NamedField<b3_, T3_> p3,
                            NamedField<b4_, T4_> p4,
                            NamedField<b5_, T5_> p5
                         ) :
                        v1(p1),
                        v2(p2),
                        v3(p3),
                        v4(p4),
                        v5(p5)
                    {
                    }

                    Named<true, b2_, b3_, b4_, b5_> operator() (
                            const typename T1_::NamedKeyFirstParamType &,
                            const typename T1_::NamedKeySecondParamType & v)
                    {
                        return Named<! b1_, b2_, b3_, b4_, b5_>(v, v2, v3, v4, v5);
                    }

                    Named<b1_, true, b3_, b4_, b5_> operator() (
                            const typename T2_::NamedKeyFirstParamType &,
                            const typename T2_::NamedKeySecondParamType & v)
                    {
                        return Named<b1_, ! b2_, b3_, b4_, b5_>(v1, v, v3, v4, v5);
                    }

                    Named<b1_, b2_, true, b4_, b5_> operator() (
                            const typename T3_::NamedKeyFirstParamType &,
                            const typename T3_::NamedKeySecondParamType & v)
                    {
                        return Named<b1_, b2_, ! b3_, b4_, b5_>(v1, v2, v, v4, v5);
                    }

                    Named<b1_, b2_, b3_, true, b5_> operator() (
                            const typename T4_::NamedKeyFirstParamType &,
                            const typename T4_::NamedKeySecondParamType & v)
                    {
                        return Named<b1_, b2_, b3_, ! b4_, b5_>(v1, v2, v3, v, v5);
                    }

                    Named<b1_, b2_, b3_, b4_, true> operator() (
                            const typename T5_::NamedKeyFirstParamType &,
                            const typename T5_::NamedKeySecondParamType & v)
                    {
                        return Named<b1_, b2_, b3_, b4_, ! b5_>(v1, v2, v3, v4, v);
                    }
                };

                static Named<false, false, false, false, false> named_create()
                {
                    return Named<false, false, false, false, false>();
                }

                template <
                    bool b1_,
                    bool b2_,
                    bool b3_,
                    bool b4_,
                    bool b5_>
                KeyedClass(const Named<b1_, b2_, b3_, b4_, b5_> & named) :
                    Part<T1_>(named.v1),
                    Part<T2_>(named.v2),
                    Part<T3_>(named.v3),
                    Part<T4_>(named.v4),
                    Part<T5_>(named.v5)
                {
                }
        };

    }
}

#endif