Modernized GAlib  3.0.0 current
GAParameter.h
1 /* ----------------------------------------------------------------------------
2  mbwall 14jul95
3  Copyright (c) 1995 Massachusetts Institute of Technology
4  all rights reserved
5 ---------------------------------------------------------------------------- */
6 #pragma once
7 
8 #include <gaconfig.h>
9 #include <gatypes.h>
10 
11 #include <istream>
12 #include <list>
13 #include <ostream>
14 
15 enum class ParType
16 {
17  BOOLEAN,
18  CHAR,
19  STRING,
20  INT,
21  FLOAT,
22  DOUBLE,
23  POINTER
24 };
25 
32 {
33  public:
34  GAParameter(const std::string &fn, const std::string &sn, ParType tp, const void *v);
35  GAParameter(const GAParameter &orig);
36  GAParameter &operator=(const GAParameter &orig)
37  {
38  copy(orig);
39  return *this;
40  }
41  virtual ~GAParameter();
42  void copy(const GAParameter &);
43  std::string fullname() const { return fname; }
44  std::string shrtname() const { return sname; }
45  const void *value() const
46  {
47  return (t == ParType::STRING
48  ? val.sval
49  : (t == ParType::POINTER ? val.pval : &val));
50  }
51  const void *value(const void *v)
52  {
53  setvalue(v);
54  return (t == ParType::STRING
55  ? val.sval
56  : (t == ParType::POINTER ? val.pval : &val));
57  }
58  ParType type() const { return t; }
59 
60  protected:
61  std::string fname;
62  std::string sname;
63  union Value {
64  int ival;
65  char cval;
66  char *sval;
67  float fval;
68  double dval;
69  const void *pval;
70  } val;
71  ParType t;
72  void setvalue(const void *);
73 };
74 
82 class GAParameterList : public std::list<GAParameter>
83 {
84  public:
85  bool get(const std::string &name, void *) const;
86  bool set(const std::string &name, const void *);
87  bool set(const std::string &name, int v) { return set(name, (void *)&v); }
88  bool set(const std::string &name, unsigned int v) { return set(name, (void *)&v); }
89  bool set(const std::string &name, char v) { return set(name, (void *)&v); }
90  bool set(const std::string &name, const char *v) { return set(name, (void *)v); }
91  bool set(const std::string &name, double v);
92  bool add(const std::string &fn, const std::string &sn, ParType, const void *);
93  bool parse(int &argc, char **argv, bool flag = true);
94 
95  bool write(const char *filename) const;
96  bool write(std::ostream &os) const;
97  bool read(const std::string &filename, bool flag = true);
98  bool read(std::istream &is, bool flag = true);
99 };
100 
101 inline std::ostream &operator<<(std::ostream &os, const GAParameterList &plist)
102 {
103  plist.write(os);
104  return os;
105 }
106 inline std::istream &operator>>(std::istream &is, GAParameterList &plist)
107 {
108  plist.read(is);
109  return is;
110 }
Parameter List.
Definition: GAParameter.h:83
Naming the parameters.
Definition: GAParameter.h:32
Definition: GAParameter.h:63