]> git.saurik.com Git - wxWidgets.git/blame - include/wx/flags.h
Fixes
[wxWidgets.git] / include / wx / flags.h
CommitLineData
ae820c69
SC
1/////////////////////////////////////////////////////////////////////////////
2// Name: wx/flags.h
a62848fd 3// Purpose: a bitset suited for replacing the current style flags
ae820c69 4// Author: Stefan Csomor
a62848fd 5// Modified by:
ae820c69
SC
6// Created: 27/07/03
7// RCS-ID: $Id$
8// Copyright: (c) 2003 Stefan Csomor
65571936 9// Licence: wxWindows licence
ae820c69
SC
10/////////////////////////////////////////////////////////////////////////////
11
12#ifndef _WX_SETH__
13#define _WX_SETH__
14
15#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
16#pragma interface "flags.h"
17#endif
18
0886e254 19// wxBitset should be applied to an enum, then this can be used like
ae820c69
SC
20// bitwise operators but keeps the type safety and information, the
21// enums must be in a sequence , their value determines the bit position
22// that they represent
a62848fd 23// The api is made as close as possible to <bitset>
ae820c69 24
0886e254 25template <class T> class wxBitset
ae820c69 26{
a62848fd 27 friend class wxEnumData ;
ae820c69 28public:
0886e254
SC
29 // creates a wxBitset<> object with all flags initialized to 0
30 wxBitset() { m_data = 0; }
9c8046dd 31
a62848fd 32 // created a wxBitset<> object initialized according to the bits of the
9c8046dd 33 // integral value val
0886e254 34 wxBitset(unsigned long val) { m_data = val ; }
9c8046dd 35
0886e254
SC
36 // copies the content in the new wxBitset<> object from another one
37 wxBitset(const wxBitset &src) { m_data = src.m_data; }
9c8046dd 38
0886e254
SC
39 // creates a wxBitset<> object that has the specific flag set
40 wxBitset(const T el) { m_data |= 1 << el; }
ae820c69 41
9c8046dd
SC
42 // returns the integral value that the bits of this object represent
43 unsigned long to_ulong() const { return m_data ; }
ae820c69 44
9c8046dd 45 // assignment
0886e254 46 wxBitset &operator =(const wxBitset &rhs)
ae820c69 47 {
a62848fd
WS
48 m_data = rhs.m_data;
49 return *this;
ae820c69 50 }
9c8046dd
SC
51
52 // bitwise or operator, sets all bits that are in rhs and leaves
53 // the rest unchanged
a62848fd 54 wxBitset &operator |=(const wxBitset &rhs)
ae820c69 55 {
a62848fd
WS
56 m_data |= rhs.m_data;
57 return *this;
ae820c69 58 }
9c8046dd
SC
59
60 // bitwsie exclusive-or operator, toggles the value of all bits
61 // that are set in bits and leaves all others unchanged
0886e254 62 wxBitset &operator ^=(const wxBitset &rhs) // difference
ae820c69 63 {
a62848fd
WS
64 m_data ^= rhs.m_data;
65 return *this;
ae820c69
SC
66 }
67
9c8046dd
SC
68 // bitwise and operator, resets all bits that are not in rhs and leaves
69 // all others unchanged
0886e254 70 wxBitset &operator &=(const wxBitset &rhs) // intersection
ae820c69 71 {
a62848fd
WS
72 m_data &= rhs.m_data;
73 return *this;
ae820c69
SC
74 }
75
a62848fd 76 // bitwise or operator, returns a new bitset that has all bits set that set are in
9c8046dd 77 // bitset2 or in this bitset
a62848fd 78 wxBitset operator |(const wxBitset &bitset2) const // union
ae820c69 79 {
a62848fd
WS
80 wxBitset<T> s;
81 s.m_data = m_data | bitset2.m_data;
82 return s;
ae820c69 83 }
9c8046dd 84
a62848fd 85 // bitwise exclusive-or operator, returns a new bitset that has all bits set that are set either in
9c8046dd 86 // bitset2 or in this bitset but not in both
0886e254 87 wxBitset operator ^(const wxBitset &bitset2) const // difference
ae820c69 88 {
a62848fd
WS
89 wxBitset<T> s;
90 s.m_data = m_data ^ bitset2.m_data;
91 return s;
ae820c69 92 }
9c8046dd 93
a62848fd 94 // bitwise and operator, returns a new bitset that has all bits set that are set both in
9c8046dd 95 // bitset2 and in this bitset
0886e254 96 wxBitset operator &(const wxBitset &bitset2) const // intersection
ae820c69 97 {
a62848fd
WS
98 wxBitset<T> s;
99 s.m_data = m_data & bitset2.m_data;
100 return s;
ae820c69
SC
101 }
102
9c8046dd 103 // sets appropriate the bit to true
0886e254 104 wxBitset& set(const T el) //Add element
ae820c69 105 {
a62848fd
WS
106 m_data |= 1 << el;
107 return *this;
ae820c69 108 }
a62848fd 109
9c8046dd 110 // clears the appropriate flag to false
0886e254 111 wxBitset& reset(const T el) //remove element
ae820c69 112 {
a62848fd
WS
113 m_data &= ~(1 << el);
114 return *this;
ae820c69
SC
115 }
116
9c8046dd 117 // clear all flags
0886e254 118 wxBitset& reset()
ae820c69 119 {
a62848fd
WS
120 m_data = 0;
121 return *this;
ae820c69
SC
122 }
123
9c8046dd
SC
124 // true if this flag is set
125 bool test(const T el) const
ae820c69 126 {
a62848fd 127 return (m_data & (1 << el)) ? true : false;
ae820c69
SC
128 }
129
9c8046dd
SC
130 // true if no flag is set
131 bool none() const
ae820c69 132 {
a62848fd 133 return m_data == 0;
ae820c69
SC
134 }
135
9c8046dd
SC
136 // true if any flag is set
137 bool any() const
138 {
a62848fd 139 return m_data != 0;
9c8046dd
SC
140 }
141
142 // true if both have the same flags
0886e254 143 bool operator ==(const wxBitset &rhs) const
ae820c69 144 {
a62848fd 145 return m_data == rhs.m_data;
ae820c69
SC
146 }
147
9c8046dd 148 // true if both differ in their flags set
0886e254 149 bool operator !=(const wxBitset &rhs) const
ae820c69 150 {
a62848fd 151 return !operator==(rhs);
ae820c69 152 }
9c8046dd
SC
153
154 bool operator[] (const T el) const { return test(el) ; }
155
ae820c69 156private :
a62848fd 157 unsigned long m_data;
ae820c69
SC
158};
159
0886e254 160#define WX_DEFINE_FLAGS( flags ) \
583150e3 161 class WXDLLEXPORT flags \
0886e254 162 {\
583150e3
SC
163 public : \
164 flags(long data=0) :m_data(data) {} \
0886e254 165 long m_data ;\
583150e3 166 bool operator ==(const flags &rhs) const { return m_data == rhs.m_data; }\
0886e254 167 } ;
ae820c69
SC
168
169#endif