OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
t_generator.h
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
20 #ifndef T_GENERATOR_H
21 #define T_GENERATOR_H
22 
23 #include <string>
24 #include <iostream>
25 #include <fstream>
26 #include <sstream>
27 #include "parse/t_program.h"
28 #include "globals.h"
29 #include "t_generator_registry.h"
30 
37 class t_generator {
38  public:
39  t_generator(t_program* program) {
40  tmp_ = 0;
41  indent_ = 0;
42  program_ = program;
44  escape_['\n'] = "\\n";
45  escape_['\r'] = "\\r";
46  escape_['\t'] = "\\t";
47  escape_['"'] = "\\\"";
48  escape_['\\'] = "\\\\";
49  }
50 
51  virtual ~t_generator() {}
52 
58  virtual void generate_program();
59 
60  const t_program* get_program() const { return program_; }
61 
62  void generate_docstring_comment(std::ofstream& out,
63  const std::string& comment_start,
64  const std::string& line_prefix,
65  const std::string& contents,
66  const std::string& comment_end);
67 
76  static bool is_valid_namespace(const std::string& sub_namespace) {
77  (void) sub_namespace;
78  return false;
79  }
80 
84  virtual std::string escape_string(const std::string &in) const;
85 
86  std::string get_escaped_string(t_const_value* constval) {
87  return escape_string(constval->get_string());
88  }
89 
90  protected:
91 
97  virtual void init_generator() {}
98  virtual void close_generator() {}
99 
100  virtual void generate_consts(std::vector<t_const*> consts);
101 
106  virtual void generate_typedef (t_typedef* ttypedef) = 0;
107  virtual void generate_enum (t_enum* tenum) = 0;
108  virtual void generate_const (t_const* tconst) {
109  (void) tconst;
110  }
111  virtual void generate_struct (t_struct* tstruct) = 0;
112  virtual void generate_service (t_service* tservice) = 0;
113  virtual void generate_xception (t_struct* txception) {
114  // By default exceptions are the same as structs
115  generate_struct(txception);
116  }
117 #ifdef SANDESH
118  virtual void generate_sandesh (t_sandesh* tsandesh) = 0;
119  virtual void generate_sandesh_info () {}
120 #endif
121 
125  virtual std::string get_program_name(t_program* tprogram) {
126  return tprogram->get_name();
127  }
128 
132  virtual std::string get_service_name(t_service* tservice) {
133  return tservice->get_name();
134  }
135 
136 #ifdef SANDESH
137 
140  virtual std::string get_sandesh_name(t_sandesh* tsandesh) {
141  return tsandesh->get_name();
142  }
143 #endif
144 
148  virtual std::string get_out_dir() const {
150  return program_->get_out_path() + "/";
151  }
152 
153  return program_->get_out_path() + out_dir_base_ + "/";
154  }
155 
160  std::string tmp(std::string name) {
161  std::ostringstream out;
162  out << name << tmp_++;
163  return out.str();
164  }
165 
170  void indent_up(){
171  ++indent_;
172  }
173 
174  void indent_down() {
175  --indent_;
176  }
177 
181  std::string indent() {
182  std::string ind = "";
183  int i;
184  for (i = 0; i < indent_; ++i) {
185  ind += " ";
186  }
187  return ind;
188  }
189 
193  std::ostream& indent(std::ostream &os) {
194  return os << indent();
195  }
196 
200  std::string capitalize(std::string in) {
201  in[0] = toupper(in[0]);
202  return in;
203  }
204  std::string decapitalize(std::string in) {
205  in[0] = tolower(in[0]);
206  return in;
207  }
208  std::string lowercase(std::string in) {
209  for (size_t i = 0; i < in.size(); ++i) {
210  in[i] = tolower(in[i]);
211  }
212  return in;
213  }
214  std::string uppercase(std::string in) {
215  for (size_t i = 0; i < in.size(); ++i) {
216  in[i] = toupper(in[i]);
217  }
218  return in;
219  }
228  std::string underscore(std::string in) {
229  in[0] = tolower(in[0]);
230  for (size_t i = 1; i < in.size(); ++i) {
231  if (isupper(in[i])) {
232  in[i] = tolower(in[i]);
233  in.insert(i, "_");
234  }
235  }
236  return in;
237  }
244  std::string camelcase(std::string in) {
245  std::ostringstream out;
246  bool underscore = false;
247 
248  for (size_t i = 0; i < in.size(); i++) {
249  if (in[i] == '_') {
250  underscore = true;
251  continue;
252  }
253  if (underscore) {
254  out << (char) toupper(in[i]);
255  underscore = false;
256  continue;
257  }
258  out << in[i];
259  }
260 
261  return out.str();
262  }
263 
264  public:
269  return type->get_true_type();
270  }
271 
272  protected:
277 
282  std::string program_name_;
283 
288  std::string service_name_;
289 
290 #ifdef SANDESH
291 
295  std::string sandesh_name_;
296 #endif
297 
301  std::string out_dir_base_;
302 
306  std::map<char, std::string> escape_;
307 
308  private:
312  int indent_;
313 
317  int tmp_;
318 };
319 
320 #endif
std::string indent()
Definition: t_generator.h:181
std::string decapitalize(std::string in)
Definition: t_generator.h:204
virtual void generate_program()
Definition: t_generator.cc:30
std::string tmp(std::string name)
Definition: t_generator.h:160
Definition: t_enum.h:30
t_program * program_
Definition: t_generator.h:276
std::string lowercase(std::string in)
Definition: t_generator.h:208
virtual void generate_enum(t_enum *tenum)=0
Definition: t_type.h:48
const std::string & get_name() const
Definition: t_program.h:92
virtual void generate_const(t_const *tconst)
Definition: t_generator.h:108
std::string uppercase(std::string in)
Definition: t_generator.h:214
virtual void generate_struct(t_struct *tstruct)=0
void indent_down()
Definition: t_generator.h:174
virtual std::string get_out_dir() const
Definition: t_generator.h:148
const t_program * get_program() const
Definition: t_generator.h:60
std::ostream & indent(std::ostream &os)
Definition: t_generator.h:193
std::string service_name_
Definition: t_generator.h:288
virtual std::string escape_string(const std::string &in) const
Definition: t_generator.cc:87
virtual void generate_service(t_service *tservice)=0
virtual void generate_xception(t_struct *txception)
Definition: t_generator.h:113
uint8_t type
Definition: load_balance.h:109
std::string out_dir_base_
Definition: t_generator.h:301
bool is_out_path_absolute()
Definition: t_program.h:89
virtual void close_generator()
Definition: t_generator.h:98
t_type * get_true_type()
Definition: parse.cc:21
virtual void generate_typedef(t_typedef *ttypedef)=0
static bool is_valid_namespace(const std::string &sub_namespace)
Definition: t_generator.h:76
virtual void init_generator()
Definition: t_generator.h:97
virtual std::string get_program_name(t_program *tprogram)
Definition: t_generator.h:125
virtual const std::string & get_name() const
Definition: t_type.h:56
const std::string & get_out_path() const
Definition: t_program.h:86
std::string underscore(std::string in)
Definition: t_generator.h:228
std::string get_string() const
Definition: t_const_value.h:63
static t_type * get_true_type(t_type *type)
Definition: t_generator.h:268
virtual void generate_consts(std::vector< t_const * > consts)
Definition: t_generator.cc:100
void indent_up()
Definition: t_generator.h:170
std::map< char, std::string > escape_
Definition: t_generator.h:306
virtual std::string get_service_name(t_service *tservice)
Definition: t_generator.h:132
t_generator(t_program *program)
Definition: t_generator.h:39
std::string program_name_
Definition: t_generator.h:282
void generate_docstring_comment(std::ofstream &out, const std::string &comment_start, const std::string &line_prefix, const std::string &contents, const std::string &comment_end)
Definition: t_generator.cc:107
std::string capitalize(std::string in)
Definition: t_generator.h:200
std::string get_escaped_string(t_const_value *constval)
Definition: t_generator.h:86
std::string camelcase(std::string in)
Definition: t_generator.h:244
virtual ~t_generator()
Definition: t_generator.h:51