QtProtobuf  0.6
Protobuf plugin to generate Qt classes
descriptorprinterbase.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 "baseprinter.h"
29 
30 #include "utils.h"
31 #include "templates.h"
32 #include "generatoroptions.h"
33 #include "generatorcommon.h"
34 
35 namespace QtProtobuf {
36 namespace generator {
37 
38 using PropertyMap = std::map<std::string, std::string>;
39 
46 template<typename T>
47 class DescriptorPrinterBase : public BasePrinter
48 {
49 public:
50  DescriptorPrinterBase(const T* descriptor, const std::shared_ptr<::google::protobuf::io::Printer> &printer) : BasePrinter(printer)
51  , mDescriptor(descriptor)
52  , mName(utils::upperCaseName(descriptor->name()))// TODO: migrate to typemaps for the client and service generators
53  {}
54  virtual ~DescriptorPrinterBase() = default;
55 public:
56  void encloseClass() {
57  mPrinter->Print(Templates::SemicolonBlockEnclosureTemplate);
58  mPrinter->Print("\n");
59  }
60 
61  void printNamespaces() {
62  auto namespaces = common::getNamespaces(mDescriptor);
63  if (!GeneratorOptions::instance().extraNamespace().empty()) {
64  namespaces.insert(namespaces.begin(), GeneratorOptions::instance().extraNamespace());
65  }
66  mPrinter->Print("\n");
67  for (auto ns : namespaces) {
68  mPrinter->Print({{"namespace", ns}}, Templates::NamespaceTemplate);
69  }
70  }
71 
72  void encloseNamespaces() {
73  auto namespacesSize = common::getNamespaces(mDescriptor).size();
74  if (!GeneratorOptions::instance().extraNamespace().empty()) {
75  namespacesSize++;
76  }
77  mPrinter->Print("\n");
78  for (size_t i = 0; i < namespacesSize; ++i) {
79  mPrinter->Print(Templates::SimpleBlockEnclosureTemplate);
80  }
81  mPrinter->Print("\n");
82  }
83 
84 protected:
85  const T* mDescriptor;
86  std::string mName;// TODO: migrate to typemaps for the client and service generators
87  TypeMap mTypeMap;
88 };
89 
90 } //namespace generator
91 } //namespace QtProtobuf