libsdr  0.1.0
A simple SDR library
options.hh
1 #ifndef __SDR_OPTIONS_HH__
2 #define __SDR_OPTIONS_HH__
3 
4 #include <string>
5 #include <map>
6 
7 namespace sdr {
8 
10 class Options
11 {
12 public:
14  class Value {
15  protected:
17  typedef enum {
18  NONE,
22  } Type;
23 
24  public:
26  Value();
28  Value(long value);
30  Value(double value);
32  Value(const std::string &value);
34  Value(const Value &other);
36  ~Value();
38  const Value &operator=(const Value &other);
40  bool isNone() const;
42  bool isInteger() const;
44  bool isFloat() const;
46  bool isString() const;
48  long toInteger() const;
50  double toFloat() const;
52  std::string toString() const;
53 
54  protected:
58  union {
59  long as_int;
60  double as_float;
61  char *as_string;
62  } _value;
63  };
64 
66  typedef enum {
67  FLAG,
70  ANY
71  } ArgType;
72 
74  typedef struct {
75  const char *name;
76  char short_name;
78  const char *help;
79  } Definition;
80 
83  static bool parse(const Definition defs[], int argc, char *argv[], Options &options);
85  static void print_help(std::ostream &stream, const Definition defs[]);
86 
87 public:
89  Options();
91  bool has(const char *name);
93  const Value &get(const char *name);
94 
95 protected:
97  std::map<std::string, Value> _options;
98 };
99 
100 }
101 
102 #endif // __SDR_OPTIONS_HH__
std::map< std::string, Value > _options
The table of option names and argument values.
Definition: options.hh:97
bool isInteger() const
Returns true if the value is an integer.
Definition: options.cc:181
Value()
Empty constructor.
Definition: options.cc:130
~Value()
Destructor.
Definition: options.cc:154
Definition: autocast.hh:8
Argument definition.
Definition: options.hh:74
union sdr::Options::Value::@0 _value
Values.
bool isNone() const
Returns true if the value is empty.
Definition: options.cc:176
bool has(const char *name)
Returns true if the specified option was found (long name).
Definition: options.cc:22
static void print_help(std::ostream &stream, const Definition defs[])
Serializes a help text derived from given the definitions into stream.
Definition: options.cc:99
Empty or invalid value.
Definition: options.hh:18
long toInteger() const
Turns the value into an integer.
Definition: options.cc:196
A floating point number (double).
Definition: options.hh:20
bool isFloat() const
Returns true if the value is a floating point number.
Definition: options.cc:186
Any argument (string).
Definition: options.hh:70
std::string toString() const
Turns the value into a string.
Definition: options.cc:206
double toFloat() const
Turns the value into a floating point number.
Definition: options.cc:201
ArgType
Possible argument types.
Definition: options.hh:66
Floating point argument.
Definition: options.hh:69
An integer (long int).
Definition: options.hh:19
const Value & operator=(const Value &other)
Assignment.
Definition: options.cc:165
No argument (flag).
Definition: options.hh:67
char short_name
Argument name (short).
Definition: options.hh:76
Integer argument.
Definition: options.hh:68
An ASCII string.
Definition: options.hh:21
const char * name
Argument name (long).
Definition: options.hh:75
static bool parse(const Definition defs[], int argc, char *argv[], Options &options)
Parse the given arguments (argc, argv) using the definitions defs and stores the results into options...
Definition: options.cc:32
const char * help
Help string.
Definition: options.hh:78
The argument value.
Definition: options.hh:14
Convenience functions for command line arguments.
Definition: options.hh:10
Options()
Empty constructor.
Definition: options.cc:15
bool isString() const
Returns true if the value is a string.
Definition: options.cc:191
ArgType type
Argument type.
Definition: options.hh:77
Type
Value type.
Definition: options.hh:17
Type _type
The type of the value.
Definition: options.hh:56