* pattern given to the constructor. For each entry of the map,
* parameters have to be entered in the form <code>key: value</code>. In
* other words, a map is described in the form
- * <code>key1: value1, key2: value2, key3: value3, ...</code>.
+ * <code>key1: value1, key2: value2, key3: value3, ...</code>. A constructor
+ * argument allows to choose a delimiter between pairs other than the comma.
*
* With two additional parameters, the number of elements this list has
* to have can be specified. If none is specified, the map may have zero
* Constructor. Take the given parameter as the specification of valid
* elements of the list.
*
- * The two other arguments can be used to denote minimal and maximal
- * allowable lengths of the list.
+ * The three other arguments can be used to denote minimal and maximal
+ * allowable lengths of the list as well as the separator used to delimit
+ * pairs of the map.
*/
Map (const PatternBase &key_pattern,
const PatternBase &value_pattern,
const unsigned int min_elements = 0,
- const unsigned int max_elements = max_int_value);
+ const unsigned int max_elements = max_int_value,
+ const std::string &separator = ",");
/**
* Destructor.
*/
const unsigned int max_elements;
+ /**
+ * Separator between elements of the list.
+ */
+ const std::string separator;
+
/**
* Initial part of description
*/
std::string separator;
if (!is)
std::getline(is, separator, '>');
+ else
+ separator = ",";
return new List(*base_pattern, min_elements, max_elements, separator);
}
Map::Map (const PatternBase &p_key,
const PatternBase &p_value,
const unsigned int min_elements,
- const unsigned int max_elements)
+ const unsigned int max_elements,
+ const std::string &separator)
:
key_pattern (p_key.clone()),
value_pattern (p_value.clone()),
min_elements (min_elements),
- max_elements (max_elements)
+ max_elements (max_elements),
+ separator (separator)
{
Assert (min_elements <= max_elements,
ExcInvalidRange (min_elements, max_elements));
+ Assert (separator.size() > 0,
+ ExcMessage ("The separator must have a non-zero length."));
+ Assert (separator != ":",
+ ExcMessage ("The separator can not be a colon ':' sicne that "
+ "is the separator between the two elements of <key:value> pairs"));
}
{
std::string tmp = test_string_list;
std::vector<std::string> split_list;
- split_list.reserve (std::count (tmp.begin(), tmp.end(), ',')+1);
// first split the input list at comma sites
while (tmp.length() != 0)
std::string map_entry;
map_entry = tmp;
- if (map_entry.find(",") != std::string::npos)
+ if (map_entry.find(separator) != std::string::npos)
{
- map_entry.erase (map_entry.find(","), std::string::npos);
- tmp.erase (0, tmp.find(",")+1);
+ map_entry.erase (map_entry.find(separator), std::string::npos);
+ tmp.erase (0, tmp.find(separator)+separator.size());
}
else
tmp = "";
map_entry.erase (map_entry.length()-1, 1);
split_list.push_back (map_entry);
- };
+ }
if ((split_list.size() < min_elements) ||
(split_list.size() > max_elements))
<< key_pattern->description() << ":"
<< value_pattern->description() << ">"
<< " of length " << min_elements << "..." << max_elements
- << " (inclusive)"
- << "]";
+ << " (inclusive)";
+ if (separator != ",")
+ description << " separated by <" << separator << ">";
+ description << "]";
return description.str();
}
PatternBase *
Map::clone () const
{
- return new Map(*key_pattern, *value_pattern, min_elements, max_elements);
+ return new Map(*key_pattern, *value_pattern,
+ min_elements, max_elements,
+ separator);
}
{
return (sizeof(*this) +
MemoryConsumption::memory_consumption (*key_pattern) +
- MemoryConsumption::memory_consumption (*value_pattern));
+ MemoryConsumption::memory_consumption (*value_pattern) +
+ MemoryConsumption::memory_consumption (separator));
}
if (!(is >> max_elements))
return new Map(*key_pattern, *value_pattern, min_elements);
- return new Map(*key_pattern, *value_pattern, min_elements, max_elements);
+ is.ignore(strlen(" separated by <"));
+ std::string separator;
+ if (!is)
+ std::getline(is, separator, '>');
+ else
+ separator = ",";
+
+ return new Map(*key_pattern, *value_pattern,
+ min_elements, max_elements,
+ separator);
}
else
return 0;
--- /dev/null
+// ---------------------------------------------------------------------
+// $Id$
+//
+// Copyright (C) 2002 - 2013 by the deal.II authors
+//
+// This file is part of the deal.II library.
+//
+// The deal.II library is free software; you can use it, redistribute
+// it, and/or modify it under the terms of the GNU Lesser General
+// Public License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+// The full text of the license can be found in the file LICENSE at
+// the top level of the deal.II distribution.
+//
+// ---------------------------------------------------------------------
+
+
+
+// check the Patterns::Map pattern with a separator other than the default ','
+
+#include "../tests.h"
+#include <deal.II/base/logstream.h>
+#include <deal.II/base/parameter_handler.h>
+#include <fstream>
+
+void check (const char *p)
+{
+ ParameterHandler prm;
+ prm.declare_entry ("test_13", "-1:a, 0:b, 1:c",
+ Patterns::Map(Patterns::Integer(-1,1),
+ Patterns::Selection("a|b|c"),
+ 2,3, "xyz"));
+
+ std::ifstream in(p);
+ prm.read_input (in);
+
+ deallog << "test_13=" << prm.get ("test_13") << std::endl;
+}
+
+
+int main ()
+{
+ std::ofstream logfile("parameter_handler_13a/output");
+ deallog.attach(logfile);
+ deallog.depth_console(0);
+ deallog.threshold_double(1.e-10);
+
+ check ("parameter_handler_13a/prm");
+
+ return 0;
+}