default_constructible_function_proxy.h 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. //
  2. // default_constructible_function_proxy.h
  3. // iterator
  4. //
  5. // Created by Sam Jaffe on 4/1/23.
  6. // Copyright © 2023 Sam Jaffe. All rights reserved.
  7. //
  8. #include <optional>
  9. #include <iterator/detail/macro.h>
  10. namespace iterator::detail {
  11. template <typename F, typename = void>
  12. class default_constructible_function_proxy {
  13. public:
  14. default_constructible_function_proxy() = default;
  15. default_constructible_function_proxy(F func) : func_(func) {}
  16. template <typename... Args> decltype(auto) operator()(Args &&... args) const {
  17. if (!func_) { throw; }
  18. return std::invoke(*func_, FWD(args)...);
  19. }
  20. private:
  21. std::optional<F> func_;
  22. };
  23. template <typename F>
  24. class default_constructible_function_proxy<
  25. F, std::enable_if_t<std::is_default_constructible_v<F>>> {
  26. public:
  27. default_constructible_function_proxy() = default;
  28. default_constructible_function_proxy(F func) : func_(func) {}
  29. template <typename... Args> decltype(auto) operator()(Args &&... args) const {
  30. return std::invoke(func_, FWD(args)...);
  31. }
  32. private:
  33. F func_;
  34. };
  35. }
  36. #include <iterator/detail/undef.h>