Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r66416 - trunk/boost/gil
From: chhenning_at_[hidden]
Date: 2010-11-06 14:17:12


Author: chhenning
Date: 2010-11-06 14:17:10 EDT (Sat, 06 Nov 2010)
New Revision: 66416
URL: http://svn.boost.org/trac/boost/changeset/66416

Log:
Introducing base_channel_type metafunction which helps to avoid useless casting between integral data types. For older compilers it also reduces warnings about those castings operations.
Text files modified:
   trunk/boost/gil/channel.hpp | 27 +++++++++++++++++++++++++++
   trunk/boost/gil/channel_algorithm.hpp | 6 +++---
   2 files changed, 30 insertions(+), 3 deletions(-)

Modified: trunk/boost/gil/channel.hpp
==============================================================================
--- trunk/boost/gil/channel.hpp (original)
+++ trunk/boost/gil/channel.hpp 2010-11-06 14:17:10 EDT (Sat, 06 Nov 2010)
@@ -27,6 +27,7 @@
 #include <limits>
 #include <cassert>
 #include <boost/cstdint.hpp>
+#include <boost/type_traits/remove_cv.hpp>
 #include "gil_config.hpp"
 #include "utilities.hpp"
 
@@ -669,4 +670,30 @@
 
 }
 
+// \brief Determines the fundamental type which may be used, e.g., to cast from larger to smaller channel types.
+namespace boost { namespace gil {
+template <typename T>
+struct base_channel_type_impl { typedef T type; };
+
+template <int N>
+struct base_channel_type_impl<packed_channel_value<N> >
+{ typedef typename packed_channel_value<N>::integer_t type; };
+
+template <typename B, int F, int N, bool M>
+struct base_channel_type_impl<packed_channel_reference<B, F, N, M> >
+{ typedef typename packed_channel_reference<B,F,N,M>::integer_t type; };
+
+template <typename B, int N, bool M>
+struct base_channel_type_impl<packed_dynamic_channel_reference<B, N, M> >
+{ typedef typename packed_dynamic_channel_reference<B,N,M>::integer_t type; };
+
+template <typename ChannelValue, typename MinV, typename MaxV>
+struct base_channel_type_impl<scoped_channel_value<ChannelValue, MinV, MaxV> >
+{ typedef ChannelValue type; };
+
+template <typename T>
+struct base_channel_type : base_channel_type_impl<typename remove_cv<T>::type > {};
+
+} } //namespace boost::gil
+
 #endif

Modified: trunk/boost/gil/channel_algorithm.hpp
==============================================================================
--- trunk/boost/gil/channel_algorithm.hpp (original)
+++ trunk/boost/gil/channel_algorithm.hpp 2010-11-06 14:17:10 EDT (Sat, 06 Nov 2010)
@@ -226,8 +226,8 @@
 template <typename SrcChannelV, typename DstChannelV>
 struct channel_converter_unsigned_integral_nondivisible<SrcChannelV,DstChannelV,true,false> {
     DstChannelV operator()(SrcChannelV src) const {
- typedef typename detail::min_fast_uint<unsigned_integral_num_bits<SrcChannelV>::value+unsigned_integral_num_bits<DstChannelV>::value>::type integer_t;
- return DstChannelV(integer_t(src * unsigned_integral_max_value<DstChannelV>::value) / unsigned_integral_max_value<SrcChannelV>::value);
+ typedef typename base_channel_type<DstChannelV>::type dest_t;
+ return DstChannelV(static_cast<dest_t>( src * unsigned_integral_max_value<DstChannelV>::value) / unsigned_integral_max_value<SrcChannelV>::value);
     }
 };
 
@@ -410,7 +410,7 @@
 template <typename ChannelValue>
 struct channel_multiplier_unsigned : public std::binary_function<ChannelValue,ChannelValue,ChannelValue> {
     ChannelValue operator()(ChannelValue a, ChannelValue b) const {
- return ChannelValue(a / double(channel_traits<ChannelValue>::max_value()) * b);
+ return ChannelValue(static_cast<typename base_channel_type<ChannelValue>::type>(a / double(channel_traits<ChannelValue>::max_value()) * b));
     }
 };
 


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk