2 * Copyright 2016 Google Inc. All rights reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "flatbuffers/code_generators.h"
19 #include "flatbuffers/base.h"
20 #include "flatbuffers/util.h"
25 # pragma warning(push)
26 # pragma warning(disable : 4127) // C4127: conditional expression is constant
29 namespace flatbuffers {
31 void CodeWriter::operator+=(std::string text) {
32 if (!ignore_ident_ && !text.empty()) AppendIdent(stream_);
35 auto begin = text.find("{{");
36 if (begin == std::string::npos) { break; }
38 auto end = text.find("}}");
39 if (end == std::string::npos || end < begin) { break; }
41 // Write all the text before the first {{ into the stream.
42 stream_.write(text.c_str(), begin);
44 // The key is between the {{ and }}.
45 const std::string key = text.substr(begin + 2, end - begin - 2);
47 // Find the value associated with the key. If it exists, write the
48 // value into the stream, otherwise write the key itself into the stream.
49 auto iter = value_map_.find(key);
50 if (iter != value_map_.end()) {
51 const std::string &value = iter->second;
54 FLATBUFFERS_ASSERT(false && "could not find key");
58 // Update the text to everything after the }}.
59 text = text.substr(end + 2);
61 if (!text.empty() && string_back(text) == '\\') {
66 ignore_ident_ = false;
67 stream_ << text << std::endl;
71 void CodeWriter::AppendIdent(std::stringstream &stream) {
72 int lvl = cur_ident_lvl_;
74 stream.write(pad_.c_str(), static_cast<std::streamsize>(pad_.size()));
78 const char *BaseGenerator::FlatBuffersGeneratedWarning() {
79 return "automatically generated by the FlatBuffers compiler,"
83 std::string BaseGenerator::NamespaceDir(const Parser &parser,
84 const std::string &path,
85 const Namespace &ns) {
86 EnsureDirExists(path);
87 if (parser.opts.one_file) return path;
88 std::string namespace_dir = path; // Either empty or ends in separator.
89 auto &namespaces = ns.components;
90 for (auto it = namespaces.begin(); it != namespaces.end(); ++it) {
91 namespace_dir += *it + kPathSeparator;
92 EnsureDirExists(namespace_dir);
97 std::string BaseGenerator::NamespaceDir(const Namespace &ns) const {
98 return BaseGenerator::NamespaceDir(parser_, path_, ns);
101 std::string BaseGenerator::FullNamespace(const char *separator,
102 const Namespace &ns) {
103 std::string namespace_name;
104 auto &namespaces = ns.components;
105 for (auto it = namespaces.begin(); it != namespaces.end(); ++it) {
106 if (namespace_name.length()) namespace_name += separator;
107 namespace_name += *it;
109 return namespace_name;
112 std::string BaseGenerator::LastNamespacePart(const Namespace &ns) {
113 if (!ns.components.empty())
114 return ns.components.back();
116 return std::string("");
119 // Ensure that a type is prefixed with its namespace.
120 std::string BaseGenerator::WrapInNameSpace(const Namespace *ns,
121 const std::string &name) const {
122 std::string qualified_name = qualifying_start_;
123 for (auto it = ns->components.begin(); it != ns->components.end(); ++it)
124 qualified_name += *it + qualifying_separator_;
125 return qualified_name + name;
128 std::string BaseGenerator::WrapInNameSpace(const Definition &def) const {
129 return WrapInNameSpace(def.defined_namespace, def.name);
132 std::string BaseGenerator::GetNameSpace(const Definition &def) const {
133 const Namespace *ns = def.defined_namespace;
134 if (CurrentNameSpace() == ns) return "";
135 std::string qualified_name = qualifying_start_;
136 for (auto it = ns->components.begin(); it != ns->components.end(); ++it) {
137 qualified_name += *it;
138 if ((it + 1) != ns->components.end()) {
139 qualified_name += qualifying_separator_;
143 return qualified_name;
146 // Generate a documentation comment, if available.
147 void GenComment(const std::vector<std::string> &dc, std::string *code_ptr,
148 const CommentConfig *config, const char *prefix) {
149 if (dc.begin() == dc.end()) {
150 // Don't output empty comment blocks with 0 lines of comment content.
154 std::string &code = *code_ptr;
155 if (config != nullptr && config->first_line != nullptr) {
156 code += std::string(prefix) + std::string(config->first_line) + "\n";
158 std::string line_prefix =
159 std::string(prefix) +
160 ((config != nullptr && config->content_line_prefix != nullptr)
161 ? config->content_line_prefix
163 for (auto it = dc.begin(); it != dc.end(); ++it) {
164 code += line_prefix + *it + "\n";
166 if (config != nullptr && config->last_line != nullptr) {
167 code += std::string(prefix) + std::string(config->last_line) + "\n";
172 std::string FloatConstantGenerator::GenFloatConstantImpl(
173 const FieldDef &field) const {
174 const auto &constant = field.value.constant;
176 auto done = StringToNumber(constant.c_str(), &v);
177 FLATBUFFERS_ASSERT(done);
179 #if (!defined(_MSC_VER) || (_MSC_VER >= 1800))
180 if (std::isnan(v)) return NaN(v);
181 if (std::isinf(v)) return Inf(v);
183 return Value(v, constant);
185 return "#"; // compile time error
188 std::string FloatConstantGenerator::GenFloatConstant(
189 const FieldDef &field) const {
190 switch (field.value.type.base_type) {
191 case BASE_TYPE_FLOAT: return GenFloatConstantImpl<float>(field);
192 case BASE_TYPE_DOUBLE: return GenFloatConstantImpl<double>(field);
194 FLATBUFFERS_ASSERT(false);
195 return "INVALID_BASE_TYPE";
200 TypedFloatConstantGenerator::TypedFloatConstantGenerator(
201 const char *double_prefix, const char *single_prefix,
202 const char *nan_number, const char *pos_inf_number,
203 const char *neg_inf_number)
204 : double_prefix_(double_prefix),
205 single_prefix_(single_prefix),
206 nan_number_(nan_number),
207 pos_inf_number_(pos_inf_number),
208 neg_inf_number_(neg_inf_number) {}
210 std::string TypedFloatConstantGenerator::MakeNaN(
211 const std::string &prefix) const {
212 return prefix + nan_number_;
214 std::string TypedFloatConstantGenerator::MakeInf(
215 bool neg, const std::string &prefix) const {
217 return !neg_inf_number_.empty() ? (prefix + neg_inf_number_)
218 : ("-" + prefix + pos_inf_number_);
220 return prefix + pos_inf_number_;
223 std::string TypedFloatConstantGenerator::Value(double v,
224 const std::string &src) const {
229 std::string TypedFloatConstantGenerator::Inf(double v) const {
230 return MakeInf(v < 0, double_prefix_);
233 std::string TypedFloatConstantGenerator::NaN(double v) const {
235 return MakeNaN(double_prefix_);
238 std::string TypedFloatConstantGenerator::Value(float v,
239 const std::string &src) const {
244 std::string TypedFloatConstantGenerator::Inf(float v) const {
245 return MakeInf(v < 0, single_prefix_);
248 std::string TypedFloatConstantGenerator::NaN(float v) const {
250 return MakeNaN(single_prefix_);
253 SimpleFloatConstantGenerator::SimpleFloatConstantGenerator(
254 const char *nan_number, const char *pos_inf_number,
255 const char *neg_inf_number)
256 : nan_number_(nan_number),
257 pos_inf_number_(pos_inf_number),
258 neg_inf_number_(neg_inf_number) {}
260 std::string SimpleFloatConstantGenerator::Value(double v,
261 const std::string &src) const {
266 std::string SimpleFloatConstantGenerator::Inf(double v) const {
267 return (v < 0) ? neg_inf_number_ : pos_inf_number_;
270 std::string SimpleFloatConstantGenerator::NaN(double v) const {
275 std::string SimpleFloatConstantGenerator::Value(float v,
276 const std::string &src) const {
277 return this->Value(static_cast<double>(v), src);
280 std::string SimpleFloatConstantGenerator::Inf(float v) const {
281 return this->Inf(static_cast<double>(v));
284 std::string SimpleFloatConstantGenerator::NaN(float v) const {
285 return this->NaN(static_cast<double>(v));
288 } // namespace flatbuffers
290 #if defined(_MSC_VER)
291 # pragma warning(pop)