KItinerary

datatypes.h
1/*
2 SPDX-FileCopyrightText: 2018 Volker Krause <vkrause@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#pragma once
8
9#include "internal/parameter_type.h"
10
11#include <QMetaType>
12#include <QSharedDataPointer>
13#include <QVariant>
14
15class QString;
16
17namespace KItinerary {
18
19/** JSON-LD data type helper functions. */
20namespace JsonLd {
21
22/** Returns @c true if @p value is of type @p T. */
23template <typename T>
24inline bool isA(const QVariant &value)
25{
26 return value.userType() == qMetaTypeId<T>();
27}
28
29/** Checks if the given value can be up-cast to @p T */
30template <typename T>
31inline bool canConvert(const QVariant &value)
32{
33 const auto mt = QMetaType(value.userType());
34 // for enums/flags, this is the enclosing meta object starting with Qt6!
35 const auto mo = mt.metaObject();
36 if ((mt.flags() & QMetaType::IsGadget) == 0 || !mo) {
37 return false;
38 }
39 return mo->inherits(&T::staticMetaObject);
40}
41
42/** Up-cast @p value to @p T.
43 * @note This does not perform any safety checks!
44 * @see canConvert
45 */
46template <typename T>
47inline T convert(const QVariant &value)
48{
49 return T(*static_cast<const T*>(value.constData()));
50}
51
52}
53}
54
55/** Macro to mark a vocabulary type class.
56 * Adds Qt introspection, value type semantics, comparison operators and additional
57 * type information needed for JSON-LD (de)serialziation and QML access.
58 */
59#define KITINERARY_GADGET(Class) \
60 Q_GADGET \
61 Q_PROPERTY(QString className READ className STORED false CONSTANT) \
62 QString className() const; \
63public: \
64 Class(); \
65 Class(const Class &other); \
66 ~Class(); \
67 Class& operator=(const Class &other); \
68 bool operator<(const Class &other) const; \
69 bool operator==(const Class &other) const; \
70 inline bool operator!=(const Class &other) const { return !(*this == other); } \
71 operator QVariant () const; \
72 static const char* typeName(); \
73protected: \
74 Class(Class ## Private *dd); \
75private:
76
77/** Macro to add a vocabulary type property.
78 * This adds Qt introspection and declarations for getter and setter methods.
79 */
80#define KITINERARY_PROPERTY(Type, Name, SetName) \
81 Q_PROPERTY(Type Name READ Name WRITE SetName STORED true) \
82public: \
83 Type Name() const; \
84 void SetName(KItinerary::detail::parameter_type<Type>::type value); \
85private:
86
bool isA(const QVariant &value)
Returns true if value is of type T.
Definition datatypes.h:24
bool canConvert(const QVariant &value)
Checks if the given value can be up-cast to T.
Definition datatypes.h:31
T convert(const QVariant &value)
Up-cast value to T.
Definition datatypes.h:47
Classes for reservation/travel data models, data extraction and data augmentation.
Definition berelement.h:17
const void * constData() const const
int userType() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jun 14 2024 11:54:39 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.