ACE中的参数截断工具-truncate
变量截断工具是将类型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的更多相关文章
- c#数据绑定(4)——向查询中添加参数
本实例主要练习了ADO.Net 连接到外部数据库的基础上,向查询中添加参数.使用的是ACCESS数据库. 在ACCESS数据库中可以用MSSQL的形式定义操作字符串,也可以采用OLEDB的形式. MS ...
- SpringMVC无法获取请求中的参数的问题的调查与解决(1)
*更新:本文第一版中犯了比较大的错误,无论@RequestBody还是@RequestParam注解一样,都会使用全局的Encoding进行解码,会导致特殊编码的参数值丢失. 只要抛弃掉注解,就完全可 ...
- 【转】【UML】使用Visual Studio 2010 Team System中的架构师工具(设计与建模)
Lab 1: 应用程序建模 实验目标 这个实验的目的是展示如何在Visual Studio 2010旗舰版中进行应用程序建模.团队中的架构师会通过建模确定应用程序是否满足客户的需求. 你可以创建不同级 ...
- [转载]linux下编译php中configure参数具体含义
编译N次了 原来这么回事 原文地址:linux下编译php中configure参数具体含义作者:捷心特 php编译参数的含义 ./configure –prefix=/usr/local/php ...
- Dubbo 泛化调用的参数解析问题及一个强大的参数解析工具 PojoUtils
排查了3个多小时,因为一个简单的错误,发现一个强大的参数解析工具,记录一下. 背景 Nodejs 通过 tether 调用 Java Dubbo 服务.请求类的某个参数对象 EsCondition 有 ...
- MongoDB中的数据聚合工具Aggregate和Group
周煦辰 2016-01-16 来说说MongoDB中的数据聚合工具. Aggregate是MongoDB提供的众多工具中的比较重要的一个,类似于SQL语句中的GROUP BY.聚合工具可以让开发人员直 ...
- 转:轻松把玩HttpClient之封装HttpClient工具类(一)(现有网上分享中的最强大的工具类)
搜了一下网络上别人封装的HttpClient,大部分特别简单,有一些看起来比较高级,但是用起来都不怎么好用.调用关系不清楚,结构有点混乱.所以也就萌生了自己封装HttpClient工具类的想法.要做就 ...
- Python 数据分析中常用的可视化工具
Python 数据分析中常用的可视化工具 1 Matplotlib 用于创建出版质量图表的绘图工具库,目的是为 Python 构建一个 Matlab 式的绘图接口. 1.1 安装 Anaconada ...
- java中的Arrays这个工具类你真的会用吗
Java源码系列三-工具类Arrays 今天分享java的源码的第三弹,Arrays这个工具类的源码.因为近期在复习数据结构,了解到Arrays里面的排序算法和二分查找等的实现,收益匪浅,决定研读 ...
随机推荐
- Asp.Net Mvc3.0(MEF依赖注入理论)
前言 Managed Extensibility Framework(MEF)是.NET平台下的一个扩展性管理框架,它是一系列特性的集合,包括依赖注入(DI)等.MEF为开发人员提供了一个工具,让我们 ...
- Android之TextView部分颜色变动
public class StringHandleExampleActivity extends Activity { /** Called when the activity is first cr ...
- 应用内截屏的代码,在Activity中测试可用
截屏功能让我十分头疼,想做个无需root的又找不到资料.这里暂且分享一个无需root的,在应用内截屏的代码,本文转自:http://blog.csdn.net/csh159/article/detai ...
- 使用PHP生成二维码图像
1.PHP生成二维码图像的类QRcode http://www.phper.org.cn/?post=128 QRcode是用于生成二维条形码的开放源码 (LGPL) 库.提供 API 创建条码图像. ...
- python文档生成工具:pydoc、sphinx;django如何使用sphinx?
文档生成工具: 自带的pydoc,比较差 建议使用sphinx 安装: pip install sphinx 安装主题: 由各种主题,我选择常用的sphinx_rtd_theme pip instal ...
- IDEA重写toString()模板,转成json格式
1.类中Alt + Insert,弹出下框 2.点击新增 public java.lang.String toString() { final java.lang.StringBuilder sb = ...
- 分析器错误消息: 未能找到 CodeDom 提供程序类型
ylbtech-Error-WebForm:分析器错误消息: 未能找到 CodeDom 提供程序类型“Microsoft.CodeDom.Providers.DotNetCompilerPlatfor ...
- 案例导入和导出Scott用户
ylbtech-Oracle:案例导入和导出Scott用户 导入和导出Scott用户 1. 导出Scott用户下的所有对象返回顶部 1.1, Microsoft Windows [版本 ] 版权所有 ...
- windows核心编程-互斥器(Mutexes)
线程同步的方式主要有:临界区.互斥区.事件.信号量四种方式. 前边讲过了临界区线程同步-----windows核心编程-关键段(临界区)线程同步,这章我来介绍一下互斥器(Mutexes)在线程同步中的 ...
- json传输二进制的方案【转】
本文转自:http://wiyi.org/binary-to-string.html json 是一种很简洁的协议,但可惜的是,它只能传递基本的数型(int,long,string等),但不能传递by ...