flags.hh revision 11320
12SN/A/*
21762SN/A * Copyright (c) 2008 The Hewlett-Packard Development Company
39983Sstever@gmail.com * All rights reserved.
49983Sstever@gmail.com *
52SN/A * Redistribution and use in source and binary forms, with or without
62SN/A * modification, are permitted provided that the following conditions are
72SN/A * met: redistributions of source code must retain the above copyright
82SN/A * notice, this list of conditions and the following disclaimer;
92SN/A * redistributions in binary form must reproduce the above copyright
102SN/A * notice, this list of conditions and the following disclaimer in the
112SN/A * documentation and/or other materials provided with the distribution;
122SN/A * neither the name of the copyright holders nor the names of its
132SN/A * contributors may be used to endorse or promote products derived from
142SN/A * this software without specific prior written permission.
152SN/A *
162SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
202SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272SN/A *
282SN/A * Authors: Nathan Binkert
292665Ssaidi@eecs.umich.edu */
302760Sbinkertn@umich.edu
312760Sbinkertn@umich.edu#ifndef __BASE_FLAGS_HH__
322665Ssaidi@eecs.umich.edu#define __BASE_FLAGS_HH__
332SN/A
342SN/Atemplate <typename T>
358229Snate@binkert.orgclass Flags
362SN/A{
37363SN/A  private:
382SN/A    T _flags;
398229Snate@binkert.org
402SN/A  public:
412SN/A    typedef T Type;
422SN/A    Flags() : _flags(0) {}
432SN/A    Flags(Type flags) : _flags(flags) {}
442SN/A
45363SN/A    operator const Type() const { return _flags; }
4656SN/A
471388SN/A    template <typename U>
48217SN/A    const Flags<T> &
49363SN/A    operator=(const Flags<U> &flags)
5056SN/A    {
5156SN/A        _flags = flags._flags;
5256SN/A        return *this;
531638SN/A    }
5456SN/A
552SN/A    const Flags<T> &
562356SN/A    operator=(T flags)
572356SN/A    {
582356SN/A        _flags = flags;
592SN/A        return *this;
602SN/A    }
614762Snate@binkert.org
624762Snate@binkert.org    bool isSet() const { return _flags; }
634762Snate@binkert.org    bool isSet(Type flags) const { return (_flags & flags); }
644762Snate@binkert.org    bool allSet() const { return !(~_flags); }
654762Snate@binkert.org    bool allSet(Type flags) const { return (_flags & flags) == flags; }
664762Snate@binkert.org    bool noneSet() const { return _flags == 0; }
674762Snate@binkert.org    bool noneSet(Type flags) const { return (_flags & flags) == 0; }
684762Snate@binkert.org    void clear() { _flags = 0; }
694762Snate@binkert.org    void clear(Type flags) { _flags &= ~flags; }
704762Snate@binkert.org    void set(Type flags) { _flags |= flags; }
714762Snate@binkert.org    void set(Type f, bool val) { _flags = (_flags & ~f) | (val ? f : 0); }
724762Snate@binkert.org    void
734762Snate@binkert.org    update(Type flags, Type mask)
744762Snate@binkert.org    {
754762Snate@binkert.org        _flags = (_flags & ~mask) | (flags & mask);
764762Snate@binkert.org    }
774762Snate@binkert.org};
784762Snate@binkert.org
794762Snate@binkert.org#endif // __BASE_FLAGS_HH__
804762Snate@binkert.org