clockUtils  1.1
BasicVariable.h
Go to the documentation of this file.
1 /*
2  * clockUtils
3  * Copyright (2015) Michael Baer, Daniel Bonrath, All rights reserved.
4  *
5  * This file is part of clockUtils; clockUtils is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library 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 GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
25 #ifndef __CLOCKUTILS_ARGPARSER_BASICVARIABLE_H__
26 #define __CLOCKUTILS_ARGPARSER_BASICVARIABLE_H__
27 
29 
30 #include <sstream>
31 #include <string>
32 #include <vector>
33 
34 namespace clockUtils {
35 namespace argParser {
36 
37  class Parser;
38 
43  friend class Parser;
44 
45  public:
49  BasicVariable(const std::string & longname, const std::string & shortname, const std::string & description, bool required, bool multiple);
50 
54  virtual ~BasicVariable();
55 
59  inline std::string getLongname() const {
60  return _longname;
61  }
62 
66  inline std::string getShortname() const {
67  return _shortname;
68  }
69 
73  virtual bool isBool() const = 0;
74 
78  virtual bool setValue(const std::string & value) = 0;
79 
83  bool isSet() const {
84  return _set;
85  }
86 
90  bool isRequired() const {
91  return _required;
92  }
93 
97  bool canHaveMultiple() const {
98  return _multiple;
99  }
100 
104  virtual void resetToDefault() = 0;
105 
106  protected:
110  bool _set;
111 
112  private:
116  std::string _longname;
117 
121  std::string _shortname;
122 
126  std::string _description;
127 
131  bool _required;
132 
136  bool _multiple;
137  };
138 
142  template<typename T>
143  class Variable : public BasicVariable {
144  public:
148  Variable(const std::string & longname, const std::string & shortname, const std::string & description, T value, bool required, bool multiple) : BasicVariable(longname, shortname, description, required, multiple), _value(value), _values(), _defaultValue(value) {
149  }
150 
154  bool isBool() const override {
155  return std::is_same<T, bool>::value;
156  }
157 
161  bool setValue(const std::string & value) override {
162  std::stringstream ss(value);
163  T val;
164  bool ret = !(ss >> (val)).fail() && ss.eof();
165  if (!_set) {
166  _value = val;
167  } else {
168  _values.push_back(val);
169  }
170  return ret;
171  }
172 
176  friend bool operator==(const T & first, const Variable<T> & second) {
177  return first == second._value;
178  }
179 
180  friend bool operator==(const Variable<T> & first, const T & second) {
181  return first._value == second;
182  }
183 
184  friend bool operator!=(const T & first, const Variable<T> & second) {
185  return first != second._value;
186  }
187 
188  friend bool operator!=(const Variable<T> & first, const T & second) {
189  return first._value != second;
190  }
191 
195  T & operator=(const T & val) {
196  _value = val;
197  return _value;
198  }
199 
203  operator T() const {
204  return _value;
205  }
206 
210  friend std::ostream & operator<<(std::ostream & out, Variable & v) {
211  out << v._value;
212  return out;
213  }
214 
218  size_t count() const {
219  return 1 + _values.size();
220  }
221 
225  T at(size_t idx) const {
226  return (idx == 0) ? _value : _values[idx - 1];
227  }
228 
229  private:
233  T _value;
234 
238  std::vector<T> _values;
239 
244  T _defaultValue;
245 
249  void resetToDefault() override {
250  _value = _defaultValue;
251  _values.clear();
252  _set = false;
253  }
254  };
255 
259  template<>
260  bool CLOCK_ARGPARSER_API Variable<std::string>::setValue(const std::string & value);
261 
265  template<>
266  bool CLOCK_ARGPARSER_API Variable<char>::setValue(const std::string & value);
267 
271  template<>
272  bool CLOCK_ARGPARSER_API Variable<bool>::setValue(const std::string & value);
273 
274 } /* namespace argParser */
275 } /* namespace clockUtils */
276 
277 #endif /* __CLOCKUTILS_ARGPARSER_BASICVARIABLE_H__ */
278 
base class for Variable handling
Definition: BasicVariable.h:42
bool canHaveMultiple() const
returns true, if this variable can have multiple values
Definition: BasicVariable.h:97
friend std::ostream & operator<<(std::ostream &out, Variable &v)
stream operator
size_t count() const
returns amount of values provided if using multiple
#define CLOCK_ARGPARSER_API
bool isSet() const
returns true, if this variable was set via command line
Definition: BasicVariable.h:83
friend bool operator==(const Variable< T > &first, const T &second)
std::string getShortname() const
returns the argument of the variable
Definition: BasicVariable.h:66
T & operator=(const T &val)
assignment operator taking type T
Variable(const std::string &longname, const std::string &shortname, const std::string &description, T value, bool required, bool multiple)
initializes a new variable taking the argument name, the description text and a default value...
specialization of the variable, depending on the template type
bool setValue(const std::string &value) override
sets the parsed value of the argument and returns true, if value was valid, otherwise false ...
bool isRequired() const
returns true, if this variable is required to be set
Definition: BasicVariable.h:90
bool _set
tells whether the variable was set via command line or not
friend bool operator!=(const Variable< T > &first, const T &second)
std::string getLongname() const
returns the argument of the variable
Definition: BasicVariable.h:59
T at(size_t idx) const
returns value at position idx, idx = 0 is _value, idx = 1 is first element of _values, idx >= count results in undefined behaviour
class handling all known variables being able to be parsed and offering parse functionality ...
Definition: Parser.h:43
friend bool operator==(const T &first, const Variable< T > &second)
operator comparing type T and Variable containing type T
friend bool operator!=(const T &first, const Variable< T > &second)
bool isBool() const override
returns true, if T is bool, otherwise false. Default implementation always returns false...