summaryrefslogtreecommitdiff
path: root/src/base/varargs.hh
blob: 87c98d0060204d4ec50800436819f2792ace1054 (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
/*
 * Copyright (c) 2006 The Regents of The University of Michigan
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met: redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer;
 * redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution;
 * neither the name of the copyright holders nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Authors: Nathan Binkert
 */

#ifndef __BASE_VARARGS_HH__
#define __BASE_VARARGS_HH__

#include "base/refcnt.hh"

#define VARARGS_DECLARATION(receiver)                  \
    VarArgs::Argument<receiver> a01 = VarArgs::Null(), \
    VarArgs::Argument<receiver> a02 = VarArgs::Null(), \
    VarArgs::Argument<receiver> a03 = VarArgs::Null(), \
    VarArgs::Argument<receiver> a04 = VarArgs::Null(), \
    VarArgs::Argument<receiver> a05 = VarArgs::Null(), \
    VarArgs::Argument<receiver> a06 = VarArgs::Null(), \
    VarArgs::Argument<receiver> a07 = VarArgs::Null(), \
    VarArgs::Argument<receiver> a08 = VarArgs::Null(), \
    VarArgs::Argument<receiver> a09 = VarArgs::Null(), \
    VarArgs::Argument<receiver> a10 = VarArgs::Null(), \
    VarArgs::Argument<receiver> a11 = VarArgs::Null(), \
    VarArgs::Argument<receiver> a12 = VarArgs::Null(), \
    VarArgs::Argument<receiver> a13 = VarArgs::Null(), \
    VarArgs::Argument<receiver> a14 = VarArgs::Null(), \
    VarArgs::Argument<receiver> a15 = VarArgs::Null(), \
    VarArgs::Argument<receiver> a16 = VarArgs::Null()

#define VARARGS_DEFINITION(receiver) \
    VarArgs::Argument<receiver> a01, \
    VarArgs::Argument<receiver> a02, \
    VarArgs::Argument<receiver> a03, \
    VarArgs::Argument<receiver> a04, \
    VarArgs::Argument<receiver> a05, \
    VarArgs::Argument<receiver> a06, \
    VarArgs::Argument<receiver> a07, \
    VarArgs::Argument<receiver> a08, \
    VarArgs::Argument<receiver> a09, \
    VarArgs::Argument<receiver> a10, \
    VarArgs::Argument<receiver> a11, \
    VarArgs::Argument<receiver> a12, \
    VarArgs::Argument<receiver> a13, \
    VarArgs::Argument<receiver> a14, \
    VarArgs::Argument<receiver> a15, \
    VarArgs::Argument<receiver> a16

#define VARARGS_ALLARGS                     \
    a01, a02, a03, a04, a05, a06, a07, a08, \
    a09, a10, a11, a12, a13, a14, a15, a16

#define VARARGS_ADDARGS(receiver) do { \
    do {                           \
        if (!a01) break;           \
        a01.add_arg(receiver);     \
        if (!a02) break;           \
        a02.add_arg(receiver);     \
        if (!a03) break;           \
        a03.add_arg(receiver);     \
        if (!a04) break;           \
        a04.add_arg(receiver);     \
        if (!a05) break;           \
        a05.add_arg(receiver);     \
        if (!a06) break;           \
        a06.add_arg(receiver);     \
        if (!a07) break;           \
        a07.add_arg(receiver);     \
        if (!a08) break;           \
        a08.add_arg(receiver);     \
        if (!a09) break;           \
        a09.add_arg(receiver);     \
        if (!a10) break;           \
        a10.add_arg(receiver);     \
        if (!a11) break;           \
        a11.add_arg(receiver);     \
        if (!a12) break;           \
        a12.add_arg(receiver);     \
        if (!a13) break;           \
        a13.add_arg(receiver);     \
        if (!a14) break;           \
        a14.add_arg(receiver);     \
        if (!a15) break;           \
        a15.add_arg(receiver);     \
        if (!a16) break;           \
        a16.add_arg(receiver);     \
    } while (0);                   \
    receiver.end_args();           \
} while (0)

