Browse Source

Migate to shared pointers in generator

- Migate to shared pointers in generator. Preparation step
  for single-file generation
Alexey Edelev 5 years ago
parent
commit
956b4bc24e

+ 1 - 1
src/generator/classgeneratorbase.cpp

@@ -38,7 +38,7 @@ using namespace ::google::protobuf;
 using namespace ::google::protobuf::io;
 using namespace ::google::protobuf::compiler;
 
-ClassGeneratorBase::ClassGeneratorBase(const std::string &fullClassName, std::unique_ptr<::google::protobuf::io::ZeroCopyOutputStream> out) : mOutput(std::move(out))
+ClassGeneratorBase::ClassGeneratorBase(const std::string &fullClassName, const std::shared_ptr<::google::protobuf::io::ZeroCopyOutputStream> &out) : mOutput(out)
   , mPrinter(mOutput.get(), '$')
 {
     mPrinter.Print(Templates::DisclaimerTemplate);

+ 2 - 2
src/generator/classgeneratorbase.h

@@ -47,7 +47,7 @@ using PropertyMap = std::map<std::string, std::string>;
 class ClassGeneratorBase
 {
 public:
-    ClassGeneratorBase(const std::string &className, std::unique_ptr<google::protobuf::io::ZeroCopyOutputStream> out);
+    ClassGeneratorBase(const std::string &className, const std::shared_ptr<google::protobuf::io::ZeroCopyOutputStream> &out);
     virtual ~ClassGeneratorBase() = default;
     virtual void run() = 0;
 protected:
@@ -57,7 +57,7 @@ protected:
         NEIGHBOUR_ENUM
     };
 
-    std::unique_ptr<::google::protobuf::io::ZeroCopyOutputStream> mOutput;
+    std::shared_ptr<::google::protobuf::io::ZeroCopyOutputStream> mOutput;
     ::google::protobuf::io::Printer mPrinter;
     std::string mClassName;
     std::vector<std::string> mNamespaces;

+ 2 - 2
src/generator/classsourcegeneratorbase.cpp

@@ -36,8 +36,8 @@ using namespace ::google::protobuf::io;
 using namespace ::google::protobuf::compiler;
 
 ClassSourceGeneratorBase::ClassSourceGeneratorBase(const std::string &fullClassName,
-                                                   std::unique_ptr<::google::protobuf::io::ZeroCopyOutputStream> out) :
-    ClassGeneratorBase(fullClassName, std::move(out))
+                                                   const std::shared_ptr<::google::protobuf::io::ZeroCopyOutputStream> &out) :
+    ClassGeneratorBase(fullClassName, out)
 {
 
 }

+ 1 - 1
src/generator/classsourcegeneratorbase.h

