Skip to content

Commit

Permalink
Add ExtractString and ExtractDouble methods to reduce redundant code.
Browse files Browse the repository at this point in the history
Change code based on review comments

Co-authored-by: James Clarke <[email protected]>
  • Loading branch information
Despiix and jclarkeSTFC committed Feb 4, 2025
1 parent 0dd88b4 commit fb1bec0
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 41 deletions.
62 changes: 30 additions & 32 deletions Framework/LiveData/inc/MantidLiveData/ADARA/ADARAPackets.h
Original file line number Diff line number Diff line change
Expand Up @@ -483,15 +483,19 @@ class MANTID_LIVEDATA_DLL BeamMonitorConfigPkt : public Packet {
return (0);
}

double distance(uint32_t index) const {
private:
double extractDouble(uint32_t index, uint32_t fieldOffset) const {
if (index < beamMonCount()) {
double value;
std::memcpy(&value, &m_fields[(index * 6) + 5], sizeof(double));
std::memcpy(&value, &m_fields[(index * 6) + fieldOffset], sizeof(double));
return value;
} else
return (0.0);
} else {
return 0.0;
}
}

double distance(uint32_t index) const { return extractDouble(index, 5); }

private:
const uint32_t *m_fields;

Expand All @@ -513,6 +517,23 @@ class MANTID_LIVEDATA_DLL DetectorBankSetsPkt : public Packet {
// Throttle Suffix, alphanumeric, no spaces/punctuation...
static const size_t THROTTLE_SUFFIX_SIZE = 16;

private:
std::string extractString(uint32_t index, uint32_t fieldOffset, size_t size) const {
const char *start = reinterpret_cast<const char *>(&m_fields[fieldOffset]);
return std::string(start, size);
}

private:
double extractDouble(uint32_t index, uint32_t fieldOffset) const {
if (index < detBankSetCount()) {
double value;
std::memcpy(&value, &m_fields[fieldOffset], sizeof(double));
return value;
} else {
return 0.0;
}
}

enum Flags {
EVENT_FORMAT = 0x0001,
HISTO_FORMAT = 0x0002,
Expand All @@ -528,14 +549,8 @@ class MANTID_LIVEDATA_DLL DetectorBankSetsPkt : public Packet {
}

std::string name(uint32_t index) const {
if (index < detBankSetCount()) {
char name_c[SET_NAME_SIZE + 1]; // give them an inch...
std::memset(static_cast<void *>(name_c), '\0', SET_NAME_SIZE + 1);
std::strncpy(name_c, reinterpret_cast<const char *>(&(m_fields[m_sectionOffsets[index]])), SET_NAME_SIZE);
return (std::string(name_c));
} else {
return ("<Out Of Range!>");
}
return (index < detBankSetCount()) ? extractString(index, m_sectionOffsets[index], SET_NAME_SIZE)
: "<Out Of Range!>";
}

uint32_t flags(uint32_t index) const {
Expand Down Expand Up @@ -582,28 +597,11 @@ class MANTID_LIVEDATA_DLL DetectorBankSetsPkt : public Packet {
return (0);
}

double throttle(uint32_t index) const {
if (index < detBankSetCount()) {
double value;
std::memcpy(&value, &m_fields[m_after_banks_offset[index] + 3], sizeof(double));
return value;
} else
return (0.0);
}
double throttle(uint32_t index) const { return extractDouble(index, m_after_banks_offset[index] + 3); }

std::string suffix(uint32_t index) const {
if (index < detBankSetCount()) {
char suffix_c[THROTTLE_SUFFIX_SIZE + 1]; // give them an inch
std::memset(static_cast<void *>(suffix_c), '\0', THROTTLE_SUFFIX_SIZE + 1);
std::strncpy(suffix_c, reinterpret_cast<const char *>(&(m_fields[m_after_banks_offset[index] + 5])),
THROTTLE_SUFFIX_SIZE);
return (std::string(suffix_c));
} else {
std::stringstream ss;
ss << "out-of-range-";
ss << index;
return (ss.str());
}
return (index < detBankSetCount()) ? extractString(index, m_after_banks_offset[index] + 5, THROTTLE_SUFFIX_SIZE)
: "out-of-range-" + std::to_string(index);
}

private:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,16 @@ struct TCPStreamEventHeader {
GNU_DIAG_OFF("tautological-compare")
bool isValid() const {
return marker1 == marker && marker2 == marker && length >= sizeof(TCPStreamEventHeader) &&
majorVersion() == TCPStreamEventHeader::major_version &&
// This is already suppressed for gcc on Linux
// cppcheck-suppress unsignedPositive
minorVersion() >= TCPStreamEventHeader::minor_version && type != InvalidStream;
majorVersion() == TCPStreamEventHeader::major_version;
}
GNU_DIAG_ON("tautological-compare")

static const uint32_t major_version = 1; ///< starts at 1, then incremented whenever layout of this or further
/// packets changes in a non backward compatible way
static const uint32_t minor_version = 0; ///< reset to 0 in major version change, then incremented whenever
/// layout of this or further packets changes in a backward compatible
/// way
static const uint32_t current_version = (major_version << 16); ///< starts at 1, then incremented
/// whenever layout of this or
/// further packets changes
uint32_t majorVersion() const { return version >> 16; }
uint32_t minorVersion() const { return version & 0xffff; }
};

/// header for initial data packet send on initial connection and on a state
Expand Down
1 change: 0 additions & 1 deletion Framework/LiveData/inc/MantidLiveData/LoadLiveData.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ namespace LiveData {
*/
class MANTID_LIVEDATA_DLL LoadLiveData : public LiveDataAlgorithm {
public:
~LoadLiveData() override = default;
const std::string name() const override;
/// Summary of algorithms purpose
const std::string summary() const override {
Expand Down

0 comments on commit fb1bec0

Please sign in to comment.