code_generators.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright 2014 Google Inc. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef FLATBUFFERS_CODE_GENERATORS_H_
  17. #define FLATBUFFERS_CODE_GENERATORS_H_
  18. namespace flatbuffers {
  19. class BaseGenerator {
  20. public:
  21. virtual bool generate() = 0;
  22. static const std::string NamespaceDir(const Parser &parser,
  23. const std::string &path,
  24. const Namespace &ns) {
  25. EnsureDirExists(path.c_str());
  26. if (parser.opts.one_file) return path;
  27. std::string namespace_dir = path; // Either empty or ends in separator.
  28. auto &namespaces = ns.components;
  29. for (auto it = namespaces.begin(); it != namespaces.end(); ++it) {
  30. namespace_dir += *it + kPathSeparator;
  31. EnsureDirExists(namespace_dir.c_str());
  32. }
  33. return namespace_dir;
  34. }
  35. protected:
  36. BaseGenerator(const Parser &parser, const std::string &path,
  37. const std::string &file_name,
  38. const std::string qualifying_start,
  39. const std::string qualifying_separator)
  40. : parser_(parser),
  41. path_(path),
  42. file_name_(file_name),
  43. qualifying_start_(qualifying_start),
  44. qualifying_separator_(qualifying_separator){};
  45. virtual ~BaseGenerator(){};
  46. // No copy/assign.
  47. BaseGenerator &operator=(const BaseGenerator &);
  48. BaseGenerator(const BaseGenerator &);
  49. const std::string NamespaceDir(const Namespace &ns) {
  50. return BaseGenerator::NamespaceDir(parser_, path_, ns);
  51. }
  52. const char *FlatBuffersGeneratedWarning() {
  53. return "automatically generated by the FlatBuffers compiler,"
  54. " do not modify\n\n";
  55. }
  56. bool IsEverythingGenerated() {
  57. for (auto it = parser_.enums_.vec.begin(); it != parser_.enums_.vec.end();
  58. ++it) {
  59. if (!(*it)->generated) return false;
  60. }
  61. for (auto it = parser_.structs_.vec.begin();
  62. it != parser_.structs_.vec.end(); ++it) {
  63. if (!(*it)->generated) return false;
  64. }
  65. return true;
  66. }
  67. std::string FullNamespace(const char *separator, const Namespace &ns) {
  68. std::string namespace_name;
  69. auto &namespaces = ns.components;
  70. for (auto it = namespaces.begin(); it != namespaces.end(); ++it) {
  71. if (namespace_name.length()) namespace_name += separator;
  72. namespace_name += *it;
  73. }
  74. return namespace_name;
  75. }
  76. const std::string LastNamespacePart(const Namespace &ns) {
  77. auto &namespaces = ns.components;
  78. if (namespaces.size())
  79. return *(namespaces.end() - 1);
  80. else
  81. return std::string("");
  82. }
  83. // tracks the current namespace for early exit in WrapInNameSpace
  84. // c++, java and csharp returns a different namespace from
  85. // the following default (no early exit, always fully qualify),
  86. // which works for js and php
  87. virtual const Namespace *CurrentNameSpace() { return nullptr; }
  88. // Ensure that a type is prefixed with its namespace whenever it is used
  89. // outside of its namespace.
  90. std::string WrapInNameSpace(const Namespace *ns, const std::string &name) {
  91. if (CurrentNameSpace() == ns) return name;
  92. std::string qualified_name = qualifying_start_;
  93. for (auto it = ns->components.begin(); it != ns->components.end(); ++it)
  94. qualified_name += *it + qualifying_separator_;
  95. return qualified_name + name;
  96. }
  97. std::string WrapInNameSpace(const Definition &def) {
  98. return WrapInNameSpace(def.defined_namespace, def.name);
  99. }
  100. const Parser &parser_;
  101. const std::string &path_;
  102. const std::string &file_name_;
  103. const std::string qualifying_start_;
  104. const std::string qualifying_separator_;
  105. };
  106. } // namespace flatbuffers
  107. #endif // FLATBUFFERS_CODE_GENERATORS_H_