From b2801c3097b278c50c7ef22664e51acd2ef0c8d5 Mon Sep 17 00:00:00 2001 From: randomdan Date: Thu, 16 Feb 2012 11:15:23 +0000 Subject: Turn script values inside out to avoid throwing internally --- project2/common/scripts.cpp | 23 ++++++++++------------- project2/common/scripts.h | 4 ++-- project2/xml/xmlScriptParser.cpp | 12 +++++++----- project2/xml/xmlScriptParser.h | 2 +- 4 files changed, 20 insertions(+), 21 deletions(-) diff --git a/project2/common/scripts.cpp b/project2/common/scripts.cpp index 4e9f18b..997f558 100644 --- a/project2/common/scripts.cpp +++ b/project2/common/scripts.cpp @@ -20,24 +20,21 @@ ScriptNode::variable(const Glib::ustring & n, const VariableType & def) const VariableType ScriptNode::value(const Glib::ustring & n, const VariableType & def) const { - try { - return value(n); - } - catch (const ValueNotFound &) { - return def; + VariableType r; + if (applyValue(n, r)) { + return r; } + return def; } -bool -ScriptNode::applyValue(const Glib::ustring & n, VariableType & target) const +VariableType +ScriptNode::value(const Glib::ustring & n) const { - try { - target = value(n); - return true; - } - catch (const ValueNotFound &) { - return false; + VariableType r; + if (applyValue(n, r)) { + return r; } + throw ValueNotFound(n); } void diff --git a/project2/common/scripts.h b/project2/common/scripts.h index b7fdec9..405056e 100644 --- a/project2/common/scripts.h +++ b/project2/common/scripts.h @@ -38,11 +38,11 @@ class ScriptNode : public IntrusivePtrBase { virtual ScriptNodes childrenIn(const Glib::ustring & sub) const = 0; virtual bool valueExists(const Glib::ustring & name) const = 0; - bool applyValue(const Glib::ustring & name, VariableType & target) const; + virtual bool applyValue(const Glib::ustring & name, VariableType & target) const = 0; virtual VariableImpl * variable(const boost::optional & defaultSource = boost::optional()) const = 0; virtual VariableImpl * variable(const Glib::ustring & name) const = 0; VariableImpl * variable(const Glib::ustring & name, const VariableType & def) const; - virtual VariableType value(const Glib::ustring & name) const = 0; + VariableType value(const Glib::ustring & name) const; VariableType value(const Glib::ustring & name, const VariableType & def) const; virtual void composeWithCallbacks(const LiteralCallback &, const NodeCallback &) const = 0; diff --git a/project2/xml/xmlScriptParser.cpp b/project2/xml/xmlScriptParser.cpp index 5091f31..af69566 100644 --- a/project2/xml/xmlScriptParser.cpp +++ b/project2/xml/xmlScriptParser.cpp @@ -194,11 +194,12 @@ XmlScriptNode::variable(const boost::optional & defaultSource) co } } -VariableType -XmlScriptNode::value(const Glib::ustring & n) const +bool +XmlScriptNode::applyValue(const Glib::ustring & n, VariableType & val) const { if (const xmlpp::Attribute * a = element->get_attribute(n)) { - return a->get_value(); + val = a->get_value(); + return true; } const xmlpp::Element::NodeList cs = element->get_children(n); if (cs.size() == 1) { @@ -210,10 +211,11 @@ XmlScriptNode::value(const Glib::ustring & n) const else { v = new VariableLiteral(new XmlScriptNode(c, script)); } - return v->value(); + val = v->value(); + return false; } } - throw ValueNotFound(n); + return false; } void diff --git a/project2/xml/xmlScriptParser.h b/project2/xml/xmlScriptParser.h index 2d2d5db..9862f49 100644 --- a/project2/xml/xmlScriptParser.h +++ b/project2/xml/xmlScriptParser.h @@ -24,7 +24,7 @@ class XmlScriptNode : public ScriptNode { bool valueExists(const Glib::ustring&) const; VariableImpl * variable(const boost::optional & defaultSource) const; VariableImpl * variable(const Glib::ustring&) const; - VariableType value(const Glib::ustring&) const; + bool applyValue(const Glib::ustring & name, VariableType & target) const; void composeWithCallbacks(const ScriptNode::LiteralCallback&, const ScriptNode::NodeCallback&) const; private: -- cgit v1.2.3