变量截断工具是将类型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. information_schema系列十

    information_schema系列十   1:INNODB_FT_CONFIG 这张表存的是全文索引的信息,查询前可以先通过以下语句查询一下开启全文索引的表: show variables li ...

  2. 说说初用 Mock 工具测试碰到的坑

    我是一个在校实习生,作为一个程序猿,是个菜鸟中战斗机!对于测试,只写过一点点简单到不能再简单了的 Junit 单元测试的例子(因为当时这足以应付学校课程的内容与要求).这几天在公司里要真枪实弹做测试的 ...

  3. 洛谷 P1164 小A点菜

    题目背景 uim神犇拿到了uoi的ra(镭牌)后,立刻拉着基友小A到了一家……餐馆,很低端的那种. uim指着墙上的价目表(太低级了没有菜单),说:“随便点”. 题目描述 不过uim由于买了一些辅(e ...

  4. emouse思·睿—评论与观点整理之四

    虽说我主要做的硬件,平时的兴趣爱好比较关注移动互联网,混迹于虎嗅.爱范儿.雷锋网.36Kr.cnBeta.瘾科技.i黑马.TechWeb等这类科技以及创业媒体,遗憾的是系统的去写的并不多,好在还算充分 ...

  5. iOS开发-UIScreenEdgePanGestureRecognizer实战

    UIScreenEdgePanGestureRecognizer名字很长,而且关于其文档也是少的的可怜,苹果官方给的唯一的一个属性是edges,文档中的解释是这样的: A UIScreenEdgePa ...

  6. 组件化 得到 DDComponent JIMU 模块 插件 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  7. Centos7 搭建lnmp环境 (centos7+nginx+MySQL5.7.9+PHP7)

    阿里云一台服务器出现问题! 我估计是一键安装包环境的原因,所以打算重新搭建下环境! 首先,当然是先做好快照!安全第一! 对系统盘做更换系统操作,装上纯净版的centos. 装好后,进入系统 一.挂载数 ...

  8. Android 混合式开发AppCan介绍

    Android程序员开发已从最早的异常火热到现在已经逐渐趋向稳定,目前企业针对Android开发工程师的要求要求已逐步提高,现在想从众多的面试者中脱颖而出,必须打好坚实的代码基础. 今天为大家介绍一款 ...

  9. tensorflow基本操作介绍

    1.tensorflow的基本运作 为了快速的熟悉TensorFlow编程,下面从一段简单的代码开始: import tensorflow as tf #定义‘符号’变量,也称为占位符 a = tf. ...

  10. fasttext使用笔记

    http://blog.csdn.net/m0_37306360/article/details/72832606 这里记录使用fastText训练word vector笔记 github地址:htt ...