变量截断工具是将类型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变量设为最大值,否则仍旧静态转换后赋值

ACE中的参数截断工具-truncate的更多相关文章

  1. c#数据绑定(4)——向查询中添加参数

    本实例主要练习了ADO.Net 连接到外部数据库的基础上,向查询中添加参数.使用的是ACCESS数据库. 在ACCESS数据库中可以用MSSQL的形式定义操作字符串,也可以采用OLEDB的形式. MS ...

  2. SpringMVC无法获取请求中的参数的问题的调查与解决(1)

    *更新:本文第一版中犯了比较大的错误,无论@RequestBody还是@RequestParam注解一样,都会使用全局的Encoding进行解码,会导致特殊编码的参数值丢失. 只要抛弃掉注解,就完全可 ...

  3. 【转】【UML】使用Visual Studio 2010 Team System中的架构师工具(设计与建模)

    Lab 1: 应用程序建模 实验目标 这个实验的目的是展示如何在Visual Studio 2010旗舰版中进行应用程序建模.团队中的架构师会通过建模确定应用程序是否满足客户的需求. 你可以创建不同级 ...

  4. [转载]linux下编译php中configure参数具体含义

    编译N次了   原来这么回事 原文地址:linux下编译php中configure参数具体含义作者:捷心特 php编译参数的含义 ./configure –prefix=/usr/local/php ...

  5. Dubbo 泛化调用的参数解析问题及一个强大的参数解析工具 PojoUtils

    排查了3个多小时,因为一个简单的错误,发现一个强大的参数解析工具,记录一下. 背景 Nodejs 通过 tether 调用 Java Dubbo 服务.请求类的某个参数对象 EsCondition 有 ...

  6. MongoDB中的数据聚合工具Aggregate和Group

    周煦辰 2016-01-16 来说说MongoDB中的数据聚合工具. Aggregate是MongoDB提供的众多工具中的比较重要的一个,类似于SQL语句中的GROUP BY.聚合工具可以让开发人员直 ...

  7. 转:轻松把玩HttpClient之封装HttpClient工具类(一)(现有网上分享中的最强大的工具类)

    搜了一下网络上别人封装的HttpClient,大部分特别简单,有一些看起来比较高级,但是用起来都不怎么好用.调用关系不清楚,结构有点混乱.所以也就萌生了自己封装HttpClient工具类的想法.要做就 ...

  8. Python 数据分析中常用的可视化工具

    Python 数据分析中常用的可视化工具 1 Matplotlib 用于创建出版质量图表的绘图工具库,目的是为 Python 构建一个 Matlab 式的绘图接口. 1.1 安装 Anaconada ...

  9. java中的Arrays这个工具类你真的会用吗

    Java源码系列三-工具类Arrays ​ 今天分享java的源码的第三弹,Arrays这个工具类的源码.因为近期在复习数据结构,了解到Arrays里面的排序算法和二分查找等的实现,收益匪浅,决定研读 ...

随机推荐

  1. Android之多种Bitmap效果

    1. 将图片变为圆角 2. 获取缩略图图片 3. LOMO特效 4. 旧时光特效 5. 暖意特效 6. 根据饱和度.色相.亮度调整图片 7. 添加图片外边框 8. 添加内边框 9. 创建一个缩放的图片 ...

  2. 一分钟了解:String & StringBuilder & StringBuffer

    这三个都是字符串对象,本篇就来分析下它们的使用途径,力求简单明了. 一.String String 长度是不可变的,如果你要改变string对象的字符或者是拼接字符的话,系统就会新建一个string, ...

  3. Universal-Image-Loader解析(三)——用ListView和ViewPager加载网络中的图片

           现在我们终于可以通过这个框架来实现ListView中加载图片了,至于ViewPager还是别的,原理其实都是一样的 一.ListView 1.布局文件 list_layout.xml & ...

  4. Easyui dialog Y轴滚动条定位

    使用Easyui dialog进行数据新增的时候,如果Y轴方向有滚动条,当关闭之前的时候,将滚动条拉到最下方,再次打开的时候,滚动条还是定位在最下方. 需求: 每次打开的时候dialog Y轴滚动条定 ...

  5. Guava CaseFormat

    概述 CaseFormat用来转换各种不同的编程语言间的变量名命名格式, 主要用到的方法只有一个 CaseFormat.to(CaseFormat from, String s) CaseFormat ...

  6. codevs 2190 有理逼近

    2190 有理逼近  时间限制: 1 s  空间限制: 32000 KB  题目等级 : 黄金 Gold   题目描述 Description 对于一个素数P,我们可以用一系列有理分数(分子.分母都是 ...

  7. mysql 触发器 插入

    MySQL触发器 重要学习资料: http://dev.mysql.com/doc/refman/5.1/zh/triggers.html 在使用触发器的时候,遇到一个错误: [SQL]insert ...

  8. jboss支持远程访问配置

    jboss有个特点,在本机启动之后,只能在本机的网页访问 http://localhost:8080,在其他机器上访问不了jboss服务. 1.关闭jboss服务端的防火墙 解决方法如下: (dcm4 ...

  9. RecyclerView的使用(2)之多Item布局的载入

    原创文章,转载请注明 http://blog.csdn.net/leejizhou/article/details/50708349 李济洲的博客 上一篇介绍的了RecyclerView的基础使用ht ...

  10. Linux echo 显示内容颜色

    Linux echo 显示内容颜色 https://www.cnblogs.com/kimbo/p/6816566.html #字体颜色:30m-37m 黑.红.绿.黄.蓝.紫.青.白 str=&qu ...