Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members

PisaPhnxParv1.h

Go to the documentation of this file.
00001 #ifndef __PISAPHNXPARV1_H__
00002 #define __PISAPHNXPARV1_H__
00003 
00004 #include "PisaPhnxPar.h"
00005 #include <cassert>
00006 #include <cctype>
00007 #include <fstream>
00008 #include <iostream>
00009 #include <vector>
00010 
00011 // Small traits template to give personal names to the types we use
00012 
00013 template<typename T> class Ptraits;
00014 
00015 template<>
00016 class Ptraits<double>
00017 {
00018  public:
00019   static const char* name() { return "floating point (double precision)"; }
00020 };
00021 
00022 template<>
00023 class Ptraits<std::string>
00024 {
00025  public:
00026   static const char* name() { return "character string"; }
00027 };
00028 
00029 template<>
00030 class Ptraits<int>
00031 {
00032  public:
00033   static const char* name() { return "integer"; }
00034 };
00035 
00036 template<>
00037 class Ptraits<bool>
00038 {
00039  public:
00040   static const char* name() { return "boolean"; }
00041 };
00042 
00043 
00044 class PisaPhnxSubParv1 : public PisaPhnxSubPar
00045 {
00046  public:
00047 
00048   PisaPhnxSubParv1() {}
00049   virtual ~PisaPhnxSubParv1() {}
00050 
00051   bool has(const char* parname) const
00052   {
00053     return (count(parname)>0);
00054   }
00055 
00056   size_t count(const char* parname) const
00057   {
00058     return
00059       ( fDoubles.has(parname) ? 1:0 ) +
00060       ( fStrings.has(parname) ? 1:0 ) +
00061       ( fInts.has(parname) ? 1:0 ) +
00062       ( fBooleans.has(parname) ? 1:0 );
00063   }
00064 
00065   void appendBoolean(const char* parname, bool value)
00066   {
00067     setBoolean(parname,fBooleans.size(parname),value);
00068   }
00069 
00070   void appendDouble(const char* parname, double value)
00071   {
00072     setDouble(parname,fDoubles.size(parname),value);
00073   }
00074 
00075   void appendInt(const char* parname, int value)
00076   {
00077     setInt(parname,fInts.size(parname),value);
00078   }
00079 
00080   void appendString(const char* parname, const char* value)
00081   {
00082     setString(parname,fStrings.size(parname),value);
00083   }
00084 
00085   bool getBoolean(const char* parname, size_t index) const
00086   {
00087     return fBooleans.get(parname,index);
00088   }
00089 
00090   void setBoolean(const char* parname, size_t index, bool value)
00091   {
00092     return fBooleans.set(parname,index,value);
00093   }
00094 
00095   double getDouble(const char* parname, size_t index) const
00096   {
00097     return fDoubles.get(parname,index);
00098   }
00099 
00100   void setDouble(const char* parname, size_t index, double value)
00101   {
00102     fDoubles.set(parname,index,value);
00103   }
00104 
00105   int getInt(const char* parname, size_t index) const
00106   {
00107     return fInts.get(parname,index);
00108   }
00109 
00110   void setInt(const char* parname, size_t index, int value)
00111   {
00112     fInts.set(parname,index,value);
00113   }
00114 
00115   const char* getString(const char* parname, size_t index) const
00116   {
00117     return fStrings.get(parname,index).c_str();
00118   }
00119 
00120   void setString(const char* parname, size_t index, const char* value)
00121   {
00122     fStrings.set(parname,index,value);
00123   }
00124 
00125   void print(std::ostream& os) const
00126   {
00127     fDoubles.print(os);
00128     fStrings.print(os);
00129     fInts.print(os);
00130     fBooleans.print(os);
00131   }
00132 
00133   size_t size(const char* parname) const
00134   {
00135     size_t n = 0;
00136     size_t rv = 0;
00137 
00138     if ( (rv=fDoubles.size(parname)) )
00139       {
00140         ++n;    
00141       }
00142     if ( (rv=fStrings.size(parname)) )
00143       {
00144         ++n;
00145       }
00146     if ( (rv=fInts.size(parname)) )
00147       {
00148         ++n;
00149       }
00150     if ( (rv=fBooleans.size(parname)) )
00151       {
00152         ++n;
00153       }
00154     assert(n<=1);
00155     return rv;
00156   }
00157 
00158  private:
00159 
00160   template<typename T>
00161   class P
00162   {
00163   public:
00164 
00165     size_t size(const char* parname) const
00166     {
00167       typename TMAP::const_iterator it = fMap.find(parname);
00168       if (it!=fMap.end())
00169         {
00170           return it->second.size();
00171         }
00172       else
00173         {
00174           return 0;
00175         }
00176     }
00177 
00178     bool has(const char* parname) const
00179     {
00180       return fMap.find(parname) != fMap.end();
00181     }
00182 
00183     T get(const char* parname, size_t index) const
00184     {
00185       typename TMAP::const_iterator it = fMap.find(parname);
00186       if (it!=fMap.end())
00187         {
00188           typename std::map<size_t,T>::const_iterator it2;
00189           it2 = it->second.find(index);
00190           if (it2 != it->second.end())
00191             {
00192               return it2->second;
00193             }
00194         }
00195       std::cerr << __FILE__ << ":" << __LINE__ 
00196                 << " unknow parameter/or index" 
00197                 << parname << "/" << index
00198                 << std::endl;
00199       return T();
00200     }
00201 
00202     void set(const char* parname, size_t index, T value)
00203     {
00204       fMap[parname][index]=value;
00205     }
00206 
00207     void print(std::ostream& os) const
00208     {
00209       if ( fMap.empty() )
00210         {
00211           return;
00212         }
00213       std::string name = Ptraits<T>::name();
00214       os << "Parameters of type: " << name << std::endl;
00215       typename TMAP::const_iterator it;
00216       for ( it = fMap.begin(); it != fMap.end(); ++it ) 
00217         {
00218           typename std::map<size_t,T>::const_iterator it2;
00219           for ( it2 = it->second.begin(); it2 != it->second.end(); ++it2 )
00220             {
00221               os << it->first << "[" << it2->first << "]="
00222                  << it2->second << ",";
00223         }
00224           os << std::endl;
00225         }
00226       os << std::endl;
00227     }
00228 
00229   private:
00230     typedef std::map<std::string, std::map<size_t,T> > TMAP;
00231     TMAP fMap;
00232   };
00233 
00234   P<double> fDoubles;
00235   P<std::string> fStrings;
00236   P<int> fInts;
00237   P<bool> fBooleans;
00238 
00239   ClassDef(PisaPhnxSubParv1,1)
00240 };
00241 
00242 class PisaPhnxParv1 : public PisaPhnxPar
00243 {
00244  public:
00245 
00246   PisaPhnxParv1(const char* file);
00247   virtual ~PisaPhnxParv1() {}
00248 
00249   bool has(const char* subsystem) const;
00250 
00251   void print(std::ostream& os = std::cout) const;
00252 
00253   const PisaPhnxSubParv1* get(const char* subsystem) const;
00254 
00255  private:
00256   void closeParameter(const std::string& subsystem,
00257                       const std::string& parameter,
00258                       const std::string& value);
00259 
00260  private:
00261   typedef std::map<std::string,PisaPhnxSubParv1> TMAP;
00262   TMAP fMap;
00263 
00264   ClassDef(PisaPhnxParv1,1)
00265 };
00266 
00267 #endif