210 lines
6.3 KiB
C++
210 lines
6.3 KiB
C++
#ifndef __JABYENGINE_COMPLEX_BITMAP_HPP__
|
|
#define __JABYENGINE_COMPLEX_BITMAP_HPP__
|
|
#include "bits.hpp"
|
|
|
|
namespace JabyEngine {
|
|
struct ClearBitValue {
|
|
size_t bit;
|
|
|
|
constexpr ClearBitValue(size_t bit) : bit(bit) {
|
|
}
|
|
};
|
|
|
|
template<typename T>
|
|
struct Bit {
|
|
typedef T ValueType;
|
|
|
|
size_t value;
|
|
|
|
constexpr Bit(size_t value) : value(value) {
|
|
}
|
|
|
|
constexpr operator size_t() const {
|
|
return this->value;
|
|
}
|
|
|
|
constexpr ClearBitValue operator!() const {
|
|
return ClearBitValue(this->value);
|
|
}
|
|
};
|
|
|
|
template<typename T>
|
|
struct BitRangeValue {
|
|
T value;
|
|
size_t begin;
|
|
size_t length;
|
|
|
|
constexpr BitRangeValue() = default;
|
|
constexpr BitRangeValue(T value, size_t begin, size_t length) : value(value), begin(begin), length(length) {
|
|
}
|
|
};
|
|
|
|
template<typename T>
|
|
struct BitRange {
|
|
typedef T ValueType;
|
|
|
|
size_t begin;
|
|
size_t length;
|
|
|
|
static constexpr BitRange<T> from_to(size_t start, size_t end) {
|
|
return {start, (end - start + 1)};
|
|
}
|
|
|
|
constexpr BitRangeValue<T> with(T value) const {
|
|
return BitRangeValue(value, this->begin, this->length);
|
|
}
|
|
|
|
constexpr BitRangeValue<T> max() const {
|
|
return BitRange<T>::with((1 << this->length) - 1);
|
|
}
|
|
};
|
|
|
|
template<typename T>
|
|
static constexpr __always_inline BitRangeValue<T> operator<<(const BitRange<T>& range, T value) {
|
|
return BitRangeValue{value, range.begin, range.length};
|
|
}
|
|
|
|
template<typename T>
|
|
class ComplexBitMap {
|
|
public:
|
|
typedef T UnderlyingType;
|
|
T raw;
|
|
|
|
private:
|
|
template<typename S>
|
|
constexpr __always_inline ComplexBitMap<T>& set_va(const S& value) {
|
|
return this->set(value);
|
|
}
|
|
|
|
template<typename S, typename...ARGS>
|
|
constexpr __always_inline ComplexBitMap<T>& set_va(const S& value, const ARGS&...args) {
|
|
return this->set_va(value).set_va(args...);
|
|
}
|
|
|
|
public:
|
|
template<typename...ARGS>
|
|
static constexpr __always_inline ComplexBitMap<T> with(ARGS...args) {
|
|
return ComplexBitMap().set_va(args...);
|
|
}
|
|
|
|
//Accesssing bits
|
|
template<typename S>
|
|
constexpr ComplexBitMap<T>& set_bit(S bit) {
|
|
this->raw = bit::set(this->raw, static_cast<size_t>(bit));
|
|
return *this;
|
|
}
|
|
|
|
template<typename S>
|
|
constexpr void set_bit(S bit) volatile {
|
|
this->raw = bit::set(this->raw, static_cast<size_t>(bit));
|
|
}
|
|
|
|
template<typename S>
|
|
constexpr ComplexBitMap<T>& clear_bit(S bit) {
|
|
this->raw = bit::clear(this->raw, static_cast<size_t>(bit));
|
|
return *this;
|
|
}
|
|
|
|
template<typename S>
|
|
constexpr void clear_bit(S bit) volatile {
|
|
this->raw = bit::clear(this->raw, static_cast<size_t>(bit));
|
|
}
|
|
|
|
template<typename S>
|
|
constexpr bool is_bit_set(S bit) {
|
|
return bit::is_set(this->raw, static_cast<size_t>(bit));
|
|
}
|
|
|
|
template<typename S>
|
|
constexpr bool is_bit_set(S bit) const volatile {
|
|
return bit::is_set(this->raw, static_cast<size_t>(bit));
|
|
}
|
|
|
|
//Accessing values
|
|
template<typename S>
|
|
constexpr ComplexBitMap<T>& set_value(S value, const BitRange<S>& range) {
|
|
this->raw = bit::value::set_normalized(this->raw, static_cast<T>(value), range.begin, range.length);
|
|
return *this;
|
|
}
|
|
|
|
template<typename S>
|
|
constexpr void set_value(S value, const BitRange<S>& range) volatile {
|
|
this->raw = bit::value::set_normalized(this->raw, static_cast<T>(value), range.begin, range.length);
|
|
}
|
|
|
|
template<typename S>
|
|
constexpr ComplexBitMap<T>& clear_value(const BitRange<S>& range) {
|
|
this->raw = bit::value::clear_normalized(this->raw, range.begin, range.length);
|
|
return *this;
|
|
}
|
|
|
|
template<typename S>
|
|
constexpr void clear_value(const BitRange<S>& range) volatile {
|
|
this->raw = bit::value::clear_normalized(this->raw, range.begin, range.length);
|
|
}
|
|
|
|
template<typename S>
|
|
constexpr S get_value(const BitRange<S>& range) const {
|
|
return static_cast<S>(bit::value::get_normalized(this->raw, range.begin, range.length));
|
|
}
|
|
|
|
template<typename S>
|
|
constexpr S get_value(const BitRange<S>& range) const volatile {
|
|
return static_cast<S>(bit::value::get_normalized(this->raw, range.begin, range.length));
|
|
}
|
|
|
|
//For easier checking
|
|
constexpr bool is(Bit<T> bit) const {
|
|
return ComplexBitMap::is_bit_set(bit);
|
|
}
|
|
|
|
constexpr bool is(Bit<T> bit) const volatile {
|
|
return ComplexBitMap::is_bit_set(bit);
|
|
}
|
|
|
|
// For easier constructing
|
|
template<typename S>
|
|
constexpr __always_inline ComplexBitMap<T>& set(const BitRange<S>& range, T value) {
|
|
this->set_value(value, range);
|
|
return *this;
|
|
}
|
|
|
|
template<typename S>
|
|
constexpr __always_inline ComplexBitMap<T>& set(const BitRangeValue<S>& value) {
|
|
this->set_value(value.value, {value.begin, value.length});
|
|
return *this;
|
|
}
|
|
|
|
template<typename S>
|
|
constexpr __always_inline ComplexBitMap<T>& set(const Bit<S>& bit) {
|
|
this->set_bit(bit.value);
|
|
return *this;
|
|
}
|
|
|
|
constexpr __always_inline ComplexBitMap<T>& set(const ClearBitValue& value) {
|
|
this->clear_bit(value.bit);
|
|
return *this;
|
|
}
|
|
|
|
constexpr __always_inline ComplexBitMap<T>& operator|(const BitRangeValue<T>& value) {
|
|
this->set_value(value.value, value.range);
|
|
return *this;
|
|
}
|
|
|
|
constexpr __always_inline ComplexBitMap<T>& operator|(const Bit<T>& bit) {
|
|
this->set_bit(bit.value);
|
|
return *this;
|
|
}
|
|
|
|
constexpr __always_inline ComplexBitMap<T>& operator|(const ClearBitValue& value) {
|
|
this->clear_bit(value.bit);
|
|
return *this;
|
|
}
|
|
|
|
constexpr __always_inline operator T() const {
|
|
return this->raw;
|
|
}
|
|
};
|
|
}
|
|
|
|
#endif //!__JABYENGINE_COMPLEX_BITMAP_HPP__
|