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