RAUL  0.8.0
Configuration.hpp
1 /* This file is part of Raul.
2  * Copyright (C) 2009 David Robillard <http://drobilla.net>
3  *
4  * Raul is free software; you can redistribute it and/or modify it under the
5  * terms of the GNU General Public License as published by the Free Software
6  * Foundation; either version 2 of the License, or (at your option) any later
7  * version.
8  *
9  * Raul is distributed in the hope that it will be useful, but WITHOUT ANY
10  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16  */
17 
18 #ifndef RAUL_CONFIGURATION_HPP
19 #define RAUL_CONFIGURATION_HPP
20 
21 #include <exception>
22 #include <list>
23 #include <map>
24 #include <ostream>
25 #include <string>
26 
27 #include "raul/Atom.hpp"
28 
29 namespace Raul {
30 
36 public:
37  Configuration(const std::string& shortdesc, const std::string& desc)
38  : _shortdesc(shortdesc)
39  , _desc(desc)
40  , _max_name_length(0)
41  {}
42 
44  const std::string& name,
45  char letter,
46  const std::string& desc,
47  const Atom::Type type,
48  const Atom& value);
49 
50  void print_usage(const std::string& program, std::ostream& os);
51 
52  struct CommandLineError : public std::exception {
53  explicit CommandLineError(const std::string& m) : msg(m) {}
54  ~CommandLineError() throw() {}
55  const char* what() const throw() { return msg.c_str(); }
56  std::string msg;
57  };
58 
59  void parse(int argc, char** argv) throw (CommandLineError);
60 
61  void print(std::ostream& os, const std::string mime_type="text/plain") const;
62 
63  const Raul::Atom& option(const std::string& long_name);
64 
65 private:
66  struct Option {
67  public:
68  Option(const std::string& n, char l, const std::string& d,
69  const Atom::Type type, const Raul::Atom& def)
70  : name(n), letter(l), desc(d), type(type), default_value(def), value(def)
71  {}
72 
73  std::string name;
74  char letter;
75  std::string desc;
76  Atom::Type type;
77  Raul::Atom default_value;
78  Raul::Atom value;
79  };
80 
81  struct OptionNameOrder {
82  inline bool operator()(const Option& a, const Option& b) {
83  return a.name < b.name;
84  }
85  };
86 
87  typedef std::map<std::string, Option> Options;
88  typedef std::map<char, std::string> ShortNames;
89  typedef std::list<std::string> Files;
90 
91  int set_value_from_string(Configuration::Option& option, const std::string& value)
92  throw (Configuration::CommandLineError);
93 
94  const std::string _shortdesc;
95  const std::string _desc;
96  Options _options;
97  ShortNames _short_names;
98  Files _files;
99  size_t _max_name_length;
100 };
101 
102 } // namespace Raul
103 
104 #endif // RAUL_CONFIGURATION_HPP
105 
Raul::Atom::type
Type type() const
Type of this atom.
Definition: Atom.hpp:165
Raul::Configuration::parse
void parse(int argc, char **argv)
Parse command line arguments.
Definition: Configuration.cpp:103
Raul::Configuration::add
Configuration & add(const std::string &name, char letter, const std::string &desc, const Atom::Type type, const Atom &value)
Add a configuration option.
Definition: Configuration.cpp:38
Raul::Configuration
Program configuration (command line options and/or configuration file).
Definition: Configuration.hpp:35
Raul::Atom
A piece of data with some type.
Definition: Atom.hpp:43