profiler.hpp 1.1 KB

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