blob: a3270adc3a9fc3b0f9692e8319e844bb1830436a [file] [log] [blame]
Mark Slee31985722006-05-24 21:45:31 +00001#ifndef T_PROGRAM_H
2#define T_PROGRAM_H
3
Mark Sleee8540632006-05-30 09:24:40 +00004#include <map>
Mark Slee31985722006-05-24 21:45:31 +00005#include <string>
6#include <vector>
7
Mark Sleef0712dc2006-10-25 19:03:57 +00008// For program_name()
9#include "main.h"
10
11#include "t_scope.h"
Mark Slee31985722006-05-24 21:45:31 +000012#include "t_base_type.h"
13#include "t_typedef.h"
14#include "t_enum.h"
15#include "t_struct.h"
16#include "t_service.h"
Mark Sleee8540632006-05-30 09:24:40 +000017#include "t_list.h"
18#include "t_map.h"
19#include "t_set.h"
Mark Slee31985722006-05-24 21:45:31 +000020
21/**
22 * Top level class representing an entire thrift program. A program consists
23 * fundamentally of the following:
24 *
25 * Typedefs
26 * Enumerations
27 * Structs
Mark Slee9cb7c612006-09-01 22:17:45 +000028 * Exceptions
Mark Slee31985722006-05-24 21:45:31 +000029 * Services
30 *
Mark Sleef5377b32006-10-10 01:42:59 +000031 * The program module also contains the definitions of the base types.
32 *
Mark Slee31985722006-05-24 21:45:31 +000033 * @author Mark Slee <mcslee@facebook.com>
34 */
35class t_program {
36 public:
Mark Sleef0712dc2006-10-25 19:03:57 +000037 t_program(std::string path, std::string name) :
38 path_(path),
39 name_(name) {
40 scope_ = new t_scope();
Mark Slee31985722006-05-24 21:45:31 +000041 }
42
Mark Sleef0712dc2006-10-25 19:03:57 +000043 t_program(std::string path) :
44 path_(path) {
45 name_ = program_name(path);
46 scope_ = new t_scope();
Mark Slee31985722006-05-24 21:45:31 +000047 }
48
Mark Sleef0712dc2006-10-25 19:03:57 +000049 // Path accessor
50 const std::string& get_path() const { return path_; }
51
Mark Slee31985722006-05-24 21:45:31 +000052 // Name accessor
Mark Sleef0712dc2006-10-25 19:03:57 +000053 const std::string& get_name() const { return name_; }
Mark Slee31985722006-05-24 21:45:31 +000054
Mark Slee9cb7c612006-09-01 22:17:45 +000055 // Namespace
Mark Sleef0712dc2006-10-25 19:03:57 +000056 const std::string& get_namespace() const { return namespace_; }
Mark Slee9cb7c612006-09-01 22:17:45 +000057
Mark Slee31985722006-05-24 21:45:31 +000058 // Accessors for program elements
Mark Slee9cb7c612006-09-01 22:17:45 +000059 const std::vector<t_typedef*>& get_typedefs() const { return typedefs_; }
60 const std::vector<t_enum*>& get_enums() const { return enums_; }
61 const std::vector<t_struct*>& get_structs() const { return structs_; }
62 const std::vector<t_struct*>& get_xceptions() const { return xceptions_; }
63 const std::vector<t_service*>& get_services() const { return services_; }
Mark Slee31985722006-05-24 21:45:31 +000064
Mark Sleef0712dc2006-10-25 19:03:57 +000065 // Program elements
66 void add_typedef (t_typedef* td) { typedefs_.push_back(td); }
67 void add_enum (t_enum* te) { enums_.push_back(te); }
68 void add_struct (t_struct* ts) { structs_.push_back(ts); }
69 void add_xception (t_struct* tx) { xceptions_.push_back(tx); }
70 void add_service (t_service* ts) { services_.push_back(ts); }
Mark Slee31985722006-05-24 21:45:31 +000071
Mark Sleef0712dc2006-10-25 19:03:57 +000072 // Programs to include
73 const std::vector<t_program*>& get_includes() const { return includes_; }
Mark Sleee8540632006-05-30 09:24:40 +000074
Mark Sleef0712dc2006-10-25 19:03:57 +000075 // Scoping and namespacing
Mark Slee9cb7c612006-09-01 22:17:45 +000076 void set_namespace(std::string name) {
77 namespace_ = name;
78 }
79
Mark Sleef0712dc2006-10-25 19:03:57 +000080 // Scope accessor
81 t_scope* scope() {
82 return scope_;
Mark Sleee8540632006-05-30 09:24:40 +000083 }
Mark Sleef5377b32006-10-10 01:42:59 +000084
Mark Sleef0712dc2006-10-25 19:03:57 +000085 // Includes
86
87 void add_include(std::string path) {
88 includes_.push_back(new t_program(path));
Mark Sleee8540632006-05-30 09:24:40 +000089 }
Mark Sleef5377b32006-10-10 01:42:59 +000090
Mark Sleef0712dc2006-10-25 19:03:57 +000091 std::vector<t_program*>& get_includes() {
92 return includes_;
Mark Sleee8540632006-05-30 09:24:40 +000093 }
Mark Sleef5377b32006-10-10 01:42:59 +000094
Mark Sleef0712dc2006-10-25 19:03:57 +000095 // Language specific namespace / packaging
96
97 void set_cpp_namespace(std::string cpp_namespace) {
98 cpp_namespace_ = cpp_namespace;
Mark Slee9cb7c612006-09-01 22:17:45 +000099 }
Mark Sleef5377b32006-10-10 01:42:59 +0000100
Mark Sleef0712dc2006-10-25 19:03:57 +0000101 const std::string& get_cpp_namespace() const {
102 return cpp_namespace_;
Mark Sleee8540632006-05-30 09:24:40 +0000103 }
104
Mark Sleef0712dc2006-10-25 19:03:57 +0000105 void add_cpp_include(std::string path) {
106 cpp_includes_.push_back(path);
107 }
108
109 const std::vector<std::string>& get_cpp_includes() {
110 return cpp_includes_;
111 }
112
113 void set_java_package(std::string java_package) {
114 java_package_ = java_package;
115 }
116
117 const std::string& get_java_package() const {
118 return java_package_;
119 }
120
121
Mark Slee31985722006-05-24 21:45:31 +0000122 private:
Mark Sleef5377b32006-10-10 01:42:59 +0000123
Mark Sleef0712dc2006-10-25 19:03:57 +0000124 // File path
125 std::string path_;
Mark Slee9cb7c612006-09-01 22:17:45 +0000126
Mark Slee31985722006-05-24 21:45:31 +0000127 // Name
128 std::string name_;
129
Mark Slee9cb7c612006-09-01 22:17:45 +0000130 // Namespace
131 std::string namespace_;
132
Mark Sleef0712dc2006-10-25 19:03:57 +0000133 // Included programs
134 std::vector<t_program*> includes_;
Mark Sleee8540632006-05-30 09:24:40 +0000135
Mark Sleef0712dc2006-10-25 19:03:57 +0000136 // Identifier lookup scope
137 t_scope* scope_;
Mark Sleee8540632006-05-30 09:24:40 +0000138
Mark Sleef0712dc2006-10-25 19:03:57 +0000139 // Components to generate code for
140 std::vector<t_typedef*> typedefs_;
141 std::vector<t_enum*> enums_;
142 std::vector<t_struct*> structs_;
143 std::vector<t_struct*> xceptions_;
144 std::vector<t_service*> services_;
145
146 // C++ namespace
147 std::string cpp_namespace_;
148
149 // C++ extra includes
150 std::vector<std::string> cpp_includes_;
151
152 // Java package
153 std::string java_package_;
154
Mark Slee31985722006-05-24 21:45:31 +0000155};
156
157#endif