00001 /* ///////////////////////////////////////////////////////////////////////// 00002 * File: stlsoft/conversion/truncation_cast.hpp 00003 * 00004 * Purpose: Runtime checking for numeric conversions. 00005 * 00006 * Created: 10th August 2006 00007 * Updated: 10th August 2009 00008 * 00009 * Home: http://stlsoft.org/ 00010 * 00011 * Copyright (c) 2006-2009, 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 00047 #ifndef STLSOFT_INCL_STLSOFT_CONVERSION_HPP_TRUNCATION_CAST 00048 #define STLSOFT_INCL_STLSOFT_CONVERSION_HPP_TRUNCATION_CAST 00049 00050 #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION 00051 # define STLSOFT_VER_STLSOFT_CONVERSION_HPP_TRUNCATION_CAST_MAJOR 1 00052 # define STLSOFT_VER_STLSOFT_CONVERSION_HPP_TRUNCATION_CAST_MINOR 0 00053 # define STLSOFT_VER_STLSOFT_CONVERSION_HPP_TRUNCATION_CAST_REVISION 3 00054 # define STLSOFT_VER_STLSOFT_CONVERSION_HPP_TRUNCATION_CAST_EDIT 41 00055 #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */ 00056 00057 /* ///////////////////////////////////////////////////////////////////////// 00058 * Auto-generation and compatibility 00059 */ 00060 00061 /* 00062 */ 00063 00064 /* ///////////////////////////////////////////////////////////////////////// 00065 * Includes 00066 */ 00067 00068 #ifndef STLSOFT_INCL_STLSOFT_H_STLSOFT 00069 # include <stlsoft/stlsoft.h> 00070 #endif /* !STLSOFT_INCL_STLSOFT_H_STLSOFT */ 00071 00072 #ifndef STLSOFT_INCL_STLSOFT_CONVERSION_HPP_TRUNCATION_TEST 00073 # include <stlsoft/conversion/truncation_test.hpp> 00074 #endif /* !STLSOFT_INCL_STLSOFT_CONVERSION_HPP_TRUNCATION_TEST */ 00075 00076 #ifndef STLSOFT_INCL_STDEXCEPT 00077 # define STLSOFT_INCL_STDEXCEPT 00078 # include <stdexcept> 00079 #endif /* !STLSOFT_INCL_STDEXCEPT */ 00080 00081 /* ///////////////////////////////////////////////////////////////////////// 00082 * Namespace 00083 */ 00084 00085 #ifndef _STLSOFT_NO_NAMESPACE 00086 namespace stlsoft 00087 { 00088 #endif /* _STLSOFT_NO_NAMESPACE */ 00089 00090 /* ///////////////////////////////////////////////////////////////////////// 00091 * Functions 00092 */ 00093 00094 template< ss_typename_param_k TO 00095 , ss_typename_param_k FROM 00096 > 00097 inline TO truncation_cast(FROM from) 00098 { 00099 if(!truncation_test<TO>(from)) 00100 { 00101 throw std::out_of_range("Cast would result in truncation"); 00102 } 00103 00104 return static_cast<TO>(from); 00105 } 00106 00108 // Unit-testing 00109 00110 #ifdef STLSOFT_UNITTEST 00111 # include "./unittest/truncation_cast_unittest_.h" 00112 #endif /* STLSOFT_UNITTEST */ 00113 00114 /* ////////////////////////////////////////////////////////////////////// */ 00115 00116 #ifndef _STLSOFT_NO_NAMESPACE 00117 } // namespace stlsoft 00118 #endif /* _STLSOFT_NO_NAMESPACE */ 00119 00120 /* ////////////////////////////////////////////////////////////////////// */ 00121 00122 #endif /* !STLSOFT_INCL_STLSOFT_CONVERSION_HPP_TRUNCATION_CAST */ 00123 00124 /* ///////////////////////////// end of file //////////////////////////// */