You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1016B

  1. #pragma once
  2. namespace meta
  3. {
  4. // Update if different =========================================================
  5. template<typename T>
  6. bool updateIfDifferent (T& lhs, const T& rhs)
  7. {
  8. if (lhs == rhs)
  9. return false;
  10. lhs = rhs;
  11. return true;
  12. }
  13. template<typename T>
  14. bool updateIfDifferent (T& lhs, const T&& rhs)
  15. {
  16. if (lhs == rhs)
  17. return false;
  18. lhs = rhs;
  19. return true;
  20. }
  21. // Protected math call =========================================================
  22. template<typename T>
  23. T exp(T exponent)
  24. {
  25. static const T maxExponent = std::nextafter(std::log(std::numeric_limits<T>::max()), T(0));
  26. static const T maxValue = std::exp(maxExponent);
  27. return (exponent < maxExponent) ? std::exp(exponent) : maxValue;
  28. }
  29. template<typename T>
  30. T min(T a, T b)
  31. {
  32. return (a < b) ? a : b;
  33. }
  34. template<typename T>
  35. T max(T a, T b)
  36. {
  37. return (a > b) ? a : b;
  38. }
  39. template<typename T>
  40. T clamp(T value, T minValue, T maxValue)
  41. {
  42. return max(min(value, maxValue), minValue);
  43. }
  44. } // namespace