forked from dreiss/old-thrift
/
t_program.h
211 lines (169 loc) · 5.54 KB
/
t_program.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Copyright (c) 2006- Facebook
// Distributed under the Thrift Software License
//
// See accompanying file LICENSE or visit the Thrift site at:
// http://developers.facebook.com/thrift/
#ifndef T_PROGRAM_H
#define T_PROGRAM_H
#include <map>
#include <string>
#include <vector>
// For program_name()
#include "main.h"
#include "t_doc.h"
#include "t_scope.h"
#include "t_base_type.h"
#include "t_typedef.h"
#include "t_enum.h"
#include "t_const.h"
#include "t_struct.h"
#include "t_service.h"
#include "t_list.h"
#include "t_map.h"
#include "t_set.h"
//#include "t_doc.h"
/**
* Top level class representing an entire thrift program. A program consists
* fundamentally of the following:
*
* Typedefs
* Enumerations
* Constants
* Structs
* Exceptions
* Services
*
* The program module also contains the definitions of the base types.
*
* @author Mark Slee <mcslee@facebook.com>
*/
class t_program : public t_doc {
public:
t_program(std::string path, std::string name) :
path_(path),
name_(name),
out_path_("./") {
scope_ = new t_scope();
}
t_program(std::string path) :
path_(path),
out_path_("./") {
name_ = program_name(path);
scope_ = new t_scope();
}
// Path accessor
const std::string& get_path() const { return path_; }
// Output path accessor
const std::string& get_out_path() const { return out_path_; }
// Name accessor
const std::string& get_name() const { return name_; }
// Namespace
const std::string& get_namespace() const { return namespace_; }
// Include prefix accessor
const std::string& get_include_prefix() const { return include_prefix_; }
// Accessors for program elements
const std::vector<t_typedef*>& get_typedefs() const { return typedefs_; }
const std::vector<t_enum*>& get_enums() const { return enums_; }
const std::vector<t_const*>& get_consts() const { return consts_; }
const std::vector<t_struct*>& get_structs() const { return structs_; }
const std::vector<t_struct*>& get_xceptions() const { return xceptions_; }
const std::vector<t_struct*>& get_objects() const { return objects_; }
const std::vector<t_service*>& get_services() const { return services_; }
// Program elements
void add_typedef (t_typedef* td) { typedefs_.push_back(td); }
void add_enum (t_enum* te) { enums_.push_back(te); }
void add_const (t_const* tc) { consts_.push_back(tc); }
void add_struct (t_struct* ts) { objects_.push_back(ts);
structs_.push_back(ts); }
void add_xception (t_struct* tx) { objects_.push_back(tx);
xceptions_.push_back(tx); }
void add_service (t_service* ts) { services_.push_back(ts); }
// Programs to include
const std::vector<t_program*>& get_includes() const { return includes_; }
void set_out_path(std::string out_path) {
out_path_ = out_path;
// Ensure that it ends with a trailing '/' (or '\' for windows machines)
char c = out_path_.at(out_path_.size() - 1);
if (!(c == '/' || c == '\\')) {
out_path_.push_back('/');
}
}
// Scoping and namespacing
void set_namespace(std::string name) {
namespace_ = name;
}
// Scope accessor
t_scope* scope() {
return scope_;
}
// Includes
void add_include(std::string path, std::string include_site) {
t_program* program = new t_program(path);
// include prefix for this program is the site at which it was included
// (minus the filename)
std::string include_prefix;
std::string::size_type last_slash = std::string::npos;
if ((last_slash = include_site.rfind("/")) != std::string::npos) {
include_prefix = include_site.substr(0, last_slash);
}
program->set_include_prefix(include_prefix);
includes_.push_back(program);
}
std::vector<t_program*>& get_includes() {
return includes_;
}
void set_include_prefix(std::string include_prefix) {
include_prefix_ = include_prefix;
// this is intended to be a directory; add a trailing slash if necessary
int len = include_prefix_.size();
if (len > 0 && include_prefix_[len - 1] != '/') {
include_prefix_ += '/';
}
}
// Language neutral namespace / packaging
void set_namespace(std::string language, std::string name_space) {
namespaces_[language] = name_space;
}
std::string get_namespace(std::string language) const {
std::map<std::string, std::string>::const_iterator iter = namespaces_.find(language);
if (iter == namespaces_.end()) {
return std::string();
}
return iter->second;
}
// Language specific namespace / packaging
void add_cpp_include(std::string path) {
cpp_includes_.push_back(path);
}
const std::vector<std::string>& get_cpp_includes() {
return cpp_includes_;
}
private:
// File path
std::string path_;
// Name
std::string name_;
// Output directory
std::string out_path_;
// Namespace
std::string namespace_;
// Included programs
std::vector<t_program*> includes_;
// Include prefix for this program, if any
std::string include_prefix_;
// Identifier lookup scope
t_scope* scope_;
// Components to generate code for
std::vector<t_typedef*> typedefs_;
std::vector<t_enum*> enums_;
std::vector<t_const*> consts_;
std::vector<t_struct*> objects_;
std::vector<t_struct*> structs_;
std::vector<t_struct*> xceptions_;
std::vector<t_service*> services_;
// Dynamic namespaces
std::map<std::string, std::string> namespaces_;
// C++ extra includes
std::vector<std::string> cpp_includes_;
};
#endif