Added BoolSet utility class

git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@1564 e03df62e-2008-0410-955e-edbf42e46eb7
This commit is contained in:
brain 2005-05-30 05:06:12 +00:00
parent 571542d1f0
commit 02c18fcb82
2 changed files with 117 additions and 0 deletions

View File

@ -88,5 +88,70 @@ public:
char* GetExt(std::string key);
};
const int bitfields[] = {1,2,4,8,16,32,64,128};
const int inverted_bitfields[] = {~1,~2,~4,~8,~16,~32,~64,~128};
/** BoolSet is a utility class designed to hold eight bools in a bitmask.
* Use BoolSet::Set and BoolSet::Get to set and get bools in the bitmask,
* and Unset and Invert for special operations upon them.
*/
class BoolSet
{
char bits;
public:
/** The default constructor initializes the BoolSet to all values unset.
*/
BoolSet();
/** This constructor copies the default bitmask from a char
*/
BoolSet(char bitmask);
/** The Set method sets one bool in the set.
*
* @param number The number of the item to set. This must be between 0 and 7.
*/
void Set(int number);
/** The Get method returns the value of one bool in the set
*
* @param number The number of the item to retrieve. This must be between 0 and 7.
*
* @return True if the item is set, false if it is unset.
*/
bool Get(int number);
/** The Unset method unsets one value in the set
*
* @param number The number of the item to set. This must be between 0 and 7.
*/
void Unset(int number);
/** The Unset method inverts (flips) one value in the set
*
* @param number The number of the item to invert. This must be between 0 and 7.
*/
void Invert(int number);
/** Compare two BoolSets
*/
bool operator==(BoolSet other);
/** OR two BoolSets together
*/
BoolSet operator|(BoolSet other);
/** AND two BoolSets together
*/
BoolSet operator&(BoolSet other);
/** Assign one BoolSet to another
*/
bool operator=(BoolSet other);
};
#endif

View File

@ -60,3 +60,55 @@ char* Extensible::GetExt(std::string key)
return NULL;
}
void BoolSet::Set(int number)
{
this->bits |= bitfields[number];
}
void BoolSet::Unset(int number)
{
this->bits &= inverted_bitfields[number];
}
void BoolSet::Invert(int number)
{
this->bits ^= bitfields[number];
}
bool BoolSet::Get(int number)
{
return ((this->bits | bitfields[number]) > 0);
}
bool BoolSet::operator==(BoolSet other)
{
return (this->bits == other.bits);
}
BoolSet BoolSet::operator|(BoolSet other)
{
BoolSet x(this->bits | other.bits);
return x;
}
BoolSet BoolSet::operator&(BoolSet other)
{
BoolSet x(this->bits & other.bits);
return x;
}
BoolSet::BoolSet()
{
this->bits = 0;
}
BoolSet::BoolSet(char bitmask)
{
this->bits = bitmask;
}
bool operator=(BoolSet other)
{
this->bits = other.bits;
return true;
}