NeoPZ
arglib.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2012 by Edson Borin *
3  * edson@ic.unicamp.br *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
20 
79 #ifndef ARGLIB_H
80 #define ARGLIB_H
81 
82 #include <string>
83 #include <sstream>
84 #include <iostream>
85 #include <vector>
86 
87 #include "pzreal.h" // PRETTY_FUNCTION for WIN32 Macro definition
88 
89 using namespace std;
90 
91 namespace clarg
92 {
93 
98  int parse_arguments(int argc, char *argv[]);
99 
104  int dump_arguments_to_file(ostream& os);
105 
110  int parse_arguments_from_file(istream& is);
111 
116  void arguments_descriptions(ostream& os, string prefix, string suffix);
117 
122  void values(ostream& os, bool defined_only=true);
123 
124  class args_container;
125 
127  class arg_base
128  {
129  public:
130 
131  arg_base(const char* name, const char* desc);
132 
133  const string& get_name() const { return arg_name; }
134 
135  const string& get_desc() const { return arg_desc; }
136 
137  /* Return true if the argument was set, false otherwise. */
138  bool was_set() const { return arg_set; }
139 
140  void mark_set(bool m) { arg_set = m; }
141 
142  protected:
143 
150  virtual int parse_parameters (int argc, char* argv []) = 0;
151 
155  virtual void write_parameters (ostream& os, bool def=false) const = 0;
156 
158  bool arg_set;
160  string arg_name;
162  string arg_desc;
163 
164  friend class args_container;
165  };
166 
168  template<class T>
169  class argT : public arg_base
170  {
171  public:
172 
173  argT(const char* arg, const char* desc) :
174  arg_base (arg, desc) {}
175 
176  /* Returns the argument value. */
177  const T& get_value() const { return value; }
178 
179  /* Sets the argument value. */
180  void set_value(const T& v) const { value = v; }
181 
182  protected:
183 
184  /* Argument value */
185  T value;
186  /* Default argument value */
188  };
189 
193  class argString : public argT<string>
194  {
195  public:
196  argString(const char* arg, const char* desc, string v) :
197  argT<string>(arg,desc)
198  {
199  def_value = v;
200  value = def_value;
201  }
202  protected:
203  int parse_parameters (int argc, char* argv [])
204  {
205  if (argc <= 0) return -1;
206  value = string(argv[0]);
207  return 1;
208  }
209  void write_parameters (ostream& os, bool def) const
210  {
211  if (def)
212  os << def_value;
213  else
214  os << value;
215  }
216 
217  };
218 
222  class argInt : public argT<int>
223  {
224  public:
225  argInt(const char* arg, const char* desc, int v = 0) :
226  argT<int>(arg,desc)
227  {
228  def_value = v;
229  value = def_value;
230  }
231  protected:
232  int parse_parameters (int argc, char* argv [])
233  {
234  if (argc <= 0) return -1;
235  try {
236  std::stringstream(argv[0]) >> value;
237  }
238  catch (const exception& ) {return -1;}
239  return 1;
240  }
241  void write_parameters (ostream& os, bool def) const
242  {
243  if (def)
244  os << def_value;
245  else
246  os << value;
247  }
248  };
249 
253  class argDouble : public argT<double>
254  {
255  public:
256  argDouble(const char* arg, const char* desc, double v = 0.0) :
257  argT<double>(arg,desc)
258  {
259  def_value = v;
260  value = def_value;
261  }
262  protected:
263  int parse_parameters (int argc, char* argv [])
264  {
265  if (argc <= 0) return -1;
266  try {
267  std::stringstream(argv[0]) >> value;
268  }
269  catch (const exception& ) {return -1;}
270  return 1;
271  }
272  void write_parameters (ostream& os, bool def) const
273  {
274  if (def)
275  os << def_value;
276  else
277  os << value;
278  }
279  };
280 
284  class argBool : public argT<bool>
285  {
286  public:
287  argBool(const char* arg, const char* desc, bool v = false) :
288  argT<bool>(arg,desc)
289  {
290  def_value = v;
291  value = def_value;
292  }
293  protected:
294  int parse_parameters (int argc, char* argv [])
295  { value = true; return 0; }
296  void write_parameters (ostream& os, bool def) const
297  {}
298  };
299 
300 }
301 
302 #endif
const string & get_name() const
Definition: arglib.h:133
void write_parameters(ostream &os, bool def) const
Definition: arglib.h:241
void set_value(const T &v) const
Definition: arglib.h:180
string arg_name
Definition: arglib.h:160
argInt(const char *arg, const char *desc, int v=0)
Definition: arglib.h:225
string arg_desc
Definition: arglib.h:162
argBool(const char *arg, const char *desc, bool v=false)
Definition: arglib.h:287
void write_parameters(ostream &os, bool def) const
Definition: arglib.h:296
argDouble(const char *arg, const char *desc, double v=0.0)
Definition: arglib.h:256
int parse_parameters(int argc, char *argv [])
Definition: arglib.h:232
argString(const char *arg, const char *desc, string v)
Definition: arglib.h:196
void write_parameters(ostream &os, bool def) const
Definition: arglib.h:209
int parse_parameters(int argc, char *argv [])
Definition: arglib.h:263
int parse_parameters(int argc, char *argv [])
Definition: arglib.h:203
int parse_arguments(int argc, char *argv[])
Definition: arglib.cpp:195
T def_value
Definition: arglib.h:187
bool arg_set
Definition: arglib.h:158
void mark_set(bool m)
Definition: arglib.h:140
void arguments_descriptions(ostream &os, string prefix, string suffix)
Definition: arglib.cpp:189
int dump_arguments_to_file(ostream &of)
Definition: arglib.cpp:204
void write_parameters(ostream &os, bool def) const
Definition: arglib.h:272
argT(const char *arg, const char *desc)
Definition: arglib.h:173
bool was_set() const
Definition: arglib.h:138
int parse_arguments_from_file(istream &is)
const T & get_value() const
Definition: arglib.h:177
Contains the declaration of TPZFlopCounter class and TPZCounter struct.
def values
Definition: rdt.py:119
clarg::argString m("-m", "input matrix file name (text format)", "matrix.txt")
const string & get_desc() const
Definition: arglib.h:135
Definition: arglib.cpp:33
int parse_parameters(int argc, char *argv [])
Definition: arglib.h:294