00001 /**************************************************************************************** 00002 descriptor.h 00003 ---------------- 00004 copyright : (C) 2006 Jean-Luc Perret - Pierre Mahé 00005 email : jean-luc.perret@unine.ch - pierre.mahe@ensmp.fr 00006 ***************************************************************************************/ 00007 00008 /**************************************************************************************** 00009 * * 00010 * This program is free software; you can redistribute it and/or * 00011 * modify it under the terms of the GNU Lesser General Public * 00012 * License as published by the Free Software Foundation; either * 00013 * version 2.1 of the License, or (at your option) any later version. * 00014 * * 00015 * This program is distributed in the hope that it will be useful, * 00016 * but 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * 00023 * * 00024 ****************************************************************************************/ 00025 00026 00027 00028 #ifndef DESCRIPTOR_H 00029 #define DESCRIPTOR_H 00030 00031 #include<iostream> 00032 #include<sstream> 00033 using std::string; 00034 using std::endl; 00035 using std::stringstream; 00036 00037 #include <constant.h> 00038 #include <cerror.h> 00039 00071 template <class T> class Descriptor { 00072 public: 00074 00075 00077 Descriptor(); 00078 00081 Descriptor(T avalue); 00082 00085 Descriptor(string aLabel, T aValue, string aUnit, string aComment); 00086 00087 //~Descriptor(){} 00089 00090 00092 00093 00095 void setValue( T ); 00096 00099 void setLabel( string aLabel ){ label = aLabel; } 00100 00103 void setUnit( string aUnit ){ unit = aUnit; } 00104 00107 void setComment( string aComment){ comment = aComment; } 00108 00111 void setEmpty(){ empty = true; } 00112 00115 T getValue( bool silentError = false ) throw( CError ); 00116 00119 string getLabel(){ return( label ); } 00120 00123 string getUnit(){ return( unit ); } 00124 00127 string getComment(){ return( comment ); } 00128 00131 bool isEmpty(){ return(empty); } 00133 00135 00136 00138 string toString(); 00139 00142 string toStringShort(); 00143 00146 void describe(); 00147 00150 void describeShort(); 00151 00153 00154 private: 00155 00158 string label; 00159 00162 T value; 00163 00166 string unit; 00167 00170 string comment; 00171 00174 bool empty; 00175 00176 }; 00177 00178 00179 00180 00181 00182 template <class T> 00183 Descriptor<T>::Descriptor(){ 00184 setLabel( "no name" ); 00185 setUnit( "no unit" ); 00186 setComment( "no comment" ); 00187 empty = true; 00188 } 00189 00190 template <class T> 00191 Descriptor<T>::Descriptor( T avalue ){ 00192 //Descriptor(); 00193 setLabel( "no name" ); 00194 setUnit( "no unit" ); 00195 setComment( "no comment" ); 00196 empty = true; 00197 00198 setValue( avalue ); 00199 } 00200 00201 template <class T> 00202 Descriptor<T>::Descriptor( string aLabel, T aValue, string aUnit, string aComment ){ 00203 setLabel( aLabel ); 00204 setUnit( aUnit ); 00205 setComment( aComment ); 00206 setValue( aValue ); 00207 } 00208 00209 00210 template <class T> 00211 void Descriptor<T>::setValue( T avalue ){ 00212 value = avalue; 00213 empty = false; 00214 } 00215 00216 template <class T> 00217 T Descriptor<T>::getValue( bool silentError ) throw( CError ){ 00218 if( !isEmpty() ){ 00219 return value; 00220 }else{ 00221 // value is emtpy, so ERROR_NA exception is raised 00222 CError e( ERRORNA, getLabel() + " is empty" ); 00223 if( silentError == false){ 00224 e.describe(); 00225 } 00226 throw( e ); 00227 } 00228 } 00229 00230 template <class T> 00231 void Descriptor<T>::describe(){ 00232 cout << toString() << endl; 00233 } 00234 00235 template <class T> 00236 void Descriptor<T>::describeShort(){ 00237 cout << toStringShort() << endl; 00238 } 00239 00240 template <class T> 00241 string Descriptor<T>::toString(){ 00242 stringstream out; 00243 if( empty == false ){ 00244 out << getComment() << ": " << getLabel() << " = " << getValue() << " (" << getUnit() << ") "; 00245 }else{ 00246 out << getComment() << ": " << getLabel() << " = " << "NA" << " (" << getUnit() << ") "; 00247 } 00248 return( out.str() ); 00249 } 00250 00251 template <class T> 00252 string Descriptor<T>::toStringShort(){ 00253 stringstream out; 00254 if( empty == false ){ 00255 out << getLabel() << " = " << getValue() << " (" << getUnit() << ") "; 00256 }else{ 00257 out << getLabel() << " = " << "NA" << " (" << getUnit() << ") "; 00258 } 00259 return( out.str() ); 00260 } 00261 00262 #endif