profiler.hpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #pragma once
  2. #include "profiler.h"
  3. struct ScopeProfiler {
  4. const char *name;
  5. bool enabled = true;
  6. ScopeProfiler(const char *name) : name(name) { profile_start(name); }
  7. ~ScopeProfiler() { Stop(); }
  8. ScopeProfiler(const ScopeProfiler &) = delete;
  9. ScopeProfiler(ScopeProfiler &&other) : name(other.name), enabled(other.enabled) { other.enabled = false; }
  10. ScopeProfiler &operator=(const ScopeProfiler &) = delete;
  11. ScopeProfiler &operator=(ScopeProfiler &&other) = delete;
  12. void Stop()
  13. {
  14. if (!enabled)
  15. return;
  16. profile_end(name);
  17. enabled = false;
  18. }
  19. };
  20. #ifndef NO_PROFILER_MACROS
  21. #define ScopeProfiler_NameConcatImpl(x, y) x##y
  22. #define ScopeProfiler_NameConcat(x, y) ScopeProfiler_NameConcatImpl(x, y)
  23. #ifdef __COUNTER__
  24. #define ScopeProfiler_Name(x) ScopeProfiler_NameConcat(x, __COUNTER__)
  25. #else
  26. #define ScopeProfiler_Name(x) ScopeProfiler_NameConcat(x, __LINE__)
  27. #endif
  28. #define ProfileScope(x) \
  29. ScopeProfiler ScopeProfiler_Name(SCOPE_PROFILE) \
  30. { \
  31. x \
  32. }
  33. #endif