Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-08-27 09:30:24

0001 /*
0002  * Copyright 2014 Google Inc. All rights reserved.
0003  *
0004  * Licensed under the Apache License, Version 2.0 (the "License");
0005  * you may not use this file except in compliance with the License.
0006  * You may obtain a copy of the License at
0007  *
0008  *     http://www.apache.org/licenses/LICENSE-2.0
0009  *
0010  * Unless required by applicable law or agreed to in writing, software
0011  * distributed under the License is distributed on an "AS IS" BASIS,
0012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0013  * See the License for the specific language governing permissions and
0014  * limitations under the License.
0015  */
0016 
0017 #ifndef FLATBUFFERS_CODE_GENERATORS_H_
0018 #define FLATBUFFERS_CODE_GENERATORS_H_
0019 
0020 #include <map>
0021 #include <sstream>
0022 
0023 #include "flatbuffers/idl.h"
0024 
0025 namespace flatbuffers {
0026 
0027 // Utility class to assist in generating code through use of text templates.
0028 //
0029 // Example code:
0030 //   CodeWriter code("\t");
0031 //   code.SetValue("NAME", "Foo");
0032 //   code += "void {{NAME}}() { printf("%s", "{{NAME}}"); }";
0033 //   code.SetValue("NAME", "Bar");
0034 //   code += "void {{NAME}}() { printf("%s", "{{NAME}}"); }";
0035 //   std::cout << code.ToString() << std::endl;
0036 //
0037 // Output:
0038 //  void Foo() { printf("%s", "Foo"); }
0039 //  void Bar() { printf("%s", "Bar"); }
0040 class CodeWriter {
0041  public:
0042   CodeWriter(std::string pad = std::string())
0043       : pad_(pad), cur_ident_lvl_(0), ignore_ident_(false) {}
0044 
0045   // Clears the current "written" code.
0046   void Clear() {
0047     stream_.str("");
0048     stream_.clear();
0049   }
0050 
0051   // Associates a key with a value.  All subsequent calls to operator+=, where
0052   // the specified key is contained in {{ and }} delimiters will be replaced by
0053   // the given value.
0054   void SetValue(const std::string &key, const std::string &value) {
0055     value_map_[key] = value;
0056   }
0057 
0058   std::string GetValue(const std::string &key) const {
0059     const auto it = value_map_.find(key);
0060     return it == value_map_.end() ? "" : it->second;
0061   }
0062 
0063   // Appends the given text to the generated code as well as a newline
0064   // character.  Any text within {{ and }} delimiters is replaced by values
0065   // previously stored in the CodeWriter by calling SetValue above.  The newline
0066   // will be suppressed if the text ends with the \\ character.
0067   void operator+=(std::string text);
0068 
0069   // Returns the current contents of the CodeWriter as a std::string.
0070   std::string ToString() const { return stream_.str(); }
0071 
0072   // Increase ident level for writing code
0073   void IncrementIdentLevel() { cur_ident_lvl_++; }
0074   // Decrease ident level for writing code
0075   void DecrementIdentLevel() {
0076     if (cur_ident_lvl_) cur_ident_lvl_--;
0077   }
0078 
0079   void SetPadding(const std::string &padding) { pad_ = padding; }
0080 
0081  private:
0082   std::map<std::string, std::string> value_map_;
0083   std::stringstream stream_;
0084   std::string pad_;
0085   int cur_ident_lvl_;
0086   bool ignore_ident_;
0087 
0088   // Add ident padding (tab or space) based on ident level
0089   void AppendIdent(std::stringstream &stream);
0090 };
0091 
0092 class BaseGenerator {
0093  public:
0094   virtual bool generate() = 0;
0095 
0096   static std::string NamespaceDir(const Parser &parser, const std::string &path,
0097                                   const Namespace &ns,
0098                                   const bool dasherize = false);
0099 
0100   std::string GeneratedFileName(const std::string &path,
0101                                 const std::string &file_name,
0102                                 const IDLOptions &options) const;
0103 
0104  protected:
0105   BaseGenerator(const Parser &parser, const std::string &path,
0106                 const std::string &file_name, std::string qualifying_start,
0107                 std::string qualifying_separator, std::string default_extension)
0108       : parser_(parser),
0109         path_(path),
0110         file_name_(file_name),
0111         qualifying_start_(qualifying_start),
0112         qualifying_separator_(qualifying_separator),
0113         default_extension_(default_extension) {}
0114   virtual ~BaseGenerator() {}
0115 
0116   // No copy/assign.
0117   BaseGenerator &operator=(const BaseGenerator &);
0118   BaseGenerator(const BaseGenerator &);
0119 
0120   std::string NamespaceDir(const Namespace &ns,
0121                            const bool dasherize = false) const;
0122 
0123   static const char *FlatBuffersGeneratedWarning();
0124 
0125   static std::string FullNamespace(const char *separator, const Namespace &ns);
0126 
0127   static std::string LastNamespacePart(const Namespace &ns);
0128 
0129   // tracks the current namespace for early exit in WrapInNameSpace
0130   // c++, java and csharp returns a different namespace from
0131   // the following default (no early exit, always fully qualify),
0132   // which works for js and php
0133   virtual const Namespace *CurrentNameSpace() const { return nullptr; }
0134 
0135   // Ensure that a type is prefixed with its namespace even within
0136   // its own namespace to avoid conflict between generated method
0137   // names and similarly named classes or structs
0138   std::string WrapInNameSpace(const Namespace *ns,
0139                               const std::string &name) const;
0140 
0141   std::string WrapInNameSpace(const Definition &def,
0142                               const std::string &suffix = "") const;
0143 
0144   std::string GetNameSpace(const Definition &def) const;
0145 
0146   const Parser &parser_;
0147   const std::string &path_;
0148   const std::string &file_name_;
0149   const std::string qualifying_start_;
0150   const std::string qualifying_separator_;
0151   const std::string default_extension_;
0152 };
0153 
0154 struct CommentConfig {
0155   const char *first_line;
0156   const char *content_line_prefix;
0157   const char *last_line;
0158 };
0159 
0160 extern void GenComment(const std::vector<std::string> &dc,
0161                        std::string *code_ptr, const CommentConfig *config,
0162                        const char *prefix = "");
0163 
0164 class FloatConstantGenerator {
0165  public:
0166   virtual ~FloatConstantGenerator() {}
0167   std::string GenFloatConstant(const FieldDef &field) const;
0168 
0169  private:
0170   virtual std::string Value(double v, const std::string &src) const = 0;
0171   virtual std::string Inf(double v) const = 0;
0172   virtual std::string NaN(double v) const = 0;
0173 
0174   virtual std::string Value(float v, const std::string &src) const = 0;
0175   virtual std::string Inf(float v) const = 0;
0176   virtual std::string NaN(float v) const = 0;
0177 
0178   template<typename T>
0179   std::string GenFloatConstantImpl(const FieldDef &field) const;
0180 };
0181 
0182 class SimpleFloatConstantGenerator : public FloatConstantGenerator {
0183  public:
0184   SimpleFloatConstantGenerator(const char *nan_number,
0185                                const char *pos_inf_number,
0186                                const char *neg_inf_number);
0187 
0188  private:
0189   std::string Value(double v,
0190                     const std::string &src) const FLATBUFFERS_OVERRIDE;
0191   std::string Inf(double v) const FLATBUFFERS_OVERRIDE;
0192   std::string NaN(double v) const FLATBUFFERS_OVERRIDE;
0193 
0194   std::string Value(float v, const std::string &src) const FLATBUFFERS_OVERRIDE;
0195   std::string Inf(float v) const FLATBUFFERS_OVERRIDE;
0196   std::string NaN(float v) const FLATBUFFERS_OVERRIDE;
0197 
0198   const std::string nan_number_;
0199   const std::string pos_inf_number_;
0200   const std::string neg_inf_number_;
0201 };
0202 
0203 // C++, C#, Java like generator.
0204 class TypedFloatConstantGenerator : public FloatConstantGenerator {
0205  public:
0206   TypedFloatConstantGenerator(const char *double_prefix,
0207                               const char *single_prefix, const char *nan_number,
0208                               const char *pos_inf_number,
0209                               const char *neg_inf_number = "");
0210 
0211  private:
0212   std::string Value(double v,
0213                     const std::string &src) const FLATBUFFERS_OVERRIDE;
0214   std::string Inf(double v) const FLATBUFFERS_OVERRIDE;
0215 
0216   std::string NaN(double v) const FLATBUFFERS_OVERRIDE;
0217 
0218   std::string Value(float v, const std::string &src) const FLATBUFFERS_OVERRIDE;
0219   std::string Inf(float v) const FLATBUFFERS_OVERRIDE;
0220   std::string NaN(float v) const FLATBUFFERS_OVERRIDE;
0221 
0222   std::string MakeNaN(const std::string &prefix) const;
0223   std::string MakeInf(bool neg, const std::string &prefix) const;
0224 
0225   const std::string double_prefix_;
0226   const std::string single_prefix_;
0227   const std::string nan_number_;
0228   const std::string pos_inf_number_;
0229   const std::string neg_inf_number_;
0230 };
0231 
0232 std::string JavaCSharpMakeRule(const bool java, const Parser &parser,
0233                                const std::string &path,
0234                                const std::string &file_name);
0235 
0236 }  // namespace flatbuffers
0237 
0238 #endif  // FLATBUFFERS_CODE_GENERATORS_H_