File: Property.h

package info (click to toggle)
veyon 4.5.3%2Brepack1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 10,884 kB
  • sloc: cpp: 44,678; ansic: 6,852; python: 218; makefile: 200; sh: 47
file content (181 lines) | stat: -rw-r--r-- 5,112 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
 * Configuration/Property.h - Configuration::Property class
 *
 * Copyright (c) 2019-2021 Tobias Junghans <tobydox@veyon.io>
 *
 * This file is part of Veyon - https://veyon.io
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program (see COPYING); if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 */

#pragma once

#include <QUuid>

#include "QtCompat.h"
#include "VeyonCore.h"
#include "Configuration/Password.h"

namespace Configuration
{

class Object;
class Proxy;

template<typename T, typename = void> struct CheapestType          { using Type = const T &; };
template<typename T> struct CheapestType<T, typename std::enable_if<std::is_enum<T>::value >::type> { using Type = T; };
template<>           struct CheapestType<bool>    { using Type = bool; };
template<>           struct CheapestType<quint8>  { using Type = quint8; };
template<>           struct CheapestType<quint16> { using Type = quint16; };
template<>           struct CheapestType<quint32> { using Type = quint32; };
template<>           struct CheapestType<quint64> { using Type = quint64; };
template<>           struct CheapestType<qint8>   { using Type = qint8; };
template<>           struct CheapestType<qint16>  { using Type = qint16; };
template<>           struct CheapestType<qint32>  { using Type = qint32; };
template<>           struct CheapestType<qint64>  { using Type = qint64; };
template<>           struct CheapestType<float>   { using Type = float; };
template<>           struct CheapestType<double>  { using Type = double; };
template<>           struct CheapestType<QUuid>   { using Type = QUuid; };
template<typename T> struct CheapestType<T *>     { using Type = T *; };


class VEYON_CORE_EXPORT Property : public QObject
{
	Q_OBJECT
public:
	enum class Flag
	{
		Standard = 0x01,
		Advanced = 0x02,
		Hidden = 0x04,
		Legacy = 0x08
	};
	Q_DECLARE_FLAGS(Flags, Flag)

	// work around QTBUG-47652 where Q_FLAG() is broken for enum classes when using Qt < 5.12
#if QT_VERSION >= QT_VERSION_CHECK(5, 12, 0)
	Q_FLAG(Flags)
#endif

	Property( Object* object, const QString& key, const QString& parentKey,
			  const QVariant& defaultValue, Flags flags );

	Property( Proxy* proxy, const QString& key, const QString& parentKey,
			  const QVariant& defaultValue, Flags flags );

	QObject* lambdaContext() const;

	QVariant variantValue() const;

	void setVariantValue( const QVariant& value ) const;

	static Property* find( QObject* parent, const QString& key, const QString& parentKey );

	const QString& key() const
	{
		return m_key;
	}

	const QString& parentKey() const
	{
		return m_parentKey;
	}

	QString absoluteKey() const
	{
		if( m_parentKey.isEmpty() )
		{
			return m_key;
		}

		return m_parentKey + QLatin1Char('/') + m_key;
	}

	const QVariant& defaultValue() const
	{
		return m_defaultValue;
	}

	Flags flags() const
	{
		return m_flags;
	}

private:
	Object* m_object;
	Proxy* m_proxy;
	const QString m_key;
	const QString m_parentKey;
	const QVariant m_defaultValue;
	const Flags m_flags;

};


template<class T>
class VEYON_CORE_EXPORT TypedProperty : public Property {
public:
	using Type = typename CheapestType<T>::Type;

	TypedProperty( Object* object, const QString& key, const QString& parentKey,
				   const QVariant& defaultValue, Flags flags ) :
		Property( object, key, parentKey, defaultValue, flags )
	{
	}

	TypedProperty( Proxy* proxy, const QString& key, const QString& parentKey,
				   const QVariant& defaultValue, Flags flags ) :
		Property( proxy, key, parentKey, defaultValue, flags )
	{
	}

	T value() const
	{
		return QVariantHelper<T>::value( variantValue() );
	}

	void setValue( Type value ) const
	{
		setVariantValue( QVariant::fromValue( value ) );
	}
};

template<>
VEYON_CORE_EXPORT Password TypedProperty<Password>::value() const;

template<>
VEYON_CORE_EXPORT void TypedProperty<Password>::setValue( const Password& value ) const;


#define DECLARE_CONFIG_PROPERTY(className,config,type, name, setter, key, parentKey, defaultValue, flags) \
	private: \
		const Configuration::TypedProperty<type>* m_##name{ new Configuration::TypedProperty<type>( this, QStringLiteral(key), QStringLiteral(parentKey), defaultValue, flags ) }; \
	public: \
		type name() const \
		{											\
			return m_##name->value();			\
		} \
		const Configuration::TypedProperty<type>& name##Property() const \
		{ \
			return *m_##name; \
		} \
		void setter( Configuration::TypedProperty<type>::Type value ) const \
		{ \
			m_##name->setValue( value ); \
		}

}