varargs.hh

来自「M5,一个功能强大的多处理器系统模拟器.很多针对处理器架构,性能的研究都使用它作」· HH 代码 · 共 293 行

HH
293
字号
/* * Copyright (c) 2006 * The Regents of The University of Michigan * All Rights Reserved * * This code is part of the M5 simulator. * * Permission is granted to use, copy, create derivative works and * redistribute this software and such derivative works for any * purpose, so long as the copyright notice above, this grant of * permission, and the disclaimer below appear in all copies made; and * so long as the name of The University of Michigan is not used in * any advertising or publicity pertaining to the use or distribution * of this software without specific, written prior authorization. * * THIS SOFTWARE IS PROVIDED AS IS, WITHOUT REPRESENTATION FROM THE * UNIVERSITY OF MICHIGAN AS TO ITS FITNESS FOR ANY PURPOSE, AND * WITHOUT WARRANTY BY THE UNIVERSITY OF MICHIGAN OF ANY KIND, EITHER * EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE. THE REGENTS OF THE UNIVERSITY OF MICHIGAN SHALL NOT BE * LIABLE FOR ANY DAMAGES, INCLUDING DIRECT, SPECIAL, INDIRECT, * INCIDENTAL, OR CONSEQUENTIAL DAMAGES, WITH RESPECT TO ANY CLAIM * ARISING OUT OF OR IN CONNECTION WITH THE USE OF THE SOFTWARE, EVEN * IF IT HAS BEEN OR IS HEREAFTER ADVISED OF THE POSSIBILITY OF SUCH * DAMAGES. * * Authors: Nathan L. 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 <class RECV>struct Argument : public RefCountingPtr<Base<RECV> >{    typedef RefCountingPtr<Base<RECV> > Base;    Argument() { }    Argument(const Null &null) { }    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 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();    }};/* end namespace VarArgs */ }#endif /* __BASE_VARARGS_HH__ */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?