summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/base.h65
-rw-r--r--src/base.cpp52
2 files changed, 117 insertions, 0 deletions
diff --git a/include/base.h b/include/base.h
index d253c5dc3..13c361c0b 100644
--- a/include/base.h
+++ b/include/base.h
@@ -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
diff --git a/src/base.cpp b/src/base.cpp
index a6edcb22c..81aea40e2 100644
--- a/src/base.cpp
+++ b/src/base.cpp
@@ -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;
+}