• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 #ifndef FLATBUFFERS_CODE_GENERATORS_H_
18 #define FLATBUFFERS_CODE_GENERATORS_H_
19 
20 #include <map>
21 #include <sstream>
22 #include "flatbuffers/idl.h"
23 
24 namespace flatbuffers {
25 
26 // Utility class to assist in generating code through use of text templates.
27 //
28 // Example code:
29 //   CodeWriter code;
30 //   code.SetValue("NAME", "Foo");
31 //   code += "void {{NAME}}() { printf("%s", "{{NAME}}"); }";
32 //   code.SetValue("NAME", "Bar");
33 //   code += "void {{NAME}}() { printf("%s", "{{NAME}}"); }";
34 //   std::cout << code.ToString() << std::endl;
35 //
36 // Output:
37 //  void Foo() { printf("%s", "Foo"); }
38 //  void Bar() { printf("%s", "Bar"); }
39 class CodeWriter {
40  public:
CodeWriter()41   CodeWriter() {}
42 
43   // Clears the current "written" code.
Clear()44   void Clear() {
45     stream_.str("");
46     stream_.clear();
47   }
48 
49   // Associates a key with a value.  All subsequent calls to operator+=, where
50   // the specified key is contained in {{ and }} delimiters will be replaced by
51   // the given value.
SetValue(const std::string & key,const std::string & value)52   void SetValue(const std::string& key, const std::string& value) {
53     value_map_[key] = value;
54   }
55 
56   // Appends the given text to the generated code as well as a newline
57   // character.  Any text within {{ and }} delimeters is replaced by values
58   // previously stored in the CodeWriter by calling SetValue above.  The newline
59   // will be suppressed if the text ends with the \\ character.
60   void operator+=(std::string text);
61 
62   // Returns the current contents of the CodeWriter as a std::string.
ToString()63   std::string ToString() const { return stream_.str(); }
64 
65  private:
66   std::map<std::string, std::string> value_map_;
67   std::stringstream stream_;
68 };
69 
70 class BaseGenerator {
71  public:
72   virtual bool generate() = 0;
73 
74   static std::string NamespaceDir(const Parser &parser,
75                                   const std::string &path,
76                                   const Namespace &ns);
77 
78  protected:
BaseGenerator(const Parser & parser,const std::string & path,const std::string & file_name,const std::string qualifying_start,const std::string qualifying_separator)79   BaseGenerator(const Parser &parser, const std::string &path,
80                 const std::string &file_name,
81                 const std::string qualifying_start,
82                 const std::string qualifying_separator)
83       : parser_(parser),
84         path_(path),
85         file_name_(file_name),
86         qualifying_start_(qualifying_start),
87         qualifying_separator_(qualifying_separator) {}
~BaseGenerator()88   virtual ~BaseGenerator() {}
89 
90   // No copy/assign.
91   BaseGenerator &operator=(const BaseGenerator &);
92   BaseGenerator(const BaseGenerator &);
93 
94   std::string NamespaceDir(const Namespace &ns) const;
95 
96   static const char *FlatBuffersGeneratedWarning();
97 
98   bool IsEverythingGenerated() const;
99 
100   static std::string FullNamespace(const char *separator, const Namespace &ns);
101 
102   static std::string LastNamespacePart(const Namespace &ns);
103 
104   // tracks the current namespace for early exit in WrapInNameSpace
105   // c++, java and csharp returns a different namespace from
106   // the following default (no early exit, always fully qualify),
107   // which works for js and php
CurrentNameSpace()108   virtual const Namespace *CurrentNameSpace() const { return nullptr; }
109 
110   // Ensure that a type is prefixed with its namespace whenever it is used
111   // outside of its namespace.
112   std::string WrapInNameSpace(const Namespace *ns,
113                               const std::string &name) const;
114 
115   std::string WrapInNameSpace(const Definition &def) const;
116 
117   const Parser &parser_;
118   const std::string &path_;
119   const std::string &file_name_;
120   const std::string qualifying_start_;
121   const std::string qualifying_separator_;
122 };
123 
124 struct CommentConfig {
125   const char *first_line;
126   const char *content_line_prefix;
127   const char *last_line;
128 };
129 
130 extern void GenComment(const std::vector<std::string> &dc,
131                        std::string *code_ptr,
132                        const CommentConfig *config,
133                        const char *prefix = "");
134 
135 }  // namespace flatbuffers
136 
137 #endif  // FLATBUFFERS_CODE_GENERATORS_H_
138