ONE - On-device Neural Engine
Loading...
Searching...
No Matches
code_generators.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2023 Samsung Electronics Co., Ltd. All Rights Reserved
3 * Copyright 2014 Google Inc. All rights reserved.
4 *
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
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
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.
16 */
17
18#ifndef FLATBUFFERS_CODE_GENERATORS_H_
19#define FLATBUFFERS_CODE_GENERATORS_H_
20
21#include <map>
22#include <sstream>
23
24#include "flatbuffers/idl.h"
25
26namespace flatbuffers
27{
28
29// Utility class to assist in generating code through use of text templates.
30//
31// Example code:
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;
38//
39// Output:
40// void Foo() { printf("%s", "Foo"); }
41// void Bar() { printf("%s", "Bar"); }
43{
44public:
45 CodeWriter(std::string pad = std::string()) : pad_(pad), cur_ident_lvl_(0), ignore_ident_(false)
46 {
47 }
48
49 // Clears the current "written" code.
50 void Clear()
51 {
52 stream_.str("");
53 stream_.clear();
54 }
55
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
58 // the given value.
59 void SetValue(const std::string &key, const std::string &value) { value_map_[key] = value; }
60
61 std::string GetValue(const std::string &key) const
62 {
63 const auto it = value_map_.find(key);
64 return it == value_map_.end() ? "" : it->second;
65 }
66
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);
72
73 // Returns the current contents of the CodeWriter as a std::string.
74 std::string ToString() const { return stream_.str(); }
75
76 // Increase ident level for writing code
77 void IncrementIdentLevel() { cur_ident_lvl_++; }
78 // Decrease ident level for writing code
80 {
81 if (cur_ident_lvl_)
82 cur_ident_lvl_--;
83 }
84
85 void SetPadding(const std::string &padding) { pad_ = padding; }
86
87private:
88 std::map<std::string, std::string> value_map_;
89 std::stringstream stream_;
90 std::string pad_;
91 int cur_ident_lvl_;
92 bool ignore_ident_;
93
94 // Add ident padding (tab or space) based on ident level
95 void AppendIdent(std::stringstream &stream);
96};
97
99{
100public:
101 virtual bool generate() = 0;
102
103 static std::string NamespaceDir(const Parser &parser, const std::string &path,
104 const Namespace &ns, const bool dasherize = false);
105
106 static std::string ToDasherizedCase(const std::string pascal_case);
107
108 std::string GeneratedFileName(const std::string &path, const std::string &file_name,
109 const IDLOptions &options) const;
110
111protected:
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)
117 {
118 }
119 virtual ~BaseGenerator() {}
120
121 // No copy/assign.
124
125 std::string NamespaceDir(const Namespace &ns, const bool dasherize = false) const;
126
127 static const char *FlatBuffersGeneratedWarning();
128
129 static std::string FullNamespace(const char *separator, const Namespace &ns);
130
131 static std::string LastNamespacePart(const Namespace &ns);
132
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; }
138
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;
143
144 std::string WrapInNameSpace(const Definition &def) const;
145
146 std::string GetNameSpace(const Definition &def) const;
147
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_;
154};
155
157{
158 const char *first_line;
160 const char *last_line;
161};
162
163extern void GenComment(const std::vector<std::string> &dc, std::string *code_ptr,
164 const CommentConfig *config, const char *prefix = "");
165
167{
168public:
170 std::string GenFloatConstant(const FieldDef &field) const;
171
172private:
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;
176
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;
180
181 template <typename T> std::string GenFloatConstantImpl(const FieldDef &field) const;
182};
183
185{
186public:
187 SimpleFloatConstantGenerator(const char *nan_number, const char *pos_inf_number,
188 const char *neg_inf_number);
189
190private:
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;
194
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;
198
199 const std::string nan_number_;
200 const std::string pos_inf_number_;
201 const std::string neg_inf_number_;
202};
203
204// C++, C#, Java like generator.
206{
207public:
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 = "");
211
212private:
213 std::string Value(double v, const std::string &src) const FLATBUFFERS_OVERRIDE;
214 std::string Inf(double v) const FLATBUFFERS_OVERRIDE;
215
216 std::string NaN(double v) const FLATBUFFERS_OVERRIDE;
217
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;
221
222 std::string MakeNaN(const std::string &prefix) const;
223 std::string MakeInf(bool neg, const std::string &prefix) const;
224
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_;
230};
231
232} // namespace flatbuffers
233
234#endif // FLATBUFFERS_CODE_GENERATORS_H_
BaseGenerator & operator=(const BaseGenerator &)
virtual bool generate()=0
const std::string default_extension_
std::string WrapInNameSpace(const Namespace *ns, const std::string &name) const
static std::string LastNamespacePart(const Namespace &ns)
BaseGenerator(const BaseGenerator &)
std::string GetNameSpace(const Definition &def) const
static const char * FlatBuffersGeneratedWarning()
static std::string ToDasherizedCase(const std::string pascal_case)
static std::string FullNamespace(const char *separator, const Namespace &ns)
std::string GeneratedFileName(const std::string &path, const std::string &file_name, const IDLOptions &options) const
const std::string qualifying_separator_
std::string NamespaceDir(const Namespace &ns, const bool dasherize=false) const
BaseGenerator(const Parser &parser, const std::string &path, const std::string &file_name, std::string qualifying_start, std::string qualifying_separator, std::string default_extension)
const std::string qualifying_start_
std::string WrapInNameSpace(const Definition &def) const
const std::string & path_
virtual const Namespace * CurrentNameSpace() const
const std::string & file_name_
static std::string NamespaceDir(const Parser &parser, const std::string &path, const Namespace &ns, const bool dasherize=false)
void SetPadding(const std::string &padding)
CodeWriter(std::string pad=std::string())
std::string ToString() const
std::string GetValue(const std::string &key) const
void operator+=(std::string text)
void SetValue(const std::string &key, const std::string &value)
std::string GenFloatConstant(const FieldDef &field) const
SimpleFloatConstantGenerator(const char *nan_number, const char *pos_inf_number, const char *neg_inf_number)
TypedFloatConstantGenerator(const char *double_prefix, const char *single_prefix, const char *nan_number, const char *pos_inf_number, const char *neg_inf_number="")
void GenComment(const std::vector< std::string > &dc, std::string *code_ptr, const CommentConfig *config, const char *prefix="")