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