QtProtobuf  0.6
Protobuf plugin to generate Qt classes
baseprinter.h
1 /*
2  * MIT License
3  *
4  * Copyright (c) 2019 Alexey Edelev <semlanik@gmail.com>, Tatyana Borisova <tanusshhka@mail.ru>
5  *
6  * This file is part of QtProtobuf project https://git.semlanik.org/semlanik/qtprotobuf
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a copy of this
9  * software and associated documentation files (the "Software"), to deal in the Software
10  * without restriction, including without limitation the rights to use, copy, modify,
11  * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
12  * to permit persons to whom the Software is furnished to do so, subject to the following
13  * conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in all copies
16  * or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
19  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
20  * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
21  * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
22  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23  * DEALINGS IN THE SOFTWARE.
24  */
25 
26 #pragma once
27 
28 #include <google/protobuf/io/printer.h>
29 #include <google/protobuf/descriptor.h>
30 #include <memory>
31 
32 #include "utils.h"
33 #include "generatoroptions.h"
34 #include "generatorcommon.h"
35 
36 namespace google { namespace protobuf {
37 class FieldDescriptor;
38 class Descriptor;
39 class MethodDescriptor;
40 }}
41 
42 namespace QtProtobuf {
43 namespace generator {
44 
45 using PropertyMap = std::map<std::string, std::string>;
46 
52 class BasePrinter
53 {
54 public:
55  BasePrinter(const std::shared_ptr<::google::protobuf::io::Printer> &printer);
56  virtual ~BasePrinter() = default;
57 
58  void printPublicBlock();
59  void printPrivateBlock();
60  void printSignalsBlock();
61 
62  template<typename T>
63  void printComments(T *descriptor) {
64  if (!GeneratorOptions::instance().generateComments()) {
65  return;
66  }
67 
68  ::google::protobuf::SourceLocation loc;
69  descriptor->GetSourceLocation(&loc);
70 
71  utils::trim(loc.leading_comments);
72  if (loc.leading_comments.size() > 0) {
73  auto firstEntry = loc.leading_comments.find('\n');
74  bool isSingleLine = firstEntry == std::string::npos;
75 
76  if (loc.leading_comments[0] != '!' && loc.leading_comments[0] != '*' && loc.leading_comments[0] != ' ') {
77  loc.leading_comments = " " + loc.leading_comments;
78  if (!isSingleLine) {
79  loc.leading_comments = "\n" + loc.leading_comments;
80  }
81  }
82  mPrinter->Print("\n/*");
83  if (isSingleLine) {
84  mPrinter->Print(loc.leading_comments.c_str());
85  } else {
86  utils::replace(loc.leading_comments, "\n", "\n *");
87  mPrinter->Print(loc.leading_comments.c_str());
88  if (loc.leading_comments[loc.leading_comments.size() - 1] != '\n') {
89  mPrinter->Print("\n");
90  }
91  }
92  mPrinter->Print(" */");
93  }
94  }
95 
96  void Indent() {
97  mPrinter->Indent();
98  mPrinter->Indent();
99  }
100 
101  void Outdent() {
102  mPrinter->Outdent();
103  mPrinter->Outdent();
104  }
105 
106 protected:
107  std::shared_ptr<::google::protobuf::io::Printer> mPrinter;
108  std::vector<std::string> mNamespaces;
109 };
110 
111 } //namespace generator
112 } //namespace QtProtobuf