summaryrefslogtreecommitdiffstats
path: root/common/propertymapper.h
diff options
context:
space:
mode:
Diffstat (limited to 'common/propertymapper.h')
-rw-r--r--common/propertymapper.h86
1 files changed, 86 insertions, 0 deletions
diff --git a/common/propertymapper.h b/common/propertymapper.h
new file mode 100644
index 0000000..0c6c16f
--- /dev/null
+++ b/common/propertymapper.h
@@ -0,0 +1,86 @@
1/*
2 * Copyright (C) 2014 Christian Mollekopf <chrigi_1@fastmail.fm>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#pragma once
21
22#include <QVariant>
23#include <QByteArray>
24#include <functional>
25#include <flatbuffers/flatbuffers.h>
26
27/**
28 * Defines how to convert qt primitives to flatbuffer ones
29 */
30template <class T>
31flatbuffers::uoffset_t variantToProperty(const QVariant &, flatbuffers::FlatBufferBuilder &fbb);
32
33/**
34 * Defines how to convert flatbuffer primitives to qt ones
35 */
36template <typename T>
37QVariant propertyToVariant(const flatbuffers::String *);
38
39
40/**
41 * The property mapper is a non-typesafe virtual dispatch.
42 *
43 * Instead of using an interface and requring each implementation to override
44 * a virtual method per property, the property mapper can be filled with accessors
45 * that extract the properties from resource types.
46 */
47template<typename BufferType>
48class ReadPropertyMapper
49{
50public:
51 virtual QVariant getProperty(const QByteArray &key, BufferType const *buffer) const
52 {
53 if (mReadAccessors.contains(key)) {
54 auto accessor = mReadAccessors.value(key);
55 return accessor(buffer);
56 }
57 return QVariant();
58 }
59 bool hasMapping(const QByteArray &key) const { return mReadAccessors.contains(key); }
60 QList<QByteArray> availableProperties() const { return mReadAccessors.keys(); }
61 void addMapping(const QByteArray &property, const std::function<QVariant(BufferType const *)> &mapping) {
62 mReadAccessors.insert(property, mapping);
63 }
64private:
65 QHash<QByteArray, std::function<QVariant(BufferType const *)> > mReadAccessors;
66};
67
68template<typename BufferBuilder>
69class WritePropertyMapper
70{
71public:
72 virtual void setProperty(const QByteArray &key, const QVariant &value, QList<std::function<void(BufferBuilder &)> > &builderCalls, flatbuffers::FlatBufferBuilder &fbb) const
73 {
74 if (mWriteAccessors.contains(key)) {
75 auto accessor = mWriteAccessors.value(key);
76 builderCalls << accessor(value, fbb);
77 }
78 }
79 bool hasMapping(const QByteArray &key) const { return mWriteAccessors.contains(key); }
80 void addMapping(const QByteArray &property, const std::function<std::function<void(BufferBuilder &)>(const QVariant &, flatbuffers::FlatBufferBuilder &)> &mapping) {
81 mWriteAccessors.insert(property, mapping);
82 }
83private:
84 QHash<QByteArray, std::function<std::function<void(BufferBuilder &)>(const QVariant &, flatbuffers::FlatBufferBuilder &)> > mWriteAccessors;
85};
86