namespace VarArgs {

struct Null {};

template <typename T>
struct Traits
{
    enum { enabled = true };
};

template <>
struct Traits<Null>
{
    enum { enabled = false };
};

template <class RECV>
struct Base : public RefCounted
{
    virtual void add_arg(RECV &receiver) const = 0;
};

template <typename T, class RECV>
struct Any : public Base<RECV>
{
    const T &argument;

    Any(const T &arg) : argument(arg) {}

    virtual void
    add_arg(RECV &receiver) const
    {
        receiver.add_arg(argument);
    }
};

template <typename T, class RECV>
struct Any<T *, RECV> : public Base<RECV>
{
    const T *argument;

    Any(const T *arg) : argument(arg) {}

    virtual void
    add_arg(RECV &receiver) const
    {
        receiver.add_arg(argument);
    }
};

template <class RECV>
struct Argument : public RefCountingPtr<Base<RECV> >
{
    typedef RefCountingPtr<VarArgs::Base<RECV> > Base;

    Argument() { }
    Argument(const Null &null) { }
    template <typename T>
    Argument(const T& arg) : Base(new Any<T, RECV>(arg)) { }
    template <typename T>
    Argument(const T* arg) : Base(new Any<T *, RECV>(arg)) { }

    void
    add_arg(RECV &receiver) const
    {
        if (this->data)
            this->data->add_arg(receiver);
    }
};

template<class RECV>
class List
{
  public:
    typedef VarArgs::Argument<RECV> Argument;
    typedef std::list<Argument> list;
    typedef typename list::iterator iterator;
    typedef typename list::const_iterator const_iterator;
    typedef typename list::size_type size_type;

  protected:
    list l;

  public:
    List() {}
    List(Argument a01, Argument a02, Argument a03, Argument a04,
         Argument a05, Argument a06, Argument a07, Argument a08,
         Argument a09, Argument a10, Argument a11, Argument a12,
         Argument a13, Argument a14, Argument a15, Argument a16)
    {
        if (!a01) return;
        l.push_back(a01);
        if (!a02) return;
        l.push_back(a02);
        if (!a03) return;
        l.push_back(a03);
        if (!a04) return;
        l.push_back(a04);
        if (!a05) return;
        l.push_back(a05);
        if (!a06) return;
        l.push_back(a06);
        if (!a07) return;
        l.push_back(a07);
        if (!a08) return;
        l.push_back(a08);
        if (!a09) return;
        l.push_back(a09);
        if (!a10) return;
        l.push_back(a10);
        if (!a11) return;
        l.push_back(a11);
        if (!a12) return;
        l.push_back(a12);
        if (!a13) return;
        l.push_back(a13);
        if (!a14) return;
        l.push_back(a14);
        if (!a15) return;
        l.push_back(a15);
        if (!a16) return;
        l.push_back(a16);
    }

    size_type size() const { return l.size(); }
    bool empty() const { return l.empty(); }

    iterator begin() { return l.begin(); }
    const_iterator begin() const { return l.begin(); }

    iterator end() { return l.end(); }
    const_iterator end() const { return l.end(); }

    void
    push_back(const Argument &arg)
    {
        if (arg)
            l.push_back(arg);
    }

    void
    push_front(const Argument &arg)
    {
        if (arg)
            l.push_front(arg);
    }

    template <typename T>
    void
    push_back(const T &arg)
    {
        if (Traits<T>::enabled)
            l.push_back(arg);
    }

    template <typename T>
    void
    push_front(const T &arg)
    {
        if (Traits<T>::enabled)
            l.push_front(arg);
    }

    Argument& front() { return l.front(); }
    const Argument& front() const { return l.front(); }
    Argument& back() { return l.back(); }
    const Argument& back() const { return l.back(); }

    void erase(iterator position) { return l.erase(position); }
    void erase(iterator first, iterator last) { return l.erase(first, last); }
    void clear() { return l.clear(); }
    void pop_front() { return l.pop_front(); }
    void pop_back() { return l.pop_back(); }
    void reverse() { l.reverse(); }

    /*
     * Functions specific to variable arguments
     */
    void
    add_args(RECV &recv) const
    {
        const_iterator i = l.begin();
        const_iterator end = l.end();
        while (i != end) {
            i->add_arg(recv);
            ++i;
        }

        recv.end_args();
    }
};

} // namespace VarArgs

#endif /* __BASE_VARARGS_HH__ */