/////////////////////////////////////////////////////////////////////////////// // // Copyright (c) 2018 martysama0134. All rights reserved. // // This code is licensed under the MIT License (MIT). // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. // /////////////////////////////////////////////////////////////////////////////// #pragma once #include #include namespace msl { //! @brief msl::truncate_error struct struct truncate_error : std::exception { const char * what() const noexcept override { return "MSL::Truncate Cast Exception"; } }; //! @brief msl::truncate_cast truncate with no check template constexpr T truncate_cast(U u) { return static_cast(std::trunc(u)); } //! @brief msl::truncate: truncate and throw msl::truncate_error if check fails template constexpr T truncate(U u) { auto t = truncate_cast(u); if (static_cast(t) != std::trunc(u)) throw truncate_error(); return t; } //! @brief msl::integral_cast: from floating points to integral types only and throw msl::truncate_error if check fails (slight faster than truncate) template constexpr T integral_cast(U u) { auto t = static_cast(u); if (static_cast(t) != std::trunc(u)) throw truncate_error(); return t; } //! @brief more meaningful cast between dynamic_cast and static_cast template constexpr T inherit_cast(U && u) noexcept { return static_cast(std::forward(u)); } } // namespace msl