• Main Page
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List

Configuration.hpp

00001 /* This file is part of Raul.
00002  * Copyright (C) 2009 David Robillard <http://drobilla.net>
00003  *
00004  * Raul is free software; you can redistribute it and/or modify it under the
00005  * terms of the GNU General Public License as published by the Free Software
00006  * Foundation; either version 2 of the License, or (at your option) any later
00007  * version.
00008  *
00009  * Raul is distributed in the hope that it will be useful, but WITHOUT ANY
00010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00011  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for details.
00012  *
00013  * You should have received a copy of the GNU General Public License along
00014  * with this program; if not, write to the Free Software Foundation, Inc.,
00015  * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00016  */
00017 
00018 #ifndef RAUL_CONFIGURATION_HPP
00019 #define RAUL_CONFIGURATION_HPP
00020 
00021 #include <exception>
00022 #include <list>
00023 #include <map>
00024 #include <ostream>
00025 #include <string>
00026 
00027 #include "raul/Atom.hpp"
00028 
00029 namespace Raul {
00030 
00035 class Configuration {
00036 public:
00037     Configuration(const std::string& shortdesc, const std::string& desc)
00038         : _shortdesc(shortdesc)
00039         , _desc(desc)
00040         , _max_name_length(0)
00041     {}
00042 
00043     Configuration& add(
00044             const std::string& name,
00045             char               letter,
00046             const std::string& desc,
00047             const Atom::Type   type,
00048             const Atom&        value);
00049 
00050     void print_usage(const std::string& program, std::ostream& os);
00051 
00052     struct CommandLineError : public std::exception {
00053         explicit CommandLineError(const std::string& m) : msg(m) {}
00054         ~CommandLineError() throw() {}
00055         const char* what() const throw() { return msg.c_str(); }
00056         std::string msg;
00057     };
00058 
00059     void parse(int argc, char** argv) throw (CommandLineError);
00060 
00061     void print(std::ostream& os, const std::string mime_type="text/plain") const;
00062 
00063     const Raul::Atom& option(const std::string& long_name);
00064 
00065 private:
00066     struct Option {
00067     public:
00068         Option(const std::string& n, char l, const std::string& d,
00069                const Atom::Type type, const Raul::Atom& def)
00070             : name(n), letter(l), desc(d), type(type), default_value(def), value(def)
00071         {}
00072 
00073         std::string name;
00074         char        letter;
00075         std::string desc;
00076         Atom::Type  type;
00077         Raul::Atom  default_value;
00078         Raul::Atom  value;
00079     };
00080 
00081     struct OptionNameOrder {
00082         inline bool operator()(const Option& a, const Option& b) {
00083             return a.name < b.name;
00084         }
00085     };
00086 
00087     typedef std::map<std::string, Option> Options;
00088     typedef std::map<char, std::string>   ShortNames;
00089     typedef std::list<std::string>        Files;
00090 
00091     int set_value_from_string(Configuration::Option& option, const std::string& value)
00092             throw (Configuration::CommandLineError);
00093 
00094     const std::string _shortdesc;
00095     const std::string _desc;
00096     Options           _options;
00097     ShortNames        _short_names;
00098     Files             _files;
00099     size_t            _max_name_length;
00100 };
00101 
00102 } // namespace Raul
00103 
00104 #endif // RAUL_CONFIGURATION_HPP
00105 

Generated on Tue Jan 11 2011 18:26:17 for RAUL by  doxygen 1.7.1