aboutsummaryrefslogtreecommitdiff
path: root/0.8.0/misc/make_sr.bash
blob: be98244afce56c38e011ed2cf1b7b11a0c5fae87 (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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
#!/bin/bash
# vim: set sw=4 sts=4 et tw=0 :

echo -n "/* vim"
echo ": set ro : */"
echo
echo "/* ******************************************************** */"
echo "/* THIS IS A GENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY */"
echo "/* ******************************************************** */"
echo

what_to_make=${1}
shift

for src in ${@} ; do
    if ! source ${src} ; then
        echo "source ${src} failed" 1>&2
        exit 7
    fi
done

make_const_ref()
{
    local x=${@}
    if [[ "${x%\*}" != "${x}" ]] ; then
        echo "${@}" const
    elif [[ "${1#const}" != "${1}" ]] ; then
        echo "${@}" const "&"
    else
        echo const "${@}" "&"
    fi
}

set | grep '^make_class_.*() $' | sort -r | \
while read a ; do
    a=${a##make_class_}
    a=${a%% *}

    want_named_args=
    want_visible=
    want_keys=( )
    want_key_types=( )
    want_comparison_operators=
    want_comparison_fields=( )

    extra_constructors()
    {
        :
    }

    extra_methods()
    {
        :
    }

    allow_named_args()
    {
        want_named_args=yes
    }

    visible()
    {
        want_visible=yes
    }

    doxygen_comment()
    {
        :
    }

    key()
    {
        want_keys=( "${want_keys[@]}" "$1" )
        want_key_types=( "${want_key_types[@]}" "$2" )
    }

    comparison_operators()
    {
        want_comparison_operators=$1
        shift
        want_comparison_fields=( $@ )
    }

    make_class_${a}

    if [[ "${want_comparison_fields[0]}" == "all" ]] ; then
        want_comparison_fields=( ${want_keys[@]} )
    fi

    doxygen_comment()
    {
        cat
    }

    allow_named_args()
    {
        :
    }

    key()
    {
        :
    }

    comparison_operators()
    {
        :
    }


    make_class_${a}

    doxygen_comment()
    {
        :
    }


    extra_constructors()
    {
        echo
        cat
    }

    extra_methods()
    {
        echo
        cat
    }

    if [[ "${what_to_make}" == "--header" ]] ; then
        if [[ -z ${want_visible} ]] ; then
            echo "class ${a}"
        else
            echo "class PALUDIS_VISIBLE ${a}"
        fi
        echo "{"
        echo "    public:"
        echo
        echo "        ///\\name Data members"
        echo "        ///\\{"
        echo

        for (( k = 0 ; k < ${#want_keys[@]} ; k++ )) ; do
            echo "        ${want_key_types[${k}]} ${want_keys[${k}]};"
        done

        echo
        echo "        ///\\}"
        echo

        if [[ -n "${want_comparison_operators}" ]] ; then

            echo "        ///\\name Comparison operators"
            echo "        ///\\{"
            echo

            if [[ "${want_comparison_operators}" == "all" ]] ; then
                want_comparison_operators=( "==" "!=" "<" ">" "<=" ">=" )
                echo "        int compare (const ${a} & other) const;"
            elif [[ "${want_comparison_operators}" == "equality" ]] ; then
                want_comparison_operators=( "==" "!=" )
            else
                echo "bad first parameter for comparison_operators" 1>&2
                exit 1
            fi

            for (( k = 0 ; k < ${#want_comparison_operators[@]} ; k++ )) ; do
                echo "        bool operator${want_comparison_operators[${k}]} (const ${a} & other) const;"
            done

            echo
            echo "        ///\\}"
            echo
        fi

        echo "        ///\\name Basic operations"
        echo "        ///\\{"
        echo

        echo "        ${a}("
        for (( k = 0 ; k < ${#want_keys[@]} ; k++ )) ; do
            echo -n "                $(make_const_ref "${want_key_types[${k}]}") value_for_${want_keys[${k}]}"
            if [[ $(( ${k} + 1 )) -lt ${#want_keys[@]} ]] ; then
                echo ","
            else
                echo
            fi
        done
        echo "            );"
        echo
        echo "        ${a}(const ${a} & other);"
        echo
        echo "        const ${a} & operator= (const ${a} & other);"
        echo
        echo "        ~${a}();"

        make_class_${a}

        echo
        echo "        ///\\}"
        echo

        if [[ -n "${want_named_args}" ]] ; then

            echo "        ///\\name Named argument constructor"
            echo "        ///\\{"
            echo

            for (( k = 0 ; k < ${#want_keys[@]} ; k++ )) ; do
                n=${want_keys[${k}]}
                t=${want_key_types[${k}]}
                echo "        /**"
                echo "         * Holds the named parameter construction value for ${a}.${n}"
                echo "         */"
                echo "        class Param_${n}"
                echo "        {"
                echo "            private:"
                echo "                Param_${n}();"
                echo
                echo "            public:"
                echo "                /// The value"
                echo "                $(make_const_ref "${t}" ) ${n};"
                echo
                echo "                ///\\name Basic operations"
                echo "                ///\\{"
                echo
                echo "                Param_${n}(const Empty &, $(make_const_ref "${t}" ) value_for_${n}) :"
                echo "                    ${n}(value_for_${n})"
                echo "                {"
                echo "                }"
                echo
                echo "                /// Constructor"
                echo "                Param_${n}(const Empty &, const Param_${n} & other) :"
                echo "                    ${n}(other.${n})"
                echo "                {"
                echo "                }"
                echo
                echo "                ///\\}"
                echo "        };"
                echo
                echo "        /**"
                echo "         * Provides the ${n} method for ${a} named parameters."
                echo "         */"
                echo "        template <typename Before_, typename After_>"
                echo "        class ParamNeeded_${n}"
                echo "        {"
                echo "            private:"
                echo "                const Before_ & _before;"
                echo
                echo "            public:"
                echo "                ///\\name Basic operations"
                echo "                ///\\{"
                echo
                echo "                After_"
                echo "                ${n}($(make_const_ref "${t}" ) value_for_${n})"
                echo "                {"
                echo "                    return After_("
                for (( j = 0 ; j < ${#want_keys[@]} ; j++ )) ; do
                    if [[ ${j} == ${k} ]] ; then
                        echo -n "                            Param_${n}(_before, value_for_${n})"
                    else
                        echo -n "                            _before"
                    fi
                    if [[ $(( ${j} + 1 )) -lt ${#want_keys[@]} ]] ; then
                        echo ","
                    else
                        echo
                    fi
                done
                echo "                        );"
                echo "                }"
                echo
                echo "                ParamNeeded_${n}(const Before_ & before, const Empty &) :"
                echo "                    _before(before)"
                echo "                {"
                echo "                }"
                echo
                echo "                ///\\}"
                echo "        };"
                echo
            done

            echo "        /**"
            echo "         * Handle named parameters for ${a}."
            echo "         */"
            echo "        template <"
            for (( k = 0 ; k < ${#want_keys[@]} ; k++ )) ; do
                echo -n "            bool has_${want_keys[${k}]}_"
                if [[ $(( ${k} + 1 )) -lt ${#want_keys[@]} ]] ; then
                    echo ","
                else
                    echo
                fi
            done
            echo "            >"
            echo "        class Params :"
            for (( k = 0 ; k < ${#want_keys[@]} ; k++ )) ; do
                echo "            public Select<has_${want_keys[${k}]}_,"
                echo "                    Param_${want_keys[${k}]},"
                echo "                    ParamNeeded_${want_keys[${k}]}<"
                echo -n "                            Params<"
                for (( j = 0 ; j < ${#want_keys[@]} ; j++ )) ; do
                    if [[ ${j} == ${k} ]] ; then
                        echo -n "false"
                    else
                        echo -n "has_${want_keys[${j}]}_"
                    fi
                    if [[ $(( ${j} + 1 )) -lt ${#want_keys[@]} ]] ; then
                        echo -n ", "
                    else
                        echo ">,"
                    fi
                done
                echo -n "                            Params<"
                for (( j = 0 ; j < ${#want_keys[@]} ; j++ )) ; do
                    if [[ ${j} == ${k} ]] ; then
                        echo -n "true"
                    else
                        echo -n "has_${want_keys[${j}]}_"
                    fi
                    if [[ $(( ${j} + 1 )) -lt ${#want_keys[@]} ]] ; then
                        echo -n ", "
                    else
                        echo "> >"
                    fi
                done
                echo "                >::Type,"
            done
            echo "            public Empty"
            echo "        {"
            echo "            private:"
            echo "                Params();"
            echo
            echo "            public:"
            echo "                ///\\name Basic operations"
            echo "                ///\\{"
            echo
            echo "                Params("
            for (( k = 0 ; k < ${#want_keys[@]} ; k++ )) ; do
                n=${want_keys[${k}]}
                echo -n "                        "
                echo -n "const typename Select<has_${n}_, Param_${n}, Empty>::Type & value_for_${n}"
                if [[ $(( ${k} + 1 )) -lt ${#want_keys[@]} ]] ; then
                    echo ","
                else
                    echo
                fi
            done
            echo "                    ) :"
            for (( k = 0 ; k < ${#want_keys[@]} ; k++ )) ; do
                echo "                    Select<has_${want_keys[${k}]}_,"
                echo "                            Param_${want_keys[${k}]},"
                echo "                            ParamNeeded_${want_keys[${k}]}<"
                echo -n "                                    Params<"
                for (( j = 0 ; j < ${#want_keys[@]} ; j++ )) ; do
                    if [[ ${j} == ${k} ]] ; then
                        echo -n "false"
                    else
                        echo -n "has_${want_keys[${j}]}_"
                    fi
                    if [[ $(( ${j} + 1 )) -lt ${#want_keys[@]} ]] ; then
                        echo -n ", "
                    else
                        echo ">,"
                    fi
                done
                echo -n "                                    Params<"
                for (( j = 0 ; j < ${#want_keys[@]} ; j++ )) ; do
                    if [[ ${j} == ${k} ]] ; then
                        echo -n "true"
                    else
                        echo -n "has_${want_keys[${j}]}_"
                    fi
                    if [[ $(( ${j} + 1 )) -lt ${#want_keys[@]} ]] ; then
                        echo -n ", "
                    else
                        echo "> >"
                    fi
                done
                echo "                        >::Type(*this, value_for_${want_keys[${k}]}),"
            done

            echo "                    Empty()"
            echo "                {"
            echo "                }"
            echo
            echo "                operator ${a} () const"
            echo "                {"
            echo "                    return ${a}("
            for (( k = 0 ; k < ${#want_keys[@]} ; k++ )) ; do
                echo -n "                            this->${want_keys[${k}]}"
                if [[ $(( ${k} + 1 )) -lt ${#want_keys[@]} ]] ; then
                    echo ", "
                else
                    echo
                fi
            done

            echo "                        );"
            echo "                }"
            echo
            echo "                ///\\}"
            echo
            echo "        };"
            echo

            echo "            /// Create, via named parameters"
            echo -n "                static Params<"
            for (( k = 0 ; k < ${#want_keys[@]} ; k++ )) ; do
                echo -n "false"
                if [[ $(( ${k} + 1 )) -lt ${#want_keys[@]} ]] ; then
                    echo -n ", "
                fi
            done
            echo "> create();"
            echo
        fi

        echo "};"

    elif [[ "${what_to_make}" == "--source" ]] ; then

        echo "${a}::${a}("
        for (( k = 0 ; k < ${#want_keys[@]} ; k++ )) ; do
            echo -n "        $(make_const_ref "${want_key_types[${k}]}") value_for_${want_keys[${k}]}"
            if [[ $(( ${k} + 1 )) -lt ${#want_keys[@]} ]] ; then
                echo ","
            else
                echo ") :"
            fi
        done
        for (( k = 0 ; k < ${#want_keys[@]} ; k++ )) ; do
            echo -n "    ${want_keys[${k}]}(value_for_${want_keys[${k}]})"
            if [[ $(( ${k} + 1 )) -lt ${#want_keys[@]} ]] ; then
                echo ","
            else
                echo
            fi
        done
        echo "{"
        echo "}"
        echo

        echo "${a}::${a}(const ${a} & other) :"
        for (( k = 0 ; k < ${#want_keys[@]} ; k++ )) ; do
            echo -n "    ${want_keys[${k}]}(other.${want_keys[${k}]})"
            if [[ $(( ${k} + 1 )) -lt ${#want_keys[@]} ]] ; then
                echo ","
            else
                echo
            fi
        done
        echo "{"
        echo "}"
        echo

        echo "const ${a} &"
        echo "${a}::operator= (const ${a} & other)"
        echo "{"
        for (( k = 0 ; k < ${#want_keys[@]} ; k++ )) ; do
            echo "    ${want_keys[${k}]} = other.${want_keys[${k}]};"
        done
        echo "    return *this;"
        echo "}"

        echo "${a}::~${a}()"
        echo "{"
        echo "}"
        echo

        if [[ -n "${want_comparison_operators}" ]] ; then

            if [[ "${want_comparison_operators}" == "all" ]] ; then
                want_comparison_operators=( "==" "!=" "<" ">" "<=" ">=" )
                echo "int"
                echo "${a}::compare(const ${a} & other) const"
                echo "{"
                for (( k = 0 ; k < ${#want_comparison_fields[@]} ; k++ )) ; do
                    w=${want_comparison_fields[${k}]}
                    echo "    switch (paludis::compare(${w}, ${w//[^\*]}other.${w#\*}))"
                    echo "    {"
                    echo "        case -1:"
                    echo "            return -1;"
                    echo "        case 1:"
                    echo "            return 1;"
                    echo "    }"
                    echo
                done
                echo "    return 0;"
                echo "}"
                echo
                for (( k = 0 ; k < ${#want_comparison_operators[@]} ; k++ )) ; do
                    echo "bool"
                    echo "${a}::operator${want_comparison_operators[${k}]} (const ${a} & other) const"
                    echo "{"
                    echo "    return compare(other) ${want_comparison_operators[${k}]} 0;"
                    echo "}"
                    echo
                done
            elif [[ "${want_comparison_operators}" == "equality" ]] ; then
                echo "bool"
                echo "${a}::operator== (const ${a} & other) const"
                echo "{"
                for (( k = 0 ; k < ${#want_comparison_fields[@]} ; k++ )) ; do
                    echo "    if (${want_comparison_fields[${k}]} != other.${want_comparison_fields[${k}]})"
                    echo "        return false;"
                done
                echo "    return true;"
                echo "}"
                echo
                echo "bool"
                echo "${a}::operator!= (const ${a} & other) const"
                echo "{"
                echo "    return ! operator== (other);"
                echo "}"
                echo
            else
                echo "bad first parameter for comparison_operators" 1>&2
                exit 1
            fi
        fi

        if [[ -n "${want_named_args}" ]] ; then

            echo -n "${a}::Params<"
            for (( k = 0 ; k < ${#want_keys[@]} ; k++ )) ; do
                echo -n "false"
                if [[ $(( ${k} + 1 )) -lt ${#want_keys[@]} ]] ; then
                    echo -n ", "
                fi
            done
            echo ">"
            echo "${a}::create()"
            echo "{"
            echo "    return Params<"
            for (( k = 0 ; k < ${#want_keys[@]} ; k++ )) ; do
                echo -n "false"
                if [[ $(( ${k} + 1 )) -lt ${#want_keys[@]} ]] ; then
                    echo -n ", "
                fi
            done
            echo -n ">("
            for (( k = 0 ; k < ${#want_keys[@]} ; k++ )) ; do
                echo -n "Empty()"
                if [[ $(( ${k} + 1 )) -lt ${#want_keys[@]} ]] ; then
                    echo -n ", "
                fi
            done
            echo ");"
            echo "}"
        fi

    else
        echo "bad argument (expected --header or --source)" 1>&2
        exit 1
    fi
done