callback.h 805 B

123456789101112131415161718192021222324252627282930
  1. #pragma once
  2. #include "pch.h"
  3. #include "./include/api.h"
  4. template <typename... Args>
  5. struct Callback {
  6. /// Type of the raw callback function.
  7. /// The first parameter is the opaque user data pointer.
  8. using callback_type = void(*)(void*, Args...);
  9. /// Pointer to the raw function to invoke.
  10. callback_type callback_{};
  11. /// User-provided opaque pointer passed as first argument to the raw function.
  12. void* user_data_{};
  13. int32_t peer{};
  14. int32_t index{};
  15. /// Check if the callback has a valid function pointer.
  16. constexpr explicit operator bool() const {
  17. return (callback_ != nullptr);
  18. }
  19. /// Invoke the callback with the given arguments |args|.
  20. constexpr void operator()(Args... args) const {
  21. if (callback_ != nullptr) {
  22. (*callback_)(user_data_, std::forward<Args>(args)...);
  23. }
  24. }
  25. };