function_view-inl.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #pragma once
  2. #ifndef FUNCTION_VIEW_INL_H_
  3. #error "Direct inclusion of this file is not allowed, include function_view.h"
  4. // For the sake of sane code completion.
  5. #include "function_view.h"
  6. #endif
  7. #include <library/cpp/yt/assert/assert.h>
  8. namespace NYT {
  9. ////////////////////////////////////////////////////////////////////////////////
  10. template <class TResult, bool NoExcept, class... TArgs>
  11. template <CTypeErasable<TResult(TArgs...) noexcept(NoExcept)> TConcrete>
  12. TFunctionView<TResult(TArgs...) noexcept(NoExcept)>::TFunctionView(TConcrete& concreteRef) noexcept
  13. : TFunctionView(&concreteRef)
  14. { }
  15. template <class TResult, bool NoExcept, class... TArgs>
  16. template <CTypeErasable<TResult(TArgs...) noexcept(NoExcept)> TConcrete>
  17. TFunctionView<TResult(TArgs...) noexcept(NoExcept)>::TFunctionView(TConcrete* concretePtr) noexcept
  18. {
  19. Ptr_ = reinterpret_cast<void*>(concretePtr);
  20. Invoke_ = &TFunctionView::ConcreteInvoke<TConcrete>;
  21. }
  22. template <class TResult, bool NoExcept, class... TArgs>
  23. TFunctionView<TResult(TArgs...) noexcept(NoExcept)>
  24. TFunctionView<TResult(TArgs...) noexcept(NoExcept)>::Release() noexcept
  25. {
  26. auto copy = *this;
  27. Reset();
  28. return copy;
  29. }
  30. template <class TResult, bool NoExcept, class... TArgs>
  31. TResult TFunctionView<TResult(TArgs...) noexcept(NoExcept)>::operator()(TArgs... args) noexcept(NoExcept)
  32. {
  33. YT_VERIFY(Ptr_);
  34. return Invoke_(std::forward<TArgs>(args)..., Ptr_);
  35. }
  36. template <class TResult, bool NoExcept, class... TArgs>
  37. template <class TConcrete>
  38. TResult TFunctionView<TResult(TArgs...) noexcept(NoExcept)>::ConcreteInvoke(TArgs... args, TErasedPtr ptr) noexcept(NoExcept)
  39. {
  40. return (*reinterpret_cast<TConcrete*>(ptr))(std::forward<TArgs>(args)...);
  41. }
  42. template <class TResult, bool NoExcept, class... TArgs>
  43. TFunctionView<TResult(TArgs...) noexcept(NoExcept)>::operator bool() const noexcept
  44. {
  45. return IsValid();
  46. }
  47. template <class TResult, bool NoExcept, class... TArgs>
  48. bool TFunctionView<TResult(TArgs...) noexcept(NoExcept)>::IsValid() const noexcept
  49. {
  50. return Ptr_ != nullptr;
  51. }
  52. template <class TResult, bool NoExcept, class... TArgs>
  53. void TFunctionView<TResult(TArgs...) noexcept(NoExcept)>::Reset() noexcept
  54. {
  55. Ptr_ = nullptr;
  56. }
  57. ////////////////////////////////////////////////////////////////////////////////
  58. } // namespace NYT