2003-08-22 20:37:55 -04:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: wx/flags.h
|
2004-09-14 08:08:28 -04:00
|
|
|
// Purpose: a bitset suited for replacing the current style flags
|
2003-08-22 20:37:55 -04:00
|
|
|
// Author: Stefan Csomor
|
2004-09-14 08:08:28 -04:00
|
|
|
// Modified by:
|
2003-08-22 20:37:55 -04:00
|
|
|
// Created: 27/07/03
|
|
|
|
// RCS-ID: $Id$
|
|
|
|
// Copyright: (c) 2003 Stefan Csomor
|
2004-05-23 16:53:33 -04:00
|
|
|
// Licence: wxWindows licence
|
2003-08-22 20:37:55 -04:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#ifndef _WX_SETH__
|
|
|
|
#define _WX_SETH__
|
|
|
|
|
2003-08-23 18:37:11 -04:00
|
|
|
// wxBitset should be applied to an enum, then this can be used like
|
2003-08-22 20:37:55 -04:00
|
|
|
// 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
|
2004-09-14 08:08:28 -04:00
|
|
|
// The api is made as close as possible to <bitset>
|
2003-08-22 20:37:55 -04:00
|
|
|
|
2003-08-23 18:37:11 -04:00
|
|
|
template <class T> class wxBitset
|
2003-08-22 20:37:55 -04:00
|
|
|
{
|
2004-09-14 08:08:28 -04:00
|
|
|
friend class wxEnumData ;
|
2003-08-22 20:37:55 -04:00
|
|
|
public:
|
2003-08-23 18:37:11 -04:00
|
|
|
// creates a wxBitset<> object with all flags initialized to 0
|
|
|
|
wxBitset() { m_data = 0; }
|
2003-08-23 15:40:04 -04:00
|
|
|
|
2004-09-14 08:08:28 -04:00
|
|
|
// created a wxBitset<> object initialized according to the bits of the
|
2003-08-23 15:40:04 -04:00
|
|
|
// integral value val
|
2003-08-23 18:37:11 -04:00
|
|
|
wxBitset(unsigned long val) { m_data = val ; }
|
2003-08-23 15:40:04 -04:00
|
|
|
|
2003-08-23 18:37:11 -04:00
|
|
|
// copies the content in the new wxBitset<> object from another one
|
|
|
|
wxBitset(const wxBitset &src) { m_data = src.m_data; }
|
2003-08-23 15:40:04 -04:00
|
|
|
|
2003-08-23 18:37:11 -04:00
|
|
|
// creates a wxBitset<> object that has the specific flag set
|
|
|
|
wxBitset(const T el) { m_data |= 1 << el; }
|
2003-08-22 20:37:55 -04:00
|
|
|
|
2003-08-23 15:40:04 -04:00
|
|
|
// returns the integral value that the bits of this object represent
|
|
|
|
unsigned long to_ulong() const { return m_data ; }
|
2003-08-22 20:37:55 -04:00
|
|
|
|
2003-08-23 15:40:04 -04:00
|
|
|
// assignment
|
2003-08-23 18:37:11 -04:00
|
|
|
wxBitset &operator =(const wxBitset &rhs)
|
2003-08-22 20:37:55 -04:00
|
|
|
{
|
2004-09-14 08:08:28 -04:00
|
|
|
m_data = rhs.m_data;
|
|
|
|
return *this;
|
2003-08-22 20:37:55 -04:00
|
|
|
}
|
2003-08-23 15:40:04 -04:00
|
|
|
|
|
|
|
// bitwise or operator, sets all bits that are in rhs and leaves
|
|
|
|
// the rest unchanged
|
2004-09-14 08:08:28 -04:00
|
|
|
wxBitset &operator |=(const wxBitset &rhs)
|
2003-08-22 20:37:55 -04:00
|
|
|
{
|
2004-09-14 08:08:28 -04:00
|
|
|
m_data |= rhs.m_data;
|
|
|
|
return *this;
|
2003-08-22 20:37:55 -04:00
|
|
|
}
|
2003-08-23 15:40:04 -04:00
|
|
|
|
|
|
|
// bitwsie exclusive-or operator, toggles the value of all bits
|
|
|
|
// that are set in bits and leaves all others unchanged
|
2003-08-23 18:37:11 -04:00
|
|
|
wxBitset &operator ^=(const wxBitset &rhs) // difference
|
2003-08-22 20:37:55 -04:00
|
|
|
{
|
2004-09-14 08:08:28 -04:00
|
|
|
m_data ^= rhs.m_data;
|
|
|
|
return *this;
|
2003-08-22 20:37:55 -04:00
|
|
|
}
|
|
|
|
|
2003-08-23 15:40:04 -04:00
|
|
|
// bitwise and operator, resets all bits that are not in rhs and leaves
|
|
|
|
// all others unchanged
|
2003-08-23 18:37:11 -04:00
|
|
|
wxBitset &operator &=(const wxBitset &rhs) // intersection
|
2003-08-22 20:37:55 -04:00
|
|
|
{
|
2004-09-14 08:08:28 -04:00
|
|
|
m_data &= rhs.m_data;
|
|
|
|
return *this;
|
2003-08-22 20:37:55 -04:00
|
|
|
}
|
|
|
|
|
2004-09-14 08:08:28 -04:00
|
|
|
// bitwise or operator, returns a new bitset that has all bits set that set are in
|
2003-08-23 15:40:04 -04:00
|
|
|
// bitset2 or in this bitset
|
2004-09-14 08:08:28 -04:00
|
|
|
wxBitset operator |(const wxBitset &bitset2) const // union
|
2003-08-22 20:37:55 -04:00
|
|
|
{
|
2004-09-14 08:08:28 -04:00
|
|
|
wxBitset<T> s;
|
|
|
|
s.m_data = m_data | bitset2.m_data;
|
|
|
|
return s;
|
2003-08-22 20:37:55 -04:00
|
|
|
}
|
2003-08-23 15:40:04 -04:00
|
|
|
|
2004-09-14 08:08:28 -04:00
|
|
|
// bitwise exclusive-or operator, returns a new bitset that has all bits set that are set either in
|
2003-08-23 15:40:04 -04:00
|
|
|
// bitset2 or in this bitset but not in both
|
2003-08-23 18:37:11 -04:00
|
|
|
wxBitset operator ^(const wxBitset &bitset2) const // difference
|
2003-08-22 20:37:55 -04:00
|
|
|
{
|
2004-09-14 08:08:28 -04:00
|
|
|
wxBitset<T> s;
|
|
|
|
s.m_data = m_data ^ bitset2.m_data;
|
|
|
|
return s;
|
2003-08-22 20:37:55 -04:00
|
|
|
}
|
2003-08-23 15:40:04 -04:00
|
|
|
|
2004-09-14 08:08:28 -04:00
|
|
|
// bitwise and operator, returns a new bitset that has all bits set that are set both in
|
2003-08-23 15:40:04 -04:00
|
|
|
// bitset2 and in this bitset
|
2003-08-23 18:37:11 -04:00
|
|
|
wxBitset operator &(const wxBitset &bitset2) const // intersection
|
2003-08-22 20:37:55 -04:00
|
|
|
{
|
2004-09-14 08:08:28 -04:00
|
|
|
wxBitset<T> s;
|
|
|
|
s.m_data = m_data & bitset2.m_data;
|
|
|
|
return s;
|
2003-08-22 20:37:55 -04:00
|
|
|
}
|
|
|
|
|
2003-08-23 15:40:04 -04:00
|
|
|
// sets appropriate the bit to true
|
2003-08-23 18:37:11 -04:00
|
|
|
wxBitset& set(const T el) //Add element
|
2003-08-22 20:37:55 -04:00
|
|
|
{
|
2004-09-14 08:08:28 -04:00
|
|
|
m_data |= 1 << el;
|
|
|
|
return *this;
|
2003-08-22 20:37:55 -04:00
|
|
|
}
|
2004-09-14 08:08:28 -04:00
|
|
|
|
2003-08-23 15:40:04 -04:00
|
|
|
// clears the appropriate flag to false
|
2003-08-23 18:37:11 -04:00
|
|
|
wxBitset& reset(const T el) //remove element
|
2003-08-22 20:37:55 -04:00
|
|
|
{
|
2004-09-14 08:08:28 -04:00
|
|
|
m_data &= ~(1 << el);
|
|
|
|
return *this;
|
2003-08-22 20:37:55 -04:00
|
|
|
}
|
|
|
|
|
2003-08-23 15:40:04 -04:00
|
|
|
// clear all flags
|
2003-08-23 18:37:11 -04:00
|
|
|
wxBitset& reset()
|
2003-08-22 20:37:55 -04:00
|
|
|
{
|
2004-09-14 08:08:28 -04:00
|
|
|
m_data = 0;
|
|
|
|
return *this;
|
2003-08-22 20:37:55 -04:00
|
|
|
}
|
|
|
|
|
2003-08-23 15:40:04 -04:00
|
|
|
// true if this flag is set
|
|
|
|
bool test(const T el) const
|
2003-08-22 20:37:55 -04:00
|
|
|
{
|
2004-09-14 08:08:28 -04:00
|
|
|
return (m_data & (1 << el)) ? true : false;
|
2003-08-22 20:37:55 -04:00
|
|
|
}
|
|
|
|
|
2003-08-23 15:40:04 -04:00
|
|
|
// true if no flag is set
|
|
|
|
bool none() const
|
2003-08-22 20:37:55 -04:00
|
|
|
{
|
2004-09-14 08:08:28 -04:00
|
|
|
return m_data == 0;
|
2003-08-22 20:37:55 -04:00
|
|
|
}
|
|
|
|
|
2003-08-23 15:40:04 -04:00
|
|
|
// true if any flag is set
|
|
|
|
bool any() const
|
|
|
|
{
|
2004-09-14 08:08:28 -04:00
|
|
|
return m_data != 0;
|
2003-08-23 15:40:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// true if both have the same flags
|
2003-08-23 18:37:11 -04:00
|
|
|
bool operator ==(const wxBitset &rhs) const
|
2003-08-22 20:37:55 -04:00
|
|
|
{
|
2004-09-14 08:08:28 -04:00
|
|
|
return m_data == rhs.m_data;
|
2003-08-22 20:37:55 -04:00
|
|
|
}
|
|
|
|
|
2003-08-23 15:40:04 -04:00
|
|
|
// true if both differ in their flags set
|
2003-08-23 18:37:11 -04:00
|
|
|
bool operator !=(const wxBitset &rhs) const
|
2003-08-22 20:37:55 -04:00
|
|
|
{
|
2004-09-14 08:08:28 -04:00
|
|
|
return !operator==(rhs);
|
2003-08-22 20:37:55 -04:00
|
|
|
}
|
2003-08-23 15:40:04 -04:00
|
|
|
|
|
|
|
bool operator[] (const T el) const { return test(el) ; }
|
|
|
|
|
2003-08-22 20:37:55 -04:00
|
|
|
private :
|
2004-09-14 08:08:28 -04:00
|
|
|
unsigned long m_data;
|
2003-08-22 20:37:55 -04:00
|
|
|
};
|
|
|
|
|
2003-08-23 18:37:11 -04:00
|
|
|
#define WX_DEFINE_FLAGS( flags ) \
|
2007-12-11 20:35:53 -05:00
|
|
|
class WXDLLIMPEXP_BASE flags \
|
2003-08-23 18:37:11 -04:00
|
|
|
{\
|
2003-08-25 19:29:55 -04:00
|
|
|
public : \
|
|
|
|
flags(long data=0) :m_data(data) {} \
|
2003-08-23 18:37:11 -04:00
|
|
|
long m_data ;\
|
2003-08-25 19:29:55 -04:00
|
|
|
bool operator ==(const flags &rhs) const { return m_data == rhs.m_data; }\
|
2003-08-23 18:37:11 -04:00
|
|
|
} ;
|
2003-08-22 20:37:55 -04:00
|
|
|
|
|
|
|
#endif
|