From 9b1da9ce762c6bcc27b65784c95d597201b8c83a Mon Sep 17 00:00:00 2001
From: Oliver Sander <sander@dune-project.org>
Date: Mon, 15 Mar 2010 16:11:38 +0000
Subject: [PATCH] new, standard-conforming constification of hasSub() and
 hasKey().  Thanks to Benedikt and Carsten for their help

[[Imported from SVN: r5932]]
---
 dune/common/configparser.cc | 21 +++++++++++++++++----
 dune/common/configparser.hh | 12 ++++++++++--
 2 files changed, 27 insertions(+), 6 deletions(-)

diff --git a/dune/common/configparser.cc b/dune/common/configparser.cc
index bb6b7b2b4..dfedca30f 100644
--- a/dune/common/configparser.cc
+++ b/dune/common/configparser.cc
@@ -137,7 +137,7 @@ void ConfigParser::report(const string prefix) const
   }
 }
 
-bool ConfigParser::hasKey(const string& key)
+bool ConfigParser::hasKey(const string& key) const
 {
   string::size_type dot = key.find(".");
 
@@ -147,14 +147,14 @@ bool ConfigParser::hasKey(const string& key)
     if (subs.count(prefix) == 0)
       return false;
 
-    ConfigParser& s = sub(prefix);
+    const ConfigParser& s = sub(prefix);
     return s.hasKey(key.substr(dot+1));
   }
   else
     return (values.count(key) != 0);
 }
 
-bool ConfigParser::hasSub(const string& key)
+bool ConfigParser::hasSub(const string& key) const
 {
   string::size_type dot = key.find(".");
 
@@ -164,7 +164,7 @@ bool ConfigParser::hasSub(const string& key)
     if (subs.count(prefix) == 0)
       return false;
 
-    ConfigParser& s = sub(prefix);
+    const ConfigParser& s = sub(prefix);
     return s.hasSub(key.substr(dot+1));
   }
   else
@@ -184,6 +184,19 @@ ConfigParser& ConfigParser::sub(const string& key)
     return subs[key];
 }
 
+const ConfigParser& ConfigParser::sub(const string& key) const
+{
+  string::size_type dot = key.find(".");
+
+  if (dot != string::npos)
+  {
+    const ConfigParser& s = sub(key.substr(0,dot));
+    return s.sub(key.substr(dot+1));
+  }
+  else
+    return subs.find(key)->second;
+}
+
 string& ConfigParser::operator[] (const string& key)
 {
   string::size_type dot = key.find(".");
diff --git a/dune/common/configparser.hh b/dune/common/configparser.hh
index 79e7498f1..7d0076b28 100644
--- a/dune/common/configparser.hh
+++ b/dune/common/configparser.hh
@@ -94,7 +94,7 @@ namespace Dune {
      * \param key key name
      * \return true if key exists in structure, otherwise false
      */
-    bool hasKey(const std::string& key);
+    bool hasKey(const std::string& key) const;
 
 
     /** \brief test for substructure
@@ -104,7 +104,7 @@ namespace Dune {
      * \param sub substructure name
      * \return true if substructure exists in structure, otherwise false
      */
-    bool hasSub(const std::string& sub);
+    bool hasSub(const std::string& sub) const;
 
 
     /** \brief get value reference for key
@@ -140,6 +140,14 @@ namespace Dune {
     ConfigParser& sub(const std::string& sub);
 
 
+    /** \brief get const substructure by name
+     *
+     * \param sub substructure name
+     * \return reference to substructure
+     */
+    const ConfigParser& sub(const std::string& sub) const;
+
+
     /** \brief get value as string
      *
      * Returns pure string value for given key.
-- 
GitLab