/**************************************************************************** ** ** Copyright (C) 2016 Jochen Becher ** Contact: https://www.qt.io/licensing/ ** ** This file is part of Qt Creator. ** ** Commercial License Usage ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and The Qt Company. For licensing terms ** and conditions see https://www.qt.io/terms-conditions. For further ** information use the contact form at https://www.qt.io/contact-us. ** ** GNU General Public License Usage ** Alternatively, this file may be used under the terms of the GNU ** General Public License version 3 as published by the Free Software ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT ** included in the packaging of this file. Please review the following ** information to ensure the GNU General Public License requirements will ** be met: https://www.gnu.org/licenses/gpl-3.0.html. ** ****************************************************************************/ #pragma once #include "parameters.h" #include #include namespace qark { static Flag ENFORCE_REFERENCED_ITEMS; // QList template inline void save(Archive &archive, const QList &list, const Parameters &) { archive << tag("qlist"); foreach (const T &t, list) archive << attr(QStringLiteral("item"), t); archive << end; } template inline void save(Archive &archive, const QList &list, const Parameters ¶meters) { archive << tag("qlist"); if (parameters.hasFlag(ENFORCE_REFERENCED_ITEMS)) { foreach (const T *t, list) archive << ref(QStringLiteral("item"), t); } else { foreach (const T *t, list) archive << attr(QStringLiteral("item"), t); } archive << end; } template inline void load(Archive &archive, QList &list, const Parameters &) { archive >> tag(QStringLiteral("qlist")); archive >> attr, const T &>(QStringLiteral("item"), list, &QList::append); archive >> end; } template inline void load(Archive &archive, QList &list, const Parameters ¶meters) { archive >> tag(QStringLiteral("qlist")); if (parameters.hasFlag(ENFORCE_REFERENCED_ITEMS)) { // why does the following line not compile but the line below selects the correct function? //archive >> ref, T * const &>("item", list, &QList::append); archive >> ref(QStringLiteral("item"), list, &QList::append); } else { archive >> attr, T * const &>(QStringLiteral("item"), list, &QList::append); } archive >> end; } // QSet template inline void save(Archive &archive, const QSet &set, const Parameters &) { archive << tag("qset"); foreach (const T &t, set) archive << attr(QStringLiteral("item"), t); archive << end; } template inline void save(Archive &archive, const QSet &set, const Parameters ¶meters) { archive << tag("qset"); if (parameters.hasFlag(ENFORCE_REFERENCED_ITEMS)) { foreach (const T *t, set) archive << ref(QStringLiteral("item"), t); } else { foreach (const T *t, set) archive << attr(QStringLiteral("item"), t); } archive << end; } namespace impl { template void insertIntoSet(QSet &set, const T &t) { set.insert(t); } } // namespace impl template inline void load(Archive &archive, QSet &set, const Parameters &) { archive >> tag(QStringLiteral("qset")); archive >> attr, const T &>(QStringLiteral("item"), set, &impl::insertIntoSet); archive >> end; } template inline void load(Archive &archive, QSet &set, const Parameters ¶meters) { archive >> tag(QStringLiteral("qset")); if (parameters.hasFlag(ENFORCE_REFERENCED_ITEMS)) archive >> ref(QStringLiteral("item"), set, &impl::insertIntoSet); else archive >> attr, T * const &>(QStringLiteral("item"), set, &impl::insertIntoSet); archive >> end; } // QHash namespace impl { template class KeyValuePair { public: KeyValuePair() = default; KeyValuePair(const KEY &key, const VALUE &value) : m_key(key), m_value(value) { } KEY m_key; VALUE m_value; }; } // namespace impl template inline void save(Archive &archive, const impl::KeyValuePair &pair, const Parameters &) { archive << tag(QStringLiteral("pair")) << attr(QStringLiteral("key"), pair.m_key) << attr(QStringLiteral("value"), pair.m_value) << end; } template inline void load(Archive &archive, impl::KeyValuePair &pair, const Parameters &) { archive >> tag(QStringLiteral("pair")) >> attr(QStringLiteral("key"), pair.m_key) >> attr(QStringLiteral("value"), pair.m_value) >> end; } template inline void save(Archive &archive, const QHash &hash, const Parameters &) { archive << tag(QStringLiteral("qhash")); for (auto it = hash.begin(); it != hash.end(); ++it) { impl::KeyValuePair pair(it.key(), it.value()); archive << attr("item", pair); } archive << end; } namespace impl { template inline void keyValuePairInsert(QHash &hash, const KeyValuePair &pair) { hash.insert(pair.m_key, pair.m_value); } } // namespace impl template inline void load(Archive &archive, QHash &hash, const Parameters &) { archive >> tag(QStringLiteral("qhash")); archive >> attr(QStringLiteral("item"), hash, &impl::keyValuePairInsert); archive >> end; } } // namespace qark