00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00049 #ifndef STLSOFT_INCL_STLSOFT_CONVERSION_HPP_UNION_CAST
00050 #define STLSOFT_INCL_STLSOFT_CONVERSION_HPP_UNION_CAST
00051
00052 #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
00053 # define STLSOFT_VER_STLSOFT_CONVERSION_HPP_UNION_CAST_MAJOR 5
00054 # define STLSOFT_VER_STLSOFT_CONVERSION_HPP_UNION_CAST_MINOR 0
00055 # define STLSOFT_VER_STLSOFT_CONVERSION_HPP_UNION_CAST_REVISION 3
00056 # define STLSOFT_VER_STLSOFT_CONVERSION_HPP_UNION_CAST_EDIT 64
00057 #endif
00058
00059
00060
00061
00062
00063 #ifndef STLSOFT_INCL_STLSOFT_H_STLSOFT
00064 # include <stlsoft/stlsoft.h>
00065 #endif
00066 #ifndef STLSOFT_INCL_STLSOFT_UTIL_HPP_CONSTRAINTS
00067 # include <stlsoft/util/constraints.hpp>
00068 #endif
00069 #if defined(STLSOFT_CF_TEMPLATE_PARTIAL_SPECIALISATION_SUPPORT)
00070 # ifndef STLSOFT_INCL_STLSOFT_META_HPP_BASE_TYPE_TRAITS
00071 # include <stlsoft/meta/base_type_traits.hpp>
00072 # endif
00073 #endif
00074
00075
00076
00077
00078
00079 #ifndef _STLSOFT_NO_NAMESPACE
00080 namespace stlsoft
00081 {
00082 #endif
00083
00084
00085
00086
00087
00104 template< ss_typename_param_k TO
00105 , ss_typename_param_k FROM
00106 #if defined(STLSOFT_COMPILER_IS_GCC) && \
00107 __GNUC__ < 3
00108 , ss_bool_t B_CHECK_ALIGN
00109 #else
00110 , ss_bool_t B_CHECK_ALIGN = true
00111 #endif
00112 >
00113 union union_caster
00114 {
00117 public:
00119 typedef TO to_type;
00121 typedef FROM from_type;
00123 typedef union_caster<TO, FROM, B_CHECK_ALIGN> class_type;
00125
00128 public:
00130 ss_explicit_k union_caster(from_type const from, ss_bool_t bCheckAlign = B_CHECK_ALIGN)
00131 : m_from(from)
00132 {
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142 #if !defined(STLSOFT_COMPILER_IS_DMC) || \
00143 __DMC__ >= 0x0833
00144
00145 STLSOFT_STATIC_ASSERT(sizeof(from_type) == sizeof(to_type));
00146
00147 stlsoft_constraint_must_be_pod(from_type);
00148 stlsoft_constraint_must_be_pod(to_type);
00149 # if defined(STLSOFT_CF_TEMPLATE_PARTIAL_SPECIALISATION_SUPPORT)
00150
00152
00153
00154
00155
00156
00157 typedef ss_typename_type_k base_type_traits<from_type>::base_type from_base_type;
00158 typedef ss_typename_type_k base_type_traits<to_type>::base_type to_base_type;
00159
00160 stlsoft_constraint_must_be_pod_or_void(from_base_type);
00161 stlsoft_constraint_must_be_pod_or_void(to_base_type);
00162
00163 if( !base_type_traits<from_type>::is_pointer &&
00164 base_type_traits<to_type>::is_pointer &&
00165 0 != from)
00166 {
00167 ss_size_t to_size = size_of<to_base_type>::value;
00168 union U
00169 {
00170 ss_size_t size_;
00171 from_type from_;
00172 # if !defined(STLSOFT_COMPILER_IS_MSVC) || \
00173 _MSC_VER < 1310
00174 inline U(from_type from)
00175 : from_(from)
00176 {}
00177 } u_(from);
00178 # else
00179 } u_;
00180 u_.from_ = from;
00181 # endif
00182 ss_size_t from_value = u_.size_;
00183
00184 STLSOFT_SUPPRESS_UNUSED(to_size);
00185 STLSOFT_SUPPRESS_UNUSED(from_value);
00186
00187
00188 STLSOFT_MESSAGE_ASSERT( "Misalignment in conversion from non-pointer to pointer", (!bCheckAlign || (0 == to_size) || (0 == ((from_value + to_size) % to_size))));
00189 }
00190 # endif
00191 #else
00192
00193 STLSOFT_ASSERT(sizeof(from_type) == sizeof(to_type));
00194 #endif
00195
00196 STLSOFT_SUPPRESS_UNUSED(bCheckAlign);
00197 }
00199
00202 public:
00204 operator to_type () const
00205 {
00206 return m_to;
00207 }
00209
00212 private:
00213 from_type const m_from;
00214 to_type m_to;
00216
00219 private:
00220 class_type& operator =(class_type const&);
00222 };
00223
00224
00225
00226
00227
00228 #if !defined(STLSOFT_COMPILER_IS_WATCOM)
00229
00246 template< ss_typename_param_k TO
00247 , ss_typename_param_k FROM
00248 >
00249 inline union_caster<TO, FROM, true> union_cast(FROM const from, ss_bool_t bCheckAlign = true)
00250 {
00251 return union_caster<TO, FROM, true>(from, bCheckAlign);
00252 }
00253
00261 template< ss_typename_param_k TO
00262 , ss_typename_param_k FROM
00263 >
00264 inline union_caster<TO, FROM, true> make_union_cast(FROM const from, ss_bool_t bCheckAlign = true)
00265 {
00266 return union_caster<TO, FROM, true>(from, bCheckAlign);
00267 }
00268
00269 #endif
00270
00271
00272
00273
00274
00275 #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
00276
00277 template< ss_typename_param_k TO
00278 , ss_typename_param_k FROM
00279 , ss_bool_t B_CHECK_ALIGN
00280 >
00281 inline ss_bool_t operator < (union_caster<TO, FROM, B_CHECK_ALIGN> const& lhs, TO const& rhs)
00282 {
00283 TO const lhs_ = lhs;
00284
00285 return lhs_ < rhs;
00286 }
00287
00288 template< ss_typename_param_k TO
00289 , ss_typename_param_k FROM
00290 , ss_bool_t B_CHECK_ALIGN
00291 >
00292 inline ss_bool_t operator < (TO const& lhs, union_caster<TO, FROM, B_CHECK_ALIGN> const& rhs)
00293 {
00294 TO const rhs_ = rhs;
00295
00296 return lhs < rhs_;
00297 }
00298
00299 template< ss_typename_param_k TO
00300 , ss_typename_param_k FROM
00301 , ss_bool_t B_CHECK_ALIGN
00302 >
00303 inline ss_bool_t operator < (union_caster<TO, FROM, B_CHECK_ALIGN> const& lhs, union_caster<TO, FROM, B_CHECK_ALIGN> const& rhs)
00304 {
00305 TO const lhs_ = lhs;
00306 TO const rhs_ = rhs;
00307
00308 return lhs_ < rhs_;
00309 }
00310
00311 #endif
00312
00314
00315
00316 #ifdef STLSOFT_UNITTEST
00317 # include "./unittest/union_cast_unittest_.h"
00318 #endif
00319
00320
00321
00322 #ifndef _STLSOFT_NO_NAMESPACE
00323 }
00324 #endif
00325
00326
00327
00328 #endif
00329
00330