OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
t_program.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_PROGRAM_H
21 #define T_PROGRAM_H
22 
23 #include <map>
24 #include <string>
25 #include <vector>
26 
27 // For program_name()
28 #include "main.h"
29 
30 #include "t_doc.h"
31 #include "t_scope.h"
32 #include "t_base_type.h"
33 #include "t_typedef.h"
34 #include "t_enum.h"
35 #include "t_const.h"
36 #include "t_struct.h"
37 #include "t_service.h"
38 #include "t_list.h"
39 #include "t_map.h"
40 #include "t_set.h"
41 #ifdef SANDESH
42 #include "t_sandesh.h"
43 #endif
45 //#include "t_doc.h"
46 
64 class t_program : public t_doc {
65  public:
66  t_program(std::string path, std::string name) :
67  path_(path),
68  name_(name),
69  out_path_("./"),
70  out_path_is_absolute_(false) {
71  scope_ = new t_scope();
72  }
73 
74  t_program(std::string path) :
75  path_(path),
76  out_path_("./"),
77  out_path_is_absolute_(false) {
78  name_ = program_name(path);
79  scope_ = new t_scope();
80  }
81 
82  // Path accessor
83  const std::string& get_path() const { return path_; }
84 
85  // Output path accessor
86  const std::string& get_out_path() const { return out_path_; }
87 
88  // Create gen-* dir accessor
90 
91  // Name accessor
92  const std::string& get_name() const { return name_; }
93 
94  // Namespace
95  const std::string& get_namespace() const { return namespace_; }
96 
97  // Include prefix accessor
98  const std::string& get_include_prefix() const { return include_prefix_; }
99 
100  // Accessors for program elements
101  const std::vector<t_typedef*>& get_typedefs() const { return typedefs_; }
102  const std::vector<t_enum*>& get_enums() const { return enums_; }
103  const std::vector<t_const*>& get_consts() const { return consts_; }
104  const std::vector<t_struct*>& get_structs() const { return structs_; }
105  const std::vector<t_struct*>& get_xceptions() const { return xceptions_; }
106  const std::vector<t_struct*>& get_objects() const { return objects_; }
107  const std::vector<t_service*>& get_services() const { return services_; }
108 #ifdef SANDESH
109  const std::vector<t_sandesh*>& get_sandeshs() const { return sandeshs_; }
110 #endif
111 
112  // Program elements
113  void add_typedef (t_typedef* td) { typedefs_.push_back(td); }
114  void add_enum (t_enum* te) { enums_.push_back(te); }
115  void add_const (t_const* tc) { consts_.push_back(tc); }
116  void add_struct (t_struct* ts) { objects_.push_back(ts);
117  structs_.push_back(ts); }
118  void add_xception (t_struct* tx) { objects_.push_back(tx);
119  xceptions_.push_back(tx); }
120  void add_service (t_service* ts) { services_.push_back(ts); }
121 #ifdef SANDESH
122  void add_sandesh (t_sandesh* ts) { sandeshs_.push_back(ts); }
123 #endif
124 
125  // Programs to include
126  const std::vector<t_program*>& get_includes() const { return includes_; }
127 
128  void set_out_path(std::string out_path, bool out_path_is_absolute) {
129  out_path_ = out_path;
130  out_path_is_absolute_ = out_path_is_absolute;
131  // Ensure that it ends with a trailing '/')
132  char c = out_path_.at(out_path_.size() - 1);
133  if (!(c == '/')) {
134  out_path_.push_back('/');
135  }
136  }
137 
138  // Scoping and namespacing
139  void set_namespace(std::string name) {
140  namespace_ = name;
141  }
142 
143  // Scope accessor
145  return scope_;
146  }
147 
148  // Includes
149 
150  void add_include(std::string path, std::string include_site) {
151  t_program* program = new t_program(path);
152 
153  // include prefix for this program is the site at which it was included
154  // (minus the filename)
155  std::string include_prefix;
156  std::string::size_type last_slash = std::string::npos;
157  if ((last_slash = include_site.rfind("/")) != std::string::npos) {
158  include_prefix = include_site.substr(0, last_slash);
159  }
160 
161  program->set_include_prefix(include_prefix);
162  includes_.push_back(program);
163  }
164 
165  std::vector<t_program*>& get_includes() {
166  return includes_;
167  }
168 
169  void set_include_prefix(std::string include_prefix) {
170  include_prefix_ = include_prefix;
171 
172  // this is intended to be a directory; add a trailing slash if necessary
173  int len = include_prefix_.size();
174  if (len > 0 && include_prefix_[len - 1] != '/') {
175  include_prefix_ += '/';
176  }
177  }
178 
179  // Language neutral namespace / packaging
180  void set_namespace(std::string language, std::string name_space) {
181 #if 0
182  if (language != "*") {
183  size_t sub_index = language.find('.');
184  std::string base_language = language.substr(0, sub_index);
185  std::string sub_namespace;
186 
187  if(base_language == "smalltalk") {
188  pwarning(1, "Namespace 'smalltalk' is deprecated. Use 'st' instead");
189  base_language = "st";
190  }
191 
193 
194  t_generator_registry::gen_map_t::iterator it;
195  it=my_copy.find(base_language);
196 
197  if (it == my_copy.end()) {
198  throw "No generator named '" + base_language + "' could be found!";
199  }
200 
201  if (sub_index != std::string::npos) {
202  std::string sub_namespace = language.substr(sub_index+1);
203  if(! it->second->is_valid_namespace(sub_namespace)) {
204  throw base_language +" generator does not accept '" + sub_namespace + "' as sub-namespace!";
205  }
206  }
207  }
208 #endif
209 
210  namespaces_[language] = name_space;
211  }
212 
213  std::string get_namespace(std::string language) const {
214  std::map<std::string, std::string>::const_iterator iter;
215  if ((iter = namespaces_.find(language)) != namespaces_.end() ||
216  (iter = namespaces_.find("*" )) != namespaces_.end()) {
217  return iter->second;
218  }
219  return std::string();
220  }
221 
222  // Language specific namespace / packaging
223 
224  void add_cpp_include(std::string path) {
225  cpp_includes_.push_back(path);
226  }
227 
228  const std::vector<std::string>& get_cpp_includes() {
229  return cpp_includes_;
230  }
231 
232  void add_c_include(std::string path) {
233  c_includes_.push_back(path);
234  }
235 
236  const std::vector<std::string>& get_c_includes() {
237  return c_includes_;
238  }
239 
240 #ifdef SANDESH
241  t_struct* get_struct(const char *name) const {
242  // Extract structs, exceptions, or unions matching the name
243  std::vector<t_struct*> objects = get_objects();
244  std::vector<t_struct*>::iterator o_iter;
245  for (o_iter = objects.begin(); o_iter != objects.end(); ++o_iter) {
246  if (strcmp(((*o_iter)->get_name()).c_str(), name) == 0) {
247  return *o_iter;
248  }
249  }
250  return NULL;
251  }
252 #endif
253 
254  private:
255 
256  // File path
257  std::string path_;
258 
259  // Name
260  std::string name_;
261 
262  // Output directory
263  std::string out_path_;
264 
265  // Output directory is absolute location for generated source (no gen-*)
267 
268  // Namespace
269  std::string namespace_;
270 
271  // Included programs
272  std::vector<t_program*> includes_;
273 
274  // Include prefix for this program, if any
275  std::string include_prefix_;
276 
277  // Identifier lookup scope
279 
280  // Components to generate code for
281  std::vector<t_typedef*> typedefs_;
282  std::vector<t_enum*> enums_;
283  std::vector<t_const*> consts_;
284  std::vector<t_struct*> objects_;
285  std::vector<t_struct*> structs_;
286  std::vector<t_struct*> xceptions_;
287  std::vector<t_service*> services_;
288 #ifdef SANDESH
289  std::vector<t_sandesh*> sandeshs_;
290 #endif
291 
292  // Dynamic namespaces
293  std::map<std::string, std::string> namespaces_;
294 
295  // C++ extra includes
296  std::vector<std::string> cpp_includes_;
297 
298  // C extra includes
299  std::vector<std::string> c_includes_;
300 
301 };
302 
303 #endif
void add_c_include(std::string path)
Definition: t_program.h:232
const std::string & get_include_prefix() const
Definition: t_program.h:98
Definition: t_enum.h:30
void add_cpp_include(std::string path)
Definition: t_program.h:224
std::vector< t_struct * > structs_
Definition: t_program.h:285
bool out_path_is_absolute_
Definition: t_program.h:266
const std::string & get_path() const
Definition: t_program.h:83
const std::vector< t_program * > & get_includes() const
Definition: t_program.h:126
string program_name(string filename)
void add_const(t_const *tc)
Definition: t_program.h:115
const std::vector< t_typedef * > & get_typedefs() const
Definition: t_program.h:101
std::string out_path_
Definition: t_program.h:263
const std::string & get_name() const
Definition: t_program.h:92
void set_namespace(std::string language, std::string name_space)
Definition: t_program.h:180
const std::vector< std::string > & get_cpp_includes()
Definition: t_program.h:228
void add_xception(t_struct *tx)
Definition: t_program.h:118
void pwarning(int level, const char *fmt,...)
std::vector< t_service * > services_
Definition: t_program.h:287
void add_include(std::string path, std::string include_site)
Definition: t_program.h:150
t_program(std::string path)
Definition: t_program.h:74
std::vector< t_typedef * > typedefs_
Definition: t_program.h:281
void set_include_prefix(std::string include_prefix)
Definition: t_program.h:169
const std::vector< t_const * > & get_consts() const
Definition: t_program.h:103
Definition: t_doc.h:27
void add_struct(t_struct *ts)
Definition: t_program.h:116
bool is_out_path_absolute()
Definition: t_program.h:89
t_scope * scope()
Definition: t_program.h:144
std::vector< std::string > c_includes_
Definition: t_program.h:299
const std::vector< t_enum * > & get_enums() const
Definition: t_program.h:102
std::string get_namespace(std::string language) const
Definition: t_program.h:213
const std::vector< t_struct * > & get_xceptions() const
Definition: t_program.h:105
t_scope * scope_
Definition: t_program.h:278
void set_out_path(std::string out_path, bool out_path_is_absolute)
Definition: t_program.h:128
std::string name_
Definition: t_program.h:260
std::vector< t_program * > includes_
Definition: t_program.h:272
std::map< std::string, t_generator_factory * > gen_map_t
static gen_map_t & get_generator_map()
Definition: t_generator.cc:174
std::vector< t_program * > & get_includes()
Definition: t_program.h:165
std::vector< t_const * > consts_
Definition: t_program.h:283
std::string namespace_
Definition: t_program.h:269
std::vector< t_struct * > xceptions_
Definition: t_program.h:286
const std::string & get_out_path() const
Definition: t_program.h:86
std::map< std::string, std::string > namespaces_
Definition: t_program.h:293
const std::string & get_namespace() const
Definition: t_program.h:95
void set_namespace(std::string name)
Definition: t_program.h:139
std::vector< t_enum * > enums_
Definition: t_program.h:282
const std::vector< t_struct * > & get_structs() const
Definition: t_program.h:104
void add_enum(t_enum *te)
Definition: t_program.h:114
const std::vector< t_service * > & get_services() const
Definition: t_program.h:107
std::string path_
Definition: t_program.h:257
std::vector< t_struct * > objects_
Definition: t_program.h:284
t_program(std::string path, std::string name)
Definition: t_program.h:66
const std::vector< t_struct * > & get_objects() const
Definition: t_program.h:106
void add_typedef(t_typedef *td)
Definition: t_program.h:113
const std::vector< std::string > & get_c_includes()
Definition: t_program.h:236
std::vector< std::string > cpp_includes_
Definition: t_program.h:296
std::string include_prefix_
Definition: t_program.h:275
void add_service(t_service *ts)
Definition: t_program.h:120