47 #ifndef TEUCHOS_YAMLPARSER_DEF_H_ 48 #define TEUCHOS_YAMLPARSER_DEF_H_ 59 for(YAML::const_iterator it = node.begin(); it != node.end(); it++)
119 std::string yamlFileName;
120 if(xmlFileName.find(
".xml") == std::string::npos)
122 yamlFileName = xmlFileName +
".yaml";
126 yamlFileName = xmlFileName.substr(0, xmlFileName.length() - 4) +
".yaml";
143 Iter i = lhs.
begin();
144 Iter j = rhs.
begin();
149 std::cout <<
"Parameter list names: \"" << lhs.
name() <<
"\" and \"" << rhs.
name() <<
"\".\n";
153 for(; i != lhs.
end(); i++)
155 const std::string& key = lhs.
name(i);
162 std::cout <<
"One list is missing parameter: \"" << key <<
"\"\n";
174 std::cout <<
"Values for key \"" << key <<
"\" have different types.\n";
194 std::cout <<
"Values for key \"" << key <<
"\" are different.\n";
206 std::cout <<
"Lists \"" << lhs.
name() <<
"\" and \"" << rhs.
name() <<
"\" have different number of parameters.\n";
213 namespace YAMLParameterList
219 std::vector<YAML::Node> baseMap = YAML::LoadAll(text);
226 std::vector<YAML::Node> baseMap = YAML::LoadAll(text);
232 std::vector<YAML::Node> baseMap = YAML::LoadAllFromFile(yamlFile);
241 for(
size_t i = 0; i < lists.size(); i++)
250 if(node.Type() != YAML::NodeType::Map)
252 throw YamlStructureError(
"All top-level elements of the YAML file must be maps.");
261 for(YAML::const_iterator i = node.begin(); i != node.end(); i++)
264 if(i->first.Type() != YAML::NodeType::Scalar)
266 throw YamlKeyError(
"Keys must be plain strings");
269 const std::string key = i->first.as<std::string>();
279 if(node.Type() == YAML::NodeType::Scalar)
283 parent.
set(key, node.as<
int>());
289 parent.
set(key, node.as<
double>());
295 std::string rawString = node.as<std::string>();
296 if(rawString ==
"true")
298 parent.
set<
bool>(key,
true);
300 else if(rawString ==
"false")
302 parent.
set<
bool>(key,
false);
306 parent.
set(key, rawString);
311 throw YamlScalarError(
"YAML scalars must be int, double, bool or string.");
316 else if(node.Type() == YAML::NodeType::Map)
328 else if(node.Type() == YAML::NodeType::Sequence)
333 node.begin()->as<
int>();
334 parent.
set(key, getYamlArray<int>(node));
340 node.
begin()->as<
double>();
341 parent.
set(key, getYamlArray<double>(node));
347 node.
begin()->as<std::string>();
348 parent.
set(key, getYamlArray<std::string>(node));
352 throw YamlSequenceError(std::string(
"Array \"") + key +
"\" must contain int, double, bool or string");
357 else if(node.Type() == YAML::NodeType::Null)
360 parent.
set(key, std::string());
365 throw YamlUndefinedNodeError(
"Value type in a key-value pair must be one of: int, double, string, array, sublist.");
373 std::ofstream yaml(yamlFile);
374 yaml <<
"%YAML 1.1\n---\n";
375 yaml <<
"ANONYMOUS:";
405 for(
int i = 0; i < indentLevel; i++)
413 writeParameterList(Teuchos::getValue<Teuchos::ParameterList>(entry), yaml, indentLevel + 2);
422 for(
int i = 0; i < arr.
size(); i++)
425 if(i != arr.size() - 1)
432 for(
int i = 0; i < arr.
size(); i++)
435 if(i != arr.
size() - 1)
442 for(
int i = 0; i < arr.
size(); i++)
445 if(i != arr.
size() - 1)
451 else if(entry.
isType<
int>())
453 yaml << Teuchos::getValue<int>(entry);
455 else if(entry.
isType<
double>())
459 else if(entry.
isType<std::string>())
461 std::string& str = Teuchos::getValue<std::string>(entry);
462 if(strchr(str.c_str(),
'\n'))
470 size_t next = str.find(
'\n', index);
471 for(
int i = 0; i < indentLevel + 2; i++)
475 if(next == std::string::npos)
477 yaml << str.substr(index, std::string::npos);
482 yaml << str.substr(index, next - index) <<
'\n';
492 else if(entry.
isType<
bool>())
494 yaml << (Teuchos::getValue<bool>(entry) ?
"true" :
"false");
503 yaml <<
'\'' << str <<
'\'';
513 yaml << std::showpoint << std::setprecision(8);
514 if(d < 1e6 && d > 1e-5)
521 yaml << std::scientific << d;
528 return strpbrk(str.c_str(),
":{}[],&*#?|-<>=!%@\\");
C++ Standard Library compatable filtered iterator.
void writeYamlFile(const std::string &yamlFile, Teuchos::RCP< Teuchos::ParameterList > &pl)
void updateParametersFromYamlFile(const std::string &yamlFileName, const Teuchos::Ptr< Teuchos::ParameterList > ¶mList)
TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT void updateParametersFromYamlCString(const char *const data, const Teuchos::Ptr< Teuchos::ParameterList > ¶mList, bool overwrite)
ConstIterator begin() const
An iterator pointing to the first entry.
ParameterList & set(std::string const &name, T const &value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
Set a parameter whose value has type T.
This object is held as the "value" in the Teuchos::ParameterList std::map.
const ParameterEntry & entry(ConstIterator i) const
Access to ParameterEntry (i.e., returns i->second)
void processKeyValueNode(const std::string &key, const YAML::Node &node, Teuchos::ParameterList &parent, bool topLevel)
Ordinal numParams() const
Get the number of stored parameters.
void generalWriteDouble(double d, std::ofstream &yaml)
Modified boost::any class, which is a container for a templated value.
Simple helper functions that make it easy to read and write XML to and from a parameterlist.
Teuchos::RCP< Teuchos::ParameterList > parseYamlText(const std::string &text)
void updateParametersFromYamlString(const std::string &yamlData, const Teuchos::Ptr< Teuchos::ParameterList > ¶mList, bool overwrite)
ConstIterator end() const
An iterator pointing beyond the last entry.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
void processMapNode(const YAML::Node &node, Teuchos::ParameterList &parent, bool topLevel)
void writeParameterList(Teuchos::ParameterList &pl, std::ofstream &yaml, int indentLevel)
Teuchos::RCP< Teuchos::ParameterList > readParams(std::vector< YAML::Node > &lists)
bool isList() const
Return whether or not the value itself is a list.
Teuchos::Array< T > getYamlArray(const YAML::Node &node)
A list of parameters of arbitrary type.
ParameterList & setParameters(const ParameterList &source)
bool isType() const
Test the type of the data being contained.
const std::type_info & type() const
Return the type of value being stored.
const std::string & name() const
The name of this ParameterList.
any & getAny(bool activeQry=true)
Direct access to the Teuchos::any data value underlying this object. The bool argument activeQry (def...
Teuchos::RCP< Teuchos::ParameterList > parseYamlFile(const std::string &yamlFile)
void push_back(const value_type &x)
ParameterList & setParametersNotAlreadySet(const ParameterList &source)
bool stringNeedsQuotes(const std::string &str)
bool isParameter(const std::string &name) const
Whether the given parameter exists in this list.
bool isArray() const
Test if the type of data being contained is a Teuchos::Array.
ParameterList & sublist(const std::string &name, bool mustAlreadyExist=false, const std::string &docString="")
Creates an empty sublist and returns a reference to the sublist name. If the list already exists...
ParameterList & setName(const std::string &name)
Set the name of *this list.
std::string convertXmlToYaml(const std::string &xmlFileName)
Teuchos::RCP< Teuchos::ParameterList > getParametersFromYamlFile(const std::string &yamlFileName)
void writeParameter(const std::string ¶mName, const Teuchos::ParameterEntry &entry, std::ofstream &yaml, int indentLevel)
ParameterEntry & getEntry(const std::string &name)
Retrieves an entry with the name name.
Simple wrapper class for raw pointers to single objects where no persisting relationship exists...
void generalWriteString(const std::string &str, std::ofstream &yaml)
bool haveSameValuesUnordered(const Teuchos::ParameterList &lhs, const Teuchos::ParameterList &rhs, bool verbose)
Replacement for std::vector that is compatible with the Teuchos Memory Management classes...