@@ -36,7 +36,7 @@ namespace generator {
 class ClassSourceGeneratorBase : public ClassGeneratorBase
 {
 public:
-    ClassSourceGeneratorBase(const std::string &className, std::unique_ptr<google::protobuf::io::ZeroCopyOutputStream> out);
+    ClassSourceGeneratorBase(const std::string &className, const std::shared_ptr<google::protobuf::io::ZeroCopyOutputStream> &out);
     ~ClassSourceGeneratorBase() = default;
     virtual void run() = 0;
 

+ 2 - 2
src/generator/clientgenerator.cpp

@@ -40,8 +40,8 @@ using namespace ::QtProtobuf::generator;
 using namespace ::google::protobuf;
 using namespace ::google::protobuf::compiler;
 
-ClientGenerator::ClientGenerator(const ServiceDescriptor *service, std::unique_ptr<io::ZeroCopyOutputStream> out) :
-    ServiceGeneratorBase(service, std::move(out))
+ClientGenerator::ClientGenerator(const ServiceDescriptor *service, const std::shared_ptr<io::ZeroCopyOutputStream> &out) :
+    ServiceGeneratorBase(service, out)
 {
     mClassName += "Client";
 }

+ 1 - 1
src/generator/clientgenerator.h

@@ -42,7 +42,7 @@ class ClientGenerator : public ServiceGeneratorBase
 {
 public:
     ClientGenerator(const ::google::protobuf::ServiceDescriptor *service,
-                    std::unique_ptr<google::protobuf::io::ZeroCopyOutputStream> out);
+                    const std::shared_ptr<google::protobuf::io::ZeroCopyOutputStream> &out);
     ~ClientGenerator() = default;
 
     void run() {

+ 2 - 2
src/generator/clientsourcegenerator.cpp

@@ -35,8 +35,8 @@ using namespace ::google::protobuf;
 using namespace ::google::protobuf::compiler;
 
 ClientSourceGenerator::ClientSourceGenerator(const google::protobuf::ServiceDescriptor *service,
-                                             std::unique_ptr<google::protobuf::io::ZeroCopyOutputStream> out) :
-    ClassSourceGeneratorBase(service->full_name(), std::move(out))
+                                             const std::shared_ptr<google::protobuf::io::ZeroCopyOutputStream> &out) :
+    ClassSourceGeneratorBase(service->full_name(), out)
   , mService(service)
 {
     mClassName += "Client";

+ 1 - 1
src/generator/clientsourcegenerator.h

@@ -34,7 +34,7 @@ class ClientSourceGenerator : public ClassSourceGeneratorBase
 {
 public:
     ClientSourceGenerator(const google::protobuf::ServiceDescriptor *service,
-                          std::unique_ptr<google::protobuf::io::ZeroCopyOutputStream> out);
+                          const std::shared_ptr<google::protobuf::io::ZeroCopyOutputStream> &out);
     void run() override {
         printClassHeaderInclude();
         printUsingNamespaces({"QtProtobuf", mNamespacesColonDelimited});

+ 9 - 9
src/generator/generator.cpp

@@ -81,13 +81,13 @@ bool QtGenerator::Generate(const FileDescriptor *file,
 
         std::string filename = baseFilename + ".h";
         ProtobufClassGenerator classGen(message,
-                                        std::move(std::unique_ptr<io::ZeroCopyOutputStream>(generatorContext->Open(filename))));
+                                        std::shared_ptr<io::ZeroCopyOutputStream>(generatorContext->Open(filename)));
         classGen.run();
 
         filename = baseFilename + ".cpp";
-            ProtobufSourceGenerator classSourceGen(message,
-                                      std::move(std::unique_ptr<io::ZeroCopyOutputStream>(generatorContext->Open(filename))));
-            classSourceGen.run();
+        ProtobufSourceGenerator classSourceGen(message,
+                                               std::shared_ptr<io::ZeroCopyOutputStream>(generatorContext->Open(filename)));
+        classSourceGen.run();
     }
 
     for (int i = 0; i < file->service_count(); i++) {
@@ -97,17 +97,17 @@ bool QtGenerator::Generate(const FileDescriptor *file,
 
         std::string fullFilename = baseFilename + "server.h";
         ServerGenerator serverGen(service,
-                                  std::move(std::unique_ptr<io::ZeroCopyOutputStream>(generatorContext->Open(fullFilename))));
+                                  std::shared_ptr<io::ZeroCopyOutputStream>(generatorContext->Open(fullFilename)));
         serverGen.run();
 
         fullFilename = baseFilename + "client.h";
         ClientGenerator clientGen(service,
-                                  std::move(std::unique_ptr<io::ZeroCopyOutputStream>(generatorContext->Open(fullFilename))));
+                                  std::shared_ptr<io::ZeroCopyOutputStream>(generatorContext->Open(fullFilename)));
         clientGen.run();
 
         fullFilename = baseFilename + "client.cpp";
         ClientSourceGenerator clientSrcGen(service,
-                                           std::move(std::unique_ptr<io::ZeroCopyOutputStream>(generatorContext->Open(fullFilename))));
+                                           std::shared_ptr<io::ZeroCopyOutputStream>(generatorContext->Open(fullFilename)));
         clientSrcGen.run();
     }
     return true;
@@ -123,11 +123,11 @@ bool QtGenerator::GenerateAll(const std::vector<const FileDescriptor *> &files,
     }
 
     GlobalEnumsGenerator enumGen(packageList,
-                                 std::move(std::unique_ptr<io::ZeroCopyOutputStream>(generatorContext->Open(globalEnumsFilename + ".h"))));
+                                 std::shared_ptr<io::ZeroCopyOutputStream>(generatorContext->Open(globalEnumsFilename + ".h")));
     enumGen.run();
 
     GlobalEnumsSourceGenerator enumSourceGen(packageList,
-                                             std::move(std::unique_ptr<io::ZeroCopyOutputStream>(generatorContext->Open(globalEnumsFilename + ".cpp"))));
+                                             std::shared_ptr<io::ZeroCopyOutputStream>(generatorContext->Open(globalEnumsFilename + ".cpp")));
     enumSourceGen.run();
     return CodeGenerator::GenerateAll(files, parameter, generatorContext, error);
 

