OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
t_generator.cc
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 #include "t_generator.h"
21 using namespace std;
22 
31  // Initialize the generator
32  init_generator();
33 
34  // Generate enums
35  vector<t_enum*> enums = program_->get_enums();
36  vector<t_enum*>::iterator en_iter;
37  for (en_iter = enums.begin(); en_iter != enums.end(); ++en_iter) {
38  generate_enum(*en_iter);
39  }
40 
41  // Generate typedefs
42  vector<t_typedef*> typedefs = program_->get_typedefs();
43  vector<t_typedef*>::iterator td_iter;
44  for (td_iter = typedefs.begin(); td_iter != typedefs.end(); ++td_iter) {
45  generate_typedef(*td_iter);
46  }
47 
48  // Generate structs, exceptions, and unions in declared order
49  vector<t_struct*> objects = program_->get_objects();
50  vector<t_struct*>::iterator o_iter;
51  for (o_iter = objects.begin(); o_iter != objects.end(); ++o_iter) {
52  if ((*o_iter)->is_xception()) {
53  generate_xception(*o_iter);
54  } else {
55  generate_struct(*o_iter);
56  }
57  }
58 
59 #ifdef SANDESH
60  // Generate sandeshs in declared order
61  vector<t_sandesh*> sandeshs = program_->get_sandeshs();
62  vector<t_sandesh*>::iterator s_iter;
63  for (s_iter = sandeshs.begin(); s_iter != sandeshs.end(); ++s_iter) {
64  sandesh_name_ = get_sandesh_name(*s_iter);
65  generate_sandesh(*s_iter);
66  }
67  // Generate sandesh info
68  generate_sandesh_info();
69 #endif
70 
71  // Generate constants
72  vector<t_const*> consts = program_->get_consts();
73  generate_consts(consts);
74 
75  // Generate services
76  vector<t_service*> services = program_->get_services();
77  vector<t_service*>::iterator sv_iter;
78  for (sv_iter = services.begin(); sv_iter != services.end(); ++sv_iter) {
79  service_name_ = get_service_name(*sv_iter);
80  generate_service(*sv_iter);
81  }
82 
83  // Close the generator
84  close_generator();
85 }
86 
87 string t_generator::escape_string(const string &in) const {
88  string result = "";
89  for (string::const_iterator it = in.begin(); it < in.end(); it++) {
90  std::map<char, std::string>::const_iterator res = escape_.find(*it);
91  if (res != escape_.end()) {
92  result.append(res->second);
93  } else {
94  result.push_back(*it);
95  }
96  }
97  return result;
98 }
99 
100 void t_generator::generate_consts(vector<t_const*> consts) {
101  vector<t_const*>::iterator c_iter;
102  for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) {
103  generate_const(*c_iter);
104  }
105 }
106 
108  const string& comment_start,
109  const string& line_prefix,
110  const string& contents,
111  const string& comment_end) {
112  if (comment_start != "") indent(out) << comment_start;
113  stringstream docs(contents, ios_base::in);
114  while (!docs.eof()) {
115  char line[1024];
116  docs.getline(line, 1024);
117 
118  // Just prnt a newline when the line & prefix are empty.
119  if (strlen(line) == 0 && line_prefix == "" && !docs.eof()) {
120  out << std::endl;
121  } else if (strlen(line) > 0 || !docs.eof()) { // skip the empty last line
122  indent(out) << line_prefix << line << std::endl;
123  }
124  }
125  if (comment_end != "") indent(out) << comment_end;
126 }
127 
128 
130  gen_map_t& the_map = get_generator_map();
131  if (the_map.find(factory->get_short_name()) != the_map.end()) {
132  failure("Duplicate generators for language \"%s\"!\n", factory->get_short_name().c_str());
133  }
134  the_map[factory->get_short_name()] = factory;
135 }
136 
138  const string& options) {
139  string::size_type colon = options.find(':');
140  string language = options.substr(0, colon);
141 
142  map<string, string> parsed_options;
143  if (colon != string::npos) {
144  string::size_type pos = colon+1;
145  while (pos != string::npos && pos < options.size()) {
146  string::size_type next_pos = options.find(',', pos);
147  string option = options.substr(pos, next_pos-pos);
148  pos = ((next_pos == string::npos) ? next_pos : next_pos+1);
149 
150  string::size_type separator = option.find('=');
151  string key, value;
152  if (separator == string::npos) {
153  key = option;
154  value = "";
155  } else {
156  key = option.substr(0, separator);
157  value = option.substr(separator+1);
158  }
159 
160  parsed_options[key] = value;
161  }
162  }
163 
164  gen_map_t& the_map = get_generator_map();
165  gen_map_t::iterator iter = the_map.find(language);
166 
167  if (iter == the_map.end()) {
168  return NULL;
169  }
170 
171  return iter->second->get_generator(program, parsed_options, options);
172 }
173 
175  // http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.12
176  static gen_map_t* the_map = new gen_map_t();
177  return *the_map;
178 }
179 
181  const std::string& short_name,
182  const std::string& long_name,
183  const std::string& documentation)
184  : short_name_(short_name)
185  , long_name_(long_name)
186  , documentation_(documentation)
187 {
189 }
virtual void generate_program()
Definition: t_generator.cc:30
void failure(const char *fmt,...)
t_generator_factory(const std::string &short_name, const std::string &long_name, const std::string &documentation)
Definition: t_generator.cc:180
static void register_generator(t_generator_factory *factory)
Definition: t_generator.cc:129
virtual std::string escape_string(const std::string &in) const
Definition: t_generator.cc:87
static Options options
std::map< std::string, t_generator_factory * > gen_map_t
static gen_map_t & get_generator_map()
Definition: t_generator.cc:174
std::string get_short_name()
virtual void generate_consts(std::vector< t_const * > consts)
Definition: t_generator.cc:100
static t_generator * get_generator(t_program *program, const std::string &options)
Definition: t_generator.cc:137
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