diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-05-03 20:09:18 +0200 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-05-03 20:09:18 +0200 |
commit | c14f07a865940de86b229b4eba0d3bbb9b13967c (patch) | |
tree | cbb9a5a522d5462fc342b208f05cd109a2c77ec4 | |
parent | 9d079a832ec9c70fccb3446843bd8d7579e3aafd (diff) | |
download | sink-c14f07a865940de86b229b4eba0d3bbb9b13967c.tar.gz sink-c14f07a865940de86b229b4eba0d3bbb9b13967c.zip |
Support for QByteArrayList
-rw-r--r-- | common/propertymapper.cpp | 30 | ||||
-rw-r--r-- | common/propertymapper.h | 18 |
2 files changed, 47 insertions, 1 deletions
diff --git a/common/propertymapper.cpp b/common/propertymapper.cpp index ebe5cb3..130ba0b 100644 --- a/common/propertymapper.cpp +++ b/common/propertymapper.cpp | |||
@@ -48,6 +48,21 @@ flatbuffers::uoffset_t variantToProperty<QDateTime>(const QVariant &property, fl | |||
48 | } | 48 | } |
49 | 49 | ||
50 | template <> | 50 | template <> |
51 | flatbuffers::uoffset_t variantToProperty<QByteArrayList>(const QVariant &property, flatbuffers::FlatBufferBuilder &fbb) | ||
52 | { | ||
53 | if (property.isValid()) { | ||
54 | const auto list = property.value<QByteArrayList>(); | ||
55 | std::vector<flatbuffers::Offset<flatbuffers::String>> vector; | ||
56 | for (const auto value : list) { | ||
57 | auto offset = fbb.CreateString(value.toStdString()); | ||
58 | vector.push_back(offset); | ||
59 | } | ||
60 | return fbb.CreateVector(vector).o; | ||
61 | } | ||
62 | return 0; | ||
63 | } | ||
64 | |||
65 | template <> | ||
51 | QVariant propertyToVariant<QString>(const flatbuffers::String *property) | 66 | QVariant propertyToVariant<QString>(const flatbuffers::String *property) |
52 | { | 67 | { |
53 | if (property) { | 68 | if (property) { |
@@ -78,6 +93,21 @@ QVariant propertyToVariant<QByteArray>(const flatbuffers::Vector<uint8_t> *prope | |||
78 | } | 93 | } |
79 | 94 | ||
80 | template <> | 95 | template <> |
96 | QVariant propertyToVariant<QByteArrayList>(const flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>> *property) | ||
97 | { | ||
98 | if (property) { | ||
99 | QByteArrayList list; | ||
100 | for (auto it = property->begin(); it != property->end();) { | ||
101 | // We have to copy the memory, otherwise it would become eventually invalid | ||
102 | list << QString::fromStdString((*it)->c_str()).toUtf8(); | ||
103 | it.operator++(); | ||
104 | } | ||
105 | return QVariant::fromValue(list); | ||
106 | } | ||
107 | return QVariant(); | ||
108 | } | ||
109 | |||
110 | template <> | ||
81 | QVariant propertyToVariant<bool>(uint8_t property) | 111 | QVariant propertyToVariant<bool>(uint8_t property) |
82 | { | 112 | { |
83 | return static_cast<bool>(property); | 113 | return static_cast<bool>(property); |
diff --git a/common/propertymapper.h b/common/propertymapper.h index cf8ce7b..edbc7f1 100644 --- a/common/propertymapper.h +++ b/common/propertymapper.h | |||
@@ -40,7 +40,8 @@ template <typename T> | |||
40 | QVariant SINK_EXPORT propertyToVariant(uint8_t); | 40 | QVariant SINK_EXPORT propertyToVariant(uint8_t); |
41 | template <typename T> | 41 | template <typename T> |
42 | QVariant SINK_EXPORT propertyToVariant(const flatbuffers::Vector<uint8_t> *); | 42 | QVariant SINK_EXPORT propertyToVariant(const flatbuffers::Vector<uint8_t> *); |
43 | 43 | template <typename T> | |
44 | QVariant SINK_EXPORT propertyToVariant(const flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>> *); | ||
44 | 45 | ||
45 | /** | 46 | /** |
46 | * The property mapper is a non-typesafe virtual dispatch. | 47 | * The property mapper is a non-typesafe virtual dispatch. |
@@ -100,6 +101,12 @@ public: | |||
100 | addMapping(name, [f](Buffer const *buffer) -> QVariant { return propertyToVariant<T>((buffer->*f)()); }); | 101 | addMapping(name, [f](Buffer const *buffer) -> QVariant { return propertyToVariant<T>((buffer->*f)()); }); |
101 | } | 102 | } |
102 | 103 | ||
104 | template <typename T, typename Buffer> | ||
105 | void addMapping(const QByteArray &name, const flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>> *(Buffer::*f)() const) | ||
106 | { | ||
107 | addMapping(name, [f](Buffer const *buffer) -> QVariant { return propertyToVariant<T>((buffer->*f)()); }); | ||
108 | } | ||
109 | |||
103 | private: | 110 | private: |
104 | QHash<QByteArray, std::function<QVariant(BufferType const *)>> mReadAccessors; | 111 | QHash<QByteArray, std::function<QVariant(BufferType const *)>> mReadAccessors; |
105 | }; | 112 | }; |
@@ -160,6 +167,15 @@ public: | |||
160 | }); | 167 | }); |
161 | } | 168 | } |
162 | 169 | ||
170 | template <typename T> | ||
171 | void addMapping(const QByteArray &name, void (BufferBuilder::*f)(flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>>>)) | ||
172 | { | ||
173 | addMapping(name, [f](const QVariant &value, flatbuffers::FlatBufferBuilder &fbb) -> std::function<void(BufferBuilder &)> { | ||
174 | auto offset = variantToProperty<T>(value, fbb); | ||
175 | return [offset, f](BufferBuilder &builder) { (builder.*f)(offset); }; | ||
176 | }); | ||
177 | } | ||
178 | |||
163 | private: | 179 | private: |
164 | QHash<QByteArray, std::function<std::function<void(BufferBuilder &)>(const QVariant &, flatbuffers::FlatBufferBuilder &)>> mWriteAccessors; | 180 | QHash<QByteArray, std::function<std::function<void(BufferBuilder &)>(const QVariant &, flatbuffers::FlatBufferBuilder &)>> mWriteAccessors; |
165 | }; | 181 | }; |