diff options
Diffstat (limited to 'project2/common/options.cpp')
-rw-r--r-- | project2/common/options.cpp | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/project2/common/options.cpp b/project2/common/options.cpp new file mode 100644 index 0000000..a99c78e --- /dev/null +++ b/project2/common/options.cpp @@ -0,0 +1,110 @@ +#include <pch.hpp> +#include "options.h" +#include "environment.h" +#include <boost/foreach.hpp> + +class NamedOption : public Options::Option { + public: + NamedOption(Glib::ustring const & n, Options::TargetPtr t, Glib::ustring const & d) : + name(n), + target(t), + desc(d) + { + } + void consume(const Glib::ustring & n, const Glib::ustring & p, const Glib::ustring & v) const { + if (n == name) { + target->consume(p, v); + } + } + void reset() const { + target->reset(); + } + + const Glib::ustring name; + const Options::TargetPtr target; + const Glib::ustring desc; +}; + +class OptionAlias : public Options::Option { + public: + OptionAlias(const Glib::ustring & a, Options::TargetPtr t) : + alias(a), + target(t) + { + } + void consume(const Glib::ustring & a, const Glib::ustring & p, const Glib::ustring & v) const { + if (a == alias) { + target->consume(p, v); + } + } + void reset() const { + target->reset(); + } + const Glib::ustring alias; + const Options::TargetPtr target; +}; + +Options::Options(Glib::ustring const&) +{ +} + +Options & +Options::operator()(const Glib::ustring & n, TargetPtr t, const Glib::ustring & d) +{ + options.push_back(new NamedOption(n, t, d)); + return *this; +} + +Options & +Options::operator()(const Glib::ustring & a) +{ + for (OptionList::const_reverse_iterator i = options.rbegin(); i != options.rend(); i++) { + if (const NamedOption * no = dynamic_cast<const NamedOption *>(i->get())) { + options.push_back(new OptionAlias(a, no->target)); + break; + } + } + return *this; +} + +Options & +Options::operator()(OptionPtr o) +{ + options.push_back(o); + return *this; +} + +void +Options::reset() const +{ + BOOST_FOREACH(const OptionPtr & o, options) { + o->reset(); + } +} + +void +Options::consume(const Glib::ustring & n, const Glib::ustring & p, const Glib::ustring & v) const +{ + BOOST_FOREACH(const OptionPtr & o, options) { + o->consume(n, p, v); + } +} + +Options::InstanceTarget::InstanceTarget() : + ts(Default) +{ +} + +void +Options::InstanceTarget::consume(const Glib::ustring & p, const Glib::ustring & v) const +{ + if (ts != Platform && p.empty()) { + assign(v); + ts = Global; + } + else if (!p.empty() && p == Environment::getCurrent()->platform()) { + assign(v); + ts = Platform; + } +} + |