code_generators.h 7.7 KB

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