2016-01-15 14:58:39 +01:00
|
|
|
/****************************************************************************
|
2015-08-16 13:11:15 +02:00
|
|
|
**
|
2016-01-15 14:58:39 +01:00
|
|
|
** Copyright (C) 2016 Jochen Becher
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2015-08-16 13:11:15 +02:00
|
|
|
**
|
|
|
|
|
** 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
|
2016-01-15 14:58:39 +01:00
|
|
|
** 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.
|
2015-08-16 13:11:15 +02:00
|
|
|
**
|
2016-01-15 14:58:39 +01:00
|
|
|
** 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.
|
2015-08-16 13:11:15 +02:00
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2016-03-18 07:55:01 +01:00
|
|
|
#pragma once
|
2015-08-16 13:11:15 +02:00
|
|
|
|
2015-10-09 21:12:53 +02:00
|
|
|
#include "parameters.h"
|
2015-08-16 13:11:15 +02:00
|
|
|
|
|
|
|
|
#include <QList>
|
|
|
|
|
#include <QHash>
|
|
|
|
|
|
|
|
|
|
namespace qark {
|
|
|
|
|
|
2015-10-09 21:12:53 +02:00
|
|
|
static Flag ENFORCE_REFERENCED_ITEMS;
|
2015-08-16 13:11:15 +02:00
|
|
|
|
|
|
|
|
// QList
|
|
|
|
|
|
|
|
|
|
template<class Archive, class T>
|
2015-10-09 21:12:53 +02:00
|
|
|
inline void save(Archive &archive, const QList<T> &list, const Parameters &)
|
2015-08-16 13:11:15 +02:00
|
|
|
{
|
|
|
|
|
archive << tag("qlist");
|
2015-11-14 16:59:30 +01:00
|
|
|
foreach (const T &t, list)
|
2017-07-30 21:35:16 +02:00
|
|
|
archive << attr("item", t);
|
2015-08-16 13:11:15 +02:00
|
|
|
archive << end;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<class Archive, class T>
|
2015-10-09 21:12:53 +02:00
|
|
|
inline void save(Archive &archive, const QList<T *> &list, const Parameters ¶meters)
|
2015-08-16 13:11:15 +02:00
|
|
|
{
|
|
|
|
|
archive << tag("qlist");
|
2015-10-09 21:12:53 +02:00
|
|
|
if (parameters.hasFlag(ENFORCE_REFERENCED_ITEMS)) {
|
2015-11-14 16:59:30 +01:00
|
|
|
foreach (const T *t, list)
|
2017-07-30 21:35:16 +02:00
|
|
|
archive << ref("item", t);
|
2015-08-16 13:11:15 +02:00
|
|
|
} else {
|
2015-11-14 16:59:30 +01:00
|
|
|
foreach (const T *t, list)
|
2017-07-30 21:35:16 +02:00
|
|
|
archive << attr("item", t);
|
2015-08-16 13:11:15 +02:00
|
|
|
}
|
|
|
|
|
archive << end;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<class Archive, class T>
|
2015-10-09 21:12:53 +02:00
|
|
|
inline void load(Archive &archive, QList<T> &list, const Parameters &)
|
2015-08-16 13:11:15 +02:00
|
|
|
{
|
2017-07-30 21:35:16 +02:00
|
|
|
archive >> tag("qlist");
|
2020-11-04 19:48:50 +01:00
|
|
|
void (QList<T>::*appendMethod)(const T &) = &QList<T>::append;
|
|
|
|
|
archive >> attr("item", list, appendMethod);
|
2015-08-16 13:11:15 +02:00
|
|
|
archive >> end;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<class Archive, class T>
|
2015-10-09 21:12:53 +02:00
|
|
|
inline void load(Archive &archive, QList<T *> &list, const Parameters ¶meters)
|
2015-08-16 13:11:15 +02:00
|
|
|
{
|
2017-07-30 21:35:16 +02:00
|
|
|
archive >> tag("qlist");
|
2015-10-09 21:12:53 +02:00
|
|
|
if (parameters.hasFlag(ENFORCE_REFERENCED_ITEMS)) {
|
2015-08-16 13:11:15 +02:00
|
|
|
// why does the following line not compile but the line below selects the correct function?
|
|
|
|
|
//archive >> ref<QList<T *>, T * const &>("item", list, &QList<T *>::append);
|
2017-07-30 21:35:16 +02:00
|
|
|
archive >> ref("item", list, &QList<T *>::append);
|
2015-08-16 13:11:15 +02:00
|
|
|
} else {
|
2020-11-04 19:48:50 +01:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
|
|
|
|
|
using ParameterType = typename QList<T *>::parameter_type;
|
|
|
|
|
#else
|
|
|
|
|
using ParameterType = T * const &;
|
|
|
|
|
#endif
|
|
|
|
|
void (QList<T *>::*appendMethod)(ParameterType) = &QList<T *>::append;
|
|
|
|
|
archive >> attr("item", list, appendMethod);
|
2015-08-16 13:11:15 +02:00
|
|
|
}
|
|
|
|
|
archive >> end;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// QSet
|
|
|
|
|
|
|
|
|
|
template<class Archive, class T>
|
2015-10-09 21:12:53 +02:00
|
|
|
inline void save(Archive &archive, const QSet<T> &set, const Parameters &)
|
2015-08-16 13:11:15 +02:00
|
|
|
{
|
|
|
|
|
archive << tag("qset");
|
2015-11-14 16:59:30 +01:00
|
|
|
foreach (const T &t, set)
|
2017-07-30 21:35:16 +02:00
|
|
|
archive << attr("item", t);
|
2015-08-16 13:11:15 +02:00
|
|
|
archive << end;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<class Archive, class T>
|
2015-10-09 21:12:53 +02:00
|
|
|
inline void save(Archive &archive, const QSet<T *> &set, const Parameters ¶meters)
|
2015-08-16 13:11:15 +02:00
|
|
|
{
|
|
|
|
|
archive << tag("qset");
|
2015-10-09 21:12:53 +02:00
|
|
|
if (parameters.hasFlag(ENFORCE_REFERENCED_ITEMS)) {
|
2015-11-14 16:59:30 +01:00
|
|
|
foreach (const T *t, set)
|
2017-07-30 21:35:16 +02:00
|
|
|
archive << ref("item", t);
|
2015-08-16 13:11:15 +02:00
|
|
|
} else {
|
2015-11-14 16:59:30 +01:00
|
|
|
foreach (const T *t, set)
|
2017-07-30 21:35:16 +02:00
|
|
|
archive << attr("item", t);
|
2015-08-16 13:11:15 +02:00
|
|
|
}
|
|
|
|
|
archive << end;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace impl {
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
|
void insertIntoSet(QSet<T> &set, const T &t) {
|
|
|
|
|
set.insert(t);
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-14 16:59:30 +01:00
|
|
|
} // namespace impl
|
2015-08-16 13:11:15 +02:00
|
|
|
|
|
|
|
|
template<class Archive, class T>
|
2015-10-09 21:12:53 +02:00
|
|
|
inline void load(Archive &archive, QSet<T> &set, const Parameters &)
|
2015-08-16 13:11:15 +02:00
|
|
|
{
|
2017-07-30 21:35:16 +02:00
|
|
|
archive >> tag("qset");
|
2020-11-04 19:48:50 +01:00
|
|
|
archive >> attr("item", set, &impl::insertIntoSet<T>);
|
2015-08-16 13:11:15 +02:00
|
|
|
archive >> end;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<class Archive, class T>
|
2015-10-09 21:12:53 +02:00
|
|
|
inline void load(Archive &archive, QSet<T *> &set, const Parameters ¶meters)
|
2015-08-16 13:11:15 +02:00
|
|
|
{
|
2017-07-30 21:35:16 +02:00
|
|
|
archive >> tag("qset");
|
2015-11-14 16:59:30 +01:00
|
|
|
if (parameters.hasFlag(ENFORCE_REFERENCED_ITEMS))
|
2017-07-30 21:35:16 +02:00
|
|
|
archive >> ref("item", set, &impl::insertIntoSet<T *>);
|
2015-11-14 16:59:30 +01:00
|
|
|
else
|
2020-11-04 19:48:50 +01:00
|
|
|
archive >> attr("item", set, &impl::insertIntoSet<T *>);
|
2015-08-16 13:11:15 +02:00
|
|
|
archive >> end;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// QHash
|
|
|
|
|
|
|
|
|
|
namespace impl {
|
|
|
|
|
|
|
|
|
|
template<typename KEY, typename VALUE>
|
2015-11-14 16:59:30 +01:00
|
|
|
class KeyValuePair
|
|
|
|
|
{
|
2015-08-16 13:11:15 +02:00
|
|
|
public:
|
2015-11-24 20:00:24 +01:00
|
|
|
KeyValuePair() = default;
|
|
|
|
|
KeyValuePair(const KEY &key, const VALUE &value) : m_key(key), m_value(value) { }
|
2015-08-16 13:11:15 +02:00
|
|
|
|
2015-11-03 22:30:46 +01:00
|
|
|
KEY m_key;
|
|
|
|
|
VALUE m_value;
|
2015-08-16 13:11:15 +02:00
|
|
|
};
|
|
|
|
|
|
2015-11-14 16:59:30 +01:00
|
|
|
} // namespace impl
|
2015-08-16 13:11:15 +02:00
|
|
|
|
|
|
|
|
template<class Archive, class KEY, class VALUE>
|
2015-10-09 21:12:53 +02:00
|
|
|
inline void save(Archive &archive, const impl::KeyValuePair<KEY, VALUE> &pair, const Parameters &)
|
2015-08-16 13:11:15 +02:00
|
|
|
{
|
2017-07-30 21:35:16 +02:00
|
|
|
archive << tag("pair")
|
|
|
|
|
<< attr("key", pair.m_key)
|
|
|
|
|
<< attr("value", pair.m_value)
|
2015-08-16 13:11:15 +02:00
|
|
|
<< end;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<class Archive, class KEY, class VALUE>
|
2015-10-09 21:12:53 +02:00
|
|
|
inline void load(Archive &archive, impl::KeyValuePair<KEY, VALUE> &pair, const Parameters &)
|
2015-08-16 13:11:15 +02:00
|
|
|
{
|
2017-07-30 21:35:16 +02:00
|
|
|
archive >> tag("pair")
|
|
|
|
|
>> attr("key", pair.m_key)
|
|
|
|
|
>> attr("value", pair.m_value)
|
2015-08-16 13:11:15 +02:00
|
|
|
>> end;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<class Archive, class KEY, class VALUE>
|
2015-10-09 21:12:53 +02:00
|
|
|
inline void save(Archive &archive, const QHash<KEY, VALUE> &hash, const Parameters &)
|
2015-08-16 13:11:15 +02:00
|
|
|
{
|
2017-07-30 21:35:16 +02:00
|
|
|
archive << tag("qhash");
|
2015-11-21 20:54:19 +01:00
|
|
|
for (auto it = hash.begin(); it != hash.end(); ++it) {
|
2015-08-16 13:11:15 +02:00
|
|
|
impl::KeyValuePair<KEY, VALUE> pair(it.key(), it.value());
|
|
|
|
|
archive << attr("item", pair);
|
|
|
|
|
}
|
|
|
|
|
archive << end;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace impl {
|
|
|
|
|
|
|
|
|
|
template<class KEY, class VALUE>
|
|
|
|
|
inline void keyValuePairInsert(QHash<KEY, VALUE> &hash, const KeyValuePair<KEY, VALUE> &pair)
|
|
|
|
|
{
|
2015-11-03 22:30:46 +01:00
|
|
|
hash.insert(pair.m_key, pair.m_value);
|
2015-08-16 13:11:15 +02:00
|
|
|
}
|
|
|
|
|
|
2015-11-14 16:59:30 +01:00
|
|
|
} // namespace impl
|
2015-08-16 13:11:15 +02:00
|
|
|
|
|
|
|
|
template<class Archive, class KEY, class VALUE>
|
2015-10-09 21:12:53 +02:00
|
|
|
inline void load(Archive &archive, QHash<KEY, VALUE> &hash, const Parameters &)
|
2015-08-16 13:11:15 +02:00
|
|
|
{
|
2017-07-30 21:35:16 +02:00
|
|
|
archive >> tag("qhash");
|
|
|
|
|
archive >> attr("item", hash, &impl::keyValuePairInsert<KEY, VALUE>);
|
2015-08-16 13:11:15 +02:00
|
|
|
archive >> end;
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-14 16:59:30 +01:00
|
|
|
} // namespace qark
|