Browse Source

Introduce serializers registry

- Create base interface of serializers registry
- Use serializers from serializers registry instead of creation in place
Alexey Edelev 5 years ago
parent
commit
94e86b297d

+ 4 - 0
src/grpc/qabstractgrpcchannel.h

@@ -28,6 +28,7 @@
 #include <QString>
 #include <QByteArray>
 #include <functional>
+#include <memory>
 
 #include "qgrpcstatus.h"
 #include "qtgrpcglobal.h"
@@ -36,6 +37,7 @@ namespace QtProtobuf {
 
 class QGrpcAsyncReply;
 class QAbstractGrpcClient;
+class QAbstractProtobufSerializer;
 /*!
  * \ingroup QtGrpc
  * \brief The QAbstractGrpcChannel class is interface that represents common gRPC channel functionality.
@@ -76,6 +78,8 @@ public:
      * \param[in] handler callback that will be called when message recevied from the server-stream
      */
     virtual void subscribe(const QString &method, const QString &service, const QByteArray &args, QAbstractGrpcClient *client, const std::function<void(const QByteArray &)> &handler) = 0;
+
+    virtual std::shared_ptr<QAbstractProtobufSerializer> serializer() const = 0;
 protected:
     QAbstractGrpcChannel() = default;
     virtual ~QAbstractGrpcChannel() = default;

+ 4 - 6
src/grpc/qabstractgrpcclient.cpp

@@ -25,8 +25,8 @@
 
 #include "qabstractgrpcclient.h"
 
-#include "qprotobufserializer.h"
 #include "qgrpcasyncreply.h"
+#include "qprotobufserializerregistry_p.h"
 
 #include <QTimer>
 
@@ -34,13 +34,12 @@ namespace QtProtobuf {
 class QAbstractGrpcClientPrivate final {
 public:
     QAbstractGrpcClientPrivate(const QString &service) : service(service) {
-        //TODO: probably this variable should be initialized by default serializer from registry
-        serializer.reset(new QProtobufSerializer);
+        serializer = QProtobufSerializerRegistry::instance().getSerializer("protobuf");
     }
 
     std::shared_ptr<QAbstractGrpcChannel> channel;
     const QString service;
-    std::unique_ptr<QAbstractProtobufSerializer> serializer;
+    std::shared_ptr<QAbstractProtobufSerializer> serializer;
 };
 }
 
@@ -57,8 +56,7 @@ QAbstractGrpcClient::~QAbstractGrpcClient()
 void QAbstractGrpcClient::attachChannel(const std::shared_ptr<QAbstractGrpcChannel> &channel)
 {
     d->channel = channel;
-    //TODO: construct serializer based on information recevied from channel
-    d->serializer.reset(new QProtobufSerializer);
+    d->serializer = channel->serializer();
 }
 
 QGrpcStatus QAbstractGrpcClient::call(const QString &method, const QByteArray &arg, QByteArray &ret)

+ 9 - 4
src/grpc/qgrpchttp2channel.cpp

@@ -33,14 +33,13 @@
 #include <QTimer>
 #include <QtEndian>
 
+#include <unordered_map>
+
 #include "qgrpcasyncreply.h"
 #include "qabstractgrpcclient.h"
 #include "abstractcredentials.h"
-
-#include <unordered_map>
-
+#include "qprotobufserializerregistry_p.h"
 #include "qtprotobuflogging.h"
-#include <qprotobufserializer.h>
 
 using namespace QtProtobuf;
 
@@ -297,3 +296,9 @@ void QGrpcHttp2Channel::abort(QGrpcAsyncReply *reply)
     reply->setData({});
     reply->error({QGrpcStatus::StatusCode::Aborted, QLatin1String("Call aborted by user or timeout")});
 }
+
+std::shared_ptr<QAbstractProtobufSerializer> QGrpcHttp2Channel::serializer() const
+{
+    //TODO: make selection based on credentials or channel settings
+    return QProtobufSerializerRegistry::instance().getSerializer("protobuf");
+}

+ 1 - 0
src/grpc/qgrpchttp2channel.h

@@ -47,6 +47,7 @@ public:
     QGrpcStatus call(const QString &method, const QString &service, const QByteArray &args, QByteArray &ret) override;
     void call(const QString &method, const QString &service, const QByteArray &args, QtProtobuf::QGrpcAsyncReply *reply) override;
     void subscribe(const QString &method, const QString &service, const QByteArray &args, QAbstractGrpcClient *client, const std::function<void (const QByteArray &)> &handler) override;
+    std::shared_ptr<QAbstractProtobufSerializer> serializer() const override;
 
 protected:
     void abort(QGrpcAsyncReply *reply) override;

+ 16 - 2
src/protobuf/CMakeLists.txt

