1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
|
#ifndef _FLAGSET_
#define _FLAGSET_
#include <bitset>
#include <cstdint>
#include "osapi/dialogs.h"
constexpr size_t UBYTE_SIZE = 8;
template<typename TEnum, size_t SIZE>
struct flag_combinator;
template<class T, size_t SIZE = static_cast < size_t >(T::NUM_VALUES)>
class flagset {
protected:
std::bitset<SIZE> values;
public:
flagset() {}
flagset(const std::initializer_list<T>& init_list) {
for (auto& val : init_list) {
set(val);
}
}
bool operator[](const T idx) const { return values[(static_cast < size_t >(idx))]; };
template<size_t COMB_SIZE>
bool operator[](const flag_combinator<T, COMB_SIZE>& combinator) const {
return combinator.check_flagset(*this);
};
flagset<T> operator&(const flagset<T>& other) const {
flagset<T> result;
result.values = this->values & other.values;
return result;
}
flagset<T> operator+(const T flag) const {
flagset<T> result = *this;
result.set(flag);
return result;
}
flagset<T>& operator+=(const T flag) {
this->set(flag);
return *this;
}
flagset<T> operator-(const T flag) const {
flagset<T> result = *this;
result.remove(flag);
return result;
}
flagset<T>& operator-=(const T flag) {
this->remove(flag);
return *this;
}
flagset<T> operator|(const flagset<T>& other) const {
flagset<T> result;
result.values = this->values | other.values;
return result;
}
flagset<T>& operator|=(const flagset<T>& other) {
this->values |= other.values;
return *this;
}
void operator|=(const T flag) {
set(flag);
}
bool operator==(const flagset<T>& other) const { return this->values == other.values; }
bool operator!=(const flagset<T>& other) const { return this->values != other.values; }
void reset() { values.reset(); }
flagset<T>& set(T idx, bool value = true) {
Assertion(static_cast<size_t>(idx) < values.size(),
"Invalid value passed to flagset::set(), get a stacktrace, a coder, an old priest and a young priest.");
values.set(static_cast < size_t >(idx), value);
return *this;
}
template<typename TIter>
flagset<T>& set_multiple(TIter begin, TIter end) {
auto current = begin;
while (current != end) {
set(*current, true);
current = std::next(current);
}
return *this;
}
template<typename TIter>
flagset<T>& set_multiple_from_source(const flagset<T>& source, TIter begin, TIter end) {
auto current = begin;
while (current != end) {
if (source[*current]) {
set(*current, true);
}
current = std::next(current);
}
return *this;
}
flagset<T>& remove(T idx) {
return set(idx, false);
}
template<typename TIter>
flagset<T>& remove_multiple(TIter begin, TIter end) {
auto current = begin;
while (current != end) {
set(*current, false);
current = std::next(current);
}
return *this;
}
size_t to_ubyte_vector(SCP_vector<ubyte>& vector_out)
{
int counter = 0; // a counter to determine how far to bitshift
ubyte ubyte_out= 0; // the holder for our bitshifted flags
for (size_t i = 0; i < SIZE; i++) {
// this needs to be at the beginning for push_back after the loop
// to work properly.
if (counter == UBYTE_SIZE) {
vector_out.push_back(ubyte_out);
counter = 0;
ubyte_out = 0;
}
// check whether that flag was set
// there is probably a more efficient way to do this,
// but not unless we can copy the underlying data safely.
if (values[i]) {
ubyte_out |= 1 << counter;
}
// iterate
++counter;
}
vector_out.push_back(ubyte_out);
return vector_out.size();
}
void set_from_vector(const SCP_vector<ubyte>& vector_in)
{
size_t flag = 0;
// just iterate through the entries, and set flags as appropriate.
for (auto& entry : vector_in) {
for (uint bitshift = 0; bitshift < UBYTE_SIZE; bitshift++){
if (entry & (1 << bitshift)) {
values.set(flag, true);
}
++flag;
// this function is used in multi where the size of flagsets can be different
// on server and client. Small differences in flagset sizes should not affect
// gameplay, but we have to make sure not to exceed the size of the flagset.
if (flag >= SIZE){
return;
}
}
}
}
flagset<T>& toggle(T idx) {
values[static_cast < size_t >(idx)] = !values[static_cast < size_t >(idx)];
return *this;
}
bool any_set() const { return values.any(); }
bool none_set() const { return values.none(); }
void from_u64(std::uint64_t num) { values = (unsigned long) num; }
std::uint64_t to_u64() const { return (std::uint64_t) values.to_ulong(); }
size_t hash() const { return std::hash<std::bitset<SIZE>>()(values); }
};
namespace std {
template <typename T, size_t N>
struct hash<::flagset<T, N>> {
size_t operator()(const ::flagset<T, N>& val) const { return val.hash(); }
};
} // namespace std
#define FLAG_LIST(Type) enum class Type : size_t
template<typename TEnum, size_t SIZE>
struct flag_combinator {
static_assert(SIZE > 2, "SIZE must be greater than 2!");
// This check makes sure that TEnum is actually a flag type
static_assert(SIZE <= static_cast<size_t>(TEnum::NUM_VALUES), "Size must be less than NUM_VALUES");
flag_combinator(const flag_combinator<TEnum, SIZE - 1>& left, TEnum right) : _otherValues(left) {
_value = right;
}
bool check_flagset(const flagset<TEnum>& flagset) const {
return flagset[_value] || _otherValues.check_flagset(flagset);
}
protected:
flag_combinator<TEnum, SIZE - 1> _otherValues;
TEnum _value;
};
template<typename TEnum>
struct flag_combinator<TEnum, 2> {
TEnum left;
TEnum right;
flag_combinator(TEnum in_left, TEnum in_right) {
left = in_left;
right = in_right;
}
bool check_flagset(const flagset <TEnum>& flagset) const {
return flagset[left] || flagset[right];
}
};
template<typename TEnum>
struct flag_enum_checker
{
static const bool value = std::is_enum<TEnum>::value;
};
template<typename TEnum>
typename std::enable_if<flag_enum_checker<TEnum>::value, flag_combinator<TEnum, 2>>::type
operator,(TEnum left, TEnum right) {
return flag_combinator<TEnum, 2>(left, right);
};
template<typename TEnum, size_t SIZE>
typename std::enable_if<flag_enum_checker<TEnum>::value, flag_combinator<TEnum, SIZE + 1>>::type
operator,(const flag_combinator<TEnum, SIZE>& left, TEnum right) {
return flag_combinator<TEnum, SIZE + 1>(left, right);
};
#endif
|