Skip to content

Commit 1e8ff39

Browse files
committed
fix: issues with scopes
1 parent 2c0b556 commit 1e8ff39

2 files changed

Lines changed: 39 additions & 10 deletions

File tree

include/plugify/profiler.hpp

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

3+
#include <memory>
34
#include <string_view>
45

56
namespace plugify {
@@ -50,7 +51,7 @@ namespace plugify {
5051
//virtual void TrackFree(void* ptr, std::string_view pool = {}) = 0;
5152

5253
// Thread metadata
53-
virtual void SetThreadName(std::string_view name) = 0;
54+
virtual void SetThread(std::string_view name) = 0;
5455

5556
// Capability query
5657
virtual std::string_view GetName() const = 0; // "Tracy", "Optick", …
@@ -59,17 +60,38 @@ namespace plugify {
5960

6061
class ScopedZone {
6162
public:
62-
ScopedZone(IProfiler* profiler, const ZoneDesc& desc) : _profiler(profiler) {
63+
ScopedZone() = default;
64+
65+
ScopedZone(const std::shared_ptr<IProfiler>& profiler, const ZoneDesc& desc) : _profiler(profiler.get()) {
6366
if (_profiler) _handle = _profiler->BeginZone(desc);
6467
}
68+
6569
~ScopedZone() {
6670
if (_profiler && _handle) _profiler->EndZone(_handle);
6771
}
6872

6973
ScopedZone(const ScopedZone&) = delete;
7074
ScopedZone& operator=(const ScopedZone&) = delete;
71-
ScopedZone(ScopedZone&&) = delete;
72-
ScopedZone& operator=(ScopedZone&&) = delete;
75+
76+
ScopedZone(ScopedZone&& other) noexcept
77+
: _profiler(other._profiler)
78+
, _handle(other._handle) {
79+
other._profiler = nullptr;
80+
other._handle = {};
81+
}
82+
83+
ScopedZone& operator=(ScopedZone&& other) noexcept {
84+
if (this != &other) {
85+
if (_profiler && _handle) _profiler->EndZone(_handle);
86+
87+
_profiler = other._profiler;
88+
_handle = other._handle;
89+
90+
other._profiler = nullptr;
91+
other._handle = {};
92+
}
93+
return *this;
94+
}
7395

7496
private:
7597
IProfiler* _profiler = nullptr;

src/core/extension_loader.hpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#pragma once
22

33
#include "plugify/assembly.hpp"
4-
#include "plugify/config.hpp"
54
#include "plugify/extension.hpp"
65
#include "plugify/file_system.hpp"
76
#include "plugify/language_module.hpp"
@@ -14,14 +13,17 @@ namespace plugify {
1413
class ScopedTimer {
1514
public:
1615
explicit ScopedTimer(Callback&& cb) noexcept(std::is_nothrow_move_constructible_v<Callback>)
17-
: _callback(std::forward<Callback>(cb))
16+
: _callback(std::in_place, std::forward<Callback>(cb))
1817
, _start(std::chrono::steady_clock::now()) {
1918
}
2019

2120
~ScopedTimer() noexcept(std::is_nothrow_invocable_v<Callback>) {
22-
auto end = std::chrono::steady_clock::now();
23-
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - _start);
24-
_callback(elapsed);
21+
if (_callback) {
22+
auto end = std::chrono::steady_clock::now();
23+
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - _start);
24+
25+
(*_callback)(elapsed);
26+
}
2527
}
2628

2729
ScopedTimer(const ScopedTimer&) = delete;
@@ -30,18 +32,23 @@ namespace plugify {
3032
ScopedTimer(ScopedTimer&& other) noexcept
3133
: _callback(std::move(other._callback))
3234
, _start(other._start) {
35+
other._callback.reset();
3336
}
3437

3538
ScopedTimer& operator=(ScopedTimer&& other) noexcept {
3639
if (this != &other) {
40+
_callback.reset();
41+
3742
_callback = std::move(other._callback);
3843
_start = other._start;
44+
45+
other._callback.reset();
3946
}
4047
return *this;
4148
}
4249

4350
private:
44-
PLUGIFY_NO_UNIQUE_ADDRESS Callback _callback;
51+
std::optional<Callback> _callback;
4552
std::chrono::steady_clock::time_point _start;
4653
};
4754

0 commit comments

Comments
 (0)