generatorbase.cpp 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2019 Alexey Edelev <semlanik@gmail.com>
  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. #include "generatorbase.h"
  26. #include <google/protobuf/descriptor.h>
  27. #include <google/protobuf/stubs/logging.h>
  28. #include <google/protobuf/stubs/common.h>
  29. #include <google/protobuf/io/printer.h>
  30. #include <google/protobuf/io/zero_copy_stream.h>
  31. #include "utils.h"
  32. #include "templates.h"
  33. #include "generatoroptions.h"
  34. using namespace ::QtProtobuf::generator;
  35. using namespace ::google::protobuf;
  36. using namespace ::google::protobuf::compiler;
  37. GeneratorBase::GeneratorBase(Mode mode) : m_mode(mode)
  38. {
  39. }
  40. void GeneratorBase::iterateNonNestedFileds(const ::google::protobuf::FileDescriptor *file, std::function<void(const ::google::protobuf::Descriptor *)> callback) const
  41. {
  42. for (int i = 0; i < file->message_type_count(); i++) {
  43. const Descriptor *message = file->message_type(i);
  44. //Detect nested fields and filter maps fields
  45. int mapsFieldsCount = 0;
  46. for (int j = 0; j < message->nested_type_count(); j++) {
  47. for (int k = 0; k < message->field_count(); k++) {
  48. if (message->field(k)->is_map() && message->field(k)->message_type() == message->nested_type(j)) {
  49. ++mapsFieldsCount;
  50. }
  51. }
  52. }
  53. if (message->nested_type_count() > 0 && message->nested_type_count() > mapsFieldsCount) {
  54. std::cerr << file->name() << ":" << (message->index() + 1) << ": " << " Error: Meta object features not supported for nested classes in " << message->full_name() << std::endl;
  55. continue;
  56. }
  57. callback(message);
  58. }
  59. }
  60. bool GeneratorBase::GenerateAll(const std::vector<const FileDescriptor *> &files, const string &parameter, GeneratorContext *generatorContext, string *error) const
  61. {
  62. return CodeGenerator::GenerateAll(files, parameter, generatorContext, error);
  63. }
  64. std::string GeneratorBase::generateBaseName(const ::google::protobuf::FileDescriptor *file, std::string name)
  65. {
  66. std::vector<std::string> packages;
  67. utils::split(file->package(), packages, '.');
  68. std::string outFileBasename = "";
  69. for (auto package : packages) {
  70. outFileBasename += package + "/";
  71. }
  72. if (name != "") {
  73. outFileBasename += name;
  74. } else {
  75. outFileBasename += utils::extractFileName(file->name());
  76. }
  77. return outFileBasename;
  78. }