1+ #pragma once
2+
3+ #include < string_view>
4+
5+ namespace plugify {
6+ struct ZoneHandle {
7+ uint64_t opaque = 0 ;
8+
9+ explicit operator bool () const { return opaque != 0 ; }
10+ };
11+
12+ struct ZoneDesc {
13+ std::string_view name;
14+ std::string_view function;
15+ std::string_view file;
16+ size_t line;
17+ uint32_t color;
18+
19+ ZoneDesc (std::string_view name,
20+ #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 and __GNUC_MINOR__ >= 8)) || (defined(_MSC_VER) && _MSC_VER > 1925)
21+ const std::string_view function = __builtin_FUNCTION(),
22+ const std::string_view file = __builtin_FILE(),
23+ const size_t line = __builtin_LINE(),
24+ #else
25+ const std::string_view function = " " ,
26+ const std::string_view file = " " ,
27+ const size_t line = 0 ,
28+ #endif
29+ const uint32_t color = 0
30+ )
31+ : name(name), function(function), file(file), line(line), color(color) {}
32+ };
33+
34+ class IProfiler {
35+ public:
36+ virtual ~IProfiler () = default ;
37+
38+ // Frame boundary
39+ // Call once per application frame (or per plugin tick).
40+ virtual void MarkFrame (std::string_view name) = 0;
41+
42+ // CPU zones
43+ // BeginZone / EndZone must be balanced. Prefer the RAII
44+ // ScopedZone helper below — don't call these directly.
45+ virtual ZoneHandle BeginZone (const ZoneDesc& desc) = 0;
46+ virtual void EndZone (ZoneHandle handle) = 0;
47+
48+ // Memory tracking
49+ // virtual void TrackAlloc(void* ptr, size_t size, std::string_view pool = {}) = 0;
50+ // virtual void TrackFree(void* ptr, std::string_view pool = {}) = 0;
51+
52+ // Thread metadata
53+ virtual void SetThreadName (std::string_view name) = 0;
54+
55+ // Capability query
56+ virtual std::string_view GetName () const = 0; // "Tracy", "Optick", …
57+ virtual bool IsActive () const = 0;
58+ };
59+
60+ class ScopedZone {
61+ public:
62+ ScopedZone (IProfiler* profiler, const ZoneDesc& desc) : _profiler(profiler) {
63+ if (_profiler) _handle = _profiler->BeginZone (desc);
64+ }
65+ ~ScopedZone () {
66+ if (_profiler && _handle) _profiler->EndZone (_handle);
67+ }
68+
69+ ScopedZone (const ScopedZone&) = delete ;
70+ ScopedZone& operator =(const ScopedZone&) = delete ;
71+ ScopedZone (ScopedZone&&) = delete ;
72+ ScopedZone& operator =(ScopedZone&&) = delete ;
73+
74+ private:
75+ IProfiler* _profiler = nullptr ;
76+ ZoneHandle _handle = {};
77+ };
78+ }
0 commit comments