Support 'with'

This commit is contained in:
Jaby 2022-09-05 22:35:38 +02:00
parent 34876bf23e
commit beb3d6cb86
2 changed files with 31 additions and 11 deletions

View File

@ -27,6 +27,13 @@ struct Bit {
}
};
template<typename T>
struct BitRangeValue {
T value;
size_t begin;
size_t length;
};
template<typename T>
struct BitRange {
typedef T ValueType;
@ -37,20 +44,15 @@ struct BitRange {
static constexpr BitRange<T> from_to(size_t start, size_t end) {
return {start, (end - start + 1)};
}
};
template<typename T>
struct BitRangeValue {
T value;
BitRange<T> range;
constexpr BitRangeValue(BitRange<T> range, T value) : value(value), range(range) {
constexpr BitRangeValue<T> with(T value) const {
return {value, this->begin, this->length};
}
};
template<typename T>
static constexpr __always_inline BitRangeValue<T> operator<<(const BitRange<T>& range, T value) {
return BitRangeValue(range, value);
return BitRangeValue{value, range.begin, range.length};
}
template<typename T>
@ -58,11 +60,26 @@ class __no_align ComplexBitMap {
private:
T value = 0;
template<typename S>
constexpr ComplexBitMap<T>& set_va(const S& value) {
return this->set(value);
}
template<typename S, typename...ARGS>
constexpr ComplexBitMap<T>& set_va(const S& value, const ARGS&...args) {
return this->set_va(value).set_va(args...);
}
public:
constexpr ComplexBitMap() = default;
constexpr ComplexBitMap(T value) : value(value) {
}
template<typename...ARGS>
static constexpr ComplexBitMap<T> with(ARGS...args) {
return ComplexBitMap().set_va(args...);
}
//Accesssing bits
template<typename S>
constexpr ComplexBitMap<T>& set_bit(S bit) {
@ -136,7 +153,7 @@ public:
}
constexpr ComplexBitMap<T>& set(const BitRangeValue<T>& value) {
this->set_value(value.value, value.range);
this->set_value(value.value, {value.begin, value.length});
return *this;
}
@ -241,6 +258,9 @@ static constexpr uintptr_t IO_Base_Adr = 0x10000000;
using ComplexBitMap::operator=; \
constexpr name() = default; \
constexpr name(ComplexBitMap value) : ComplexBitMap(value) { \
}\
template<typename...ARGS> \
constexpr name(ARGS...args) : ComplexBitMap(args...) {\
}
#endif //!__JABYENGINE_IOPORT_HPP__

View File

@ -10,7 +10,7 @@ namespace SPU {
}
static void clear_main_volume() {
static constexpr auto StartVol = SweepVolume().set(!SweepVolume::SweepEnable).set(SweepVolume::Volume, I16_MAX >> 2);
static constexpr auto StartVol = SweepVolume::with(!SweepVolume::SweepEnable, SweepVolume::Volume.with(I16_MAX >> 2));
MainVolume::left.write(StartVol);
MainVolume::right.write(StartVol);
@ -58,7 +58,7 @@ namespace SPU {
}
static void setup_control_register() {
static constexpr auto SetupValue = ControlRegister().set(ControlRegister::Enable).set(ControlRegister::Unmute).set(ControlRegister::CDAudioEnable);
static constexpr auto SetupValue = ControlRegister::with(ControlRegister::Enable, ControlRegister::Unmute, ControlRegister::CDAudioEnable);
Control.write(SetupValue);
}