diff --git a/Code/Framework/AzCore/AzCore/Casting/numeric_cast.h b/Code/Framework/AzCore/AzCore/Casting/numeric_cast.h index 4cfe3a3d8d..c31a2e2f32 100644 --- a/Code/Framework/AzCore/AzCore/Casting/numeric_cast.h +++ b/Code/Framework/AzCore/AzCore/Casting/numeric_cast.h @@ -8,14 +8,25 @@ #pragma once +// This is disabled by default because it puts in costly runtime checking of casted values. +// You can either change it here to enable it across the engine, or use push/pop_macro to enable per file/feature. +// Note that if using push/pop_macro, you may get some of the functions not inline and the definition coming from +// another compilation unit, in such case, you will have to push/pop_macro on that compilation unit as well. +// #define AZ_NUMERICCAST_ENABLED 1 + +#if !AZ_NUMERICCAST_ENABLED + +#define aznumeric_cast static_cast + +#else + +#include #include #include #include #include #include -#include #include -#include #include #include #include @@ -28,7 +39,7 @@ // enabled. // // Because we can't do partial function specialization, I'm using enable_if to chop up the implementation into one of these -// implementations. If none of these fit, then we will get a compile error because it is an unknown conversionr. +// implementations. If none of these fit, then we will get a compile error because it is an unknown conversion. // //-------------------------------------------- // TYPE <- TYPE DigitLoss @@ -51,85 +62,7 @@ // (K) Floating Floating Y */ -// This is disabled by default because it puts in costly runtime checking of casted values. -// You can either change it here to enable it across the engine, or use push/pop_macro to enable per file/feature. -// Note that if using push/pop_macro, you may get some of the functions not inline and the definition coming from -// another compilation unit, in such case, you will have to push/pop_macro on that compilation unit as well. -// #define AZ_NUMERICCAST_ENABLED 1 - -#if AZ_NUMERICCAST_ENABLED #define AZ_NUMERIC_ASSERT(expr, ...) AZ_Assert(expr, __VA_ARGS__) -#else -#define AZ_NUMERIC_ASSERT(expr, ...) void(0) -#endif - -#pragma push_macro("max") -#undef max - -namespace NumericCastInternal -{ - template - inline constexpr typename AZStd::enable_if< - !AZStd::is_integral::value || !AZStd::is_floating_point::value - , bool> ::type UnderflowsToType(const FromType& value) - { - return (value < static_cast(std::numeric_limits::lowest())); - } - - template - inline constexpr typename AZStd::enable_if< - AZStd::is_integral::value && AZStd::is_floating_point::value - , bool> ::type UnderflowsToType(const FromType& value) - { - return (static_cast(value) < std::numeric_limits::lowest()); - } - - template - inline constexpr typename AZStd::enable_if< - !AZStd::is_integral::value || !AZStd::is_floating_point::value - , bool> ::type OverflowsToType(const FromType& value) - { - return (value > static_cast(std::numeric_limits::max())); - } - - template - inline constexpr typename AZStd::enable_if< - AZStd::is_integral::value && AZStd::is_floating_point::value - , bool> ::type OverflowsToType(const FromType& value) - { - return (static_cast(value) > std::numeric_limits::max()); - } - - template - inline constexpr typename AZStd::enable_if< - AZStd::is_integral::value && AZStd::is_integral::value - && std::numeric_limits::digits <= std::numeric_limits::digits - && AZStd::is_signed::value && AZStd::is_unsigned::value - , bool> ::type FitsInToType(const FromType& value) - { - return !NumericCastInternal::UnderflowsToType(value); - } - - template - inline constexpr typename AZStd::enable_if< - AZStd::is_integral::value && AZStd::is_integral::value - && (std::numeric_limits::digits > std::numeric_limits::digits) - && AZStd::is_unsigned::value - , bool> ::type FitsInToType(const FromType& value) - { - return !NumericCastInternal::OverflowsToType(value); - } - - template - inline constexpr typename AZStd::enable_if< - (!AZStd::is_integral::value || !AZStd::is_integral::value) - || ((std::numeric_limits::digits <= std::numeric_limits::digits) && (AZStd::is_unsigned::value || AZStd::is_signed::value)) - || ((std::numeric_limits::digits > std::numeric_limits::digits) && AZStd::is_signed::value) - , bool> ::type FitsInToType(const FromType& value) - { - return !NumericCastInternal::OverflowsToType(value) && !NumericCastInternal::UnderflowsToType(value); - } -} // namespace AZ // INTEGER -> INTEGER // (A) Not losing digits or risking sign loss @@ -276,8 +209,10 @@ inline constexpr auto aznumeric_cast(FromType&& value) -> return static_cast(value); } +#endif + // This is a helper class that lets us induce the destination type of a numeric cast -// It should never be directly used by anything other than azlossy_caster. +// It should never be directly used by anything other than aznumeric_caster. namespace AZ { template @@ -295,7 +230,7 @@ namespace AZ FromType m_value; }; -} +} // namespace AZ // This is the primary function we should use when doing numeric casting, since it induces the // type we need to cast to from the code rather than requiring an explicit coupling in the source. @@ -305,4 +240,3 @@ inline constexpr AZ::NumericCasted aznumeric_caster(FromType value) return AZ::NumericCasted(value); } -#pragma pop_macro("max") diff --git a/Code/Framework/AzCore/AzCore/Casting/numeric_cast_internal.h b/Code/Framework/AzCore/AzCore/Casting/numeric_cast_internal.h new file mode 100644 index 0000000000..ee2f91e95a --- /dev/null +++ b/Code/Framework/AzCore/AzCore/Casting/numeric_cast_internal.h @@ -0,0 +1,81 @@ +/* + * Copyright (c) Contributors to the Open 3D Engine Project. + * For complete copyright and license terms please see the LICENSE at the root of this distribution. + * + * SPDX-License-Identifier: Apache-2.0 OR MIT + * + */ + +#pragma once + +#include +#include +#include +#include +#include +#include + +namespace NumericCastInternal +{ + template + inline constexpr typename AZStd::enable_if::value || !AZStd::is_floating_point::value, bool>::type + UnderflowsToType(const FromType& value) + { + return (value < static_cast(std::numeric_limits::lowest())); + } + + template + inline constexpr typename AZStd::enable_if::value && AZStd::is_floating_point::value, bool>::type + UnderflowsToType(const FromType& value) + { + return (static_cast(value) < std::numeric_limits::lowest()); + } + + template + inline constexpr typename AZStd::enable_if::value || !AZStd::is_floating_point::value, bool>::type + OverflowsToType(const FromType& value) + { + return (value > static_cast(std::numeric_limits::max())); + } + + template + inline constexpr typename AZStd::enable_if::value && AZStd::is_floating_point::value, bool>::type + OverflowsToType(const FromType& value) + { + return (static_cast(value) > std::numeric_limits::max()); + } + + template + inline constexpr typename AZStd::enable_if< + AZStd::is_integral::value && AZStd::is_integral::value && + std::numeric_limits::digits <= std::numeric_limits::digits && AZStd::is_signed::value && + AZStd::is_unsigned::value, + bool>::type + FitsInToType(const FromType& value) + { + return !NumericCastInternal::UnderflowsToType(value); + } + + template + inline constexpr typename AZStd::enable_if< + AZStd::is_integral::value && AZStd::is_integral::value && + (std::numeric_limits::digits > std::numeric_limits::digits) && AZStd::is_unsigned::value, + bool>::type + FitsInToType(const FromType& value) + { + return !NumericCastInternal::OverflowsToType(value); + } + + template + inline constexpr typename AZStd::enable_if< + (!AZStd::is_integral::value || !AZStd::is_integral::value) || + ((std::numeric_limits::digits <= std::numeric_limits::digits) && + (AZStd::is_unsigned::value || AZStd::is_signed::value)) || + ((std::numeric_limits::digits > std::numeric_limits::digits) && AZStd::is_signed::value), + bool>::type + FitsInToType(const FromType& value) + { + return !NumericCastInternal::OverflowsToType(value) && !NumericCastInternal::UnderflowsToType(value); + } + +} diff --git a/Code/Framework/AzCore/AzCore/Serialization/Json/CastingHelpers.h b/Code/Framework/AzCore/AzCore/Serialization/Json/CastingHelpers.h index 36b0426a2d..bcd1a84d03 100644 --- a/Code/Framework/AzCore/AzCore/Serialization/Json/CastingHelpers.h +++ b/Code/Framework/AzCore/AzCore/Serialization/Json/CastingHelpers.h @@ -8,14 +8,14 @@ #pragma once -#include +#include #include #include #include namespace AZ { - //! A helper function to casts between numeric types, and consider the data which is being converted comse from user data. + //! A helper function to casts between numeric types, and consider the data which is being converted comes from user data. //! If a conversion from FromType to ToType will not cause overflow or underflow, the result is stored in result, and the function returns Success //! Otherwise, the target is left untouched. template @@ -24,9 +24,9 @@ namespace AZ { using namespace JsonSerializationResult; - if (NumericCastInternal::FitsInToType(value)) + if (NumericCastInternal::template FitsInToType(value)) { - result = aznumeric_cast(value); + result = static_cast(value); return reporting("Successfully cast number.", ResultCode(Tasks::Convert, Outcomes::Success), path); } else diff --git a/Code/Framework/AzCore/AzCore/azcore_files.cmake b/Code/Framework/AzCore/AzCore/azcore_files.cmake index a41107e539..6c498c3335 100644 --- a/Code/Framework/AzCore/AzCore/azcore_files.cmake +++ b/Code/Framework/AzCore/AzCore/azcore_files.cmake @@ -35,6 +35,7 @@ set(FILES Asset/AssetInternal/WeakAsset.h Casting/lossy_cast.h Casting/numeric_cast.h + Casting/numeric_cast_internal.h Component/Component.cpp Component/Component.h Component/ComponentApplication.cpp diff --git a/Code/Framework/AzQtComponents/AzQtComponents/Components/FancyDocking.cpp b/Code/Framework/AzQtComponents/AzQtComponents/Components/FancyDocking.cpp index 4ced4ea635..d73ebbec28 100644 --- a/Code/Framework/AzQtComponents/AzQtComponents/Components/FancyDocking.cpp +++ b/Code/Framework/AzQtComponents/AzQtComponents/Components/FancyDocking.cpp @@ -9,6 +9,7 @@ #include #include +#include #include #include diff --git a/Code/Framework/AzQtComponents/AzQtComponents/Components/Widgets/GradientSlider.cpp b/Code/Framework/AzQtComponents/AzQtComponents/Components/Widgets/GradientSlider.cpp index 1ffff57ade..920ff17eac 100644 --- a/Code/Framework/AzQtComponents/AzQtComponents/Components/Widgets/GradientSlider.cpp +++ b/Code/Framework/AzQtComponents/AzQtComponents/Components/Widgets/GradientSlider.cpp @@ -114,7 +114,7 @@ void GradientSlider::mouseMoveEvent(QMouseEvent* event) { int intValue = Slider::valueFromPosition(this, event->pos(), width(), height(), rect().bottom()); - qreal value = (aznumeric_cast(intValue - minimum()) / aznumeric_cast(maximum() - minimum())); + qreal value = (aznumeric_cast(intValue - minimum()) / aznumeric_cast(maximum() - minimum())); const QString toolTipText = m_toolTipFunction(value);