File indexing completed on 2024-09-22 04:53:11

0001 /*
0002  * SPDX-FileCopyrightText: 2008 Trevor Pounds
0003  * SPDX-License-Identifier: MIT
0004  */
0005 
0006 #ifndef __MOCKITOPP_HORRIBLE_CAST_HPP__
0007 #define __MOCKITOPP_HORRIBLE_CAST_HPP__
0008 
0009 #include <mockitopp/detail/util/cxx0x_static_assert.hpp>
0010 
0011 namespace mockitopp
0012 {
0013    namespace detail
0014    {
0015       /**
0016        * A potentially unsafe cast using a union of two possibly
0017        * incompatible data types that can be used to completely
0018        * subvert the compiler's cast system...USE WITH CAUTION!
0019        *
0020        * @author Trevor Pounds
0021        */
0022       template <typename T, typename F>
0023       T horrible_cast(F from)
0024       {
0025          /**
0026           * Abort compilation to avoid casting potentially
0027           * incompatible types due obvious differences in size.
0028           */
0029          //mockitopp_static_assert(sizeof(T) == sizeof(F));
0030 
0031          union
0032          {
0033             F from;
0034             T to;
0035          } u;
0036          u.from = from;
0037          return u.to;
0038       }
0039    } // namespace detail
0040 } // namespace mockitopp
0041 
0042 #endif //__MOCKITOPP_HORRIBLE_CAST_HPP__