aboutsummaryrefslogtreecommitdiff
path: root/include/lldb/Core/Flags.h
blob: 5f4f8830456ba5836c2891d41cbeaea07c28f0a1 (plain) (blame)
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
//===-- Flags.h -------------------------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#ifndef liblldb_Flags_h_
#define liblldb_Flags_h_
#if defined(__cplusplus)


#include <stdint.h>

namespace lldb_private {

//----------------------------------------------------------------------
/// @class Flags Flags.h "lldb/Core/Flags.h"
/// @brief A class to manage flags.
///
/// The Flags class managed flag bits and allows testing and 
/// modification of individual or multiple flag bits.
//----------------------------------------------------------------------
class Flags
{
public:
    //----------------------------------------------------------------------
    /// The value type for flags is a 32 bit unsigned integer type.
    //----------------------------------------------------------------------
    typedef uint32_t ValueType;

    //----------------------------------------------------------------------
    /// Construct with initial flag bit values.
    ///
    /// Constructs this object with \a mask as the initial value for all
    /// of the flags.
    ///
    /// @param[in] mask
    ///     The initial value for all flags.
    //----------------------------------------------------------------------
    Flags (ValueType flags = 0) :
        m_flags (flags)
    {
    }

    //----------------------------------------------------------------------
    /// Copy constructor.
    ///
    /// Construct and copy the flags from \a rhs.
    ///
    /// @param[in] rhs
    ///     A const Flags object reference to copy.
    //----------------------------------------------------------------------
    Flags (const Flags& rhs) :
        m_flags(rhs.m_flags)
    {
    }

    //----------------------------------------------------------------------
    /// Destructor.
    //----------------------------------------------------------------------
    ~Flags ()
    {
    }

    //----------------------------------------------------------------------
    /// Get accessor for all flags.
    ///
    /// @return
    ///     Returns all of the flags as a Flags::ValueType.
    //----------------------------------------------------------------------
    ValueType
    Get () const
    {
        return m_flags;
    }

    //----------------------------------------------------------------------
    /// Return the number of flags that can be represented in this 
    /// object.
    ///
    /// @return
    ///     The maximum number bits in this flag object.
    //----------------------------------------------------------------------
    size_t
    GetBitSize() const
    {
        return sizeof (ValueType) * 8;
    }

    //----------------------------------------------------------------------
    /// Set accessor for all flags.
    ///
    /// @param[in] flags
    ///     The bits with which to replace all of the current flags.
    //----------------------------------------------------------------------
    void
    Reset (ValueType flags)
    {
        m_flags = flags;
    }

    //----------------------------------------------------------------------
    /// Clear one or more flags.
    ///
    /// @param[in] mask
    ///     A bitfield containing one or more flags.
    ///
    /// @return
    ///     The new flags after clearing all bits from \a mask.
    //----------------------------------------------------------------------
    ValueType
    Clear (ValueType mask = ~(ValueType)0)
    {
        m_flags &= ~mask;
        return m_flags;
    }


    //----------------------------------------------------------------------
    /// Set one or more flags by logical OR'ing \a mask with the current
    /// flags.
    ///
    /// @param[in] mask
    ///     A bitfield containing one or more flags.
    ///
    /// @return
    ///     The new flags after setting all bits from \a mask.
    //----------------------------------------------------------------------
    ValueType
    Set (ValueType mask)
    {
        m_flags |= mask;
        return m_flags;
    }


    //----------------------------------------------------------------------
    /// Test if all bits in \a mask are 1 in the current flags
    ///
    /// @return
    ///     \b true if all flags in \a mask are 1, \b false
    ///     otherwise.
    //----------------------------------------------------------------------
    bool
    AllSet (ValueType mask) const
    {
        return (m_flags & mask) == mask;
    }

    //----------------------------------------------------------------------
    /// Test one or more flags.
    ///
    /// @return
    ///     \b true if any flags in \a mask are 1, \b false
    ///     otherwise.
    //----------------------------------------------------------------------
    bool
    AnySet (ValueType mask) const
    {
        return (m_flags & mask) != 0;
    }

    //----------------------------------------------------------------------
    /// Test a single flag bit.
    ///
    /// @return
    ///     \b true if \a bit is set, \b false otherwise.
    //----------------------------------------------------------------------
    bool
    Test (ValueType bit) const
    {
        return (m_flags & bit) != 0;
    }
    
    //----------------------------------------------------------------------
    /// Test if all bits in \a mask are clear.
    ///
    /// @return
    ///     \b true if \b all flags in \a mask are clear, \b false
    ///     otherwise.
    //----------------------------------------------------------------------
    bool
    AllClear (ValueType mask) const
    {
        return (m_flags & mask) == 0;
    }

    bool
    AnyClear (ValueType mask) const
    {
        return (m_flags & mask) != mask;
    }
    
    //----------------------------------------------------------------------
    /// Test a single flag bit to see if it is clear (zero).
    ///
    /// @return
    ///     \b true if \a bit is 0, \b false otherwise.
    //----------------------------------------------------------------------
    bool
    IsClear (ValueType bit) const
    {
        return (m_flags & bit) == 0;
    }

    //----------------------------------------------------------------------
    /// Get the number of zero bits in \a m_flags.
    ///
    /// @return
    ///     The number of bits that are set to 0 in the current flags.
    //----------------------------------------------------------------------
    size_t
    ClearCount () const
    {
        size_t count = 0;
        for (ValueType shift = 0; shift < sizeof(ValueType)*8; ++shift)
        {
            if ((m_flags & (1u << shift)) == 0)
                ++count;
        }
        return count;
    }

    //----------------------------------------------------------------------
    /// Get the number of one bits in \a m_flags.
    ///
    /// @return
    ///     The number of bits that are set to 1 in the current flags.
    //----------------------------------------------------------------------
    size_t
    SetCount () const
    {
        size_t count = 0;
        for (ValueType mask = m_flags; mask; mask >>= 1)
        {
            if (mask & 1u)
                ++count;
        }
        return count;
    }

protected:
    ValueType   m_flags;    ///< The flags.
};

} // namespace lldb_private

#endif  // #if defined(__cplusplus)
#endif  // liblldb_Flags_h_