profiler.hpp 990 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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)
  10. : name(other.name), enabled(other.enabled)
  11. {
  12. other.enabled = false;
  13. }
  14. ScopeProfiler &operator=(const ScopeProfiler &) = delete;
  15. ScopeProfiler &operator=(ScopeProfiler &&other) = delete;
  16. void Stop()
  17. {
  18. if (!enabled)
  19. return;
  20. profile_end(name);
  21. enabled = false;
  22. }
  23. };
  24. #ifndef NO_PROFILER_MACROS
  25. #define ScopeProfiler_NameConcatImpl(x, y) x##y
  26. #define ScopeProfiler_NameConcat(x, y) ScopeProfiler_NameConcatImpl(x, y)
  27. #ifdef __COUNTER__
  28. #define ScopeProfiler_Name(x) ScopeProfiler_NameConcat(x, __COUNTER__)
  29. #else
  30. #define ScopeProfiler_Name(x) ScopeProfiler_NameConcat(x, __LINE__)
  31. #endif
  32. #define ProfileScope(x) \
  33. ScopeProfiler ScopeProfiler_Name(SCOPE_PROFILE) { x }
  34. #endif