|
|
|
|
|
|
|
|
|
|
|
|||||||
00001 /* 00002 * File: stlsoft_sap_cast.h 00003 * 00004 * Purpose: sap_cast - a dangerous weapon in the hands of the unwary. 00005 * 00006 * Created: 25th February 2004 00007 * Updated: 11th September 2004 00008 * 00009 * Home: http://stlsoft.org/ 00010 * 00011 * Copyright (c) 2004, Matthew Wilson and Synesis Software 00012 * All rights reserved. 00013 * 00014 * Redistribution and use in source and binary forms, with or without 00015 * modification, are permitted provided that the following conditions are met: 00016 * 00017 * - Redistributions of source code must retain the above copyright notice, this 00018 * list of conditions and the following disclaimer. 00019 * - Redistributions in binary form must reproduce the above copyright notice, 00020 * this list of conditions and the following disclaimer in the documentation 00021 * and/or other materials provided with the distribution. 00022 * - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of 00023 * any contributors may be used to endorse or promote products derived from 00024 * this software without specific prior written permission. 00025 * 00026 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00027 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00028 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00029 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00030 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00031 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00032 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00033 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00034 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00035 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00036 * POSSIBILITY OF SUCH DAMAGE. 00037 * 00038 * 00039 00040 00044 00045 #ifndef STLSOFT_INCL_H_STLSOFT_SAP_CAST 00046 #define STLSOFT_INCL_H_STLSOFT_SAP_CAST 00047 00048 #ifndef __STLSOFT_DOCUMENTATION_SKIP_SECTION 00049 # define STLSOFT_VER_H_STLSOFT_SAP_CAST_MAJOR 2 00050 # define STLSOFT_VER_H_STLSOFT_SAP_CAST_MINOR 0 00051 # define STLSOFT_VER_H_STLSOFT_SAP_CAST_REVISION 1 00052 # define STLSOFT_VER_H_STLSOFT_SAP_CAST_EDIT 11 00053 #endif /* !__STLSOFT_DOCUMENTATION_SKIP_SECTION */ 00054 00055 /* 00056 * Includes 00057 */ 00058 00059 #ifndef STLSOFT_INCL_H_STLSOFT 00060 # include "stlsoft.h" // Include the STLSoft root header 00061 #endif /* !STLSOFT_INCL_H_STLSOFT */ 00062 #ifndef STLSOFT_INCL_H_CONSTRAINTS 00063 # include "stlsoft_constraints.h" // stlsoft_constraint_must_be_pod 00064 #endif /* !STLSOFT_INCL_H_CONSTRAINTS */ 00065 #ifndef STLSOFT_INCL_H_TYPE_TRAITS 00066 # include "stlsoft_type_traits.h" // 00067 #endif /* !STLSOFT_INCL_H_TYPE_TRAITS */ 00068 00069 /* 00070 * Namespace 00071 */ 00072 00073 #ifndef _STLSOFT_NO_NAMESPACE 00074 namespace stlsoft 00075 { 00076 #endif /* _STLSOFT_NO_NAMESPACE */ 00077 00078 /* 00079 * Functions 00080 */ 00081 00082 template< ss_typename_param_k TO 00083 , ss_typename_param_k FROM 00084 > 00085 inline TO sap_cast(FROM from) 00086 { 00087 #if defined(__STLSOFT_CF_TEMPLATE_PARTIAL_SPECIALISATION_SUPPORT) && \ 00088 ( !defined(__STLSOFT_COMPILER_IS_BORLAND) || \ 00089 __BORLANDC__ >= 0x0564) 00090 // Both types must be pointer types 00091 stlsoft_static_assert(0 != base_type_traits<FROM>::is_pointer); 00092 stlsoft_static_assert(0 != base_type_traits<TO>::is_pointer); 00093 00094 typedef ss_typename_type_k base_type_traits<FROM>::base_type from_base_type; 00095 typedef ss_typename_type_k base_type_traits<TO>::base_type to_base_type; 00096 00097 // The intermediate type might be void *, void const *, void volatile * or 00098 // void const volatile * 00099 typedef ss_typename_type_k select_first_type< void const * 00100 , void * 00101 , base_type_traits<FROM>::is_const 00102 >::type non_volatile_type; 00103 typedef ss_typename_type_k select_first_type< void const volatile * 00104 , void volatile * 00105 , base_type_traits<FROM>::is_const 00106 >::type volatile_type; 00107 typedef ss_typename_type_k select_first_type< volatile_type 00108 , non_volatile_type 00109 , base_type_traits<FROM>::is_volatile 00110 >::type pointer_type; 00111 00112 // "static_cast" to void (const) (volatile) * 00113 pointer_type pv = from; 00114 #else /* __STLSOFT_CF_TEMPLATE_PARTIAL_SPECIALISATION_SUPPORT */ 00115 void const volatile *p1 = from; 00116 void *pv = const_cast<void*>(p1); 00117 #endif /* __STLSOFT_CF_TEMPLATE_PARTIAL_SPECIALISATION_SUPPORT */ 00118 00119 // static_cast to destination type 00120 return static_cast<TO>(pv); 00121 } 00122 00124 // Unit-testing 00125 00126 #ifdef STLSOFT_UNITTEST 00127 00128 namespace unittest 00129 { 00130 ss_bool_t test_stlsoft_sap_cast(unittest_reporter *r) 00131 { 00132 ss_bool_t bSuccess = true; 00133 00134 unittest_initialiser init(r, "STLSoft", "sap_cast", __FILE__); 00135 00136 int i; 00137 void *pv = &i; 00138 short *ps = reinterpret_cast<short*>(&i); 00139 00140 if(sap_cast<short*>(pv) != ps) 00141 { 00142 r->report("sap_cast failed ", __LINE__); 00143 bSuccess = false; 00144 } 00145 00146 return bSuccess; 00147 } 00148 00149 unittest_registrar unittest_stlsoft_sap_cast(test_stlsoft_sap_cast); 00150 00151 } // namespace unittest 00152 00153 #endif /* STLSOFT_UNITTEST */ 00154 00155 /* 00156 00157 #ifndef _STLSOFT_NO_NAMESPACE 00158 } // namespace stlsoft 00159 #endif /* _STLSOFT_NO_NAMESPACE */ 00160 00161 /* 00162 00163 #endif /* !STLSOFT_INCL_H_STLSOFT_SAP_CAST */ 00164 00165 /*
|
|
| STLSoft Libraries documentation © Synesis Software Pty Ltd, 2001-2004 |