ACE中的参数截断工具-truncate

时间:2024-04-17 14:36:36

变量截断工具是将类型A变量赋予类型B变量时使用,可自行判断变量是否需要截断,并且自动进行类型转换。

其全部为c实现

其入口为: ACE_Utils::truncate_cast<int> (val)

   /**
* @class truncate_cast
*
* @brief Helper function to truncate an integral value to the
* maximum value of the given type.
*
* Very useful since ACE methods return @c int very often and
* the value's source is often a different-size integral
* type, such as @c size_t. This function hides the
* truncation logic and resolves compiler diagnostics.
*
* @internal Internal use only.
*/
template<typename TO, typename FROM>
inline TO truncate_cast (FROM val)
{
// If the size of FROM is less than the size of TO, "val" will
// never be greater than the maximum "TO" value, so there is no
// need to attempt to truncate.
typedef typename ACE::If_Then_Else<
(sizeof (FROM) < sizeof (TO)),
Noop_Truncator<FROM, TO>,
Truncator<FROM, TO> >::result_type truncator; return truncator() (val);
}

其中判断函数实现为:

 namespace ACE
{ /**
* @struct If_Then_Else
*
* @brief Compile-time selection of type based on a boolean value.
*
* This primary template selects the second or third argument based
* on the value of the boolean first argument.
*
* Usage example:
*
* \code
*
* template <typename T>
* class Foo
* {
* public:
* // Set "TheType" to be the larger of "T" and "int".
* typedef typename If_Then_Else<(sizeof (T) > sizeof (int)),
* T,
* int>::result_type TheType;
* };
*
* \endcode
*
* @note This merely a forward declaration since we really only care
* about the partial specializations below.
*/
template <bool C, typename Ta, typename Tb>
struct If_Then_Else; /**
* @struct If_Then_Else
*
* @brief Select of type @a Ta if boolean value is @c true.
*
* This partial specialization selects the type @a Ta if the boolean
* first argument is @c true.
*/
template <typename Ta, typename Tb>
struct If_Then_Else<true, Ta, Tb>
{
typedef Ta result_type;
}; /**
* @struct If_Then_Else
*
* @brief Select of type @a Tb if boolean value is @c false.
*
* This partial specialization selects the type @a Tb if the boolean
* first argument is @c false.
*/
template <typename Ta, typename Tb>
struct If_Then_Else<false, Ta, Tb>
{
typedef Tb result_type;
}; }

如果FROM类型小于等于TO类型,则直接进入Noop_Truncator,也就是直接静态类型转换。这里不会有编译警告,而会将FROM变量的值直接赋给TO变量

 // -----------------------------------------------------
/**
* @struct Noop_Truncator
*
* @brief No-op truncation.
*
* This structure/functor performs no truncation since it assumes
* that @c sizeof(FROM) @c < @c sizeof(TO), meaning that
* @c numeric_limits<FROM>::max() @c < @c numeric_limits<TO>::max().
*/
template<typename FROM, typename TO>
struct Noop_Truncator
{
TO operator() (FROM val)
{
return static_cast<TO> (val);
}
};
// -----------------------------------------------------

而FROM类型大小大于TO类型,则会进入Truncator

 /**
* @struct Truncator
*
* @brief Truncate value of type @c FROM to value of type @c TO.
*
* Truncate a value of type @c FROM to value of type @c TO, if the
* value is larger than the maximum of value of type @c TO.
*/
template<typename FROM, typename TO>
struct Truncator
{
static bool const
// max FROM always greater than max TO
MAX_FROM_GT_MAX_TO = (sizeof(FROM) > sizeof (TO)
|| (sizeof(FROM) == sizeof (TO)
&& Sign_Check<FROM>::is_signed == )); typedef typename ACE::If_Then_Else<
MAX_FROM_GT_MAX_TO,
FROM,
TO>::result_type comp_to_type; // Take advantage of knowledge that we're casting a positive value
// to a type large enough to hold it so that we can bypass
// negative value checks at compile-time. Otherwise fallback on
// the safer comparison.
typedef typename ACE::If_Then_Else<
MAX_FROM_GT_MAX_TO,
Fast_Comparator<FROM, comp_to_type>,
typename Comparator<FROM, comp_to_type>::comp_type>::result_type comparator; /// Truncate a value of type @c FROM to value of type @c TO, if
/// the value is larger than the maximum of value of type @c TO.
TO operator() (FROM val)
{
return
(comparator::greater_than (val, ACE_Numeric_Limits<TO>::max ())
? ACE_Numeric_Limits<TO>::max ()
: static_cast<TO> (val));
} };

这里会直接对val进行判断,如果该值大于TO类型的最大值,则将TO变量设为最大值,否则仍旧静态转换后赋值