2 * Copyright (c) 2023 Samsung Electronics Co., Ltd. All Rights Reserved
3 * Copyright 2014 Google Inc. All rights reserved.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #ifndef FLATBUFFERS_CODE_GENERATORS_H_
19 #define FLATBUFFERS_CODE_GENERATORS_H_
24 #include "flatbuffers/idl.h"
29 // Utility class to assist in generating code through use of text templates.
32 // CodeWriter code("\t");
33 // code.SetValue("NAME", "Foo");
34 // code += "void {{NAME}}() { printf("%s", "{{NAME}}"); }";
35 // code.SetValue("NAME", "Bar");
36 // code += "void {{NAME}}() { printf("%s", "{{NAME}}"); }";
37 // std::cout << code.ToString() << std::endl;
40 // void Foo() { printf("%s", "Foo"); }
41 // void Bar() { printf("%s", "Bar"); }
45 CodeWriter(std::string pad = std::string()) : pad_(pad), cur_ident_lvl_(0), ignore_ident_(false)
49 // Clears the current "written" code.
56 // Associates a key with a value. All subsequent calls to operator+=, where
57 // the specified key is contained in {{ and }} delimiters will be replaced by
59 void SetValue(const std::string &key, const std::string &value) { value_map_[key] = value; }
61 std::string GetValue(const std::string &key) const
63 const auto it = value_map_.find(key);
64 return it == value_map_.end() ? "" : it->second;
67 // Appends the given text to the generated code as well as a newline
68 // character. Any text within {{ and }} delimiters is replaced by values
69 // previously stored in the CodeWriter by calling SetValue above. The newline
70 // will be suppressed if the text ends with the \\ character.
71 void operator+=(std::string text);
73 // Returns the current contents of the CodeWriter as a std::string.
74 std::string ToString() const { return stream_.str(); }
76 // Increase ident level for writing code
77 void IncrementIdentLevel() { cur_ident_lvl_++; }
78 // Decrease ident level for writing code
79 void DecrementIdentLevel()
85 void SetPadding(const std::string &padding) { pad_ = padding; }
88 std::map<std::string, std::string> value_map_;
89 std::stringstream stream_;
94 // Add ident padding (tab or space) based on ident level
95 void AppendIdent(std::stringstream &stream);
101 virtual bool generate() = 0;
103 static std::string NamespaceDir(const Parser &parser, const std::string &path,
104 const Namespace &ns, const bool dasherize = false);
106 static std::string ToDasherizedCase(const std::string pascal_case);
108 std::string GeneratedFileName(const std::string &path, const std::string &file_name,
109 const IDLOptions &options) const;
112 BaseGenerator(const Parser &parser, const std::string &path, const std::string &file_name,
113 std::string qualifying_start, std::string qualifying_separator,
114 std::string default_extension)
115 : parser_(parser), path_(path), file_name_(file_name), qualifying_start_(qualifying_start),
116 qualifying_separator_(qualifying_separator), default_extension_(default_extension)
119 virtual ~BaseGenerator() {}
122 BaseGenerator &operator=(const BaseGenerator &);
123 BaseGenerator(const BaseGenerator &);
125 std::string NamespaceDir(const Namespace &ns, const bool dasherize = false) const;
127 static const char *FlatBuffersGeneratedWarning();
129 static std::string FullNamespace(const char *separator, const Namespace &ns);
131 static std::string LastNamespacePart(const Namespace &ns);
133 // tracks the current namespace for early exit in WrapInNameSpace
134 // c++, java and csharp returns a different namespace from
135 // the following default (no early exit, always fully qualify),
136 // which works for js and php
137 virtual const Namespace *CurrentNameSpace() const { return nullptr; }
139 // Ensure that a type is prefixed with its namespace even within
140 // its own namespace to avoid conflict between generated method
141 // names and similarly named classes or structs
142 std::string WrapInNameSpace(const Namespace *ns, const std::string &name) const;
144 std::string WrapInNameSpace(const Definition &def) const;
146 std::string GetNameSpace(const Definition &def) const;
148 const Parser &parser_;
149 const std::string &path_;
150 const std::string &file_name_;
151 const std::string qualifying_start_;
152 const std::string qualifying_separator_;
153 const std::string default_extension_;
158 const char *first_line;
159 const char *content_line_prefix;
160 const char *last_line;
163 extern void GenComment(const std::vector<std::string> &dc, std::string *code_ptr,
164 const CommentConfig *config, const char *prefix = "");
166 class FloatConstantGenerator
169 virtual ~FloatConstantGenerator() {}
170 std::string GenFloatConstant(const FieldDef &field) const;
173 virtual std::string Value(double v, const std::string &src) const = 0;
174 virtual std::string Inf(double v) const = 0;
175 virtual std::string NaN(double v) const = 0;
177 virtual std::string Value(float v, const std::string &src) const = 0;
178 virtual std::string Inf(float v) const = 0;
179 virtual std::string NaN(float v) const = 0;
181 template <typename T> std::string GenFloatConstantImpl(const FieldDef &field) const;
184 class SimpleFloatConstantGenerator : public FloatConstantGenerator
187 SimpleFloatConstantGenerator(const char *nan_number, const char *pos_inf_number,
188 const char *neg_inf_number);
191 std::string Value(double v, const std::string &src) const FLATBUFFERS_OVERRIDE;
192 std::string Inf(double v) const FLATBUFFERS_OVERRIDE;
193 std::string NaN(double v) const FLATBUFFERS_OVERRIDE;
195 std::string Value(float v, const std::string &src) const FLATBUFFERS_OVERRIDE;
196 std::string Inf(float v) const FLATBUFFERS_OVERRIDE;
197 std::string NaN(float v) const FLATBUFFERS_OVERRIDE;
199 const std::string nan_number_;
200 const std::string pos_inf_number_;
201 const std::string neg_inf_number_;
204 // C++, C#, Java like generator.
205 class TypedFloatConstantGenerator : public FloatConstantGenerator
208 TypedFloatConstantGenerator(const char *double_prefix, const char *single_prefix,
209 const char *nan_number, const char *pos_inf_number,
210 const char *neg_inf_number = "");
213 std::string Value(double v, const std::string &src) const FLATBUFFERS_OVERRIDE;
214 std::string Inf(double v) const FLATBUFFERS_OVERRIDE;
216 std::string NaN(double v) const FLATBUFFERS_OVERRIDE;
218 std::string Value(float v, const std::string &src) const FLATBUFFERS_OVERRIDE;
219 std::string Inf(float v) const FLATBUFFERS_OVERRIDE;
220 std::string NaN(float v) const FLATBUFFERS_OVERRIDE;
222 std::string MakeNaN(const std::string &prefix) const;
223 std::string MakeInf(bool neg, const std::string &prefix) const;
225 const std::string double_prefix_;
226 const std::string single_prefix_;
227 const std::string nan_number_;
228 const std::string pos_inf_number_;
229 const std::string neg_inf_number_;
232 } // namespace flatbuffers
234 #endif // FLATBUFFERS_CODE_GENERATORS_H_