blob: 98968ff27483da366169172dfeea1e5a5ef78070 [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);
35 type_byte = new t_base_type("byte", t_base_type::TYPE_BYTE);
Mark Slee9cb7c612006-09-01 22:17:45 +000036 type_i16 = new t_base_type("i16", t_base_type::TYPE_I16);
Mark Slee31985722006-05-24 21:45:31 +000037 type_i32 = new t_base_type("i32", t_base_type::TYPE_I32);
Mark Slee31985722006-05-24 21:45:31 +000038 type_i64 = new t_base_type("i64", t_base_type::TYPE_I64);
Mark Slee31985722006-05-24 21:45:31 +000039 }
40
41 ~t_program() {
42 delete type_string;
43 delete type_byte;
Mark Slee9cb7c612006-09-01 22:17:45 +000044 delete type_i16;
Mark Slee31985722006-05-24 21:45:31 +000045 delete type_i32;
Mark Slee31985722006-05-24 21:45:31 +000046 delete type_i64;
Mark Slee31985722006-05-24 21:45:31 +000047 }
48
49 // Name accessor
50 const std::string& get_name() const { return name_; }
51
Mark Slee9cb7c612006-09-01 22:17:45 +000052 // Namespace
53 const std::string& get_namespace() const { return namespace_; }
54
Mark Slee31985722006-05-24 21:45:31 +000055 // Accessors for program elements
Mark Slee9cb7c612006-09-01 22:17:45 +000056 const std::vector<t_typedef*>& get_typedefs() const { return typedefs_; }
57 const std::vector<t_enum*>& get_enums() const { return enums_; }
58 const std::vector<t_struct*>& get_structs() const { return structs_; }
59 const std::vector<t_struct*>& get_xceptions() const { return xceptions_; }
60 const std::vector<t_service*>& get_services() const { return services_; }
Mark Slee31985722006-05-24 21:45:31 +000061
Mark Slee31985722006-05-24 21:45:31 +000062 // Accessors for global types
63 t_type* get_void_type() const { return type_void; }
64 t_type* get_string_type() const { return type_string; }
65 t_type* get_byte_type() const { return type_byte; }
Mark Slee9cb7c612006-09-01 22:17:45 +000066 t_type* get_i16_type() const { return type_i16; }
Mark Slee31985722006-05-24 21:45:31 +000067 t_type* get_i32_type() const { return type_i32; }
Mark Slee31985722006-05-24 21:45:31 +000068 t_type* get_i64_type() const { return type_i64; }
Mark Slee31985722006-05-24 21:45:31 +000069
Mark Sleee8540632006-05-30 09:24:40 +000070 // Custom data type lookup
Mark Sleee8540632006-05-30 09:24:40 +000071 t_type* get_custom_type(std::string name) {
72 return custom_types_[name];
73 }
74
75 // New program element addition
Mark Slee9cb7c612006-09-01 22:17:45 +000076 void set_namespace(std::string name) {
77 namespace_ = name;
78 }
79
Mark Sleee8540632006-05-30 09:24:40 +000080 void add_typedef(t_typedef* td) {
81 typedefs_.push_back(td);
82 add_custom_type(td->get_symbolic(), td);
83 }
84 void add_enum(t_enum* te) {
85 enums_.push_back(te);
86 add_custom_type(te->get_name(), te);
87 }
88 void add_struct(t_struct* ts) {
89 structs_.push_back(ts);
90 add_custom_type(ts->get_name(), ts);
91 }
Mark Slee9cb7c612006-09-01 22:17:45 +000092 void add_xception(t_struct* tx) {
93 xceptions_.push_back(tx);
94 add_custom_type(tx->get_name(), tx);
95 }
Mark Sleee8540632006-05-30 09:24:40 +000096 void add_service(t_service* ts) {
97 services_.push_back(ts);
98 }
99
Mark Slee31985722006-05-24 21:45:31 +0000100 private:
Mark Slee9cb7c612006-09-01 22:17:45 +0000101 // Add custom type for lookup
102 void add_custom_type(std::string name, t_type* type) {
103 custom_types_[name] = type;
104 }
105
Mark Slee31985722006-05-24 21:45:31 +0000106 // Name
107 std::string name_;
108
Mark Slee9cb7c612006-09-01 22:17:45 +0000109 // Namespace
110 std::string namespace_;
111
Mark Slee31985722006-05-24 21:45:31 +0000112 // Components
Mark Slee9cb7c612006-09-01 22:17:45 +0000113 std::vector<t_typedef*> typedefs_;
114 std::vector<t_enum*> enums_;
115 std::vector<t_struct*> structs_;
116 std::vector<t_struct*> xceptions_;
117 std::vector<t_service*> services_;
Mark Sleee8540632006-05-30 09:24:40 +0000118
119 // Type map
120 std::map<std::string, t_type*> custom_types_;
121
Mark Slee31985722006-05-24 21:45:31 +0000122 // Global base types
123 t_type* type_void;
124 t_type* type_string;
125 t_type* type_byte;
Mark Slee9cb7c612006-09-01 22:17:45 +0000126 t_type* type_i16;
Mark Slee31985722006-05-24 21:45:31 +0000127 t_type* type_i32;
Mark Slee31985722006-05-24 21:45:31 +0000128 t_type* type_i64;
Mark Slee31985722006-05-24 21:45:31 +0000129};
130
131#endif