functors.h 920 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // This file is part of Desktop App Toolkit,
  2. // a set of libraries for developing nice desktop applications.
  3. //
  4. // For license and copyright information please follow this link:
  5. // https://github.com/desktop-app/legal/blob/master/LEGAL
  6. //
  7. #pragma once
  8. namespace base {
  9. namespace functors {
  10. struct abs_helper {
  11. template <
  12. typename Type,
  13. typename = decltype(0 < std::declval<Type>()),
  14. typename = decltype(-std::declval<Type>())>
  15. constexpr Type operator()(Type value) const {
  16. return (0 < value) ? value : (-value);
  17. }
  18. };
  19. constexpr auto abs = abs_helper{};
  20. constexpr auto add = [](auto value) {
  21. return [value](auto other) {
  22. return value + other;
  23. };
  24. };
  25. struct negate_helper {
  26. template <
  27. typename Type,
  28. typename = decltype(-std::declval<Type>())>
  29. constexpr Type operator()(Type value) const {
  30. return -value;
  31. }
  32. };
  33. constexpr auto negate = negate_helper{};
  34. } // namespace functors
  35. } // namespace base