]>
Commit | Line | Data |
---|---|---|
ae820c69 SC |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: wx/flags.h | |
3 | // Purpose: a bitset suited for replacing the current style flags | |
4 | // Author: Stefan Csomor | |
5 | // Modified by: | |
6 | // Created: 27/07/03 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 2003 Stefan Csomor | |
9 | // Licence: wxWindows licence | |
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 | ||
19 | // wxFlags should be applied to an enum, then this can be used like | |
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 | |
23 | ||
24 | template <class T> class wxFlags | |
25 | { | |
26 | friend class wxEnumData ; | |
27 | public: | |
28 | wxFlags(long val) { m_data = val ; } | |
29 | wxFlags() { m_data = 0; } | |
30 | wxFlags(const wxFlags &src) { m_data = src.m_data; } | |
31 | wxFlags(const T el) { m_data |= 1 << el; } | |
32 | ||
33 | operator long() const { return m_data ; } | |
34 | ||
35 | wxFlags &operator =(const wxFlags &rhs) | |
36 | { | |
37 | m_data = rhs.m_data; | |
38 | return *this; | |
39 | } | |
40 | wxFlags &operator +=(const wxFlags &rhs) // union | |
41 | { | |
42 | m_data |= rhs.m_data; | |
43 | return *this; | |
44 | } | |
45 | wxFlags &operator -=(const wxFlags &rhs) // difference | |
46 | { | |
47 | m_data ^= rhs.m_data; | |
48 | return *this; | |
49 | } | |
50 | ||
51 | wxFlags &operator *=(const wxFlags &rhs) // intersection | |
52 | { | |
53 | m_data &= rhs.m_data; | |
54 | return *this; | |
55 | } | |
56 | ||
57 | wxFlags operator +(const wxFlags &rhs) const // union | |
58 | { | |
59 | wxFlags<T> s; | |
60 | s.m_data = m_data | rhs.m_data; | |
61 | return s; | |
62 | } | |
63 | wxFlags operator -(const wxFlags &rhs) const // difference | |
64 | { | |
65 | wxFlags<T> s; | |
66 | s.m_data = m_data ^ rhs.m_data; | |
67 | return s; | |
68 | } | |
69 | wxFlags operator *(const wxFlags &rhs) const // intersection | |
70 | { | |
71 | wxFlags<T> s; | |
72 | s.m_data = m_data & rhs.m_data; | |
73 | return s; | |
74 | } | |
75 | ||
76 | wxFlags& Set(const T el) //Add element | |
77 | { | |
78 | m_data |= 1 << el; | |
79 | return *this; | |
80 | } | |
81 | wxFlags& Clear(const T el) //remove element | |
82 | { | |
83 | m_data &= ~(1 << el); | |
84 | return *this; | |
85 | } | |
86 | ||
87 | bool Contains(const T el) const | |
88 | { | |
89 | return (m_data & (1 << el)) ? true : false; | |
90 | } | |
91 | ||
92 | wxFlags &Clear() | |
93 | { | |
94 | m_data = 0; | |
95 | return *this; | |
96 | } | |
97 | ||
98 | bool Empty() const | |
99 | { | |
100 | return m_data == 0; | |
101 | } | |
102 | ||
103 | bool operator ==(const wxFlags &rhs) const | |
104 | { | |
105 | return m_data == rhs.m_data; | |
106 | } | |
107 | ||
108 | bool operator !=(const wxFlags &rhs) const | |
109 | { | |
110 | return !operator==(rhs); | |
111 | } | |
112 | private : | |
113 | int m_data; | |
114 | }; | |
115 | ||
116 | ||
117 | #endif |