Skip to content

Commit

Permalink
refactor: TrackStateType safety, bitsize, conversion (#2201)
Browse files Browse the repository at this point in the history
This makes TrackStateType check it's internal pointer, fixes the size of the bitset used for manipulation, and allow auto-conversion from mutable to const.
  • Loading branch information
paulgessinger committed Jun 14, 2023
1 parent d7f13e5 commit de665e0
Showing 1 changed file with 29 additions and 3 deletions.
32 changes: 29 additions & 3 deletions Core/include/Acts/EventData/MultiTrajectory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ class ConstTrackStateType;
class TrackStateType {
public:
using raw_type = std::uint64_t;
static constexpr std::size_t kRawBits =
std::numeric_limits<std::make_unsigned<raw_type>::type>::digits;
/// Constructor from a reference to the underlying value container
/// @param raw the value container
TrackStateType(raw_type& raw) : m_raw{&raw} {}
Expand All @@ -61,6 +63,7 @@ class TrackStateType {
/// @param other the other set of flags to assign
/// @return this object
TrackStateType& operator=(const TrackStateType& other) {
assert(other.m_raw != nullptr);
*m_raw = *other.m_raw;
return *this;
}
Expand All @@ -70,19 +73,24 @@ class TrackStateType {
/// @return this object
TrackStateType& operator=(const ConstTrackStateType& other);

/// Automatically convert to const track state type
operator ConstTrackStateType();

/// Return if the bit at position @p pos is 1
/// @param pos the bit position
/// @return if the bit at @p pos is one or not
bool test(std::size_t pos) const {
std::bitset<sizeof(raw_type)> bs{*m_raw};
assert(m_raw != nullptr);
std::bitset<kRawBits> bs{*m_raw};
return bs.test(pos);
}

/// Change the value of the bit at position @p pos to @p value.
/// @param pos the position of the bit to change
/// @param value the value to change the bit to
void set(std::size_t pos, bool value = true) {
std::bitset<sizeof(raw_type)> bs{*m_raw};
assert(m_raw != nullptr);
std::bitset<kRawBits> bs{*m_raw};
bs.set(pos, value);
*m_raw = bs.to_ullong();
}
Expand All @@ -100,6 +108,8 @@ class TrackStateType {
class ConstTrackStateType {
public:
using raw_type = std::uint64_t;
static constexpr std::size_t kRawBits =
std::numeric_limits<std::make_unsigned<raw_type>::type>::digits;

/// Constructor from a reference to the underlying value container
/// @param raw the value container
Expand All @@ -109,20 +119,36 @@ class ConstTrackStateType {
/// @param pos the bit position
/// @return if the bit at @p pos is one or not
bool test(std::size_t pos) const {
std::bitset<sizeof(raw_type)> bs{*m_raw};
assert(m_raw != nullptr);
std::bitset<kRawBits> bs{*m_raw};
return bs.test(pos);
}

friend std::ostream& operator<<(std::ostream& os, ConstTrackStateType t) {
assert(t.m_raw != nullptr);
std::bitset<kRawBits> bs{*t.m_raw};
std::bitset<TrackStateFlag::NumTrackStateFlags> trunc;
for (size_t i = 0; i < TrackStateFlag::NumTrackStateFlags; i++) {
trunc[i] = bs[i];
}
os << "MPOHMS=" << trunc;
return os;
}

private:
friend class TrackStateType;
const raw_type* m_raw{nullptr};
};

inline TrackStateType& TrackStateType::operator=(
const ConstTrackStateType& other) {
assert(other.m_raw != nullptr);
*m_raw = *other.m_raw;
return *this;
}
inline TrackStateType::operator ConstTrackStateType() {
return {*m_raw};
}

// using TrackStateType = std::bitset<TrackStateFlag::NumTrackStateFlags>;

Expand Down

0 comments on commit de665e0

Please sign in to comment.