Removing implicit conversions:

RegisterAddresss and RegisterValue: Removed the implicit conversions.
RegisterAddress: Changed member name from address_ to value_ and method as well to value().
RegisterValue: Also added | operator to be able to concatenate with uint32_t. Same in python bindings (but could not find the tests to modify
This commit is contained in:
2025-12-15 15:19:27 +01:00
parent ea561e2422
commit ea9508da9f
6 changed files with 36 additions and 48 deletions

View File

@@ -22,7 +22,7 @@ void init_bit(py::module &m) {
.def(py::init<const RegisterAddress &>())
.def("__repr__", &RegisterAddress::str)
.def("str", &RegisterAddress::str)
.def("uint32", [](const RegisterAddress &v) { return static_cast<uint32_t>(v); })
.def("value", &RegisterAddress::value)
.def(py::self == py::self)
.def(py::self != py::self);
@@ -44,9 +44,12 @@ void init_bit(py::module &m) {
.def(py::init<const RegisterValue &>())
.def("__repr__", &RegisterValue::str)
.def("str", &RegisterValue::str)
.def("uint32", [](const RegisterValue &v) { return static_cast<uint32_t>(v); })
.def("value", &RegisterValue::value)
.def(py::self == py::self)
.def(py::self != py::self)
.def("__or__", [](const RegisterValue& v, uint32_t rhs) {
return v | rhs;
})
.def("__ior__", [](RegisterValue &v, uint32_t rhs) -> RegisterValue& {
v |= rhs;
return v;

View File

@@ -2835,7 +2835,7 @@ Result<uint32_t> Detector::readRegister(uint32_t addr, Positions pos) const {
auto t = pimpl->readRegister(RegisterAddress(addr), pos);
Result<uint32_t> res;
for (const auto &val : t) {
res.push_back(val);
res.push_back(val.value());
}
return res;
}

View File

@@ -2916,32 +2916,29 @@ void Module::setUpdateMode(const bool updatemode) {
}
RegisterValue Module::readRegister(RegisterAddress addr) const {
return sendToDetectorStop<RegisterValue>(F_READ_REGISTER, addr);
return sendToDetectorStop<RegisterValue>(F_READ_REGISTER, addr.value());
}
void Module::writeRegister(RegisterAddress addr, RegisterValue val,
bool validate) {
uint32_t args[]{addr, val, static_cast<uint32_t>(validate)};
uint32_t args[]{addr.value(), val.value(), static_cast<uint32_t>(validate)};
return sendToDetectorStop(F_WRITE_REGISTER, args, nullptr);
}
void Module::setBit(BitAddress bitAddr, bool validate) {
uint32_t args[] = {bitAddr.address(),
static_cast<uint32_t>(bitAddr.bitPosition()),
uint32_t args[] = {bitAddr.address().value(), bitAddr.bitPosition(),
static_cast<uint32_t>(validate)};
sendToDetectorStop(F_SET_BIT, args, nullptr);
}
void Module::clearBit(BitAddress bitAddr, bool validate) {
uint32_t args[] = {bitAddr.address(),
static_cast<uint32_t>(bitAddr.bitPosition()),
uint32_t args[] = {bitAddr.address().value(), bitAddr.bitPosition(),
static_cast<uint32_t>(validate)};
sendToDetectorStop(F_CLEAR_BIT, args, nullptr);
}
int Module::getBit(BitAddress bitAddr) const {
uint32_t args[2] = {bitAddr.address(),
static_cast<uint32_t>(bitAddr.bitPosition())};
uint32_t args[2] = {bitAddr.address().value(), bitAddr.bitPosition()};
return sendToDetectorStop<int>(F_GET_BIT, args);
}

View File

@@ -22,21 +22,20 @@ template <typename T> std::vector<int> getSetBits(T val) {
class RegisterAddress {
private:
uint32_t addr_{0};
uint32_t value_{0};
public:
constexpr RegisterAddress() noexcept = default;
constexpr explicit RegisterAddress(uint32_t address) : addr_(address) {}
explicit RegisterAddress(const std::string &address);
constexpr explicit RegisterAddress(uint32_t value) : value_(value) {}
explicit RegisterAddress(const std::string &value);
std::string str() const;
operator uint32_t() const noexcept { return addr_; }
uint32_t value() const noexcept { return value_; }
constexpr bool operator==(const RegisterAddress &other) const {
return (addr_ == other.addr_);
return (value_ == other.value_);
}
constexpr bool operator!=(const RegisterAddress &other) const {
return (addr_ != other.addr_);
return (value_ != other.value_);
}
};
@@ -50,7 +49,6 @@ class BitAddress {
BitAddress(RegisterAddress address, uint32_t bitPosition);
BitAddress(const std::string &address, const std::string &bitPosition);
std::string str() const;
RegisterAddress address() const noexcept { return addr_; }
uint32_t bitPosition() const noexcept { return bitPos_; }
@@ -72,19 +70,26 @@ class RegisterValue {
explicit RegisterValue(const std::string &value);
std::string str() const;
operator uint32_t() const noexcept { return value_; }
uint32_t value() const noexcept { return value_; }
RegisterValue &operator|=(uint32_t rhs) noexcept {
value_ |= rhs;
return *this;
}
RegisterValue operator|(uint32_t rhs) const noexcept {
RegisterValue tmp(*this);
tmp |= rhs;
return tmp;
}
constexpr bool operator==(const RegisterValue &other) const noexcept {
return value_ == other.value_;
}
constexpr bool operator!=(const RegisterValue &other) const noexcept {
return value_ != other.value_;
}
} __attribute__((packed));
};
std::ostream &operator<<(std::ostream &os, const RegisterAddress &r);
std::ostream &operator<<(std::ostream &os, const BitAddress &r);

View File

@@ -6,15 +6,14 @@
namespace sls {
RegisterAddress::RegisterAddress(const std::string &address) {
if (!is_hex_or_dec_uint(address)) {
RegisterAddress::RegisterAddress(const std::string &value) {
if (!is_hex_or_dec_uint(value)) {
throw RuntimeError("Address must be an integer value.");
}
uint32_t addr = StringTo<uint32_t>(address);
addr_ = addr;
value_ = StringTo<uint32_t>(value);
}
std::string RegisterAddress::str() const { return ToStringHex(addr_); }
std::string RegisterAddress::str() const { return ToStringHex(value_); }
BitAddress::BitAddress(RegisterAddress address, uint32_t bitPosition)
: addr_(address) {
@@ -47,8 +46,7 @@ RegisterValue::RegisterValue(const std::string &value) {
if (!is_hex_or_dec_uint(value)) {
throw RuntimeError("Value must be an integer value.");
}
uint32_t val = StringTo<uint32_t>(value);
value_ = val;
value_ = StringTo<uint32_t>(value);
}
std::string RegisterValue::str() const { return ToStringHex(value_); }

View File

@@ -57,12 +57,10 @@ TEST_CASE("Convert RegisterAddress using classes ", "[support][.bit_utils]") {
CHECK(reg0 == reg1);
if (i != 0)
CHECK(reg2 != reg1);
CHECK(reg0 == vec_addr[i]);
CHECK(reg1 == vec_addr[i]);
CHECK(reg0.value() == vec_addr[i]);
CHECK(reg1.value() == vec_addr[i]);
CHECK(reg0.str() == vec_ans[i]);
CHECK(reg1.str() == vec_ans[i]);
CHECK(static_cast<uint32_t>(reg0) == vec_addr[i]);
CHECK(static_cast<uint32_t>(reg1) == vec_addr[i]);
}
}
@@ -80,13 +78,11 @@ TEST_CASE("Convert RegisterValue using classes ", "[support][.bit_utils]") {
CHECK(reg0 == reg1);
if (i != 0)
CHECK(reg2 != reg1);
CHECK(reg0 == vec_addr[i]);
CHECK(reg1 == vec_addr[i]);
CHECK(reg0.value() == vec_addr[i]);
CHECK(reg1.value() == vec_addr[i]);
CHECK(reg0.str() == vec_ans[i]);
CHECK(reg1.str() == vec_ans[i]);
CHECK(static_cast<uint32_t>(reg0) == vec_addr[i]);
CHECK(static_cast<uint32_t>(reg1) == vec_addr[i]);
CHECK((reg0 | 0xffffffffu) == 0xffffffffu);
CHECK((reg0 | 0xffffffffu) == RegisterValue(0xffffffffu));
CHECK((reg0 | 0x0) == reg0);
}
}
@@ -190,15 +186,4 @@ TEST_CASE("Strange input throws", "[support][.bit_utils]") {
}
}
TEST_CASE("Implicitly convert to uint for sending over network",
"[support][.bit_utils]") {
RegisterAddress addr{0x305};
uint64_t a = addr;
CHECK(a == 0x305);
RegisterValue addr1{0x305};
uint64_t a1 = addr1;
CHECK(a1 == 0x305);
}
} // namespace sls