flags.hh revision 5745
1/*
2 * Copyright (c) 2008 The Hewlett-Packard Development Company
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Nathan Binkert
29 */
30
31#ifndef __BASE_FLAGS_HH__
32#define __BASE_FLAGS_HH__
33
34template <typename T>
35class Flags
36{
37  private:
38    T _flags;
39
40  public:
41    typedef T Type;
42    Flags() : _flags(0) {}
43    Flags(Type flags) : _flags(flags) {}
44
45    operator const Type() const { return _flags; }
46
47    template <typename U>
48    const Flags<T> &
49    operator=(const Flags<U> &flags)
50    {
51        _flags = flags._flags;
52        return *this;
53    }
54
55    const Flags<T> &
56    operator=(T flags)
57    {
58        _flags = flags;
59        return *this;
60    }
61
62    bool any() const { return _flags; }
63    bool any(Type flags) const { return (_flags & flags); }
64    bool all() const { return !(~_flags); }
65    bool all(Type flags) const { return (_flags & flags) == flags; }
66    bool none() const { return _flags == 0; }
67    bool none(Type flags) const { return (_flags & flags) == 0; }
68    void clear() { _flags = 0; }
69    void clear(Type flags) { _flags &= ~flags; }
70    void set(Type flags) { _flags |= flags; }
71    void set(Type f, bool val) { _flags = (_flags & ~f) | (val ? f : 0); }
72    void
73    update(Type flags, Type mask)
74    {
75        _flags = (_flags & ~mask) | (flags & mask);
76    }
77};
78
79#endif // __BASE_FLAGS_HH__
80