From 8350acca946fc925d359f2d7c8f342cef8f5b71b Mon Sep 17 00:00:00 2001 From: David Robillard Date: Mon, 19 Mar 2012 23:34:37 +0000 Subject: Use dynamic type IDs for Atoms (for direct compatibility with LV2 atoms). git-svn-id: http://svn.drobilla.net/lad/trunk/raul@4079 a436a847-0d15-0410-975c-d299462d15a1 --- raul/Configuration.hpp | 112 ++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 98 insertions(+), 14 deletions(-) (limited to 'raul/Configuration.hpp') diff --git a/raul/Configuration.hpp b/raul/Configuration.hpp index 9efca42..7983850 100644 --- a/raul/Configuration.hpp +++ b/raul/Configuration.hpp @@ -18,14 +18,17 @@ #ifndef RAUL_CONFIGURATION_HPP #define RAUL_CONFIGURATION_HPP +#include +#include +#include +#include + #include #include #include #include #include -#include "raul/Atom.hpp" - namespace Raul { /** Program configuration (command line options and/or configuration file). @@ -34,20 +37,90 @@ namespace Raul { */ class Configuration { public: - Configuration(Forge* forge, - const std::string& shortdesc, + Configuration(const std::string& shortdesc, const std::string& desc) - : _forge(forge) - , _shortdesc(shortdesc) + : _shortdesc(shortdesc) , _desc(desc) , _max_name_length(0) {} + enum OptionType { + NOTHING, + BOOL, + INT, + STRING + }; + + class Value { + public: + Value() : _type(NOTHING) { _val._string = NULL; } + Value(bool v) : _type(BOOL) { _val._bool = v; } + Value(int v) : _type(INT) { _val._int = v; } + + Value(const char* v) : _type(STRING) { + const size_t len = strlen(v); + _val._string = (char*)calloc(len + 1, 1); + memcpy(_val._string, v, len + 1); + } + + Value(const std::string& v) : _type(STRING) { + _val._string = (char*)calloc(v.length() + 1, 1); + memcpy(_val._string, v.c_str(), v.length() + 1); + } + + Value(const Value& copy) + : _type(copy._type) + , _val(copy._val) + { + if (_type == STRING) { + const size_t len = strlen(copy.get_string()); + _val._string = (char*)malloc(len + 1); + memcpy(_val._string, copy.get_string(), len + 1); + } + } + + Value& operator=(const Value& other) + { + if (_type == STRING) { + free(_val._string); + } + _type = other._type; + _val = other._val; + if (_type == STRING) { + const size_t len = strlen(other.get_string()); + _val._string = (char*)malloc(len + 1); + memcpy(_val._string, other.get_string(), len + 1); + } + return *this; + } + + ~Value() { + if (_type == STRING) { + free(_val._string); + } + } + + inline OptionType type() const { return _type; } + inline bool is_valid() const { return _type != NOTHING; } + + inline int32_t get_int() const { assert(_type == INT); return _val._int; } + inline bool get_bool() const { assert(_type == BOOL); return _val._bool; } + inline const char* get_string() const { assert(_type == STRING); return _val._string; } + + private: + OptionType _type; + union { + bool _bool; + int32_t _int; + char* _string; + } _val; + }; + Configuration& add(const std::string& name, char letter, const std::string& desc, - const Atom::Type type, - const Atom& value); + OptionType type, + const Value& value); void print_usage(const std::string& program, std::ostream& os); @@ -62,23 +135,23 @@ public: void print(std::ostream& os, const std::string mime_type="text/plain") const; - const Raul::Atom& option(const std::string& long_name); + const Value& option(const std::string& long_name); const std::list& files() const { return _files; } private: struct Option { public: Option(const std::string& n, char l, const std::string& d, - const Atom::Type type, const Raul::Atom& def) + const OptionType type, const Value& def) : name(n), letter(l), desc(d), type(type), default_value(def), value(def) {} std::string name; char letter; std::string desc; - Atom::Type type; - Raul::Atom default_value; - Raul::Atom value; + OptionType type; + Value default_value; + Value value; }; struct OptionNameOrder { @@ -94,7 +167,6 @@ private: int set_value_from_string(Configuration::Option& option, const std::string& value) throw (Configuration::CommandLineError); - Forge* _forge; const std::string _shortdesc; const std::string _desc; Options _options; @@ -105,5 +177,17 @@ private: } // namespace Raul +static inline std::ostream& +operator<<(std::ostream& os, const Raul::Configuration::Value& value) +{ + switch (value.type()) { + case Raul::Configuration::NOTHING: return os << "(nil)"; + case Raul::Configuration::INT: return os << value.get_int(); + case Raul::Configuration::BOOL: return os << (value.get_bool() ? "true" : "false"); + case Raul::Configuration::STRING: return os << value.get_string(); + } + return os; +} + #endif // RAUL_CONFIGURATION_HPP -- cgit v1.2.1