00001 /* ///////////////////////////////////////////////////////////////////////// 00002 * File: stlsoft/conversion/sap_cast.hpp 00003 * 00004 * Purpose: A cast operator function that casts between non void pointers of 00005 * the same cv-qualification. 00006 * 00007 * Created: 25th February 2004 00008 * Updated: 10th August 2009 00009 * 00010 * Home: http://stlsoft.org/ 00011 * 00012 * Copyright (c) 2004-2009, Matthew Wilson and Synesis Software 00013 * All rights reserved. 00014 * 00015 * Redistribution and use in source and binary forms, with or without 00016 * modification, are permitted provided that the following conditions are met: 00017 * 00018 * - Redistributions of source code must retain the above copyright notice, this 00019 * list of conditions and the following disclaimer. 00020 * - Redistributions in binary form must reproduce the above copyright notice, 00021 * this list of conditions and the following disclaimer in the documentation 00022 * and/or other materials provided with the distribution. 00023 * - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of 00024 * any contributors may be used to endorse or promote products derived from 00025 * this software without specific prior written permission. 00026 * 00027 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00028 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00029 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00030 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00031 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00032 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00033 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00034 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00035 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00036 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00037 * POSSIBILITY OF SUCH DAMAGE. 00038 * 00039 * ////////////////////////////////////////////////////////////////////// */ 00040 00041 00048 #ifndef STLSOFT_INCL_STLSOFT_CONVERSION_HPP_SAP_CAST 00049 #define STLSOFT_INCL_STLSOFT_CONVERSION_HPP_SAP_CAST 00050 00051 #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION 00052 # define STLSOFT_VER_STLSOFT_CONVERSION_HPP_SAP_CAST_MAJOR 4 00053 # define STLSOFT_VER_STLSOFT_CONVERSION_HPP_SAP_CAST_MINOR 0 00054 # define STLSOFT_VER_STLSOFT_CONVERSION_HPP_SAP_CAST_REVISION 2 00055 # define STLSOFT_VER_STLSOFT_CONVERSION_HPP_SAP_CAST_EDIT 46 00056 #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */ 00057 00058 /* ///////////////////////////////////////////////////////////////////////// 00059 * Includes 00060 */ 00061 00062 #ifndef STLSOFT_INCL_STLSOFT_H_STLSOFT 00063 # include <stlsoft/stlsoft.h> 00064 #endif /* !STLSOFT_INCL_STLSOFT_H_STLSOFT */ 00065 00066 #if ( defined(STLSOFT_COMPILER_IS_GCC) && \ 00067 __GNUC__ < 3) || \ 00068 ( defined(STLSOFT_COMPILER_IS_MSVC) && \ 00069 _MSC_VER < 1200) || \ 00070 defined(STLSOFT_COMPILER_IS_WATCOM) 00071 # define STLSOFT_NO_SAP_CAST 00072 #endif /* compiler */ 00073 00074 #if !defined(STLSOFT_NO_SAP_CAST) && \ 00075 defined(STLSOFT_CF_TEMPLATE_PARTIAL_SPECIALISATION_SUPPORT) 00076 # ifndef STLSOFT_INCL_STLSOFT_UTIL_HPP_CONSTRAINTS 00077 # include <stlsoft/util/constraints.hpp> 00078 # endif /* !STLSOFT_INCL_STLSOFT_UTIL_HPP_CONSTRAINTS */ 00079 # ifndef STLSOFT_INCL_STLSOFT_META_HPP_BASE_TYPE_TRAITS 00080 # include <stlsoft/meta/base_type_traits.hpp> 00081 # endif /* !STLSOFT_INCL_STLSOFT_META_HPP_BASE_TYPE_TRAITS */ 00082 # ifndef STLSOFT_INCL_STLSOFT_META_HPP_SELECT_FIRST_TYPE_IF 00083 # include <stlsoft/meta/select_first_type_if.hpp> 00084 # endif /* !STLSOFT_INCL_STLSOFT_META_HPP_SELECT_FIRST_TYPE_IF */ 00085 #endif /* !STLSOFT_NO_SAP_CAST && STLSOFT_CF_TEMPLATE_PARTIAL_SPECIALISATION_SUPPORT */ 00086 00087 /* ///////////////////////////////////////////////////////////////////////// 00088 * Namespace 00089 */ 00090 00091 #ifndef _STLSOFT_NO_NAMESPACE 00092 namespace stlsoft 00093 { 00094 #endif /* _STLSOFT_NO_NAMESPACE */ 00095 00096 /* ///////////////////////////////////////////////////////////////////////// 00097 * Functions 00098 */ 00099 00131 #if defined(STLSOFT_NO_SAP_CAST) 00132 # define sap_cast reinterpret_cast 00133 #else /* ? STLSOFT_NO_SAP_CAST */ 00134 template< ss_typename_param_k TO 00135 , ss_typename_param_k FROM 00136 > 00137 inline TO sap_cast(FROM from) 00138 { 00139 # if defined(STLSOFT_CF_TEMPLATE_PARTIAL_SPECIALISATION_SUPPORT) && \ 00140 ( !defined(STLSOFT_COMPILER_IS_BORLAND)) 00141 // Both types must be pointer types 00142 STLSOFT_STATIC_ASSERT(0 != base_type_traits<FROM>::is_pointer); 00143 STLSOFT_STATIC_ASSERT(0 != base_type_traits<TO>::is_pointer); 00144 00145 typedef ss_typename_type_k base_type_traits<FROM>::base_type from_base_type; 00146 typedef ss_typename_type_k base_type_traits<TO>::base_type to_base_type; 00147 00148 // The intermediate type might be void *, void const*, void volatile * or 00149 // void const volatile * 00150 typedef ss_typename_type_k select_first_type_if<void const* 00151 , void* 00152 , base_type_traits<FROM>::is_const 00153 >::type non_volatile_type; 00154 typedef ss_typename_type_k select_first_type_if<void const volatile* 00155 , void volatile* 00156 , base_type_traits<FROM>::is_const 00157 >::type volatile_type; 00158 typedef ss_typename_type_k select_first_type_if<volatile_type 00159 , non_volatile_type 00160 , base_type_traits<FROM>::is_volatile 00161 >::type pointer_type; 00162 00163 // "static_cast" to void (const) (volatile) * 00164 pointer_type pv = from; 00165 # else /* STLSOFT_CF_TEMPLATE_PARTIAL_SPECIALISATION_SUPPORT */ 00166 void const volatile *p1 = from; 00167 void *pv = const_cast<void*>(p1); 00168 # endif /* STLSOFT_CF_TEMPLATE_PARTIAL_SPECIALISATION_SUPPORT */ 00169 00170 // static_cast to destination type 00171 return static_cast<TO>(pv); 00172 } 00173 #endif /* STLSOFT_NO_SAP_CAST */ 00174 00176 // Unit-testing 00177 00178 #ifdef STLSOFT_UNITTEST 00179 # include "./unittest/sap_cast_unittest_.h" 00180 #endif /* STLSOFT_UNITTEST */ 00181 00182 /* ////////////////////////////////////////////////////////////////////// */ 00183 00184 #ifndef _STLSOFT_NO_NAMESPACE 00185 } // namespace stlsoft 00186 #endif /* _STLSOFT_NO_NAMESPACE */ 00187 00188 /* ////////////////////////////////////////////////////////////////////// */ 00189 00190 #endif /* !STLSOFT_INCL_STLSOFT_CONVERSION_HPP_SAP_CAST */ 00191 00192 /* ///////////////////////////// end of file //////////////////////////// */