|
IterationPack: General framework for building iterative algorithms Version of the Day
|
00001 // @HEADER 00002 // *********************************************************************** 00003 // 00004 // Moocho: Multi-functional Object-Oriented arCHitecture for Optimization 00005 // Copyright (2003) 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 // This library is free software; you can redistribute it and/or modify 00011 // it under the terms of the GNU Lesser General Public License as 00012 // published by the Free Software Foundation; either version 2.1 of the 00013 // License, or (at your option) any later version. 00014 // 00015 // This library is distributed in the hope that it will be useful, but 00016 // WITHOUT ANY WARRANTY; without even the implied warranty of 00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00018 // Lesser General Public License for more details. 00019 // 00020 // You should have received a copy of the GNU Lesser General Public 00021 // License along with this library; if not, write to the Free Software 00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 00023 // USA 00024 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov) 00025 // 00026 // *********************************************************************** 00027 // @HEADER 00028 00029 #ifndef GIP_CAST_IQ_H 00030 #define GIP_CAST_IQ_H 00031 00032 #include <stdexcept> 00033 #include <typeinfo> 00034 00035 #include "IterationPack_AlgorithmState.hpp" 00036 #include "IterationPack_IterQuantityAccess.hpp" 00037 00038 namespace IterationPack { 00039 00053 template<class T> 00054 IterQuantityAccess<T>& cast_iq( 00055 AlgorithmState& state, const std::string& iq_name ); 00056 00058 template<class T> 00059 const IterQuantityAccess<T>& cast_iq( 00060 const AlgorithmState& state, const std::string& iq_name ); 00061 00077 template<class T> 00078 IterQuantityAccess<T>& cast_iq( 00079 AlgorithmState& state, const AlgorithmState::iq_id_type iq_id, const std::string& iq_name ); 00080 00082 template<class T> 00083 const IterQuantityAccess<T>& cast_iq( 00084 const AlgorithmState& state, const AlgorithmState::iq_id_type iq_id, const std::string& iq_name ); 00085 00086 // Helper function 00087 00088 void imp_cast_iq_throw_error( 00089 const std::string& iq_name 00090 ,const std::string& iq_is_type_name 00091 ,const std::string& iq_want_type_name 00092 ); 00093 00094 void imp_cast_iq_throw_error( 00095 const AlgorithmState::iq_id_type iq_id 00096 ,const std::string& iq_name 00097 ,const std::string& iq_is_type_name 00098 ,const std::string& iq_want_type_name 00099 ); 00100 00101 // /////////////////// 00102 // Inline definitions 00103 00104 template<class T> 00105 //inline 00106 IterQuantityAccess<T>& cast_iq( 00107 AlgorithmState& state, const std::string& iq_name ) 00108 { 00109 IterQuantity 00110 &iq = state.iter_quant( iq_name ); 00111 IterQuantityAccess<T> 00112 *p = dynamic_cast<IterQuantityAccess<T>*>( &iq ); 00113 // will throw exception if iq_name does not exist 00114 if( !p ) 00115 imp_cast_iq_throw_error( iq_name, typeName(iq), TypeNameTraits<T>::name() ); 00116 return *p; 00117 } 00118 00119 template<class T> 00120 //inline 00121 const IterQuantityAccess<T>& cast_iq( 00122 const AlgorithmState& state, const std::string& iq_name ) 00123 { 00124 const IterQuantity 00125 &iq = state.iter_quant( iq_name ); 00126 const IterQuantityAccess<T> 00127 *p = dynamic_cast<const IterQuantityAccess<T>*>( &iq ); 00128 // will throw exception if iq_name does not exist 00129 if( !p ) 00130 imp_cast_iq_throw_error( iq_name, typeName(iq), TypeNameTraits<T>::name() ); 00131 return *p; 00132 } 00133 00134 template<class T> 00135 //inline 00136 IterQuantityAccess<T>& cast_iq( 00137 AlgorithmState& state, const AlgorithmState::iq_id_type iq_id, const std::string& iq_name ) 00138 { 00139 IterQuantity 00140 &iq = state.iter_quant( iq_id ); 00141 IterQuantityAccess<T> 00142 *p = dynamic_cast<IterQuantityAccess<T>*>( &iq ); 00143 // will throw exception if iq_name does not exist 00144 if( !p ) 00145 imp_cast_iq_throw_error( iq_id, iq_name, typeName(iq), TypeNameTraits<T>::name() ); 00146 return *p; 00147 } 00148 00149 template<class T> 00150 //inline 00151 const IterQuantityAccess<T>& cast_iq( 00152 const AlgorithmState& state, const AlgorithmState::iq_id_type iq_id, const std::string& iq_name ) 00153 { 00154 IterQuantity 00155 &iq = state.iter_quant( iq_id ); 00156 const IterQuantityAccess<T> 00157 *p = dynamic_cast<const IterQuantityAccess<T>*>( &iq ); 00158 // will throw exception if iq_name does not exist 00159 if( !p ) 00160 imp_cast_iq_throw_error( iq_id, iq_name, typeName(iq), TypeNameTraits<T>::name() ); 00161 return *p; 00162 } 00163 00164 } // namespace IterationPack 00165 00166 #endif // GIP_CAST_IQ_H
1.7.4