+ 1 - 0
src/generator/generator.h

@@ -26,6 +26,7 @@
 #pragma once
 
 #include <google/protobuf/compiler/code_generator.h>
+#include <google/protobuf/io/zero_copy_stream.h>
 #include <string>
 #include <memory>
 

+ 2 - 2
src/generator/globalenumsgenerator.cpp

@@ -32,8 +32,8 @@ using namespace ::google::protobuf;
 using namespace ::google::protobuf::io;
 using namespace ::google::protobuf::compiler;
 
-GlobalEnumsGenerator::GlobalEnumsGenerator(const PackagesList &packageList, std::unique_ptr<io::ZeroCopyOutputStream> out) :
-    ClassGeneratorBase(Templates::GlobalEnumClassNameTemplate, std::move(out))
+GlobalEnumsGenerator::GlobalEnumsGenerator(const PackagesList &packageList, const std::shared_ptr<io::ZeroCopyOutputStream> &out) :
+    ClassGeneratorBase(Templates::GlobalEnumClassNameTemplate, out)
   , mPackageList(packageList) {}
 
 void GlobalEnumsGenerator::startEnum(const std::vector<std::string>& namespaces) {

+ 1 - 1
src/generator/globalenumsgenerator.h

@@ -35,7 +35,7 @@ class GlobalEnumsGenerator : public ClassGeneratorBase
 {
     PackagesList mPackageList;
 public:
-    GlobalEnumsGenerator(const PackagesList &packageList, std::unique_ptr<::google::protobuf::io::ZeroCopyOutputStream> out);
+    GlobalEnumsGenerator(const PackagesList &packageList, const std::shared_ptr<google::protobuf::io::ZeroCopyOutputStream> &out);
     virtual ~GlobalEnumsGenerator() = default;
 
     void run();

+ 2 - 2
src/generator/globalenumssourcegenerator.cpp

@@ -32,8 +32,8 @@ using namespace ::google::protobuf;
 using namespace ::google::protobuf::io;
 using namespace ::google::protobuf::compiler;
 
-GlobalEnumsSourceGenerator::GlobalEnumsSourceGenerator(const PackagesList &packageList, std::unique_ptr<io::ZeroCopyOutputStream> out) :
-    ClassGeneratorBase(Templates::GlobalEnumClassNameTemplate, std::move(out))
+GlobalEnumsSourceGenerator::GlobalEnumsSourceGenerator(const PackagesList &packageList, const std::shared_ptr<io::ZeroCopyOutputStream> &out) :
+    ClassGeneratorBase(Templates::GlobalEnumClassNameTemplate, out)
   , mPackageList(packageList) {}
 
 void GlobalEnumsSourceGenerator::run() {

+ 1 - 1
src/generator/globalenumssourcegenerator.h

@@ -35,7 +35,7 @@ class GlobalEnumsSourceGenerator : public ClassGeneratorBase
 {
     PackagesList mPackageList;
 public:
-    GlobalEnumsSourceGenerator(const PackagesList &packageList, std::unique_ptr<::google::protobuf::io::ZeroCopyOutputStream> out);
+    GlobalEnumsSourceGenerator(const PackagesList &packageList, const std::shared_ptr<google::protobuf::io::ZeroCopyOutputStream> &out);
     virtual ~GlobalEnumsSourceGenerator() = default;
 
     void run() override;

+ 2 - 2
src/generator/protobufclassgenerator.cpp

@@ -35,8 +35,8 @@ using namespace ::google::protobuf;
 using namespace ::google::protobuf::io;
 using namespace ::google::protobuf::compiler;
 
-ProtobufClassGenerator::ProtobufClassGenerator(const Descriptor *message, std::unique_ptr<::google::protobuf::io::ZeroCopyOutputStream> out)
-    : ClassGeneratorBase(message->full_name(), std::move(out))
+ProtobufClassGenerator::ProtobufClassGenerator(const Descriptor *message, const std::shared_ptr<::google::protobuf::io::ZeroCopyOutputStream> &out)
+    : ClassGeneratorBase(message->full_name(), out)
     , mMessage(message)
 {
 }

+ 1 - 1
src/generator/protobufclassgenerator.h

@@ -47,7 +47,7 @@ class ProtobufClassGenerator : public ClassGeneratorBase
 {
     const ::google::protobuf::Descriptor *mMessage;
 public:
-    ProtobufClassGenerator(const ::google::protobuf::Descriptor *message, std::unique_ptr<::google::protobuf::io::ZeroCopyOutputStream> out);
+    ProtobufClassGenerator(const ::google::protobuf::Descriptor *message, const std::shared_ptr<google::protobuf::io::ZeroCopyOutputStream> &out);
     virtual ~ProtobufClassGenerator() = default;
 
     void run() override;

+ 2 - 2
src/generator/protobufsourcegenerator.cpp

@@ -34,8 +34,8 @@ using namespace ::google::protobuf::io;
 using namespace ::google::protobuf::compiler;
 
 ProtobufSourceGenerator::ProtobufSourceGenerator(const google::protobuf::Descriptor *message,
-                                                 std::unique_ptr<google::protobuf::io::ZeroCopyOutputStream> out) :
-    ClassSourceGeneratorBase(message->full_name(), std::move(out))
+                                                 const std::shared_ptr<google::protobuf::io::ZeroCopyOutputStream> &out) :
+    ClassSourceGeneratorBase(message->full_name(), out)
   , mMessage(message)
 {
 }

+ 1 - 1
src/generator/protobufsourcegenerator.h

@@ -34,7 +34,7 @@ class ProtobufSourceGenerator : public ClassSourceGeneratorBase
 {
     const google::protobuf::Descriptor *mMessage;
 public:
-    ProtobufSourceGenerator(const google::protobuf::Descriptor *message, std::unique_ptr<google::protobuf::io::ZeroCopyOutputStream> out);
+    ProtobufSourceGenerator(const google::protobuf::Descriptor *message, const std::shared_ptr<google::protobuf::io::ZeroCopyOutputStream> &out);
     void printRegisterBody();
     void printFieldsOrdering();
     void printConstructor();

+ 2 - 2
src/generator/servergenerator.cpp

@@ -40,8 +40,8 @@ using namespace ::QtProtobuf::generator;
 using namespace ::google::protobuf;
 using namespace ::google::protobuf::compiler;
 
-ServerGenerator::ServerGenerator(const ServiceDescriptor *service, std::unique_ptr<io::ZeroCopyOutputStream> out) :
-    ServiceGeneratorBase(service, std::move(out))
+ServerGenerator::ServerGenerator(const ServiceDescriptor *service, const std::shared_ptr<io::ZeroCopyOutputStream> &out) :
+    ServiceGeneratorBase(service, out)
   , mService(nullptr)
 {
     mClassName += "Server";

+ 1 - 1
src/generator/servergenerator.h

@@ -42,7 +42,7 @@ class ServerGenerator : public ServiceGeneratorBase
 {
     const google::protobuf::ServiceDescriptor *mService;
 public:
-    ServerGenerator(const google::protobuf::ServiceDescriptor *service, std::unique_ptr<google::protobuf::io::ZeroCopyOutputStream> out);
+    ServerGenerator(const google::protobuf::ServiceDescriptor *service, const std::shared_ptr<google::protobuf::io::ZeroCopyOutputStream> &out);
     virtual ~ServerGenerator() = default;
 
     void run() {

+ 2 - 2
src/generator/servicegeneratorbase.cpp

@@ -42,8 +42,8 @@ using namespace ::google::protobuf::compiler;
 using namespace QtProtobuf::generator;
 
 ServiceGeneratorBase::ServiceGeneratorBase(const ::google::protobuf::ServiceDescriptor *service,
-                                           std::unique_ptr<google::protobuf::io::ZeroCopyOutputStream> out) :
-    ClassGeneratorBase(service->full_name(), std::move(out))
+                                           const std::shared_ptr<google::protobuf::io::ZeroCopyOutputStream> &out) :
+    ClassGeneratorBase(service->full_name(), out)
   , mService(service)
 {
 }

+ 1 - 1
src/generator/servicegeneratorbase.h

@@ -45,7 +45,7 @@ protected:
 
 public:
     ServiceGeneratorBase(const ::google::protobuf::ServiceDescriptor *service,
-                         std::unique_ptr<google::protobuf::io::ZeroCopyOutputStream> out);
+                         const std::shared_ptr<google::protobuf::io::ZeroCopyOutputStream> &out);
     void run() = 0;
 
     void printIncludes();