modernize: Reformat ALL the source... again!
[quassel.git] / src / common / syncableobject.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) version 3.                                           *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #pragma once
22
23 #include "common-export.h"
24
25 #include <QDataStream>
26 #include <QMetaType>
27 #include <QObject>
28 #include <QVariantMap>
29
30 #include "signalproxy.h"
31
32 /**
33  * This macro needs to be declared in syncable objects, next to the Q_OBJECT macro.
34  *
35  * @note: Specializations of a base syncable object for core/client must not use the macro;
36  *        i.e., if you have Foo, ClientFoo and/or CoreFoo, the SYNCABLE_OBJECT macro would
37  *        only be declared in the class declaration of Foo.
38  */
39 #define SYNCABLE_OBJECT                                                                                                                    \
40 public:                                                                                                                                    \
41     const QMetaObject* syncMetaObject() const final override { return &staticMetaObject; }                                                 \
42                                                                                                                                            \
43 private:
44
45 #define SYNC(...) sync_call__(SignalProxy::Server, __func__, __VA_ARGS__);
46 #define REQUEST(...) sync_call__(SignalProxy::Client, __func__, __VA_ARGS__);
47
48 #define SYNC_OTHER(x, ...) sync_call__(SignalProxy::Server, #x, __VA_ARGS__);
49 #define REQUEST_OTHER(x, ...) sync_call__(SignalProxy::Client, #x, __VA_ARGS__);
50
51 #define ARG(x) const_cast<void*>(reinterpret_cast<const void*>(&x))
52 #define NO_ARG 0
53
54 class COMMON_EXPORT SyncableObject : public QObject
55 {
56     Q_OBJECT
57
58 public:
59     SyncableObject(QObject* parent = nullptr);
60     SyncableObject(const QString& objectName, QObject* parent = nullptr);
61     SyncableObject(const SyncableObject& other, QObject* parent = nullptr);
62     ~SyncableObject() override;
63
64     //! Stores the object's state into a QVariantMap.
65     /** The default implementation takes dynamic properties as well as getters that have
66      *  names starting with "init" and stores them in a QVariantMap. Override this method in
67      *  derived classes in order to store the object state in a custom form.
68      *  \note  This is used by SignalProxy to transmit the state of the object to clients
69      *         that request the initial object state. Later updates use a different mechanism
70      *         and assume that the state is completely covered by properties and init* getters.
71      *         DO NOT OVERRIDE THIS unless you know exactly what you do!
72      *
73      *  \return The object's state in a QVariantMap
74      */
75     virtual QVariantMap toVariantMap();
76
77     //! Initialize the object's state from a given QVariantMap.
78     /** \see toVariantMap() for important information concerning this method.
79      */
80     virtual void fromVariantMap(const QVariantMap& properties);
81
82     virtual bool isInitialized() const;
83
84     virtual const QMetaObject* syncMetaObject() const { return metaObject(); }
85
86     inline void setAllowClientUpdates(bool allow) { _allowClientUpdates = allow; }
87     inline bool allowClientUpdates() const { return _allowClientUpdates; }
88
89 public slots:
90     virtual void setInitialized();
91     void requestUpdate(const QVariantMap& properties);
92     virtual void update(const QVariantMap& properties);
93
94 protected:
95     void sync_call__(SignalProxy::ProxyMode modeType, const char* funcname, ...) const;
96
97     void renameObject(const QString& newName);
98     SyncableObject& operator=(const SyncableObject& other);
99
100 signals:
101     void initDone();
102     void updatedRemotely();
103     void updated();
104
105 private:
106     void synchronize(SignalProxy* proxy);
107     void stopSynchronize(SignalProxy* proxy);
108
109     bool setInitValue(const QString& property, const QVariant& value);
110
111     bool _initialized{false};
112     bool _allowClientUpdates{false};
113
114     QList<SignalProxy*> _signalProxies;
115
116     friend class SignalProxy;
117 };