@@ -28,7 +28,21 @@ file(GLOB HEADERS
     qtprotobuftypes.h
     qtprotobuflogging.h
     qprotobufobject.h
-    qprotobufserializerregistry.h
+    qprotobufserializerregistry_p.h
+    qqmllistpropertyconstructor.h
+    qabstractprotobufserializer.h
+    qabstractprotobufserializer_p.h
+    qprotobufserializer.h
+    qprotobufserializer_p.h
+    qprotobufjsonserializer.h
+    qprotobufselfcheckiterator.h
+    qprotobufregistrationhelper.h)
+
+file(GLOB PUBLIC_HEADERS
+    qtprotobufglobal.h
+    qtprotobuftypes.h
+    qtprotobuflogging.h
+    qprotobufobject.h
     qqmllistpropertyconstructor.h
     qabstractprotobufserializer.h
     qabstractprotobufserializer_p.h
@@ -42,7 +56,7 @@ target_compile_definitions(${TARGET} PRIVATE QT_BUILD_PROTOBUF_LIB PUBLIC QTPROT
     QTPROTOBUF_VERSION_MINOR=${PROJECT_VERSION_MINOR})
 
 add_library(${QTPROTOBUF_COMMON_NAMESPACE}::${TARGET} ALIAS ${TARGET})
-set_target_properties(${TARGET} PROPERTIES VERSION ${PROJECT_VERSION} PUBLIC_HEADER "${HEADERS}" OUTPUT_NAME ${TARGET})
+set_target_properties(${TARGET} PROPERTIES VERSION ${PROJECT_VERSION} PUBLIC_HEADER "${PUBLIC_HEADERS}" OUTPUT_NAME ${TARGET})
 target_include_directories(${TARGET} PUBLIC
     $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
     $<INSTALL_INTERFACE:${TARGET_INCLUDE_DIR}>

+ 34 - 1
src/protobuf/qprotobufserializerregistry.cpp

@@ -23,6 +23,39 @@
  * DEALINGS IN THE SOFTWARE.
  */
 
-#include "qprotobufserializerregistry.h"
+#include "qprotobufserializerregistry_p.h"
+#include "qprotobufserializer.h"
+#include "qprotobufjsonserializer.h"
+
+
+namespace QtProtobuf {
+class QProtobufSerializerRegistryPrivate {
+public:
+    QProtobufSerializerRegistryPrivate() {
+        serializers["protobuf"] = std::shared_ptr<QAbstractProtobufSerializer>(new QProtobufSerializer);
+        serializers["json"] = std::shared_ptr<QAbstractProtobufSerializer>(new QProtobufJsonSerializer);
+    }
+    std::unordered_map<QString/*id*/,  std::shared_ptr<QAbstractProtobufSerializer>> serializers;
+};
+}
 
 using namespace QtProtobuf;
+
+QProtobufSerializerRegistry::QProtobufSerializerRegistry() : d(new QProtobufSerializerRegistryPrivate)
+{
+
+}
+
+QProtobufSerializerRegistry::~QProtobufSerializerRegistry() = default;
+
+
+std::shared_ptr<QAbstractProtobufSerializer> QProtobufSerializerRegistry::getSerializer(const QString &id)
+{
+       return d->serializers.at(id); //throws
+}
+
+std::unique_ptr<QAbstractProtobufSerializer> QProtobufSerializerRegistry::acquireSerializer(const QString &/*id*/)
+{
+    Q_ASSERT_X(false, "QProtobufSerializerRegistry", "acquireSerializer is unimplemented");
+    return std::unique_ptr<QAbstractProtobufSerializer>();
+}

+ 20 - 6
src/protobuf/qprotobufserializerregistry.h → src/protobuf/qprotobufserializerregistry_p.h

@@ -38,21 +38,35 @@
 #include "qtprotobufglobal.h"
 
 namespace QtProtobuf {
+
+class QProtobufSerializerRegistryPrivate;
 /*!
  * \ingroup QtProtobuf
+ * \private
  * \brief The QProtobufSerializerRegistry class provides api to register serializers
- *        Loads serializer plugins and constructs them based on plugin identifier.
+ *        Loads serializer plugins and constructs serializer based on identifier.
  *
  */
-//TODO: this class is not implemented and is private
-class Q_PROTOBUF_EXPORT QProtobufSerializerRegistry
+
+class Q_PROTOBUF_EXPORT QProtobufSerializerRegistry final
 {
-    QProtobufSerializerRegistry() = delete;
-    ~QProtobufSerializerRegistry() = delete;
+public:
+    std::shared_ptr<QAbstractProtobufSerializer> getSerializer(const QString &id);
+    std::unique_ptr<QAbstractProtobufSerializer> acquireSerializer(const QString &id);
+
+    static QProtobufSerializerRegistry &instance() {
+        static QProtobufSerializerRegistry _instance;
+        return _instance;
+    }
+
+private:
+    QProtobufSerializerRegistry();
+    ~QProtobufSerializerRegistry();
 
     Q_DISABLE_COPY(QProtobufSerializerRegistry)
     QProtobufSerializerRegistry(QProtobufSerializerRegistry &&) = delete;
     QProtobufSerializerRegistry &operator =(QProtobufSerializerRegistry &&) = delete;
-public:
+
+    std::unique_ptr<QProtobufSerializerRegistryPrivate> d;
 };
 }