Commit 0d309fc8 authored by Jochen Becher's avatar Jochen Becher
Browse files

ModelEditor: Introduce newest version of qtserialization framework



This change unfortunately contains many single changes because it is an
update of the 3rd party component:

* Introduce new Parameter class for better serialization of containers
and other future enhancements
* Delete dead code
* Introduce user data in base archive class
* Fix minor source code issues and typos in comments
* Remove unused and conceptionally broken support of forward refernces
* Fix a memory leak in QXmlInArchive
* prefer load()/save() methods over stream operators
* throw exception on unsuported forward references in QXmlOutArchive
* refactor serialization of pointer types into own header
* remove unneeded specialisation for serialization of free functions
* check for default value of serialization using a getter

Change-Id: Ic197a92b00b22b85dab4a1b88c431cf4da8b3c3b
Reviewed-by: default avatarTobias Hunger <tobias.hunger@theqtcompany.com>
parent f4628dcc
......@@ -55,12 +55,24 @@ void save(Archive &archive, const T &t)
Access<Archive, T>::save(archive, t);
}
template<class Archive, class T>
void save(Archive &archive, const T &t, const Parameters &)
{
save(archive, t);
}
template<class Archive, class T>
void load(Archive &archive, T &t)
{
Access<Archive, T>::load(archive, t);
}
template<class Archive, class T>
void load(Archive &archive, T &t, const Parameters &)
{
load(archive, t);
}
template<class Archive, class T>
void serialize(Archive &archive, T &t)
{
......@@ -92,30 +104,9 @@ void serialize_helper(Archive &archive, T &t)
static inline void serialize(Archive &archive, TYPE &); \
};
#if 0
#define QARK_ACCESS_SPECIALIZE_LOAD_SAVE(INARCHIVE, OUTARCHIVE, TYPE) \
template<> class Access<INARCHIVE, TYPE> { public: static inline void load(INARCHIVE &archive, TYPE &); void serialize(INARCHIVE &, TYPE &); }; \
template<> class Access<OUTARCHIVE, TYPE> { public: static inline void save(OUTARCHIVE &archive, const TYPE &); void serialize(OUTARCHIVE &, TYPE &); }; \
void Access<INARCHIVE, TYPE>::serialize(INARCHIVE &, TYPE &) { } \
void Access<OUTARCHIVE, TYPE>::serialize(OUTARCHIVE &, TYPE &) { } \
template class Access<INARCHIVE, TYPE>; \
template class Access<OUTARCHIVE, TYPE>;
#endif
#define QARK_ACCESS_SPECIALIZE(INARCHIVE, OUTARCHIVE, TYPE) \
template class Access<INARCHIVE, TYPE>; \
template class Access<OUTARCHIVE, TYPE>;
#if 0
#define QARK_SPECIALIZE_SERIALIZE(INARCHIVE, OUTARCHIVE, TYPE) \
QARK_ACCESS_SPECIALIZE(INARCHIVE, OUTARCHIVE, TYPE); \
template void serialize<INARCHIVE, TYPE>(INARCHIVE &, TYPE &); \
template void serialize<OUTARCHIVE, TYPE>(OUTARCHIVE &, TYPE &);
#define QARK_SPECIALIZE_LOAD_SAVE(INARCHIVE, OUTARCHIVE, TYPE) \
template void load<INARCHIVE, TYPE>(INARCHIVE &, TYPE &); \
template void save<OUTARCHIVE, TYPE>(OUTARCHIVE &, const TYPE &);
#endif
#endif // QARK_ACCESS_H
......@@ -33,6 +33,10 @@
#include "flag.h"
#include <QVariant>
#include <QString>
#include <QHash>
namespace qark {
class ArchiveBasics
......@@ -48,8 +52,40 @@ public:
bool takeFlag(const Flag &flag) { bool f = (_flags & flag.getMask()) != 0; _flags &= ~flag.getMask(); return f; }
bool hasUserData(const QString &key)
{
return _user_data.contains(key);
}
template<typename T>
T getUserData(const QString &key)
{
return _user_data.value(key).value<T>();
}
template<typename T>
T getUserData(const QString &key, const T &default_value)
{
// gcc 4.8.2 fails to compile if the following 2 statements are written in one expression
//return _user_data.value(key, data).value<T>();
QVariant v = _user_data.value(key, default_value);
return v.value<T>();
}
template<class T>
void setUserData(const QString &key, const T &data)
{
_user_data.insert(key, data);
}
void removeUserData(const QString &key)
{
_user_data.remove(key);
}
private:
Flag::mask_type _flags;
QHash<QString, QVariant> _user_data;
};
}
......
......@@ -31,6 +31,8 @@
#ifndef QARK_ATTRIBUTE_H
#define QARK_ATTRIBUTE_H
#include "parameters.h"
#include <QString>
namespace qark {
......@@ -38,19 +40,29 @@ namespace qark {
template<typename T>
class Attr {
public:
explicit Attr(const QString &qualified_name, T *value)
Attr(const QString &qualified_name, T *value)
: _qualified_name(qualified_name),
_value(value)
{
}
Attr(const QString &qualified_name, T *value, const Parameters &parameters)
: _qualified_name(qualified_name),
_value(value),
_parameters(parameters)
{
}
const QString &getQualifiedName() const { return _qualified_name; }
T *getValue() const { return _value; }
Parameters getParameters() const { return _parameters; }
private:
QString _qualified_name;
T *_value;
Parameters _parameters;
};
template<typename T>
......@@ -59,33 +71,56 @@ Attr<T * const> attr(const QString &qualified_name, T * const &value)
return Attr<T * const>(qualified_name, &value);
}
template<typename T>
Attr<T * const> attr(const QString &qualified_name, T * const &value, const Parameters &parameters)
{
return Attr<T * const>(qualified_name, &value, parameters);
}
template<typename T>
Attr<T> attr(const QString &qualified_name, T &value)
{
return Attr<T>(qualified_name, &value);
}
template<typename T>
Attr<T> attr(const QString &qualified_name, T &value, const Parameters &parameters)
{
return Attr<T>(qualified_name, &value, parameters);
}
template<class U, typename T>
class GetterAttr {
public:
explicit GetterAttr(const QString &qualified_name, const U &u, T (U::*getter)() const)
GetterAttr(const QString &qualified_name, const U &u, T (U::*getter)() const)
: _qualified_name(qualified_name),
_u(u),
_getter(getter)
{
}
GetterAttr(const QString &qualified_name, const U &u, T (U::*getter)() const, const Parameters &parameters)
: _qualified_name(qualified_name),
_u(u),
_getter(getter),
_parameters(parameters)
{
}
const QString &getQualifiedName() const { return _qualified_name; }
const U &getObject() const { return _u; }
T (U::*getGetter() const)() const { return _getter; }
Parameters getParameters() const { return _parameters; }
private:
QString _qualified_name;
const U &_u;
T (U::*_getter)() const;
Parameters _parameters;
};
template<class U, typename T>
......@@ -94,27 +129,44 @@ GetterAttr<U, T> attr(const QString &qualified_name, const U &u, T (U::*getter)(
return GetterAttr<U, T>(qualified_name, u, getter);
}
template<class U, typename T>
GetterAttr<U, T> attr(const QString &qualified_name, const U &u, T (U::*getter)() const, const Parameters &parameters)
{
return GetterAttr<U, T>(qualified_name, u, getter, parameters);
}
template<class U, typename T>
class SetterAttr {
public:
explicit SetterAttr(const QString &qualified_name, U &u, void (U::*setter)(T))
SetterAttr(const QString &qualified_name, U &u, void (U::*setter)(T))
: _qualified_name(qualified_name),
_u(u),
_setter(setter)
{
}
SetterAttr(const QString &qualified_name, U &u, void (U::*setter)(T), const Parameters &parameters)
: _qualified_name(qualified_name),
_u(u),
_setter(setter),
_parameters(parameters)
{
}
const QString &getQualifiedName() const { return _qualified_name; }
U &getObject() const { return _u; }
void (U::*getSetter() const)(T) { return _setter; }
Parameters getParameters() const { return _parameters; }
private:
QString _qualified_name;
U &_u;
void (U::*_setter)(T);
Parameters _parameters;
};
template<class U, typename T>
......@@ -123,11 +175,17 @@ SetterAttr<U, T> attr(const QString &qualified_name, U &u, void (U::*setter)(T))
return SetterAttr<U, T>(qualified_name, u, setter);
}
template<class U, typename T>
SetterAttr<U, T> attr(const QString &qualified_name, U &u, void (U::*setter)(T), const Parameters &parameters)
{
return SetterAttr<U, T>(qualified_name, u, setter, parameters);
}
template<class U, typename T, typename V>
class GetterSetterAttr {
public:
explicit GetterSetterAttr(const QString &qualified_name, U &u, T (U::*getter)() const, void (U::*setter)(V))
GetterSetterAttr(const QString &qualified_name, U &u, T (U::*getter)() const, void (U::*setter)(V))
: _qualified_name(qualified_name),
_u(u),
_getter(getter),
......@@ -135,6 +193,15 @@ public:
{
}
GetterSetterAttr(const QString &qualified_name, U &u, T (U::*getter)() const, void (U::*setter)(V), const Parameters &parameters)
: _qualified_name(qualified_name),
_u(u),
_getter(getter),
_setter(setter),
_parameters(parameters)
{
}
const QString &getQualifiedName() const { return _qualified_name; }
U &getObject() const { return _u; }
......@@ -143,11 +210,14 @@ public:
void (U::*getSetter() const)(V) { return _setter; }
Parameters getParameters() const { return _parameters; }
private:
QString _qualified_name;
U &_u;
T (U::*_getter)() const;
void (U::*_setter)(V);
Parameters _parameters;
};
template<class U, typename T, typename V>
......@@ -156,27 +226,44 @@ GetterSetterAttr<U, T, V> attr(const QString &qualified_name, U &u, T (U::*gette
return GetterSetterAttr<U, T, V>(qualified_name, u, getter, setter);
}
template<class U, typename T, typename V>
GetterSetterAttr<U, T, V> attr(const QString &qualified_name, U &u, T (U::*getter)() const, void (U::*setter)(V), const Parameters &parameters)
{
return GetterSetterAttr<U, T, V>(qualified_name, u, getter, setter, parameters);
}
template<class U, typename T>
class GetFuncAttr {
public:
explicit GetFuncAttr(const QString &qualified_name, U &u, T (*get_func)(const U &))
GetFuncAttr(const QString &qualified_name, U &u, T (*get_func)(const U &))
: _qualified_name(qualified_name),
_u(u),
_get_func(get_func)
{
}
GetFuncAttr(const QString &qualified_name, U &u, T (*get_func)(const U &), const Parameters &parameters)
: _qualified_name(qualified_name),
_u(u),
_get_func(get_func),
_parameters(parameters)
{
}
const QString &getQualifiedName() const { return _qualified_name; }
U &getObject() const { return _u; }
T (*getGetFunc() const)(const U &) { return _get_func; }
Parameters getParameters() const { return _parameters; }
private:
QString _qualified_name;
U &_u;
T (*_get_func)(const U &);
Parameters _parameters;
};
template<class U, typename T>
......@@ -185,27 +272,44 @@ GetFuncAttr<U, T> attr(const QString &qualified_name, const U &u, T (*get_func)(
return GetFuncAttr<U, T>(qualified_name, u, get_func);
}
template<class U, typename T>
GetFuncAttr<U, T> attr(const QString &qualified_name, const U &u, T (*get_func)(const U &), const Parameters &parameters)
{
return GetFuncAttr<U, T>(qualified_name, u, get_func, parameters);
}
template<class U, typename T>
class SetFuncAttr {
public:
explicit SetFuncAttr(const QString &qualified_name, U &u, void (*set_func)(U &, T))
SetFuncAttr(const QString &qualified_name, U &u, void (*set_func)(U &, T))
: _qualified_name(qualified_name),
_u(u),
_set_func(set_func)
{
}
SetFuncAttr(const QString &qualified_name, U &u, void (*set_func)(U &, T), const Parameters &parameters)
: _qualified_name(qualified_name),
_u(u),
_set_func(set_func),
_parameters(parameters)
{
}
const QString &getQualifiedName() const { return _qualified_name; }
U &getObject() const { return _u; }
void (*getSetFunc() const)(U &, T) { return _set_func; }
Parameters getParameters() const { return _parameters; }
private:
QString _qualified_name;
U &_u;
void (*_set_func)(U &, T);
Parameters _parameters;
};
template<class U, typename T>
......@@ -214,11 +318,17 @@ SetFuncAttr<U, T> attr(const QString &qualified_name, U &u, void (*set_func)(U &
return SetFuncAttr<U, T>(qualified_name, u, set_func);
}
template<class U, typename T>
SetFuncAttr<U, T> attr(const QString &qualified_name, U &u, void (*set_func)(U &, T), const Parameters &parameters)
{
return SetFuncAttr<U, T>(qualified_name, u, set_func, parameters);
}
template<class U, typename T, typename V>
class GetSetFuncAttr {
public:
explicit GetSetFuncAttr(const QString &qualified_name, U &u, T (*get_func)(const U &), void (*set_func)(U &, V))
GetSetFuncAttr(const QString &qualified_name, U &u, T (*get_func)(const U &), void (*set_func)(U &, V))
: _qualified_name(qualified_name),
_u(u),
_get_func(get_func),
......@@ -226,6 +336,15 @@ public:
{
}
GetSetFuncAttr(const QString &qualified_name, U &u, T (*get_func)(const U &), void (*set_func)(U &, V), const Parameters &parameters)
: _qualified_name(qualified_name),
_u(u),
_get_func(get_func),
_set_func(set_func),
_parameters(parameters)
{
}
const QString &getQualifiedName() const { return _qualified_name; }
U &getObject() const { return _u; }
......@@ -234,11 +353,14 @@ public:
void (*getSetFunc() const)(U &, V) { return _set_func; }
Parameters getParameters() const { return _parameters; }
private:
QString _qualified_name;
U &_u;
T (*_get_func)(const U &);
void (*_set_func)(U &, V);
Parameters _parameters;
};
template<class U, typename T, typename V>
......@@ -247,6 +369,12 @@ GetSetFuncAttr<U, T, V> attr(const QString &qualified_name, U &u, T (*get_func)(
return GetSetFuncAttr<U, T, V>(qualified_name, u, get_func, set_func);
}
template<class U, typename T, typename V>
GetSetFuncAttr<U, T, V> attr(const QString &qualified_name, U &u, T (*get_func)(const U &), void (*set_func)(U &, V), const Parameters &parameters)
{
return GetSetFuncAttr<U, T, V>(qualified_name, u, get_func, set_func, parameters);
}
}
#endif // QARK_ATTRIBUTE_H
......@@ -32,6 +32,7 @@
#define QARK_BASECLASS_H
#include "typeregistry.h"
#include "parameters.h"
#include <QString>
......@@ -41,21 +42,31 @@ namespace qark {
template<class BASE, class DERIVED>
class Base {
public:
explicit Base(const QString &qualified_name, DERIVED &obj)
Base(const QString &qualified_name, DERIVED &obj)
: _qualified_name(qualified_name),
_base(obj)
{
}
Base(const QString &qualified_name, DERIVED &obj, const Parameters &parameters)
: _qualified_name(qualified_name),
_base(obj),
_parameters(parameters)
{
}
const QString &getQualifiedName() const { return _qualified_name; }
const BASE &getBase() const { return _base; }
BASE &getBase() { return _base; }
Parameters getParameters() const { return _parameters; }
private:
QString _qualified_name;
BASE &_base;
Parameters _parameters;
};
template<class BASE, class DERIVED>
......@@ -64,18 +75,36 @@ Base<BASE, DERIVED> base(const QString &qualified_name, DERIVED &obj)
return Base<BASE, DERIVED>(qualified_name, obj);
}
template<class BASE, class DERIVED>
Base<BASE, DERIVED> base(const QString &qualified_name, DERIVED &obj, const Parameters &parameters)
{
return Base<BASE, DERIVED>(qualified_name, obj, parameters);
}
template<class BASE, class DERIVED>
Base<BASE, DERIVED> base(const QString &qualified_name, DERIVED *&obj)
{
return Base<BASE, DERIVED>(qualified_name, *obj);
}
template<class BASE, class DERIVED>
Base<BASE, DERIVED> base(const QString &qualified_name, DERIVED *&obj, const Parameters &parameters)
{
return Base<BASE, DERIVED>(qualified_name, *obj, parameters);
}
template<class BASE, class DERIVED>
Base<BASE, DERIVED> base(DERIVED &obj)
{
return Base<BASE, DERIVED>(QString(QStringLiteral("base-%1")).arg(get_type_uid<BASE>()), obj);
}
template<class BASE, class DERIVED>
Base<BASE, DERIVED> base(DERIVED &obj, const Parameters &parameters)
{
return Base<BASE, DERIVED>(QString(QStringLiteral("base-%1")).arg(get_type_uid<BASE>()), obj, parameters);
}
}
#endif // QARK_BASECLASS_H
......@@ -28,8 +28,8 @@
**
****************************************************************************/
#ifndef QMT_FRIEND_ACCESS_H
#define QMT_FRIEND_ACCESS_H
#ifndef QARK_FRIEND_ACCESS_H
#define QARK_FRIEND_ACCESS_H
#define QARK_FRIEND_ACCESS \
template<class Archive, class T> \
......
/***************************************************************************
**
** Copyright (C) 2015 Jochen Becher
** Contact: http://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 http://www.qt.io/terms-conditions. For further information
** use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, The Qt Company gives you certain additional
** rights. These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#ifndef QARK_PARAMETER_H
#define QARK_PARAMETER_H
#include "flag.h"
namespace qark {
class Parameters
{
public:
Parameters()
: _flags(0)
{