|
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 CAST_IQ_MEMBER_H 00030 #define CAST_IQ_MEMBER_H 00031 00032 #include <limits.h> 00033 00034 #include <typeinfo> 00035 00036 #include "IterationPack_AlgorithmState.hpp" 00037 #include "IterationPack_IterQuantityAccess.hpp" 00038 00039 namespace IterationPack { 00040 00045 class CastIQMemberBase { 00046 public: 00049 const std::string& iq_name() const; 00052 bool exists_in( const AlgorithmState& s ) const; 00053 protected: 00055 CastIQMemberBase( const std::string iq_name ); 00057 void cache_iq_id( const AlgorithmState& s ) const; 00059 void throw_cast_error( const AlgorithmState::iq_id_type iq_id, const std::string& iqa_name ) const; 00061 const std::string iq_name_; 00063 mutable AlgorithmState::iq_id_type iq_id_; 00064 private: 00065 enum { NOT_SET_YET = AlgorithmState::DOES_NOT_EXIST - 1 }; 00066 CastIQMemberBase(); // not defined and not to be called. 00067 }; // end class CastIQMemberBase 00068 00140 template < class T > 00141 class CastIQMember : public CastIQMemberBase { 00142 public: 00144 CastIQMember( const std::string iq_name ); 00154 IterQuantityAccess<T>& operator()( AlgorithmState& s ) const; 00156 const IterQuantityAccess<T>& operator()( const AlgorithmState& s ) const; 00157 private: 00158 CastIQMember(); // not defined and not to be called 00159 }; // end class CastIQMember<T> 00160 00161 // ////////////////////////////////////////// 00162 // Definition of template members 00163 00164 template < class T > 00165 CastIQMember<T>::CastIQMember( const std::string iq_name ) 00166 : CastIQMemberBase(iq_name) 00167 {} 00168 00169 template < class T > 00170 IterQuantityAccess<T>& 00171 CastIQMember<T>::operator()( AlgorithmState& s ) const 00172 { 00173 cache_iq_id(s); 00174 if( iq_id_ == AlgorithmState::DOES_NOT_EXIST ) 00175 throw_cast_error(iq_id_,TypeNameTraits<T>::name()); 00176 IterQuantityAccess<T> 00177 *p = dynamic_cast<IterQuantityAccess<T>*>( &s.iter_quant( iq_id_ ) ); 00178 if( !p ) 00179 throw_cast_error(iq_id_,TypeNameTraits<T>::name()); 00180 return *p; 00181 } 00182 00183 template < class T > 00184 const IterQuantityAccess<T>& 00185 CastIQMember<T>::operator()( const AlgorithmState& s ) const 00186 { 00187 cache_iq_id(s); 00188 if( iq_id_ == AlgorithmState::DOES_NOT_EXIST ) 00189 throw_cast_error(iq_id_,TypeNameTraits<T>::name()); 00190 const IterQuantityAccess<T> 00191 *p = dynamic_cast<const IterQuantityAccess<T>*>( &s.iter_quant( iq_id_ ) ); 00192 if( !p ) 00193 throw_cast_error(iq_id_,TypeNameTraits<T>::name()); 00194 return *p; 00195 } 00196 00197 } // namespace IterationPack 00198 00199 #endif // CAST_IQ_MEMBER_H
1.7.4