summaryrefslogtreecommitdiff
path: root/src/option.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/option.h')
-rw-r--r--src/option.h82
1 files changed, 42 insertions, 40 deletions
diff --git a/src/option.h b/src/option.h
index 8f89d081..772f2b01 100644
--- a/src/option.h
+++ b/src/option.h
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2003-2010, John Wiegley. All rights reserved.
+ * Copyright (c) 2003-2012, John Wiegley. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
@@ -61,16 +61,16 @@ protected:
option_t& operator=(const option_t&);
public:
- T * parent;
- value_t value;
- bool wants_arg;
+ T * parent;
+ string value;
+ bool wants_arg;
option_t(const char * _name, const char _ch = '\0')
: name(_name), name_len(std::strlen(name)), ch(_ch),
handled(false), parent(NULL), value(),
wants_arg(name[name_len - 1] == '_') {
- TRACE_CTOR(option_t, "const char *, const char");
DEBUG("option.names", "Option: " << name);
+ TRACE_CTOR(option_t, "const char *, const char");
}
option_t(const option_t& other)
: name(other.name),
@@ -94,7 +94,8 @@ public:
out << std::right << desc();
if (wants_arg) {
out << " = ";
- value.print(out, 42);
+ out.width(42);
+ out << value;
} else {
out.width(45);
out << ' ';
@@ -123,43 +124,48 @@ public:
return handled;
}
- string& str() {
+ string str() const {
assert(handled);
- if (! value)
+ if (value.empty())
throw_(std::runtime_error, _("No argument provided for %1") << desc());
- return value.as_string_lval();
+ return value;
}
- string str() const {
- assert(handled);
- if (! value)
- throw_(std::runtime_error, _("No argument provided for %1") << desc());
- return value.as_string();
+ void on(const char * whence) {
+ on(string(whence));
}
+ void on(const optional<string>& whence) {
+ handler_thunk(whence);
- void on_only(const optional<string>& whence) {
handled = true;
source = whence;
}
- void on(const optional<string>& whence, const string& str) {
- on_with(whence, string_value(str));
+
+ void on(const char * whence, const string& str) {
+ on(string(whence), str);
}
- virtual void on_with(const optional<string>& whence,
- const value_t& val) {
+ void on(const optional<string>& whence, const string& str) {
+ string before = value;
+
+ handler_thunk(whence, str);
+
+ if (value == before)
+ value = str;
+
handled = true;
- value = val;
source = whence;
}
void off() {
handled = false;
- value = value_t();
+ value = "";
source = none;
}
- virtual void handler_thunk(call_scope_t&) {}
+ virtual void handler_thunk(const optional<string>&) {}
+ virtual void handler_thunk(const optional<string>&, const string&) {}
- virtual void handler(call_scope_t& args) {
+ value_t handler(call_scope_t& args) {
if (wants_arg) {
if (args.size() < 2)
throw_(std::runtime_error, _("No argument provided for %1") << desc());
@@ -167,7 +173,7 @@ public:
throw_(std::runtime_error, _("To many arguments provided for %1") << desc());
else if (! args[0].is_string())
throw_(std::runtime_error, _("Context argument for %1 not a string") << desc());
- on_with(args.get<string>(0), args[1]);
+ on(args.get<string>(0), args.get<string>(1));
}
else if (args.size() < 1) {
throw_(std::runtime_error, _("No argument provided for %1") << desc());
@@ -176,27 +182,18 @@ public:
throw_(std::runtime_error, _("Context argument for %1 not a string") << desc());
}
else {
- on_only(args.get<string>(0));
+ on(args.get<string>(0));
}
-
- handler_thunk(args);
- }
-
- virtual value_t handler_wrapper(call_scope_t& args) {
- handler(args);
return true;
}
virtual value_t operator()(call_scope_t& args) {
if (! args.empty()) {
args.push_front(string_value("?expr"));
- return handler_wrapper(args);
+ return handler(args);
}
else if (wants_arg) {
- if (handled)
- return value;
- else
- return NULL_VALUE;
+ return string_value(value);
}
else {
return handled;
@@ -213,17 +210,18 @@ public:
name ## option_t() : option_t<type>(#name), base
#define DECL1(type, name, vartype, var, value) \
vartype var ; \
- name ## option_t() : option_t<type>(#name), var(value)
+ name ## option_t() : option_t<type>(#name), var value
-#define DO() virtual void handler_thunk(call_scope_t&)
-#define DO_(var) virtual void handler_thunk(call_scope_t& var)
+#define DO() virtual void handler_thunk(const optional<string>& whence)
+#define DO_(var) virtual void handler_thunk(const optional<string>& whence, \
+ const string& var)
#define END(name) name ## handler
#define COPY_OPT(name, other) name ## handler(other.name ## handler)
#define MAKE_OPT_HANDLER(type, x) \
- expr_t::op_t::wrap_functor(bind(&option_t<type>::handler_wrapper, x, _1))
+ expr_t::op_t::wrap_functor(bind(&option_t<type>::handler, x, _1))
#define MAKE_OPT_FUNCTOR(type, x) \
expr_t::op_t::wrap_functor(bind(&option_t<type>::operator(), x, _1))
@@ -284,6 +282,10 @@ inline bool is_eq(const char * p, const char * n) {
} \
END(name)
+#define OTHER(name) \
+ parent->HANDLER(name).parent = parent; \
+ parent->HANDLER(name)
+
bool process_option(const string& whence, const string& name, scope_t& scope,
const char * arg, const string& varname);