Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2024-09-28 07:03:34

0001 ///////////////////////////////////////////////////////////////////////////
0002 //
0003 //    Copyright 2011
0004 //
0005 //    This file is part of starlight.
0006 //
0007 //    starlight is free software: you can redistribute it and/or modify
0008 //    it under the terms of the GNU General Public License as published by
0009 //    the Free Software Foundation, either version 3 of the License, or
0010 //    (at your option) any later version.
0011 //
0012 //    starlight is distributed in the hope that it will be useful,
0013 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
0014 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0015 //    GNU General Public License for more details.
0016 //
0017 //    You should have received a copy of the GNU General Public License
0018 //    along with starlight. If not, see <http://www.gnu.org/licenses/>.
0019 //
0020 ///////////////////////////////////////////////////////////////////////////
0021 //
0022 // File and Version Information:
0023 // $Rev:: 208                       $: revision of last commit
0024 // $Author:: jnystrand          $: author of last commit
0025 // $Date:: 2015-08-09 21:29:36 +#$: date of last commit
0026 //
0027 // Description:
0028 //
0029 //
0030 //
0031 ///////////////////////////////////////////////////////////////////////////
0032 
0033 
0034 #ifndef INPUTPARSER_H
0035 #define INPUTPARSER_H
0036 
0037 #include <string>
0038 #include <typeinfo>
0039 #include <iostream>
0040 #include <map>
0041 
0042 #include <reportingUtils.h>
0043 
0044 class inputParser
0045 {
0046 public:
0047   
0048   /** Constructor */
0049   inputParser();
0050 
0051   /** Destructor */
0052   ~inputParser();
0053 
0054   /** Parse a file */
0055   int parseFile(std::string filename);
0056 
0057   /** Parse a file */
0058   int parseString(std::string str);
0059   
0060   /** Add parameter to pass */
0061   void addIntParameter(std::string name, int *var, bool required = true);
0062 
0063   /** Add parameter to pass */
0064   void addUintParameter(std::string name, unsigned int *var, bool required = true);
0065 
0066   /** Add parameter to pass */
0067   void addFloatParameter(std::string name, float *var, bool required = true);
0068 
0069   /** Add parameter to pass */
0070   void addDoubleParameter(std::string name, double *var, bool required = true);
0071 
0072   /** Add parameter to pass */
0073   void addBoolParameter(std::string name, bool *var, bool required = true);
0074  
0075   /** Add parameter to pass */
0076   void addStringParameter(std::string name, std::string *var, bool required = true);
0077   
0078   /** Print info */
0079   void printParameterInfo(std::ostream &out = std::cout);
0080   
0081   /** Validate */
0082   bool validateParameters(std::ostream &errOut = std::cerr);
0083   
0084   /** Add a parameter */
0085   template<typename S>
0086   inline void addParameter(S &param);
0087   
0088   /** Add a parameter */
0089   template<typename P>
0090   inline void addParameter(const std::string &name, P *varPtr, bool required = false);
0091 
0092 private:
0093   
0094   template <class T>
0095   class _parameter
0096   {
0097   public:
0098     _parameter(std::string name, T *val, bool required = true, bool found = false) : _name(name), _val(val), _required(required), _found(found){}
0099     
0100     bool operator==(const _parameter &rhs) const { return _name == rhs._name; }
0101     
0102     bool operator<(const _parameter &rhs) const { return _name.c_str()[0] < rhs._name.c_str()[0]; }
0103     
0104     void printParameterInfo(std::ostream &out = std::cout) 
0105     {
0106       out << std::boolalpha << _name << "\t\t";
0107       if(_found)
0108       {
0109     out << *_val << std::endl;
0110       }
0111       else
0112       {
0113     out << "NOT FOUND" << std::endl;
0114       }
0115       out << std::noboolalpha;
0116     }
0117     
0118     
0119     std::string _name;
0120     T *_val;
0121     bool _required;
0122     bool _found;
0123   };
0124   
0125   std::map<std::string, _parameter<int> > _intParameters;
0126   std::map<std::string, _parameter<unsigned int> > _uintParameters;
0127   std::map<std::string, _parameter<float> > _floatParameters;
0128   std::map<std::string, _parameter<double> > _doubleParameters;
0129   std::map<std::string, _parameter<bool> > _boolParameters;
0130   std::map<std::string, _parameter<std::string> > _stringParameters;
0131   
0132 };
0133 
0134 template<typename S>
0135 void inputParser::addParameter(S& param)
0136 {
0137   addParameter(param.name(), param.ptr(), param.required());
0138 
0139 }
0140 
0141 template<typename P>
0142 void inputParser::addParameter(const std::string& name, P* /*varPtr*/, bool /*required*/)
0143 {
0144   printWarn << "Trying to add unknown parameter type with name: " << name;
0145 }
0146 
0147 
0148 template<>
0149 inline void inputParser::addParameter(const std::string& name, int * varPtr, bool required)
0150 {
0151   addIntParameter(name, varPtr, required);
0152 }
0153 
0154 template<>
0155 inline void inputParser::addParameter(const std::string& name, unsigned int * varPtr, bool required)
0156 {
0157   addUintParameter(name, varPtr, required);
0158 }
0159 
0160 template<>
0161 inline void inputParser::addParameter(const std::string& name, float * varPtr, bool required)
0162 {
0163   addFloatParameter(name, varPtr, required);
0164 }
0165 
0166 template<>
0167 inline void inputParser::addParameter(const std::string& name, double * varPtr, bool required)
0168 {
0169   addDoubleParameter(name, varPtr, required);
0170 }
0171 
0172 template<>
0173 inline void inputParser::addParameter(const std::string& name, bool * varPtr, bool required)
0174 {
0175   addBoolParameter(name, varPtr, required);
0176 }
0177 
0178 template<>
0179 inline void inputParser::addParameter(const std::string& name, std::string * varPtr, bool required)
0180 {
0181   addStringParameter(name, varPtr, required);
0182 }
0183 
0184 #endif // INPUTPARSER_H