Skip to content

Commit f2f04a2

Browse files
authored
Modernize Descriptor runtimeInit()
`std::string_view` is now available.
1 parent a43ab3e commit f2f04a2

16 files changed

Lines changed: 46 additions & 43 deletions

File tree

‎DataFormats/Headers/include/Headers/DataHeader.h‎

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
#include <cstring> //needed for memcmp
3939
#include <string>
4040
#include <stdexcept>
41-
#include <climits>
4241
#include <limits>
4342
#include <cerrno>
4443

@@ -258,30 +257,33 @@ struct Descriptor {
258257
///
259258
/// Note: no assignment operator operator=(const char*) as this potentially runs
260259
/// into trouble with this general pointer type.
261-
void runtimeInit(const char* string, short length = -1)
260+
void runtimeInit(std::string_view string)
262261
{
263-
char* target = str;
264-
char* targetEnd = target;
265-
if (length >= 0 && length < (int)N) {
266-
targetEnd += length;
267-
} else {
268-
targetEnd += N;
269-
}
270-
const char* source = string;
271-
for (; source != nullptr && target < targetEnd && *source != 0; ++target, ++source) {
272-
*target = *source;
262+
// empty strings and string longet than the descriptor size are not allowed
263+
if (!string.empty() && (string.size() > (int)N)) {
264+
throw std::invalid_argument("argument must not be longer than the descriptor size");
273265
}
274-
targetEnd = str + N;
275-
for (; target < targetEnd; ++target) {
276-
*target = 0;
277-
}
278-
// require the string to be not longer than the descriptor size
279-
if (source != nullptr && (*source == 0 || (length >= 0 && length <= (int)N))) {
280-
} else {
281-
throw std::invalid_argument("argument must not be longer than descriptor size");
266+
// copy the content directly
267+
std::memcpy(str, string.data(), string.size());
268+
// nullify the remainder
269+
auto* ptr = str + string.size();
270+
for (; ptr < str + N; ++ptr) {
271+
*ptr = 0;
282272
}
283273
}
284274

275+
/// compatibility version for null-terminated strings
276+
void runtimeInit(const char* string)
277+
{
278+
runtimeInit(std::string_view{string, std::strlen(string)});
279+
}
280+
281+
/// compatibility version for explicit length
282+
void runtimeInit(const char* buffer, size_t length)
283+
{
284+
runtimeInit(std::string_view{buffer, length});
285+
}
286+
285287
bool operator==(const Descriptor& other) const { return std::memcmp(this->str, other.str, N) == 0; }
286288
bool operator<(const Descriptor& other) const { return std::memcmp(this->str, other.str, N) < 0; }
287289
bool operator!=(const Descriptor& other) const { return not this->operator==(other); }
@@ -298,7 +300,8 @@ struct Descriptor {
298300

299301
/// get the descriptor as std::string
300302
template <typename T>
301-
std::enable_if_t<std::is_same<T, std::string>::value == true, T> as() const
303+
requires(std::same_as<T, std::string>)
304+
T as() const
302305
{
303306
// backward search to find first non-zero char
304307
// FIXME: can optimize this by using the int value to start at e.g. size/2

‎DataFormats/Headers/test/testDataHeader.cxx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ BOOST_AUTO_TEST_CASE(Descriptor_test)
9797
// check with runtime string
9898
std::string runtimeString = "RUNTIMES";
9999
TestDescriptorT runtimeDescriptor;
100-
runtimeDescriptor.runtimeInit(runtimeString.c_str());
100+
runtimeDescriptor.runtimeInit(runtimeString);
101101
BOOST_CHECK(runtimeDescriptor == TestDescriptorT("RUNTIMES"));
102102

103103
BOOST_CHECK(testDescriptor.as<std::string>().length() == 8);
@@ -130,7 +130,7 @@ BOOST_AUTO_TEST_CASE(DataDescription_test)
130130

131131
std::string runtimeString = "DATA_DESCRIPTION";
132132
DataDescription runtimeDesc;
133-
runtimeDesc.runtimeInit(runtimeString.c_str());
133+
runtimeDesc.runtimeInit(runtimeString);
134134
BOOST_CHECK(runtimeDesc == DataDescription("DATA_DESCRIPTION"));
135135

136136
BOOST_CHECK(desc.as<std::string>().length() == 6);

‎Detectors/DCS/testWorkflow/src/DCSDataReplaySpec.cxx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ o2::framework::DataProcessorSpec getDCSDataReplaySpec(std::vector<o2::dcs::test:
153153

154154
o2::header::DataDescription dd;
155155

156-
dd.runtimeInit(desc.c_str(), desc.size());
156+
dd.runtimeInit(desc);
157157

158158
return DataProcessorSpec{
159159
"dcs-random-data-generator",

‎Detectors/DCS/testWorkflow/src/DCSRandomDataGeneratorSpec.cxx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ o2::framework::DataProcessorSpec getDCSRandomDataGeneratorSpec(std::vector<o2::d
168168

169169
o2::header::DataDescription dd;
170170

171-
dd.runtimeInit(desc.c_str(), desc.size());
171+
dd.runtimeInit(desc);
172172

173173
return DataProcessorSpec{
174174
"dcs-random-data-generator",

‎Detectors/DCS/testWorkflow/src/dcs-proxy.cxx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ WorkflowSpec defineDataProcessing(ConfigContext const& config)
9494
std::unordered_map<DPID, std::string>* dpid2Det = mgr.getForTimeStamp<std::unordered_map<DPID, std::string>>(detStr + "/Config/DCSDPconfig", ts);
9595
for (auto& el : *dpid2Det) {
9696
o2::header::DataDescription tmpd;
97-
tmpd.runtimeInit(el.second.c_str(), el.second.size());
97+
tmpd.runtimeInit(el.second);
9898
dpid2DataDesc[el.first].push_back(tmpd);
9999
}
100100
}

‎Detectors/GlobalTrackingWorkflow/readers/src/irframe-reader-workflow.cxx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ WorkflowSpec defineDataProcessing(ConfigContext const& cc)
3939
WorkflowSpec specs;
4040
o2::conf::ConfigurableParam::updateFromString(cc.options().get<std::string>("configKeyValues"));
4141
o2::header::DataOrigin origin;
42-
origin.runtimeInit(cc.options().get<std::string>("data-origin").c_str());
42+
origin.runtimeInit(cc.options().get<std::string>("data-origin"));
4343

4444
specs.emplace_back(o2::globaltracking::getIRFrameReaderSpec(origin, (uint32_t)cc.options().get<int>("subspec"),
4545
cc.options().get<std::string>("device-name"), cc.options().get<std::string>("file-name")));

‎Detectors/ITSMFT/ITS/workflow/src/DCSGeneratorSpec.cxx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ o2::framework::DataProcessorSpec getITSDCSDataGeneratorSpec(const char* detName)
131131

132132
o2::header::DataDescription dd;
133133

134-
dd.runtimeInit(desc.c_str(), desc.size());
134+
dd.runtimeInit(desc);
135135

136136
return DataProcessorSpec{
137137
"its-dcs-data-generator",

‎Detectors/ITSMFT/common/workflow/src/STFDecoderSpec.cxx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ void STFDecoder<Mapping>::init(InitContext& ic)
7171
auto v2 = o2::utils::Str::tokenize(v1[1], '?');
7272
header::DataOrigin dataOrig;
7373
header::DataDescription dataDesc;
74-
dataOrig.runtimeInit(v1[0].c_str());
75-
dataDesc.runtimeInit(v2[0].c_str());
74+
dataOrig.runtimeInit(v1[0]);
75+
dataDesc.runtimeInit(v2[0]);
7676
Mapping map;
7777
for (int iLayer{0}; iLayer < mLayers; ++iLayer) {
7878
auto& dec = mDecoder.emplace_back(std::make_unique<RawPixelDecoder<Mapping>>());

‎Detectors/TOF/workflow/src/CompressedDecodingTask.cxx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ DataProcessorSpec getCompressedDecodingSpec(const std::string& inputDesc, bool c
458458

459459
// inputs.emplace_back(std::string("x:TOF/" + inputDesc).c_str(), 0, Lifetime::Optional);
460460
o2::header::DataDescription dataDesc;
461-
dataDesc.runtimeInit(inputDesc.c_str());
461+
dataDesc.runtimeInit(inputDesc);
462462
inputs.emplace_back("x", ConcreteDataTypeMatcher{o2::header::gDataOriginTOF, dataDesc}, Lifetime::Timeframe);
463463
std::vector<OutputSpec> outputs;
464464
outputs.emplace_back(o2::header::gDataOriginTOF, "DIGITHEADER", 0, Lifetime::Timeframe);

‎Detectors/TPC/workflow/include/TPCWorkflow/TPCDistributeCMVSpec.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ class TPCDistributeCMVSpec : public o2::framework::Task
229229
{
230230
const std::string name = fmt::format("CMVAGG{}", lane);
231231
header::DataDescription description;
232-
description.runtimeInit(name.substr(0, 16).c_str());
232+
description.runtimeInit(name);
233233
return description;
234234
}
235235

@@ -238,7 +238,7 @@ class TPCDistributeCMVSpec : public o2::framework::Task
238238
{
239239
const std::string name = fmt::format("CMVORB{}", lane);
240240
header::DataDescription description;
241-
description.runtimeInit(name.substr(0, 16).c_str());
241+
description.runtimeInit(name);
242242
return description;
243243
}
244244

0 commit comments

Comments
 (0)