1 #ifndef QTJSONSERIALIZER_METAWRITERS_H
2 #define QTJSONSERIALIZER_METAWRITERS_H
4 #include "QtJsonSerializer/qtjsonserializer_global.h"
6 #include <QtCore/qmetatype.h>
7 #include <QtCore/qvariant.h>
8 #include <QtCore/qsharedpointer.h>
9 #include <QtCore/qhash.h>
10 #include <QtCore/qreadwritelock.h>
12 #include <QtCore/qset.h>
13 #include <QtCore/qlinkedlist.h>
27 int type = QMetaType::UnknownType;
33 template <
template<
typename>
class TContainer,
typename TClass>
34 static void registerWriter();
38 static bool canWrite(
int metaTypeId);
48 virtual void reserve(
int size) = 0;
50 virtual void add(
const QVariant &value) = 0;
81 int keyType = QMetaType::UnknownType;
83 int valueType = QMetaType::UnknownType;
87 template <
template<
typename,
typename>
class TContainer,
typename TKey,
typename TValue>
88 static void registerWriter();
92 static bool canWrite(
int metaTypeId);
123 namespace Implementations {
125 template <
template<
typename>
class TContainer,
typename TClass>
129 SequentialWriterImpl(TContainer<TClass> *data)
133 SequenceInfo info() const final {
134 return {qMetaTypeId<TClass>(),
false};
138 _data->reserve(size);
142 _data->append(value.template value<TClass>());
146 TContainer<TClass> *_data;
149 template <
template<
typename>
class TContainer,
typename TClass>
150 class SequentialWriterFactoryImpl final :
public SequentialWriterFactory
160 template <
template<
typename,
typename>
class TContainer,
typename TKey,
typename TValue>
161 class AssociativeWriterImpl final :
public AssociativeWriter
164 AssociativeWriterImpl(TContainer<TKey, TValue> *data)
168 AssociationInfo
info() const final {
169 return {qMetaTypeId<TKey>(), qMetaTypeId<TValue>()};
173 _data->insert(key.template value<TKey>(),
174 value.template value<TValue>());
178 TContainer<TKey, TValue> *_data;
181 template <
template<
typename,
typename>
class TContainer,
typename TKey,
typename TValue>
182 class AssociativeWriterFactoryImpl final :
public AssociativeWriterFactory
192 template <
typename TClass>
193 class SequentialWriterImpl<
QSet, TClass> final :
public SequentialWriter
200 SequenceInfo
info() const final {
201 return {qMetaTypeId<TClass>(),
true};
209 _data->insert(value.template value<TClass>());
216 template <
typename TClass>
217 class SequentialWriterImpl<
QLinkedList, TClass> final :
public SequentialWriter
224 SequenceInfo
info() const final {
225 return {qMetaTypeId<TClass>(),
false};
231 _data->
append(value.template value<TClass>());
239 class SequentialWriterImpl<
QList,
QVariant> final :
public SequentialWriter
242 SequentialWriterImpl(QVariantList *data);
244 SequenceInfo
info() const final;
245 void reserve(
int size) final;
246 void add(const
QVariant &value) final;
258 AssociativeWriterImpl(QVariantMap *data);
260 AssociationInfo
info() const final;
271 AssociativeWriterImpl(QVariantHash *data);
273 AssociationInfo
info() const final;
282 template<template<typename> class TContainer, typename TClass>
285 registerWriter(qMetaTypeId<TContainer<TClass>>(),
286 new Implementations::SequentialWriterFactoryImpl<TContainer, TClass>{});
289 template<
template<
typename,
typename>
class TContainer,
typename TKey,
typename TValue>
292 registerWriter(qMetaTypeId<TContainer<TKey, TValue>>(),
293 new Implementations::AssociativeWriterFactoryImpl<TContainer, TKey, TValue>{});
298 #endif // QTJSONSERIALIZER_METAWRITERS_H