diff --git a/include/yaml-cpp/qtyaml.h b/include/yaml-cpp/qtyaml.h new file mode 100755 index 0000000..b0f9a8e --- /dev/null +++ b/include/yaml-cpp/qtyaml.h @@ -0,0 +1,167 @@ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 2.0 + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * ***** END LICENSE BLOCK ***** */ + +/* + * Copyright (c) 2014, Filip Brcic . All rights reserved. + * + * This file is part of lusim + */ + +#ifndef QTYAML_H +#define QTYAML_H + +#include + +#include +#include +#include +#include +#include + +namespace YAML { + +// QString +template<> +struct convert +{ + static Node encode(const QString& rhs) + { + return Node(rhs.toStdString()); + } + + static bool decode(const Node& node, QString& rhs) + { + if (!node.IsScalar()) + return false; + rhs = QString::fromStdString(node.Scalar()); + return true; + } +}; + +// QMap +template +struct convert > +{ + static Node encode(const QMap& rhs) + { + Node node(NodeType::Map); + auto it = rhs.constBegin(); + while (it != rhs.constEnd()) + { + node.force_insert(it.key(), it.value()); + ++it; + } + return node; + } + + static bool decode(const Node& node, QMap& rhs) + { + if (!node.IsMap()) + return false; + + rhs.clear(); + const_iterator it = node.begin(); + while (it != node.end()) + { + rhs[it->first.as()] = it->second.as(); + ++it; + } + return true; + } +}; + +// QVector +template +struct convert > +{ + static Node encode(const QVector& rhs) + { + Node node(NodeType::Sequence); + foreach (T value, rhs) { + node.push_back(value); + } + return node; + } + + static bool decode(const Node& node, QVector& rhs) + { + if (!node.IsSequence()) + return false; + + rhs.clear(); + const_iterator it = node.begin(); + while (it != node.end()) + { + rhs.push_back(it->as()); + ++it; + } + return true; + } +}; + +// QList +template +struct convert > +{ + static Node encode(const QList& rhs) + { + Node node(NodeType::Sequence); + foreach (T value, rhs) { + node.push_back(value); + } + return node; + } + + static bool decode(const Node& node, QList& rhs) + { + if (!node.IsSequence()) + return false; + + rhs.clear(); + const_iterator it = node.begin(); + while (it != node.end()) + { + rhs.push_back(it->as()); + ++it; + } + return true; + } +}; + +// QPair +template +struct convert > +{ + static Node encode(const QPair& rhs) + { + Node node(NodeType::Sequence); + node.push_back(rhs.first); + node.push_back(rhs.second); + return node; + } + + static bool decode(const Node& node, QPair& rhs) + { + if (!node.IsSequence()) + return false; + if (node.size() != 2) + return false; + + rhs.first = node[0].as(); + rhs.second = node[1].as(); + return true; + } +}; + +// TODO: Add the rest of the container classes +// QLinkedList, QStack, QQueue, QSet, QMultiMap, QHash, QMultiHash, QStringList, ... + +} // end namespace YAML + +#endif // QTYAML_H