diff --git a/BaseLib/ConfigTreeNew.cpp b/BaseLib/ConfigTreeNew.cpp index 8373a7facfcd5350ebc456cc4f1bf3b2b389c77c..0631983b76bee01fa79a78fd9ea6b7de87665539 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 50f55b8ea91e9b0abfbbd9a09cd54a9b96bc931b..6129c55539cd96cf6733e9ed04f678f78bb41a50 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);