X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/ae820c693aa5eaa6923a51ad1361c55095ff8900..a703012aedeb8064c8294c6aeeea95e6950ad170:/include/wx/flags.h?ds=sidebyside diff --git a/include/wx/flags.h b/include/wx/flags.h index ec14d2c2b9..0b2ffe8dc8 100644 --- a/include/wx/flags.h +++ b/include/wx/flags.h @@ -1,8 +1,8 @@ ///////////////////////////////////////////////////////////////////////////// // Name: wx/flags.h -// Purpose: a bitset suited for replacing the current style flags +// Purpose: a bitset suited for replacing the current style flags // Author: Stefan Csomor -// Modified by: +// Modified by: // Created: 27/07/03 // RCS-ID: $Id$ // Copyright: (c) 2003 Stefan Csomor @@ -12,106 +12,166 @@ #ifndef _WX_SETH__ #define _WX_SETH__ -#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) -#pragma interface "flags.h" -#endif - -// wxFlags should be applied to an enum, then this can be used like +// wxBitset should be applied to an enum, then this can be used like // bitwise operators but keeps the type safety and information, the // enums must be in a sequence , their value determines the bit position // that they represent +// The api is made as close as possible to -template class wxFlags +template class wxBitset { - friend class wxEnumData ; + friend class wxEnumData ; public: - wxFlags(long val) { m_data = val ; } - wxFlags() { m_data = 0; } - wxFlags(const wxFlags &src) { m_data = src.m_data; } - wxFlags(const T el) { m_data |= 1 << el; } + // creates a wxBitset<> object with all flags initialized to 0 + wxBitset() { m_data = 0; } + + // created a wxBitset<> object initialized according to the bits of the + // integral value val + wxBitset(unsigned long val) { m_data = val ; } - operator long() const { return m_data ; } + // copies the content in the new wxBitset<> object from another one + wxBitset(const wxBitset &src) { m_data = src.m_data; } - wxFlags &operator =(const wxFlags &rhs) + // creates a wxBitset<> object that has the specific flag set + wxBitset(const T el) { m_data |= 1 << el; } + + // returns the integral value that the bits of this object represent + unsigned long to_ulong() const { return m_data ; } + + // assignment + wxBitset &operator =(const wxBitset &rhs) { - m_data = rhs.m_data; - return *this; + m_data = rhs.m_data; + return *this; } - wxFlags &operator +=(const wxFlags &rhs) // union + + // bitwise or operator, sets all bits that are in rhs and leaves + // the rest unchanged + wxBitset &operator |=(const wxBitset &rhs) { - m_data |= rhs.m_data; - return *this; + m_data |= rhs.m_data; + return *this; } - wxFlags &operator -=(const wxFlags &rhs) // difference + + // bitwsie exclusive-or operator, toggles the value of all bits + // that are set in bits and leaves all others unchanged + wxBitset &operator ^=(const wxBitset &rhs) // difference { - m_data ^= rhs.m_data; - return *this; + m_data ^= rhs.m_data; + return *this; } - wxFlags &operator *=(const wxFlags &rhs) // intersection + // bitwise and operator, resets all bits that are not in rhs and leaves + // all others unchanged + wxBitset &operator &=(const wxBitset &rhs) // intersection { - m_data &= rhs.m_data; - return *this; + m_data &= rhs.m_data; + return *this; } - wxFlags operator +(const wxFlags &rhs) const // union + // bitwise or operator, returns a new bitset that has all bits set that set are in + // bitset2 or in this bitset + wxBitset operator |(const wxBitset &bitset2) const // union { - wxFlags s; - s.m_data = m_data | rhs.m_data; - return s; + wxBitset s; + s.m_data = m_data | bitset2.m_data; + return s; } - wxFlags operator -(const wxFlags &rhs) const // difference + + // bitwise exclusive-or operator, returns a new bitset that has all bits set that are set either in + // bitset2 or in this bitset but not in both + wxBitset operator ^(const wxBitset &bitset2) const // difference + { + wxBitset s; + s.m_data = m_data ^ bitset2.m_data; + return s; + } + + // bitwise and operator, returns a new bitset that has all bits set that are set both in + // bitset2 and in this bitset + wxBitset operator &(const wxBitset &bitset2) const // intersection { - wxFlags s; - s.m_data = m_data ^ rhs.m_data; - return s; + wxBitset s; + s.m_data = m_data & bitset2.m_data; + return s; } - wxFlags operator *(const wxFlags &rhs) const // intersection + + // sets appropriate the bit to true + wxBitset& set(const T el) //Add element { - wxFlags s; - s.m_data = m_data & rhs.m_data; - return s; + m_data |= 1 << el; + return *this; } - wxFlags& Set(const T el) //Add element + // clears the appropriate flag to false + wxBitset& reset(const T el) //remove element { - m_data |= 1 << el; - return *this; + m_data &= ~(1 << el); + return *this; } - wxFlags& Clear(const T el) //remove element + + // clear all flags + wxBitset& reset() { - m_data &= ~(1 << el); - return *this; + m_data = 0; + return *this; } - bool Contains(const T el) const + // true if this flag is set + bool test(const T el) const { - return (m_data & (1 << el)) ? true : false; + return (m_data & (1 << el)) ? true : false; } - wxFlags &Clear() + // true if no flag is set + bool none() const { - m_data = 0; - return *this; + return m_data == 0; } - bool Empty() const + // true if any flag is set + bool any() const { - return m_data == 0; + return m_data != 0; } - bool operator ==(const wxFlags &rhs) const + // true if both have the same flags + bool operator ==(const wxBitset &rhs) const { - return m_data == rhs.m_data; + return m_data == rhs.m_data; } - bool operator !=(const wxFlags &rhs) const + // true if both differ in their flags set + bool operator !=(const wxBitset &rhs) const { - return !operator==(rhs); + return !operator==(rhs); } + + bool operator[] (const T el) const { return test(el) ; } + private : - int m_data; + unsigned long m_data; }; +#if wxUSE_EXTENDED_RTTI + +#define wxDEFINE_FLAGS( flags ) \ + class WXDLLIMPEXP_BASE flags \ + {\ + public : \ + flags(long data=0) :m_data(data) {} \ + long m_data ;\ + bool operator ==(const flags &rhs) const { return m_data == rhs.m_data; }\ + } ; + +#else + +#define wxDEFINE_FLAGS( flags ) + +#endif + +#if WXWIN_COMPATIBILITY_2_8 + #define WX_DEFINE_FLAGS wxDEFINE_FLAGS +#endif #endif