Support 'with'

This commit is contained in:
Jaby 2022-09-05 22:35:38 +02:00 committed by Jaby
parent cf38a28107
commit 3ee83c049f
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> template<typename T>
struct BitRange { struct BitRange {
typedef T ValueType; typedef T ValueType;
@ -37,20 +44,15 @@ struct BitRange {
static constexpr BitRange<T> from_to(size_t start, size_t end) { static constexpr BitRange<T> from_to(size_t start, size_t end) {
return {start, (end - start + 1)}; return {start, (end - start + 1)};
} }
};
template<typename T> constexpr BitRangeValue<T> with(T value) const {
struct BitRangeValue { return {value, this->begin, this->length};
T value;
BitRange<T> range;
constexpr BitRangeValue(BitRange<T> range, T value) : value(value), range(range) {
} }
}; };
template<typename T> template<typename T>
static constexpr __always_inline BitRangeValue<T> operator<<(const BitRange<T>& range, T value) { 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> template<typename T>
@ -58,11 +60,26 @@ class __no_align ComplexBitMap {
private: private:
T value = 0; 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: public:
constexpr ComplexBitMap() = default; constexpr ComplexBitMap() = default;
constexpr ComplexBitMap(T value) : value(value) { constexpr ComplexBitMap(T value) : value(value) {
} }
template<typename...ARGS>
static constexpr ComplexBitMap<T> with(ARGS...args) {
return ComplexBitMap().set_va(args...);
}
//Accesssing bits //Accesssing bits
template<typename S> template<typename S>
constexpr ComplexBitMap<T>& set_bit(S bit) { constexpr ComplexBitMap<T>& set_bit(S bit) {
@ -136,7 +153,7 @@ public:
} }
constexpr ComplexBitMap<T>& set(const BitRangeValue<T>& value) { 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; return *this;
} }
@ -241,6 +258,9 @@ static constexpr uintptr_t IO_Base_Adr = 0x10000000;
using ComplexBitMap::operator=; \ using ComplexBitMap::operator=; \
constexpr name() = default; \ constexpr name() = default; \
constexpr name(ComplexBitMap value) : ComplexBitMap(value) { \ constexpr name(ComplexBitMap value) : ComplexBitMap(value) { \
}\
template<typename...ARGS> \
constexpr name(ARGS...args) : ComplexBitMap(args...) {\
} }
#endif //!__JABYENGINE_IOPORT_HPP__ #endif //!__JABYENGINE_IOPORT_HPP__

View File

@ -10,7 +10,7 @@ namespace SPU {
} }
static void clear_main_volume() { 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::left.write(StartVol);
MainVolume::right.write(StartVol); MainVolume::right.write(StartVol);
@ -58,7 +58,7 @@ namespace SPU {
} }
static void setup_control_register() { 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); Control.write(SetupValue);
} }