00001 #ifndef __NEW_PARAMETERMAP_H
00002 #define __NEW_PARAMETERMAP_H
00003
00004 #include <map>
00005 #include <string>
00006 #include <sstream>
00007 #include <vector>
00008 #include <fstream>
00009 #include <iostream>
00010 #include "message.h"
00011 #include "convert.h"
00012 #include "any.h"
00013
00014 using namespace std;
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 class parameterMap: public map< string, boost::any > {
00035
00036 public:
00037
00038 template< typename ValueType>
00039 ValueType getItem( const string key ){
00040 if ( !hasKey( key ) ) throw MESSAGE << "Key "<<key<<" not found in parameterMap"<< ENDM_SEVERE;
00041 ValueType answer;
00042 try {
00043 answer = boost::any_cast< ValueType >(find(key)->second);
00044 }
00045 catch ( boost::bad_any_cast ex ){
00046 throw MESSAGE << ex.what() << " when converting value for key = " << key << ENDM_SEVERE;
00047 }
00048 return answer;
00049 }
00050
00051 template< typename ValueType>
00052 ValueType getItem( const string key, const ValueType& def ){
00053 if (find(key)!=end()) return getItem< ValueType >(key); else return def;
00054 }
00055
00056 template< typename ValueType>
00057 void setItem( const string key, const ValueType& val ){
00058 if (find(key)!=end()) erase(key);
00059 insert(make_pair(key,val));
00060 }
00061
00062 bool hasKey( const string key ) const{ return find(key)!=end(); }
00063
00064 };
00065
00066
00067 namespace parameter{
00068
00069
00070 bool getB( parameterMap m, string k, bool d );
00071 int getI( parameterMap m, string k, int d );
00072 string getS( parameterMap m, string k, string d );
00073 double getD( parameterMap m, string k, double d );
00074 vector< bool > getVB( parameterMap m, string k, const vector< bool >& d );
00075 vector< int > getVI( parameterMap m, string k, const vector< int >& d );
00076 vector< double > getV( parameterMap m, string k, const vector< double >& d );
00077 vector< string > getVS( parameterMap m, string k, const vector< string >& d );
00078 vector< vector< double > > getM( parameterMap m, string k, const vector< vector< double > >& d );
00079
00080
00081 bool getB( parameterMap m, string k );
00082 int getI( parameterMap m, string k );
00083 string getS( parameterMap m, string k );
00084 double getD( parameterMap m, string k );
00085 vector< bool > getVB( parameterMap m, string k );
00086 vector< int > getVI( parameterMap m, string k );
00087 vector< double > getV( parameterMap m, string k );
00088 vector< string > getVS( parameterMap m, string k );
00089 vector< vector< double > > getM( parameterMap m, string k );
00090 parameterMap getMap( parameterMap m, string k );
00091
00092
00093 void set( parameterMap& m, string k, bool v );
00094 void set( parameterMap& m, string k, int v );
00095 void set( parameterMap& m, string k, double v );
00096 void set( parameterMap& m, string k, string v );
00097 void set( parameterMap& m, string k, char* v );
00098 void set( parameterMap& m, string k, vector< bool > v );
00099 void set( parameterMap& m, string k, vector< int > v );
00100 void set( parameterMap& m, string k, vector< double > v );
00101 void set( parameterMap& m, string k, vector< string > v );
00102 void set( parameterMap& m, string k, vector< vector< double > > v );
00103 void set( parameterMap& m, string k, parameterMap v );
00104
00105
00106
00107
00108 void ReadParsFromFile(parameterMap& apars, string inFileName);
00109 void ReadParsFromFile(parameterMap& apars, char *inFileName);
00110
00111
00112
00113
00114 void WriteParsToFile(parameterMap& apars, string outFileName);
00115
00116 void PrintPars(parameterMap&);
00117
00119
00120
00122
00123
00124
00125 };
00126
00128 ostream& operator<<( ostream& out_stream, const parameterMap& p );
00129
00131 istream& operator>>( istream& in_stream, parameterMap& p );
00132
00133 #endif