profiler.hpp 999 B

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