Skip to content

Commit 30ca532

Browse files
refactor: optimize label handling and add coverage for new paths
1 parent 8cdc89f commit 30ca532

2 files changed

Lines changed: 38 additions & 22 deletions

File tree

include/ScopeTimer.hpp

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -154,45 +154,52 @@ namespace xyzzy::scopetimer {
154154

155155
LabelData() = default;
156156

157-
explicit LabelData(std::string_view v) noexcept
158-
: view(v.empty() ? std::string_view("ScopeTimer") : v) {}
157+
explicit LabelData(std::string_view v, std::string&& owned = {}) noexcept
158+
: storage(std::move(owned)),
159+
view("ScopeTimer") {
160+
if (!storage.empty()) {
161+
view = storage;
162+
} else if (!v.empty()) {
163+
view = v;
164+
}
165+
}
159166
};
160167

161-
struct LabelArg {
168+
class LabelArg {
169+
public:
162170
LabelArg() = default;
163171

164172
template <std::size_t N>
165173
explicit LabelArg(const char (&literal)[N]) noexcept
166-
: view_(std::string_view(literal, N ? N - 1 : 0)) {}
174+
: storage_(literal, N ? N - 1 : 0) {}
167175

168176
explicit LabelArg(const char* s) noexcept
169-
: view_(s && *s ? std::string_view{s} : std::string_view{"ScopeTimer"}) {}
177+
: storage_(s && *s ? std::string_view{s} : std::string_view{"ScopeTimer"}) {}
178+
179+
explicit LabelArg(std::string_view sv)
180+
: storage_(sv) {}
170181

171182
explicit LabelArg(const std::string& s)
172-
: storage_(s), view_(storage_) {}
183+
: owned_(s) {
184+
ownsStorage_ = true;
185+
}
173186

174187
explicit LabelArg(std::string&& s) noexcept
175-
: storage_(std::move(s)), view_(storage_) {}
176-
177-
explicit LabelArg(std::string_view sv)
178-
: storage_(sv), view_(storage_) {}
188+
: owned_(std::move(s)) {
189+
ownsStorage_ = true;
190+
}
179191

180192
LabelData toLabelData() && noexcept {
181-
LabelData data;
182-
if (!storage_.empty()) {
183-
data.storage = std::move(storage_);
184-
data.view = data.storage;
185-
} else if (!view_.empty()) {
186-
data.view = view_;
187-
} else {
188-
data.view = "ScopeTimer";
193+
if (ownsStorage_) {
194+
return LabelData(std::string_view{}, std::move(owned_));
189195
}
190-
return data;
196+
return LabelData(storage_);
191197
}
192198

193199
private:
194-
std::string storage_;
195-
std::string_view view_{ "ScopeTimer" };
200+
std::string_view storage_{ "ScopeTimer" };
201+
std::string owned_;
202+
bool ownsStorage_{ false };
196203
};
197204
} // namespace detail
198205

test/ScopeTimerTest.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class ScopeTimer_TestFriend {
4040
test_labelarg_literal_and_pointer_variants();
4141
test_labeldata_manual_empty_view();
4242
test_labelarg_empty_literal_to_labeldata();
43+
test_labelarg_owned_to_labeldata();
4344
test_scope_timer_string_view_ctor();
4445
test_looped_work();
4546
test_threaded();
@@ -262,6 +263,14 @@ class ScopeTimer_TestFriend {
262263
expect(data.storage.empty(), "LabelArg empty literal does not allocate storage");
263264
}
264265

266+
static void test_labelarg_owned_to_labeldata() {
267+
std::string ownedSource = "tests:label:owned";
268+
::xyzzy::scopetimer::detail::LabelArg arg{std::move(ownedSource)};
269+
auto data = std::move(arg).toLabelData();
270+
expect(data.storage == "tests:label:owned", "LabelArg owned string moves storage");
271+
expect(data.view == data.storage, "LabelArg owned string view references storage");
272+
}
273+
265274
static void test_scope_timer_string_view_ctor() {
266275
std::string_view svLabel = "tests:label:ctor_sv";
267276
::xyzzy::scopetimer::ScopeTimer timer("tests:label:ctor_scope", svLabel);
@@ -284,7 +293,7 @@ class ScopeTimer_TestFriend {
284293

285294
std::string_view sv = "tests:label:sv";
286295
verifyLabelResult("std::string_view copy", "tests:label:sv",
287-
true, ::xyzzy::scopetimer::detail::LabelArg{sv});
296+
false, ::xyzzy::scopetimer::detail::LabelArg{sv});
288297
}
289298

290299
static void test_labelarg_literal_and_pointer_variants() {

0 commit comments

Comments
 (0)