Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame^] | 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 | #include <map> |
| 21 | #include <sstream> |
| 22 | #include "flatbuffers/idl.h" |
| 23 | |
| 24 | namespace flatbuffers { |
| 25 | |
| 26 | // Utility class to assist in generating code through use of text templates. |
| 27 | // |
| 28 | // Example code: |
| 29 | // CodeWriter code("\t"); |
| 30 | // code.SetValue("NAME", "Foo"); |
| 31 | // code += "void {{NAME}}() { printf("%s", "{{NAME}}"); }"; |
| 32 | // code.SetValue("NAME", "Bar"); |
| 33 | // code += "void {{NAME}}() { printf("%s", "{{NAME}}"); }"; |
| 34 | // std::cout << code.ToString() << std::endl; |
| 35 | // |
| 36 | // Output: |
| 37 | // void Foo() { printf("%s", "Foo"); } |
| 38 | // void Bar() { printf("%s", "Bar"); } |
| 39 | class CodeWriter { |
| 40 | public: |
| 41 | CodeWriter(std::string pad = std::string()) |
| 42 | : pad_(pad), cur_ident_lvl_(0), ignore_ident_(false) {} |
| 43 | |
| 44 | // Clears the current "written" code. |
| 45 | void Clear() { |
| 46 | stream_.str(""); |
| 47 | stream_.clear(); |
| 48 | } |
| 49 | |
| 50 | // Associates a key with a value. All subsequent calls to operator+=, where |
| 51 | // the specified key is contained in {{ and }} delimiters will be replaced by |
| 52 | // the given value. |
| 53 | void SetValue(const std::string &key, const std::string &value) { |
| 54 | value_map_[key] = value; |
| 55 | } |
| 56 | |
| 57 | std::string GetValue(const std::string &key) const { |
| 58 | const auto it = value_map_.find(key); |
| 59 | return it == value_map_.end() ? "" : it->second; |
| 60 | } |
| 61 | |
| 62 | // Appends the given text to the generated code as well as a newline |
| 63 | // character. Any text within {{ and }} delimeters is replaced by values |
| 64 | // previously stored in the CodeWriter by calling SetValue above. The newline |
| 65 | // will be suppressed if the text ends with the \\ character. |
| 66 | void operator+=(std::string text); |
| 67 | |
| 68 | // Returns the current contents of the CodeWriter as a std::string. |
| 69 | std::string ToString() const { return stream_.str(); } |
| 70 | |
| 71 | // Increase ident level for writing code |
| 72 | void IncrementIdentLevel() { cur_ident_lvl_++; } |
| 73 | // Decrease ident level for writing code |
| 74 | void DecrementIdentLevel() { |
| 75 | if (cur_ident_lvl_) cur_ident_lvl_--; |
| 76 | } |
| 77 | |
| 78 | private: |
| 79 | std::map<std::string, std::string> value_map_; |
| 80 | std::stringstream stream_; |
| 81 | std::string pad_; |
| 82 | int cur_ident_lvl_; |
| 83 | bool ignore_ident_; |
| 84 | |
| 85 | // Add ident padding (tab or space) based on ident level |
| 86 | void AppendIdent(std::stringstream &stream); |
| 87 | }; |
| 88 | |
| 89 | class BaseGenerator { |
| 90 | public: |
| 91 | virtual bool generate() = 0; |
| 92 | |
| 93 | static std::string NamespaceDir(const Parser &parser, const std::string &path, |
| 94 | const Namespace &ns); |
| 95 | |
| 96 | protected: |
| 97 | BaseGenerator(const Parser &parser, const std::string &path, |
| 98 | const std::string &file_name, |
| 99 | std::string qualifying_start, |
| 100 | std::string qualifying_separator) |
| 101 | : parser_(parser), |
| 102 | path_(path), |
| 103 | file_name_(file_name), |
| 104 | qualifying_start_(qualifying_start), |
| 105 | qualifying_separator_(qualifying_separator) {} |
| 106 | virtual ~BaseGenerator() {} |
| 107 | |
| 108 | // No copy/assign. |
| 109 | BaseGenerator &operator=(const BaseGenerator &); |
| 110 | BaseGenerator(const BaseGenerator &); |
| 111 | |
| 112 | std::string NamespaceDir(const Namespace &ns) const; |
| 113 | |
| 114 | static const char *FlatBuffersGeneratedWarning(); |
| 115 | |
| 116 | static std::string FullNamespace(const char *separator, const Namespace &ns); |
| 117 | |
| 118 | static std::string LastNamespacePart(const Namespace &ns); |
| 119 | |
| 120 | // tracks the current namespace for early exit in WrapInNameSpace |
| 121 | // c++, java and csharp returns a different namespace from |
| 122 | // the following default (no early exit, always fully qualify), |
| 123 | // which works for js and php |
| 124 | virtual const Namespace *CurrentNameSpace() const { return nullptr; } |
| 125 | |
| 126 | // Ensure that a type is prefixed with its namespace even within |
| 127 | // its own namespace to avoid conflict between generated method |
| 128 | // names and similarly named classes or structs |
| 129 | std::string WrapInNameSpace(const Namespace *ns, |
| 130 | const std::string &name) const; |
| 131 | |
| 132 | std::string WrapInNameSpace(const Definition &def) const; |
| 133 | |
| 134 | std::string GetNameSpace(const Definition &def) const; |
| 135 | |
| 136 | const Parser &parser_; |
| 137 | const std::string &path_; |
| 138 | const std::string &file_name_; |
| 139 | const std::string qualifying_start_; |
| 140 | const std::string qualifying_separator_; |
| 141 | }; |
| 142 | |
| 143 | struct CommentConfig { |
| 144 | const char *first_line; |
| 145 | const char *content_line_prefix; |
| 146 | const char *last_line; |
| 147 | }; |
| 148 | |
| 149 | extern void GenComment(const std::vector<std::string> &dc, |
| 150 | std::string *code_ptr, const CommentConfig *config, |
| 151 | const char *prefix = ""); |
| 152 | |
| 153 | class FloatConstantGenerator { |
| 154 | public: |
| 155 | virtual ~FloatConstantGenerator() {} |
| 156 | std::string GenFloatConstant(const FieldDef &field) const; |
| 157 | |
| 158 | private: |
| 159 | virtual std::string Value(double v, const std::string &src) const = 0; |
| 160 | virtual std::string Inf(double v) const = 0; |
| 161 | virtual std::string NaN(double v) const = 0; |
| 162 | |
| 163 | virtual std::string Value(float v, const std::string &src) const = 0; |
| 164 | virtual std::string Inf(float v) const = 0; |
| 165 | virtual std::string NaN(float v) const = 0; |
| 166 | |
| 167 | template<typename T> |
| 168 | std::string GenFloatConstantImpl(const FieldDef &field) const; |
| 169 | }; |
| 170 | |
| 171 | class SimpleFloatConstantGenerator : public FloatConstantGenerator { |
| 172 | public: |
| 173 | SimpleFloatConstantGenerator(const char *nan_number, |
| 174 | const char *pos_inf_number, |
| 175 | const char *neg_inf_number); |
| 176 | |
| 177 | private: |
| 178 | std::string Value(double v, |
| 179 | const std::string &src) const FLATBUFFERS_OVERRIDE; |
| 180 | std::string Inf(double v) const FLATBUFFERS_OVERRIDE; |
| 181 | std::string NaN(double v) const FLATBUFFERS_OVERRIDE; |
| 182 | |
| 183 | std::string Value(float v, const std::string &src) const FLATBUFFERS_OVERRIDE; |
| 184 | std::string Inf(float v) const FLATBUFFERS_OVERRIDE; |
| 185 | std::string NaN(float v) const FLATBUFFERS_OVERRIDE; |
| 186 | |
| 187 | const std::string nan_number_; |
| 188 | const std::string pos_inf_number_; |
| 189 | const std::string neg_inf_number_; |
| 190 | }; |
| 191 | |
| 192 | // C++, C#, Java like generator. |
| 193 | class TypedFloatConstantGenerator : public FloatConstantGenerator { |
| 194 | public: |
| 195 | TypedFloatConstantGenerator(const char *double_prefix, |
| 196 | const char *single_prefix, const char *nan_number, |
| 197 | const char *pos_inf_number, |
| 198 | const char *neg_inf_number = ""); |
| 199 | |
| 200 | private: |
| 201 | std::string Value(double v, |
| 202 | const std::string &src) const FLATBUFFERS_OVERRIDE; |
| 203 | std::string Inf(double v) const FLATBUFFERS_OVERRIDE; |
| 204 | |
| 205 | std::string NaN(double v) const FLATBUFFERS_OVERRIDE; |
| 206 | |
| 207 | std::string Value(float v, const std::string &src) const FLATBUFFERS_OVERRIDE; |
| 208 | std::string Inf(float v) const FLATBUFFERS_OVERRIDE; |
| 209 | std::string NaN(float v) const FLATBUFFERS_OVERRIDE; |
| 210 | |
| 211 | std::string MakeNaN(const std::string &prefix) const; |
| 212 | std::string MakeInf(bool neg, const std::string &prefix) const; |
| 213 | |
| 214 | const std::string double_prefix_; |
| 215 | const std::string single_prefix_; |
| 216 | const std::string nan_number_; |
| 217 | const std::string pos_inf_number_; |
| 218 | const std::string neg_inf_number_; |
| 219 | }; |
| 220 | |
| 221 | } // namespace flatbuffers |
| 222 | |
| 223 | #endif // FLATBUFFERS_CODE_GENERATORS_H_ |