From 3996030355d8c5bb0d7c86affc8982b0252490bc Mon Sep 17 00:00:00 2001 From: Christoph Lehmann <christoph.lehmann@ufz.de> Date: Fri, 8 Jan 2016 16:21:21 +0100 Subject: [PATCH] [BL] make default behaviour strict Also checks are made publicly callable --- BaseLib/ConfigTreeNew.cpp | 10 +++++++--- BaseLib/ConfigTreeNew.h | 16 +++++++++++----- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/BaseLib/ConfigTreeNew.cpp b/BaseLib/ConfigTreeNew.cpp index 8373a7facfc..0631983b76b 100644 --- a/BaseLib/ConfigTreeNew.cpp +++ b/BaseLib/ConfigTreeNew.cpp @@ -54,14 +54,14 @@ ConfigTreeNew(ConfigTreeNew && other) ConfigTreeNew::~ConfigTreeNew() { - checkFullyRead(); + checkAndInvalidate(); } ConfigTreeNew& ConfigTreeNew:: operator=(ConfigTreeNew&& other) { - checkFullyRead(); + checkAndInvalidate(); _tree = other._tree; other._tree = nullptr; @@ -221,7 +221,7 @@ markVisitedDecrement(std::string const& key) const } void -ConfigTreeNew::checkFullyRead() const +ConfigTreeNew::checkAndInvalidate() { if (!_tree) return; @@ -240,6 +240,10 @@ ConfigTreeNew::checkFullyRead() const + " time(s) less than it was present in the configuration tree."); } } + + // The following invalidates this instance, s.t. it can not be read from it anymore, + // but it also prevents double-checking. + _tree = nullptr; } } diff --git a/BaseLib/ConfigTreeNew.h b/BaseLib/ConfigTreeNew.h index 50f55b8ea91..6129c55539c 100644 --- a/BaseLib/ConfigTreeNew.h +++ b/BaseLib/ConfigTreeNew.h @@ -180,10 +180,13 @@ public: * If a custom error callback is provided, this function should break out of * the normal execution order, e.g., by throwing or by calling std::abort(), * because otherwise this class will effectively treat errors as no-errors. + * + * Defaults are strict: By default, both callbacks are set to the same function, + * i.e., warnings will also result in program abortion! */ explicit ConfigTreeNew(PTree const& tree, Callback const& error_cb = onerror, - Callback const& warning_cb = onwarning); + Callback const& warning_cb = onerror); //! copying is not compatible with the semantics of this class ConfigTreeNew(ConfigTreeNew const&) = delete; @@ -302,6 +305,13 @@ public: */ void ignoreConfParamAll(std::string const& param) const; + /*! Checks if the top level of this tree has been read entirely (and not too often). + * + * Caution: This method also invalidates the instance, i.e., afterwards it can not + * be read from the tree anymore! + */ + void checkAndInvalidate(); + //! The destructor performs the check if all nodes at the current level of the tree //! have been read. ~ConfigTreeNew(); @@ -362,10 +372,6 @@ private: //! and the number of times it exists in the ConfigTree void markVisitedDecrement(std::string const& key) const; - //! Helper method that checks if the top level of this tree has - //! been red entirely (and not too often). - void checkFullyRead() const; - //! returns a short string at suitable for error/warning messages static std::string shortString(std::string const& s); -- GitLab