📄 truncation_test.hpp
字号:
/* /////////////////////////////////////////////////////////////////////////
* File: stlsoft/conversion/truncation_test.hpp
*
* Purpose: Runtime checking for numeric conversions.
*
* Created: 10th August 2006
* Updated: 24th January 2007
*
* Home: http://stlsoft.org/
*
* Copyright (c) 2006-2007, Matthew Wilson and Synesis Software
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
* any contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* ////////////////////////////////////////////////////////////////////// */
/** \file stlsoft/conversion/truncation_test.hpp
*
* \brief [C++ only] Definition of the stlsoft::truncation_test functions
* (\ref group__library__conversion "Conversion" Library).
*/
#ifndef STLSOFT_INCL_STLSOFT_CONVERSION_HPP_TRUNCATION_TEST
#define STLSOFT_INCL_STLSOFT_CONVERSION_HPP_TRUNCATION_TEST
#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
# define STLSOFT_VER_STLSOFT_CONVERSION_HPP_TRUNCATION_TEST_MAJOR 1
# define STLSOFT_VER_STLSOFT_CONVERSION_HPP_TRUNCATION_TEST_MINOR 0
# define STLSOFT_VER_STLSOFT_CONVERSION_HPP_TRUNCATION_TEST_REVISION 3
# define STLSOFT_VER_STLSOFT_CONVERSION_HPP_TRUNCATION_TEST_EDIT 41
#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
/* /////////////////////////////////////////////////////////////////////////
* Auto-generation and compatibility
*/
/*
*/
/* /////////////////////////////////////////////////////////////////////////
* Includes
*/
#ifndef STLSOFT_INCL_STLSOFT_H_STLSOFT
# include <stlsoft/stlsoft.h>
#endif /* !STLSOFT_INCL_STLSOFT_H_STLSOFT */
#ifndef STLSOFT_INCL_STLSOFT_UTIL_H_LIMIT_TRAITS
# include <stlsoft/util/limit_traits.h>
#endif /* !STLSOFT_INCL_STLSOFT_UTIL_H_LIMIT_TRAITS */
#ifndef STLSOFT_INCL_STLSOFT_UTIL_HPP_SIGN_TRAITS
# include <stlsoft/util/sign_traits.hpp>
#endif /* !STLSOFT_INCL_STLSOFT_UTIL_HPP_SIGN_TRAITS */
#ifndef STLSOFT_INCL_STLSOFT_META_HPP_IS_INTEGRAL_TYPE
# include <stlsoft/meta/is_integral_type.hpp>
#endif /* !STLSOFT_INCL_STLSOFT_META_HPP_IS_INTEGRAL_TYPE */
#ifndef STLSOFT_INCL_STLSOFT_META_HPP_IS_SIGNED_TYPE
# include <stlsoft/meta/is_signed_type.hpp>
#endif /* !STLSOFT_INCL_STLSOFT_META_HPP_IS_SIGNED_TYPE */
#ifdef STLSOFT_UNITTEST
# include <limits.h>
#endif /* STLSOFT_UNITTEST */
#if defined(STLSOFT_UNITTEST) || \
defined(_DEBUG)
# include <typeinfo>
#endif /* STLSOFT_UNITTEST || _DEBUG */
/* /////////////////////////////////////////////////////////////////////////
* Namespace
*/
#ifndef _STLSOFT_NO_NAMESPACE
namespace stlsoft
{
#endif /* _STLSOFT_NO_NAMESPACE */
/* /////////////////////////////////////////////////////////////////////////
* Functions
*
*
* Assume 11 types:
* char, signed char, unsigned char
* short, unsigned short
* int, unsigned int
* long, unsigned long
* long long, unsigned long long
*
* That gives 121 permutations:
* (a) 11 where the type is the same
* (b) 20 where the sign is the same and sizeof(FROM) <= sizeof(TO); superset of (a)
* (c) 10 where FROM is unsigned and sizeof(FROM) < sizeof(TO)
* (d) 80 that must be determined dynamically
*
*
* The strategy is as follows:
*
if( signof(FROM) == signof(TO) && // Compile-time test 1
sizeof(FROM) <= sizeof(TO))
{
return true;
}
else if(unsigned == signof(FROM) && // Compile-time test 2
sizeof(FROM) < sizeof(TO))
{
return true;
}
else
{
return runtime_test<TO>(from);
}
*
* The runtime test evaluates to the following:
*
* If the FROM type is signed, then
*/
#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
// The permutations are:
//
// 1a FROM signed | TO signed | sizeof(FROM) < sizeof(TO) => Always yes
// 1b FROM signed | TO signed | sizeof(FROM) = sizeof(TO) => Always yes
// 1c FROM signed | TO signed | sizeof(FROM) > sizeof(TO) => Runtime test
//
// 2a FROM unsigned | TO signed | sizeof(FROM) < sizeof(TO) => Runtime test
// 2b FROM unsigned | TO signed | sizeof(FROM) = sizeof(TO) => Runtime test
// 2c FROM unsigned | TO signed | sizeof(FROM) > sizeof(TO) => Runtime test
//
// 3a FROM signed | TO unsigned | sizeof(FROM) < sizeof(TO) => Always yes
// 3b FROM signed | TO unsigned | sizeof(FROM) = sizeof(TO) => Runtime test
// 3c FROM signed | TO unsigned | sizeof(FROM) > sizeof(TO) => Runtime test
//
// 4a FROM unsigned | TO unsigned | sizeof(FROM) < sizeof(TO) => Always yes
// 4b FROM unsigned | TO unsigned | sizeof(FROM) = sizeof(TO) => Always yes
// 4c FROM unsigned | TO unsigned | sizeof(FROM) > sizeof(TO) => Runtime test
template< ss_typename_param_k TO
, ss_typename_param_k FROM
>
inline bool truncation_test_helper_runtime_test_different_sign_FROM_is_signed(FROM from, yes_type, TO)
{
#ifdef _DEBUG
char const *TO_ = typeid(TO).name();
char const *FROM_ = typeid(FROM).name();
STLSOFT_SUPPRESS_UNUSED(TO_);
STLSOFT_SUPPRESS_UNUSED(FROM_);
#endif /* _DEBUG */
enum { TO_is_signed = is_signed_type<TO>::value };
enum { FROM_is_signed = is_signed_type<FROM>::value };
const ss_size_t sizeofFROM = sizeof(FROM);
const ss_size_t sizeofTO = sizeof(TO);
STLSOFT_SUPPRESS_UNUSED(sizeofFROM);
STLSOFT_SUPPRESS_UNUSED(sizeofTO);
STLSOFT_STATIC_ASSERT((0 == int(TO_is_signed)) != (0 == int(FROM_is_signed)));
STLSOFT_STATIC_ASSERT(0 != int(FROM_is_signed));
STLSOFT_STATIC_ASSERT(0 == int(TO_is_signed));
// FROM is signed
// TO is unsigned
//
// Truncation occurs if:
//
// - from < 0
// - from > toMax
// - from
if(from < 0)
{
return false;
}
else if(sizeofFROM < sizeofTO) // 3a
{
return true;
}
else
{
if(sizeofFROM == sizeofTO)
{
TO toMax = limit_traits<TO>::maximum();
if(toMax < static_cast<TO>(from))
{
return false;
}
else
{
return true;
}
}
else // 3b & 3c
{
FROM toMax = static_cast<FROM>(limit_traits<TO>::maximum());
if(from > toMax)
{
return false;
}
else
{
return true;
}
}
}
}
template< ss_typename_param_k TO
, ss_typename_param_k FROM
>
inline bool truncation_test_helper_runtime_test_different_sign_FROM_is_signed(FROM from, no_type, TO)
{
#ifdef _DEBUG
char const *TO_ = typeid(TO).name();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -