|
Teuchos - Trilinos Tools Package Version of the Day
|
00001 // @HEADER 00002 // *********************************************************************** 00003 // 00004 // Teuchos: Common Tools Package 00005 // Copyright (2004) Sandia Corporation 00006 // 00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive 00008 // license for use of this work by or on behalf of the U.S. Government. 00009 // 00010 // Redistribution and use in source and binary forms, with or without 00011 // modification, are permitted provided that the following conditions are 00012 // met: 00013 // 00014 // 1. Redistributions of source code must retain the above copyright 00015 // notice, this list of conditions and the following disclaimer. 00016 // 00017 // 2. Redistributions in binary form must reproduce the above copyright 00018 // notice, this list of conditions and the following disclaimer in the 00019 // documentation and/or other materials provided with the distribution. 00020 // 00021 // 3. Neither the name of the Corporation nor the names of the 00022 // contributors may be used to endorse or promote products derived from 00023 // this software without specific prior written permission. 00024 // 00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY 00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE 00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00036 // 00037 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 00038 // 00039 // *********************************************************************** 00040 // @HEADER 00041 00042 #ifndef TEUCHOS_AS_HPP 00043 #define TEUCHOS_AS_HPP 00044 00045 #include "Teuchos_Assert.hpp" 00046 00047 #ifdef HAVE_TEUCHOS_QD 00048 #include <qd/qd_real.h> 00049 #include <qd/dd_real.h> 00050 #endif 00051 00052 namespace Teuchos { 00053 00054 00083 template<class TypeTo, class TypeFrom> 00084 class ValueTypeConversionTraits { 00085 public: 00086 static TypeTo convert( const TypeFrom t ) 00087 { 00088 return t; 00089 // This default implementation is just an implicit conversion and will 00090 // generate compiler warning on dangerous conversions. 00091 } 00092 static TypeTo safeConvert( const TypeFrom t ) 00093 { 00094 return t; 00095 // This default implementation is just an implicit conversion and will 00096 // generate compiler warning on dangerous conversions. No runtime 00097 // checking can be done by default; only specializations can define 00098 // meaningful and portable runtime checks of conversions. 00099 } 00100 }; 00101 00133 template<class TypeTo, class TypeFrom> 00134 inline TypeTo as( const TypeFrom& t ) 00135 { 00136 #ifdef TEUCHOS_DEBUG 00137 return ValueTypeConversionTraits<TypeTo,TypeFrom>::safeConvert(t); 00138 #else 00139 return ValueTypeConversionTraits<TypeTo,TypeFrom>::convert(t); 00140 #endif 00141 } 00142 00143 00173 template<class TypeTo, class TypeFrom> 00174 inline TypeTo asSafe( const TypeFrom& t ) 00175 { 00176 return ValueTypeConversionTraits<TypeTo,TypeFrom>::safeConvert(t); 00177 } 00178 00179 00180 template <class TypeTo> 00181 class asFunc { 00182 public: 00183 asFunc() {} 00184 00185 template <class TypeFrom> 00186 inline TypeTo operator()(const TypeFrom &t) { 00187 return as<TypeTo>(t); 00188 } 00189 }; 00190 00191 00192 // 00193 // Standard specializations of ValueTypeConversionTraits 00194 // 00195 00196 00198 template<int N> 00199 class ValueTypeConversionTraits<std::string, char[N]> { 00200 public: 00201 static std::string convert( const char t[] ) 00202 { return std::string(t); } 00203 static std::string safeConvert( const char t[] ) 00204 { return std::string(t); } 00205 }; 00206 00207 #ifdef HAVE_TEUCHOS_QD 00208 00210 template <> 00211 class ValueTypeConversionTraits<double, qd_real> { 00212 public: 00213 inline static double convert( const qd_real t ) 00214 { return to_double(t); } 00215 inline static double safeConvert( const qd_real t ) 00216 { return to_double(t); } 00217 }; 00218 00220 template <> 00221 class ValueTypeConversionTraits<float, qd_real> { 00222 public: 00223 inline static float convert( const qd_real t ) 00224 { return (float)to_double(t); } 00225 inline static float safeConvert( const qd_real t ) 00226 { return (float)to_double(t); } 00227 }; 00228 00230 template <> 00231 class ValueTypeConversionTraits<int, qd_real> { 00232 public: 00233 inline static int convert( const qd_real t ) 00234 { return to_int(t); } 00235 inline static int safeConvert( const qd_real t ) 00236 { return to_int(t); } 00237 }; 00238 00240 template <> 00241 class ValueTypeConversionTraits<dd_real, qd_real> { 00242 public: 00243 inline static dd_real convert( const qd_real t ) 00244 { return to_dd_real(t); } 00245 inline static dd_real safeConvert( const qd_real t ) 00246 { return to_dd_real(t); } 00247 }; 00248 00250 template <> 00251 class ValueTypeConversionTraits<double, dd_real> { 00252 public: 00253 inline static double convert( const dd_real t ) 00254 { return to_double(t); } 00255 inline static double safeConvert( const dd_real t ) 00256 { return to_double(t); } 00257 }; 00258 00260 template <> 00261 class ValueTypeConversionTraits<float, dd_real> { 00262 public: 00263 inline static float convert( const dd_real t ) 00264 { return (float)to_double(t); } 00265 inline static float safeConvert( const dd_real t ) 00266 { return (float)to_double(t); } 00267 }; 00268 00270 template <> 00271 class ValueTypeConversionTraits<int, dd_real> { 00272 public: 00273 inline static int convert( const dd_real t ) 00274 { return to_int(t); } 00275 inline static int safeConvert( const dd_real t ) 00276 { return to_int(t); } 00277 }; 00278 00279 #endif 00280 00281 // ToDo: Add more specializations as needed! 00282 00283 00284 } // end namespace Teuchos 00285 00286 00287 #endif // TEUCHOS_AS_HPP
1.7.4