blob: 5e5865403c7fc933c066ca55beb00c4ecadbb7b7 [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
8#include "t_base_type.h"
9#include "t_typedef.h"
10#include "t_enum.h"
11#include "t_struct.h"
12#include "t_service.h"
Mark Sleee8540632006-05-30 09:24:40 +000013#include "t_list.h"
14#include "t_map.h"
15#include "t_set.h"
Mark Slee31985722006-05-24 21:45:31 +000016
17/**
18 * Top level class representing an entire thrift program. A program consists
19 * fundamentally of the following:
20 *
21 * Typedefs
22 * Enumerations
23 * Structs
Mark Slee9cb7c612006-09-01 22:17:45 +000024 * Exceptions
Mark Slee31985722006-05-24 21:45:31 +000025 * Services
26 *
Mark Sleef5377b32006-10-10 01:42:59 +000027 * The program module also contains the definitions of the base types.
28 *
Mark Slee31985722006-05-24 21:45:31 +000029 * @author Mark Slee <mcslee@facebook.com>
30 */
31class t_program {
32 public:
33 t_program(std::string name) :
Mark Sleef5377b32006-10-10 01:42:59 +000034 name_(name),
35 namespace_() {
Mark Slee31985722006-05-24 21:45:31 +000036 type_void = new t_base_type("void", t_base_type::TYPE_VOID);
37 type_string = new t_base_type("string", t_base_type::TYPE_STRING);
Mark Slee78f58e22006-09-02 04:17:07 +000038 type_bool = new t_base_type("bool", t_base_type::TYPE_BOOL);
Mark Slee31985722006-05-24 21:45:31 +000039 type_byte = new t_base_type("byte", t_base_type::TYPE_BYTE);
Mark Slee9cb7c612006-09-01 22:17:45 +000040 type_i16 = new t_base_type("i16", t_base_type::TYPE_I16);
Mark Slee31985722006-05-24 21:45:31 +000041 type_i32 = new t_base_type("i32", t_base_type::TYPE_I32);
Mark Slee31985722006-05-24 21:45:31 +000042 type_i64 = new t_base_type("i64", t_base_type::TYPE_I64);
Mark Sleec98d0502006-09-06 02:42:25 +000043 type_double = new t_base_type("double", t_base_type::TYPE_DOUBLE);
Mark Slee31985722006-05-24 21:45:31 +000044 }
45
46 ~t_program() {
47 delete type_string;
Mark Slee78f58e22006-09-02 04:17:07 +000048 delete type_bool;
Mark Slee31985722006-05-24 21:45:31 +000049 delete type_byte;
Mark Slee9cb7c612006-09-01 22:17:45 +000050 delete type_i16;
Mark Slee31985722006-05-24 21:45:31 +000051 delete type_i32;
Mark Slee31985722006-05-24 21:45:31 +000052 delete type_i64;
Mark Sleec98d0502006-09-06 02:42:25 +000053 delete type_double;
Mark Slee31985722006-05-24 21:45:31 +000054 }
55
56 // Name accessor
Mark Sleef5377b32006-10-10 01:42:59 +000057 const std::string& get_name() const {
58 return name_;
59 }
Mark Slee31985722006-05-24 21:45:31 +000060
Mark Slee9cb7c612006-09-01 22:17:45 +000061 // Namespace
Mark Sleef5377b32006-10-10 01:42:59 +000062 const std::string& get_namespace() const {
63 return namespace_;
64 }
Mark Slee9cb7c612006-09-01 22:17:45 +000065
Mark Slee31985722006-05-24 21:45:31 +000066 // Accessors for program elements
Mark Slee9cb7c612006-09-01 22:17:45 +000067 const std::vector<t_typedef*>& get_typedefs() const { return typedefs_; }
68 const std::vector<t_enum*>& get_enums() const { return enums_; }
69 const std::vector<t_struct*>& get_structs() const { return structs_; }
70 const std::vector<t_struct*>& get_xceptions() const { return xceptions_; }
71 const std::vector<t_service*>& get_services() const { return services_; }
Mark Slee31985722006-05-24 21:45:31 +000072
Mark Slee31985722006-05-24 21:45:31 +000073 // Accessors for global types
74 t_type* get_void_type() const { return type_void; }
75 t_type* get_string_type() const { return type_string; }
Mark Slee4e755ca2006-09-12 00:46:08 +000076 t_type* get_bool_type() const { return type_bool; }
Mark Slee31985722006-05-24 21:45:31 +000077 t_type* get_byte_type() const { return type_byte; }
Mark Slee9cb7c612006-09-01 22:17:45 +000078 t_type* get_i16_type() const { return type_i16; }
Mark Slee31985722006-05-24 21:45:31 +000079 t_type* get_i32_type() const { return type_i32; }
Mark Slee31985722006-05-24 21:45:31 +000080 t_type* get_i64_type() const { return type_i64; }
Mark Sleec98d0502006-09-06 02:42:25 +000081 t_type* get_double_type() const { return type_double; }
Mark Slee31985722006-05-24 21:45:31 +000082
Mark Sleee8540632006-05-30 09:24:40 +000083 // Custom data type lookup
Mark Sleee8540632006-05-30 09:24:40 +000084 t_type* get_custom_type(std::string name) {
85 return custom_types_[name];
86 }
87
88 // New program element addition
Mark Sleef5377b32006-10-10 01:42:59 +000089
Mark Slee9cb7c612006-09-01 22:17:45 +000090 void set_namespace(std::string name) {
91 namespace_ = name;
92 }
93
Mark Sleee8540632006-05-30 09:24:40 +000094 void add_typedef(t_typedef* td) {
95 typedefs_.push_back(td);
96 add_custom_type(td->get_symbolic(), td);
97 }
Mark Sleef5377b32006-10-10 01:42:59 +000098
Mark Sleee8540632006-05-30 09:24:40 +000099 void add_enum(t_enum* te) {
100 enums_.push_back(te);
101 add_custom_type(te->get_name(), te);
102 }
Mark Sleef5377b32006-10-10 01:42:59 +0000103
Mark Sleee8540632006-05-30 09:24:40 +0000104 void add_struct(t_struct* ts) {
105 structs_.push_back(ts);
106 add_custom_type(ts->get_name(), ts);
107 }
Mark Sleef5377b32006-10-10 01:42:59 +0000108
Mark Slee9cb7c612006-09-01 22:17:45 +0000109 void add_xception(t_struct* tx) {
110 xceptions_.push_back(tx);
111 add_custom_type(tx->get_name(), tx);
112 }
Mark Sleef5377b32006-10-10 01:42:59 +0000113
Mark Sleee8540632006-05-30 09:24:40 +0000114 void add_service(t_service* ts) {
115 services_.push_back(ts);
116 }
117
Mark Slee31985722006-05-24 21:45:31 +0000118 private:
Mark Sleef5377b32006-10-10 01:42:59 +0000119
Mark Slee9cb7c612006-09-01 22:17:45 +0000120 // Add custom type for lookup
121 void add_custom_type(std::string name, t_type* type) {
122 custom_types_[name] = type;
123 }
124
Mark Slee31985722006-05-24 21:45:31 +0000125 // Name
126 std::string name_;
127
Mark Slee9cb7c612006-09-01 22:17:45 +0000128 // Namespace
129 std::string namespace_;
130
Mark Slee31985722006-05-24 21:45:31 +0000131 // Components
Mark Slee9cb7c612006-09-01 22:17:45 +0000132 std::vector<t_typedef*> typedefs_;
133 std::vector<t_enum*> enums_;
134 std::vector<t_struct*> structs_;
135 std::vector<t_struct*> xceptions_;
136 std::vector<t_service*> services_;
Mark Sleee8540632006-05-30 09:24:40 +0000137
138 // Type map
139 std::map<std::string, t_type*> custom_types_;
140
Mark Slee31985722006-05-24 21:45:31 +0000141 // Global base types
142 t_type* type_void;
143 t_type* type_string;
Mark Slee78f58e22006-09-02 04:17:07 +0000144 t_type* type_bool;
Mark Slee31985722006-05-24 21:45:31 +0000145 t_type* type_byte;
Mark Slee9cb7c612006-09-01 22:17:45 +0000146 t_type* type_i16;
Mark Slee31985722006-05-24 21:45:31 +0000147 t_type* type_i32;
Mark Slee31985722006-05-24 21:45:31 +0000148 t_type* type_i64;
Mark Sleec98d0502006-09-06 02:42:25 +0000149 t_type* type_double;
Mark Slee31985722006-05-24 21:45:31 +0000150};
151
152#endif