summaryrefslogtreecommitdiffstats
path: root/raul/Configuration.hpp
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2012-03-19 23:34:37 +0000
committerDavid Robillard <d@drobilla.net>2012-03-19 23:34:37 +0000
commit8350acca946fc925d359f2d7c8f342cef8f5b71b (patch)
tree539ca5696e7d8ef510228cb5099b90bb99b450d3 /raul/Configuration.hpp
parent4b50b01f998e575e3c219146c969332a1b0163db (diff)
downloadraul-8350acca946fc925d359f2d7c8f342cef8f5b71b.tar.gz
raul-8350acca946fc925d359f2d7c8f342cef8f5b71b.tar.bz2
raul-8350acca946fc925d359f2d7c8f342cef8f5b71b.zip
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
Diffstat (limited to 'raul/Configuration.hpp')
-rw-r--r--raul/Configuration.hpp112
1 files changed, 98 insertions, 14 deletions
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 <assert.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+
#include <exception>
#include <list>
#include <map>
#include <ostream>
#include <string>
-#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<std::string>& 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