Skip to content

Commit 6dcd436

Browse files
authored
Merge pull request #51 from ChrisHal/code_modernization
use more modern C++
2 parents 3d10c14 + 8eeb1e4 commit 6dcd436

File tree

4 files changed

+36
-40
lines changed

4 files changed

+36
-40
lines changed

QtPMbrowser/pmbrowserwindow.cpp

+7-14
Original file line numberDiff line numberDiff line change
@@ -604,28 +604,28 @@ void PMbrowserWindow::formatStimMetadataAsTableExport(std::ostream& os, int max_
604604
const auto tli = ui->treePulse->topLevelItem(i);
605605
if (tli->isHidden()) continue;
606606
const auto& grp = *(tli->data(0, Qt::UserRole).value<hkTreeNode*>());
607-
auto gpr_count = grp.extractValue<int32_t>(GrGroupCount);
607+
auto gpr_count = grp.extractValue<std::int32_t>(GrGroupCount);
608608
std::string grp_entry = formatParamListExportTable(grp, parametersGroup);
609609
int Nse = tli->childCount();
610610
for (int j = 0; j < Nse; ++j) { // level: series
611611
const auto se_item = tli->child(j);
612612
if (se_item->isHidden()) continue;
613613
const auto& series = *(se_item->data(0, Qt::UserRole).value<hkTreeNode*>());
614-
auto se_count = series.extractValue<int32_t>(SeSeriesCount);
614+
auto se_count = series.extractValue<std::int32_t>(SeSeriesCount);
615615
std::string se_entry = formatParamListExportTable(series, parametersSeries);
616616
int M = se_item->childCount();
617617
for (int k = 0; k < M; ++k) { // level: sweep
618618
const auto sw_item = se_item->child(k);
619619
if (sw_item->isHidden()) continue;
620620
const auto& sweep = *(sw_item->data(0, Qt::UserRole).value<hkTreeNode*>());
621-
auto sw_count = sweep.extractValue<int32_t>(SwSweepCount);
621+
auto sw_count = sweep.extractValue<std::int32_t>(SwSweepCount);
622622
std::string sw_entry = formatParamListExportTable(sweep, parametersSweep);
623623
int Nsw = sw_item->childCount();
624624
for (int l = 0; l < Nsw; ++l) { // level: trace
625625
const auto tr_item = sw_item->child(l);
626626
if (tr_item->isHidden()) continue;
627627
const auto& trace = *(tr_item->data(0, Qt::UserRole).value<hkTreeNode*>());
628-
auto tr_count = trace.extractValue<int32_t>(TrTraceCount);
628+
auto tr_count = trace.extractValue<std::int32_t>(TrTraceCount);
629629
std::string tr_entry = formatParamListExportTable(trace, parametersTrace);
630630
os << gpr_count << '\t' << se_count << '\t' << sw_count << '\t'
631631
<< tr_count <<
@@ -1051,29 +1051,22 @@ void PMbrowserWindow::printAmplifierState(const hkTreeNode* series)
10511051
{
10521052
assert(series->getLevel() == hkTreeNode::LevelSeries);
10531053
hkTreeNode amprecord;
1054-
amprecord.len = AmplifierStateSize;
1055-
//auto buffer = std::make_unique<char[]>(amprecord.len);
1056-
//amprecord.Data = buffer.get();
10571054
amprecord.isSwapped = series->getIsSwapped();
10581055
auto ampstateflag = series->extractInt32(SeAmplStateFlag),
10591056
ampstateref = series->extractInt32(SeAmplStateRef);
10601057
if (ampstateflag > 0 || ampstateref == 0) {
10611058
// use local amp state record
1062-
amprecord.Data = series->Data + SeOldAmpState;
1063-
//std::memcpy(buffer.get(), series->Data + SeOldAmpState, amprecord.len);
1059+
amprecord.Data = series->Data.subspan(SeOldAmpState, AmplifierStateSize);
10641060
std::string s;
10651061
formatParamList(amprecord, parametersAmpplifierState, s);
10661062
ui->textEdit->append(QString("Amplifier State:\n%1\n").arg(QString(s.c_str())));
10671063
}
10681064
else {
1069-
// auto secount = series->extractInt32(SeSeriesCount);
10701065
const auto& amproot = datfile->GetAmpTree().GetRootNode();
1071-
const auto& ampse = amproot.Children.at(size_t(ampstateref) - 1); // Is this correct? Or seCount?
1066+
const auto& ampse = amproot.Children.at(static_cast<std::size_t>(ampstateref) - 1); // Is this correct? Or seCount?
10721067
for(const auto& ampre : ampse.Children) { // there might be multiple amplifiers
10731068
auto ampstatecount = ampre.extractInt32(AmStateCount);
1074-
amprecord.Data = series->Data + SeOldAmpState;
1075-
//std::memcpy(buffer.get(), ampre.Data + AmAmplifierState, amprecord.len);
1076-
//amprecord.Data = ampre.Data.get() + AmAmplifierState;
1069+
amprecord.Data = ampre.Data.subspan(AmAmplifierState, AmplifierStateSize);
10771070
std::string s;
10781071
formatParamList(amprecord, parametersAmpplifierState, s);
10791072
ui->textEdit->append(QString("Amplifier State (Amp #%1):\n%2\n").arg(ampstatecount).arg(QString(s.c_str())));

hekatoolslib/DatFile.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -126,16 +126,16 @@ void DatFile::formatStimMetadataAsTableExport(std::ostream& os, int max_level)
126126
auto& rootnode = GetPulTree().GetRootNode();
127127
metadataCreateTableHeader(os);
128128
for (const auto& grp : rootnode.Children) {
129-
auto gpr_count = grp.extractValue<int32_t>(GrGroupCount);
129+
auto gpr_count = grp.extractValue<std::int32_t>(GrGroupCount);
130130
std::string grp_entry = formatParamListExportTable(grp, parametersGroup);
131131
for (const auto& series : grp.Children) {
132-
auto se_count = series.extractValue<int32_t>(SeSeriesCount);
132+
auto se_count = series.extractValue<std::int32_t>(SeSeriesCount);
133133
std::string se_entry = formatParamListExportTable(series, parametersSeries);
134134
for (const auto& sweep : series.Children) {
135-
auto sw_count = sweep.extractValue<int32_t>(SwSweepCount);
135+
auto sw_count = sweep.extractValue<std::int32_t>(SwSweepCount);
136136
std::string sw_entry = formatParamListExportTable(sweep, parametersSweep);
137137
for (const auto& trace : sweep.Children) {
138-
auto tr_count = trace.extractValue<int32_t>(TrTraceCount);
138+
auto tr_count = trace.extractValue<std::int32_t>(TrTraceCount);
139139
std::string tr_entry = formatParamListExportTable(trace, parametersTrace);
140140
os << gpr_count << '\t' << se_count << '\t' << sw_count << '\t'
141141
<< tr_count <<
@@ -162,10 +162,10 @@ double DatFile::getTraceHolding(const hkTreeNode& trace, std::string& unit)
162162
}
163163
if (std::isnan(holding)) {
164164
// we can also try to get this info from the stim tree (usuful for old files):
165-
auto linkedDAchannel = trace.extractValue<int32_t>(TrLinkDAChannel) - 1;
165+
auto linkedDAchannel = trace.extractValue<std::int32_t>(TrLinkDAChannel) - 1;
166166
assert(linkedDAchannel >= 0);
167167
const auto& sweep_record = *trace.getParent();
168-
int stim_index = sweep_record.extractValue<int32_t>(SwStimCount) - 1;
168+
int stim_index = sweep_record.extractValue<std::int32_t>(SwStimCount) - 1;
169169
const auto& stim_node = GetPgfTree().GetRootNode().Children.at(stim_index);
170170
const auto& channel0_record = stim_node.Children.at(linkedDAchannel);
171171
int linked_channel = channel0_record.extractInt32(chLinkedChannel) - 1;

hekatoolslib/hkTree.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,12 @@ namespace hkLib {
7070
void hkTree::LoadToNode(hkTreeNode* parent, hkTreeNode& node, char** pdata, int level)
7171
{
7272
node.tree = this;
73-
auto size = static_cast<unsigned>(LevelSizes.at(level));
73+
auto size = static_cast<std::size_t>(LevelSizes.at(level));
7474
node.level = level;
75-
node.len = size;
75+
//node.len = size;
7676
node.isSwapped = isSwapped;
7777
node.Parent = parent;
78-
node.Data = *pdata;
78+
node.Data = std::span( *pdata, size );
7979
*pdata += size;
8080
uint32_t nchildren;
8181
std::memcpy(&nchildren, *pdata, sizeof(uint32_t));
@@ -129,15 +129,15 @@ namespace hkLib {
129129

130130
char hkTreeNode::getChar(std::size_t offset) const
131131
{
132-
if (len < offset + sizeof(char)) {
132+
if (Data.size() < offset + sizeof(char)) {
133133
throw std::out_of_range("offset too large while accessing tree node");
134134
}
135135
return Data[offset];
136136
}
137137

138138
const std::optional<UserParamDescr> hkTreeNode::getUserParamDescr(std::size_t offset) const
139139
{
140-
if (len < offset + UserParamDescr::Size) {
140+
if (Data.size() < offset + UserParamDescr::Size) {
141141
//throw std::out_of_range("offset too large while accessing tree node");
142142
return std::nullopt;
143143
}
@@ -151,10 +151,10 @@ namespace hkLib {
151151

152152
const std::string_view hkTreeNode::getString(std::size_t offset) const
153153
{
154-
if (len <= offset) {
154+
if (Data.size() <= offset) {
155155
throw std::out_of_range("offset too large while accessing tree node");
156156
}
157-
return std::string_view(Data + offset);
157+
return std::string_view(Data.data() + offset);
158158
}
159159

160160
std::ostream& operator<<(std::ostream& os, const UserParamDescr& p)

hekatoolslib/hkTree.h

+16-13
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include <cstring>
3737
#include <cstdint>
3838
#include <cstddef>
39+
#include <span>
3940

4041
namespace hkLib {
4142

@@ -206,7 +207,7 @@ namespace hkLib {
206207
{
207208
static_assert(std::is_arithmetic_v<T>, "must be arithmetic type");
208209
T t{};
209-
auto src = Data + offset;
210+
auto src = Data.data() + offset;
210211
if (!isSwapped) {
211212
std::copy(src, src + sizeof t, reinterpret_cast<char*>(&t));
212213
}
@@ -215,8 +216,12 @@ namespace hkLib {
215216
}
216217
return t;
217218
}
219+
template<typename T> bool checkOffset(std::size_t offset) const noexcept
220+
{
221+
return (Data.size() >= offset + sizeof(T));
222+
}
218223
public:
219-
hkTreeNode() : Parent{ nullptr }, Data{ nullptr }, len{ 0 }, Children{}, level{ -1 }, isSwapped{ false } {};
224+
hkTreeNode() : Parent{ nullptr }, Data{ }, Children{}, level{ -1 }, isSwapped{ false } {};
220225
hkTreeNode(hkTreeNode&&) = default;
221226
hkTreeNode(const hkTreeNode&) = delete;
222227
hkTreeNode& operator=(const hkTreeNode&) = delete;
@@ -231,7 +236,7 @@ namespace hkLib {
231236
/// <returns>extracted value</returns>
232237
template<typename T> T extractValue(std::size_t offset) const
233238
{
234-
if (len < offset + sizeof(T)) {
239+
if (!checkOffset<T>(offset)) {
235240
throw std::out_of_range("offset too large while accessing tree node");
236241
}
237242
return extractValueNoCheck<T>(offset);
@@ -244,7 +249,7 @@ namespace hkLib {
244249
/// <returns>std::optional containing value if offset is valid</returns>
245250
template<typename T> std::optional<T> extractValueOpt(std::size_t offset) const
246251
{
247-
if (len < offset + sizeof(T)) {
252+
if (!checkOffset<T>(offset)) {
248253
return std::nullopt;
249254
}
250255
else {
@@ -262,7 +267,7 @@ namespace hkLib {
262267
/// <returns>extracted value or default value</returns>
263268
template<typename T> T extractValue(std::size_t offset, T defaultValue) const noexcept
264269
{
265-
if (len < offset + sizeof(T)) {
270+
if (!checkOffset<T>(offset)) {
266271
return defaultValue;
267272
}
268273
return extractValueNoCheck<T>(offset);
@@ -274,8 +279,8 @@ namespace hkLib {
274279
LevelSweep = 3,
275280
LevelTrace = 4
276281
};
277-
int32_t extractInt32(std::size_t offset) const { return extractValue<int32_t>(offset); };
278-
uint16_t extractUInt16(std::size_t offset) const { return extractValue<uint16_t>(offset); };
282+
int32_t extractInt32(std::size_t offset) const { return extractValue<std::int32_t>(offset); };
283+
uint16_t extractUInt16(std::size_t offset) const { return extractValue<std::uint16_t>(offset); };
279284
double extractLongReal(std::size_t offset) const { return extractValue<double>(offset); };
280285
double extractLongRealNoThrow(std::size_t offset) const noexcept //! instead of throwing an exception, returns NaN if out of range
281286
{
@@ -286,14 +291,14 @@ namespace hkLib {
286291
const std::optional<UserParamDescr> getUserParamDescr(std::size_t offset) const;
287292
template<std::size_t N> const std::string_view getString(std::size_t offset) const
288293
{
289-
if (len < offset + N) {
294+
if (!checkOffset<char[N]>(offset)) {
290295
return "n/a";
291296
}
292-
const auto* p = Data + offset;
297+
const auto* p = Data.data() + offset;
293298
if (p[N - 1]) {
294299
// in theory, string is not zero terminated
295300
// unfortunately, some PM version mess this up
296-
// by not prperly zero-initializing the cahr array
301+
// by not prperly zero-initializing the char array
297302
return std::string_view(p, std::min(std::strlen(p), N));
298303
}
299304
else {
@@ -315,9 +320,7 @@ namespace hkLib {
315320

316321
public:
317322
hkTreeNode* Parent;
318-
//std::unique_ptr<char[]> Data;
319-
const char* Data;
320-
std::size_t len; //!< Length (in bytes) of data
323+
std::span<char> Data;
321324
std::vector<hkTreeNode> Children;
322325
int level;
323326
bool isSwapped;

0 commit comments

Comments
 (0)