|
| 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 | + |
| 17 | +#ifndef FLATBUFFERS_CODE_GENERATORS_H_ |
| 18 | +#define FLATBUFFERS_CODE_GENERATORS_H_ |
| 19 | + |
| 20 | +namespace flatbuffers { |
| 21 | + |
| 22 | +class BaseGenerator { |
| 23 | + public: |
| 24 | + virtual bool generate() = 0; |
| 25 | + |
| 26 | + static const std::string NamespaceDir(const Parser &parser, |
| 27 | + const std::string &path, |
| 28 | + const Namespace &ns) { |
| 29 | + EnsureDirExists(path.c_str()); |
| 30 | + if (parser.opts.one_file) return path; |
| 31 | + std::string namespace_dir = path; // Either empty or ends in separator. |
| 32 | + auto &namespaces = ns.components; |
| 33 | + for (auto it = namespaces.begin(); it != namespaces.end(); ++it) { |
| 34 | + namespace_dir += *it + kPathSeparator; |
| 35 | + EnsureDirExists(namespace_dir.c_str()); |
| 36 | + } |
| 37 | + return namespace_dir; |
| 38 | + } |
| 39 | + |
| 40 | + protected: |
| 41 | + BaseGenerator(const Parser &parser, const std::string &path, |
| 42 | + const std::string &file_name, |
| 43 | + const std::string qualifying_start, |
| 44 | + const std::string qualifying_separator) |
| 45 | + : parser_(parser), |
| 46 | + path_(path), |
| 47 | + file_name_(file_name), |
| 48 | + qualifying_start_(qualifying_start), |
| 49 | + qualifying_separator_(qualifying_separator){}; |
| 50 | + virtual ~BaseGenerator(){}; |
| 51 | + |
| 52 | + // No copy/assign. |
| 53 | + BaseGenerator &operator=(const BaseGenerator &); |
| 54 | + BaseGenerator(const BaseGenerator &); |
| 55 | + |
| 56 | + const std::string NamespaceDir(const Namespace &ns) { |
| 57 | + return BaseGenerator::NamespaceDir(parser_, path_, ns); |
| 58 | + } |
| 59 | + |
| 60 | + const char *FlatBuffersGeneratedWarning() { |
| 61 | + return "automatically generated by the FlatBuffers compiler," |
| 62 | + " do not modify\n\n"; |
| 63 | + } |
| 64 | + |
| 65 | + bool IsEverythingGenerated() { |
| 66 | + for (auto it = parser_.enums_.vec.begin(); it != parser_.enums_.vec.end(); |
| 67 | + ++it) { |
| 68 | + if (!(*it)->generated) return false; |
| 69 | + } |
| 70 | + for (auto it = parser_.structs_.vec.begin(); |
| 71 | + it != parser_.structs_.vec.end(); ++it) { |
| 72 | + if (!(*it)->generated) return false; |
| 73 | + } |
| 74 | + return true; |
| 75 | + } |
| 76 | + |
| 77 | + std::string FullNamespace(const char *separator, const Namespace &ns) { |
| 78 | + std::string namespace_name; |
| 79 | + auto &namespaces = ns.components; |
| 80 | + for (auto it = namespaces.begin(); it != namespaces.end(); ++it) { |
| 81 | + if (namespace_name.length()) namespace_name += separator; |
| 82 | + namespace_name += *it; |
| 83 | + } |
| 84 | + return namespace_name; |
| 85 | + } |
| 86 | + |
| 87 | + const std::string LastNamespacePart(const Namespace &ns) { |
| 88 | + auto &namespaces = ns.components; |
| 89 | + if (namespaces.size()) |
| 90 | + return *(namespaces.end() - 1); |
| 91 | + else |
| 92 | + return std::string(""); |
| 93 | + } |
| 94 | + |
| 95 | + // tracks the current namespace for early exit in WrapInNameSpace |
| 96 | + // c++, java and csharp returns a different namespace from |
| 97 | + // the following default (no early exit, always fully qualify), |
| 98 | + // which works for js and php |
| 99 | + virtual const Namespace *CurrentNameSpace() { return nullptr; } |
| 100 | + |
| 101 | + // Ensure that a type is prefixed with its namespace whenever it is used |
| 102 | + // outside of its namespace. |
| 103 | + std::string WrapInNameSpace(const Namespace *ns, const std::string &name) { |
| 104 | + if (CurrentNameSpace() == ns) return name; |
| 105 | + std::string qualified_name = qualifying_start_; |
| 106 | + for (auto it = ns->components.begin(); it != ns->components.end(); ++it) |
| 107 | + qualified_name += *it + qualifying_separator_; |
| 108 | + return qualified_name + name; |
| 109 | + } |
| 110 | + |
| 111 | + std::string WrapInNameSpace(const Definition &def) { |
| 112 | + return WrapInNameSpace(def.defined_namespace, def.name); |
| 113 | + } |
| 114 | + |
| 115 | + const Parser &parser_; |
| 116 | + const std::string &path_; |
| 117 | + const std::string &file_name_; |
| 118 | + const std::string qualifying_start_; |
| 119 | + const std::string qualifying_separator_; |
| 120 | +}; |
| 121 | + |
| 122 | +} // namespace flatbuffers |
| 123 | + |
| 124 | +#endif // FLATBUFFERS_CODE_GENERATORS_H_ |
0 commit comments