From 9ae4fcadc9200f1c9cf6322b98449b0de69112ac Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Mon, 1 Jun 2015 09:26:31 +0200 Subject: Generalized creating the buffer. --- common/domainadaptor.h | 36 +++++++++++++++++++- examples/dummyresource/domainadaptor.cpp | 56 ++++++++++---------------------- examples/dummyresource/domainadaptor.h | 19 ++++++++++- 3 files changed, 71 insertions(+), 40 deletions(-) diff --git a/common/domainadaptor.h b/common/domainadaptor.h index c9a3a01..ca89fba 100644 --- a/common/domainadaptor.h +++ b/common/domainadaptor.h @@ -30,6 +30,7 @@ #include "entitybuffer.h" #include "propertymapper.h" #include "domain/event.h" +#include "log.h" /** * Create a buffer from a domain object using the provided mappings @@ -57,6 +58,25 @@ flatbuffers::Offset createBufferPart(const Akonadi2::ApplicationDomain:: return builder.Finish(); } +/** + * Create the buffer and finish the FlatBufferBuilder. + * + * After this the buffer can be extracted from the FlatBufferBuilder object. + */ +template +static void createBufferPartBuffer(const Akonadi2::ApplicationDomain::ApplicationDomainType &domainObject, flatbuffers::FlatBufferBuilder &fbb, WritePropertyMapper &mapper) +{ + auto pos = createBufferPart(domainObject, fbb, mapper); + // Because we cannot template the following call + // Akonadi2::ApplicationDomain::Buffer::FinishEventBuffer(fbb, pos); + // FIXME: This means all buffers in here must have the AKFB identifier + fbb.Finish(pos, "AKFB"); + flatbuffers::Verifier verifier(fbb.GetBufferPointer(), fbb.GetSize()); + if (!verifier.VerifyBuffer()) { + Warning() << "Created invalid uffer"; + } +} + /** * A generic adaptor implementation that uses a property mapper to read/write values. * @@ -149,7 +169,21 @@ public: return adaptor; } - virtual void createBuffer(const DomainType &domainType, flatbuffers::FlatBufferBuilder &fbb) Q_DECL_OVERRIDE {}; + virtual void createBuffer(const DomainType &domainObject, flatbuffers::FlatBufferBuilder &fbb) Q_DECL_OVERRIDE + { + flatbuffers::FlatBufferBuilder localFbb; + if (mLocalWriteMapper) { + createBufferPartBuffer(domainObject, localFbb, *mLocalWriteMapper); + } + + flatbuffers::FlatBufferBuilder resFbb; + if (mResourceWriteMapper) { + createBufferPartBuffer(domainObject, resFbb, *mResourceWriteMapper); + } + + Akonadi2::EntityBuffer::assembleEntityBuffer(fbb, 0, 0, resFbb.GetBufferPointer(), resFbb.GetSize(), localFbb.GetBufferPointer(), localFbb.GetSize()); + } + protected: QSharedPointer > mLocalMapper; diff --git a/examples/dummyresource/domainadaptor.cpp b/examples/dummyresource/domainadaptor.cpp index 8649bc3..67595af 100644 --- a/examples/dummyresource/domainadaptor.cpp +++ b/examples/dummyresource/domainadaptor.cpp @@ -1,23 +1,29 @@ +/* + * Copyright (C) 2015 Christian Mollekopf + * + * 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; if not, write to the + * Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ #include "domainadaptor.h" -#include -#include - #include "dummycalendar_generated.h" -#include "event_generated.h" -#include "entity_generated.h" -#include "metadata_generated.h" -#include "domainadaptor.h" -#include "log.h" -#include using namespace DummyCalendar; using namespace flatbuffers; - - - DummyEventAdaptorFactory::DummyEventAdaptorFactory() : DomainTypeAdaptorFactory() { @@ -32,29 +38,3 @@ DummyEventAdaptorFactory::DummyEventAdaptorFactory() }); } - -void DummyEventAdaptorFactory::createBuffer(const Akonadi2::ApplicationDomain::Event &event, flatbuffers::FlatBufferBuilder &fbb) -{ - flatbuffers::FlatBufferBuilder localFbb; - if (mLocalWriteMapper) { - auto pos = createBufferPart(event, localFbb, *mLocalWriteMapper); - Akonadi2::ApplicationDomain::Buffer::FinishEventBuffer(localFbb, pos); - flatbuffers::Verifier verifier(localFbb.GetBufferPointer(), localFbb.GetSize()); - if (!verifier.VerifyBuffer()) { - Warning() << "Created invalid local buffer"; - } - } - - flatbuffers::FlatBufferBuilder resFbb; - if (mResourceWriteMapper) { - auto pos = createBufferPart(event, resFbb, *mResourceWriteMapper); - DummyCalendar::FinishDummyEventBuffer(resFbb, pos); - flatbuffers::Verifier verifier(resFbb.GetBufferPointer(), resFbb.GetSize()); - if (!verifier.VerifyBuffer()) { - Warning() << "Created invalid resource buffer"; - } - } - - Akonadi2::EntityBuffer::assembleEntityBuffer(fbb, 0, 0, resFbb.GetBufferPointer(), resFbb.GetSize(), localFbb.GetBufferPointer(), localFbb.GetSize()); -} - diff --git a/examples/dummyresource/domainadaptor.h b/examples/dummyresource/domainadaptor.h index 0490387..8b6d96b 100644 --- a/examples/dummyresource/domainadaptor.h +++ b/examples/dummyresource/domainadaptor.h @@ -1,3 +1,21 @@ +/* + * Copyright (C) 2015 Christian Mollekopf + * + * 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; if not, write to the + * Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ #pragma once #include "common/domainadaptor.h" @@ -10,5 +28,4 @@ class DummyEventAdaptorFactory : public DomainTypeAdaptorFactory