aboutsummaryrefslogtreecommitdiff
path: root/misc/make_se.bash
blob: 416f99a577a43805e0a8b8631edc1cd9c9e7c6f7 (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
#!/usr/bin/env 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

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

    want_keys=( )
    want_key_descriptions=( )
    want_prefix_key=
    want_namespace=paludis
    want_destringify=

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

    prefix()
    {
        want_prefix_key=$1
    }

    namespace()
    {
        want_namespace=$1
    }

    doxygen_comment()
    {
        :
    }

    want_destringify()
    {
        want_destringify=yes
    }

    make_enum_${a}
    if [[ -z "${want_prefix_key}" ]] ; then
        echo "no prefix key set for ${a}" 1>&2
        exit 1
    fi

    key()
    {
        :
    }

    prefix()
    {
        :
    }

    namespace()
    {
        :
    }

    want_destringify()
    {
        :
    }

    if [[ "${what_to_make}" == "--header" ]] ; then

        doxygen_comment()
        {
            cat
        }

        make_enum_${a}

        echo "enum ${a}"
        echo "{"

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

        echo "    last_${want_prefix_key} ///< Number of keys in ${want_namespace}::${a}"

        echo "};"

        echo
        echo "/**"
        echo " * Write a ${a} to a stream."
        echo " */"
        echo "std::ostream &"
        echo "operator<< (std::ostream &, const $a &) PALUDIS_VISIBLE;"
        echo

        if [[ -n "${want_destringify}" ]] ; then
            echo "/**"
            echo " * Read a ${a} from a stream."
            echo " */"
            echo "std::istream &"
            echo "operator>> (std::istream &, $a &) PALUDIS_VISIBLE;"
            echo
        fi

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

        echo "std::ostream &"
        echo "${want_namespace}::operator<< (std::ostream & o, const ${a} & s)"
        echo "{"
        echo "    do"
        echo "    {"
        echo "        switch (s)"
        echo "        {"

        for (( k = 0 ; k < ${#want_keys[@]} ; k++ )) ; do
            echo "            case ${want_keys[${k}]}:"
            echo "                o << \"${want_keys[${k}]#${want_prefix_key}_}\";";
            echo "                continue;"
            echo
        done

        echo "            case last_${want_prefix_key}:"
        echo "                ;"
        echo "        }"
        echo "        throw InternalError(PALUDIS_HERE, \"Bad ${a} value '\" + paludis::stringify("
        echo "            static_cast<int>(s)));"
        echo "    } while (false);"
        echo
        echo "    return o;"
        echo "}"
        echo

        if [[ -n "${want_destringify}" ]] ; then
            echo "std::istream &"
            echo "${want_namespace}::operator>> (std::istream & s, $a & a)"
            echo "{"
            echo "    std::string value;"
            echo "    s >> value;"
            echo
            echo "    do"
            echo "    {"
            for (( k = 0 ; k < ${#want_keys[@]} ; k++ )) ; do
                echo "        if (value == \"${want_keys[${k}]#${want_prefix_key}_}\")"
                echo "        {"
                echo "            a = ${want_keys[${k}]};"
                echo "            break;"
                echo "        }"
                echo
            done
            echo "        s.setstate(std::ios::badbit);"
            echo "    } while (false);"
            echo
            echo "    return s;"
            echo "}"
            echo
        fi

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