ACE中的参数截断工具-truncate

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

其全部为c实现

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

 1   /**
 2    * @class truncate_cast
 3    *
 4    * @brief Helper function to truncate an integral value to the
 5    *        maximum  value of the given type.
 6    *
 7    *        Very useful since ACE methods return @c int very often and
 8    *        the value's source is often a different-size integral
 9    *        type, such as @c size_t.  This function hides the
10    *        truncation logic and resolves compiler diagnostics.
11    *
12    * @internal Internal use only.
13    */
14   template<typename TO, typename FROM>
15   inline TO truncate_cast (FROM val)
16   {
17     // If the size of FROM is less than the size of TO, "val" will
18     // never be greater than the maximum "TO" value, so there is no
19     // need to attempt to truncate.
20     typedef typename ACE::If_Then_Else<
21       (sizeof (FROM) < sizeof (TO)),
22       Noop_Truncator<FROM, TO>,
23       Truncator<FROM, TO> >::result_type truncator;
24 
25     return truncator() (val);
26   }

其中判断函数实现为:

 1 namespace ACE
 2 {
 3 
 4   /**
 5    * @struct If_Then_Else
 6    *
 7    * @brief Compile-time selection of type based on a boolean value.
 8    *
 9    * This primary template selects the second or third argument based
10    * on the value of the boolean first argument.
11    *
12    * Usage example:
13    *
14    * \code
15    *
16    *   template <typename T>
17    *   class Foo
18    *   {
19    *   public:
20    *     // Set "TheType" to be the larger of "T" and  "int".
21    *     typedef typename If_Then_Else<(sizeof (T) > sizeof (int)),
22    *                                   T,
23    *                                   int>::result_type TheType;
24    *   };
25    *
26    * \endcode
27    *
28    * @note This merely a forward declaration since we really only care
29    *       about the partial specializations below.
30    */
31   template <bool C, typename Ta, typename Tb>
32   struct If_Then_Else;
33 
34   /**
35    * @struct If_Then_Else
36    *
37    * @brief Select of type @a Ta if boolean value is @c true.
38    *
39    * This partial specialization selects the type @a Ta if the boolean
40    * first argument is @c true.
41    */
42   template <typename Ta, typename Tb>
43   struct If_Then_Else<true, Ta, Tb>
44   {
45     typedef Ta result_type;
46   };
47 
48   /**
49    * @struct If_Then_Else
50    *
51    * @brief Select of type @a Tb if boolean value is @c false.
52    *
53    * This partial specialization selects the type @a Tb if the boolean
54    * first argument is @c false.
55    */
56   template <typename Ta, typename Tb>
57   struct If_Then_Else<false, Ta, Tb>
58   {
59     typedef Tb result_type;
60   };
61 
62 }

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

 1 // -----------------------------------------------------
 2   /**
 3    * @struct Noop_Truncator
 4    *
 5    * @brief No-op truncation.
 6    *
 7    * This structure/functor performs no truncation since it assumes
 8    * that @c sizeof(FROM) @c < @c sizeof(TO), meaning that
 9    * @c numeric_limits<FROM>::max() @c < @c numeric_limits<TO>::max().
10    */
11   template<typename FROM, typename TO>
12   struct Noop_Truncator
13   {
14     TO operator() (FROM val)
15     {
16       return static_cast<TO> (val);
17     }
18   };
19   // -----------------------------------------------------

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

 1 /**
 2    * @struct Truncator
 3    *
 4    * @brief Truncate value of type @c FROM to value of type @c TO.
 5    *
 6    * Truncate a value of type @c FROM to value of type @c TO, if the
 7    * value is larger than the maximum of value of type @c TO.
 8    */
 9   template<typename FROM, typename TO>
10   struct Truncator
11   {
12     static bool const
13       // max FROM always greater than max TO
14       MAX_FROM_GT_MAX_TO = (sizeof(FROM) > sizeof (TO)
15                             || (sizeof(FROM) == sizeof (TO)
16                                 && Sign_Check<FROM>::is_signed == 0));
17 
18     typedef typename ACE::If_Then_Else<
19       MAX_FROM_GT_MAX_TO,
20       FROM,
21       TO>::result_type comp_to_type;
22 
23     // Take advantage of knowledge that we're casting a positive value
24     // to a type large enough to hold it so that we can bypass
25     // negative value checks at compile-time.  Otherwise fallback on
26     // the safer comparison.
27     typedef typename ACE::If_Then_Else<
28       MAX_FROM_GT_MAX_TO,
29       Fast_Comparator<FROM, comp_to_type>,
30       typename Comparator<FROM, comp_to_type>::comp_type>::result_type comparator;
31 
32     /// Truncate a value of type @c FROM to value of type @c TO, if
33     /// the value is larger than the maximum of value of type @c TO.
34     TO operator() (FROM val)
35     {
36       return
37         (comparator::greater_than (val, ACE_Numeric_Limits<TO>::max ())
38          ? ACE_Numeric_Limits<TO>::max ()
39          : static_cast<TO> (val));
40     }
41 
42   };

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

posted @ 2018-07-20 17:15  鸭子船长  阅读(354)  评论(0编辑  收藏  举报