blob: 9fa42711f36a023cf643af4d3b0af76d89ba675b [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 *
27 * @author Mark Slee <mcslee@facebook.com>
28 */
29class t_program {
30 public:
31 t_program(std::string name) :
Mark Slee9cb7c612006-09-01 22:17:45 +000032 name_(name), namespace_() {
Mark Slee31985722006-05-24 21:45:31 +000033 type_void = new t_base_type("void", t_base_type::TYPE_VOID);
34 type_string = new t_base_type("string", t_base_type::TYPE_STRING);
Mark Slee78f58e22006-09-02 04:17:07 +000035 type_bool = new t_base_type("bool", t_base_type::TYPE_BOOL);
Mark Slee31985722006-05-24 21:45:31 +000036 type_byte = new t_base_type("byte", t_base_type::TYPE_BYTE);
Mark Slee9cb7c612006-09-01 22:17:45 +000037 type_i16 = new t_base_type("i16", t_base_type::TYPE_I16);
Mark Slee31985722006-05-24 21:45:31 +000038 type_i32 = new t_base_type("i32", t_base_type::TYPE_I32);
Mark Slee31985722006-05-24 21:45:31 +000039 type_i64 = new t_base_type("i64", t_base_type::TYPE_I64);
Mark Sleec98d0502006-09-06 02:42:25 +000040 type_double = new t_base_type("double", t_base_type::TYPE_DOUBLE);
Mark Slee31985722006-05-24 21:45:31 +000041 }
42
43 ~t_program() {
44 delete type_string;
Mark Slee78f58e22006-09-02 04:17:07 +000045 delete type_bool;
Mark Slee31985722006-05-24 21:45:31 +000046 delete type_byte;
Mark Slee9cb7c612006-09-01 22:17:45 +000047 delete type_i16;
Mark Slee31985722006-05-24 21:45:31 +000048 delete type_i32;
Mark Slee31985722006-05-24 21:45:31 +000049 delete type_i64;
Mark Sleec98d0502006-09-06 02:42:25 +000050 delete type_double;
Mark Slee31985722006-05-24 21:45:31 +000051 }
52
53 // Name accessor
54 const std::string& get_name() const { return name_; }
55
Mark Slee9cb7c612006-09-01 22:17:45 +000056 // Namespace
57 const std::string& get_namespace() const { return namespace_; }
58
Mark Slee31985722006-05-24 21:45:31 +000059 // Accessors for program elements
Mark Slee9cb7c612006-09-01 22:17:45 +000060 const std::vector<t_typedef*>& get_typedefs() const { return typedefs_; }
61 const std::vector<t_enum*>& get_enums() const { return enums_; }
62 const std::vector<t_struct*>& get_structs() const { return structs_; }
63 const std::vector<t_struct*>& get_xceptions() const { return xceptions_; }
64 const std::vector<t_service*>& get_services() const { return services_; }
Mark Slee31985722006-05-24 21:45:31 +000065
Mark Slee31985722006-05-24 21:45:31 +000066 // Accessors for global types
67 t_type* get_void_type() const { return type_void; }
68 t_type* get_string_type() const { return type_string; }
Mark Slee78f58e22006-09-02 04:17:07 +000069 t_type* get_bool_type() const { return type_byte; }
Mark Slee31985722006-05-24 21:45:31 +000070 t_type* get_byte_type() const { return type_byte; }
Mark Slee9cb7c612006-09-01 22:17:45 +000071 t_type* get_i16_type() const { return type_i16; }
Mark Slee31985722006-05-24 21:45:31 +000072 t_type* get_i32_type() const { return type_i32; }
Mark Slee31985722006-05-24 21:45:31 +000073 t_type* get_i64_type() const { return type_i64; }
Mark Sleec98d0502006-09-06 02:42:25 +000074 t_type* get_double_type() const { return type_double; }
Mark Slee31985722006-05-24 21:45:31 +000075
Mark Sleee8540632006-05-30 09:24:40 +000076 // Custom data type lookup
Mark Sleee8540632006-05-30 09:24:40 +000077 t_type* get_custom_type(std::string name) {
78 return custom_types_[name];
79 }
80
81 // New program element addition
Mark Slee9cb7c612006-09-01 22:17:45 +000082 void set_namespace(std::string name) {
83 namespace_ = name;
84 }
85
Mark Sleee8540632006-05-30 09:24:40 +000086 void add_typedef(t_typedef* td) {
87 typedefs_.push_back(td);
88 add_custom_type(td->get_symbolic(), td);
89 }
90 void add_enum(t_enum* te) {
91 enums_.push_back(te);
92 add_custom_type(te->get_name(), te);
93 }
94 void add_struct(t_struct* ts) {
95 structs_.push_back(ts);
96 add_custom_type(ts->get_name(), ts);
97 }
Mark Slee9cb7c612006-09-01 22:17:45 +000098 void add_xception(t_struct* tx) {
99 xceptions_.push_back(tx);
100 add_custom_type(tx->get_name(), tx);
101 }
Mark Sleee8540632006-05-30 09:24:40 +0000102 void add_service(t_service* ts) {
103 services_.push_back(ts);
104 }
105
Mark Slee31985722006-05-24 21:45:31 +0000106 private:
Mark Slee9cb7c612006-09-01 22:17:45 +0000107 // Add custom type for lookup
108 void add_custom_type(std::string name, t_type* type) {
109 custom_types_[name] = type;
110 }
111
Mark Slee31985722006-05-24 21:45:31 +0000112 // Name
113 std::string name_;
114
Mark Slee9cb7c612006-09-01 22:17:45 +0000115 // Namespace
116 std::string namespace_;
117
Mark Slee31985722006-05-24 21:45:31 +0000118 // Components
Mark Slee9cb7c612006-09-01 22:17:45 +0000119 std::vector<t_typedef*> typedefs_;
120 std::vector<t_enum*> enums_;
121 std::vector<t_struct*> structs_;
122 std::vector<t_struct*> xceptions_;
123 std::vector<t_service*> services_;
Mark Sleee8540632006-05-30 09:24:40 +0000124
125 // Type map
126 std::map<std::string, t_type*> custom_types_;
127
Mark Slee31985722006-05-24 21:45:31 +0000128 // Global base types
129 t_type* type_void;
130 t_type* type_string;
Mark Slee78f58e22006-09-02 04:17:07 +0000131 t_type* type_bool;
Mark Slee31985722006-05-24 21:45:31 +0000132 t_type* type_byte;
Mark Slee9cb7c612006-09-01 22:17:45 +0000133 t_type* type_i16;
Mark Slee31985722006-05-24 21:45:31 +0000134 t_type* type_i32;
Mark Slee31985722006-05-24 21:45:31 +0000135 t_type* type_i64;
Mark Sleec98d0502006-09-06 02:42:25 +0000136 t_type* type_double;
Mark Slee31985722006-05-24 21:45:31 +0000137};
138
139#endif