blob: abb368bf70d19a1f0c424afc177d6cf300965ec0 [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 Slee31985722006-05-24 21:45:31 +000040 }
41
42 ~t_program() {
43 delete type_string;
Mark Slee78f58e22006-09-02 04:17:07 +000044 delete type_bool;
Mark Slee31985722006-05-24 21:45:31 +000045 delete type_byte;
Mark Slee9cb7c612006-09-01 22:17:45 +000046 delete type_i16;
Mark Slee31985722006-05-24 21:45:31 +000047 delete type_i32;
Mark Slee31985722006-05-24 21:45:31 +000048 delete type_i64;
Mark Slee31985722006-05-24 21:45:31 +000049 }
50
51 // Name accessor
52 const std::string& get_name() const { return name_; }
53
Mark Slee9cb7c612006-09-01 22:17:45 +000054 // Namespace
55 const std::string& get_namespace() const { return namespace_; }
56
Mark Slee31985722006-05-24 21:45:31 +000057 // Accessors for program elements
Mark Slee9cb7c612006-09-01 22:17:45 +000058 const std::vector<t_typedef*>& get_typedefs() const { return typedefs_; }
59 const std::vector<t_enum*>& get_enums() const { return enums_; }
60 const std::vector<t_struct*>& get_structs() const { return structs_; }
61 const std::vector<t_struct*>& get_xceptions() const { return xceptions_; }
62 const std::vector<t_service*>& get_services() const { return services_; }
Mark Slee31985722006-05-24 21:45:31 +000063
Mark Slee31985722006-05-24 21:45:31 +000064 // Accessors for global types
65 t_type* get_void_type() const { return type_void; }
66 t_type* get_string_type() const { return type_string; }
Mark Slee78f58e22006-09-02 04:17:07 +000067 t_type* get_bool_type() const { return type_byte; }
Mark Slee31985722006-05-24 21:45:31 +000068 t_type* get_byte_type() const { return type_byte; }
Mark Slee9cb7c612006-09-01 22:17:45 +000069 t_type* get_i16_type() const { return type_i16; }
Mark Slee31985722006-05-24 21:45:31 +000070 t_type* get_i32_type() const { return type_i32; }
Mark Slee31985722006-05-24 21:45:31 +000071 t_type* get_i64_type() const { return type_i64; }
Mark Slee31985722006-05-24 21:45:31 +000072
Mark Sleee8540632006-05-30 09:24:40 +000073 // Custom data type lookup
Mark Sleee8540632006-05-30 09:24:40 +000074 t_type* get_custom_type(std::string name) {
75 return custom_types_[name];
76 }
77
78 // New program element addition
Mark Slee9cb7c612006-09-01 22:17:45 +000079 void set_namespace(std::string name) {
80 namespace_ = name;
81 }
82
Mark Sleee8540632006-05-30 09:24:40 +000083 void add_typedef(t_typedef* td) {
84 typedefs_.push_back(td);
85 add_custom_type(td->get_symbolic(), td);
86 }
87 void add_enum(t_enum* te) {
88 enums_.push_back(te);
89 add_custom_type(te->get_name(), te);
90 }
91 void add_struct(t_struct* ts) {
92 structs_.push_back(ts);
93 add_custom_type(ts->get_name(), ts);
94 }
Mark Slee9cb7c612006-09-01 22:17:45 +000095 void add_xception(t_struct* tx) {
96 xceptions_.push_back(tx);
97 add_custom_type(tx->get_name(), tx);
98 }
Mark Sleee8540632006-05-30 09:24:40 +000099 void add_service(t_service* ts) {
100 services_.push_back(ts);
101 }
102
Mark Slee31985722006-05-24 21:45:31 +0000103 private:
Mark Slee9cb7c612006-09-01 22:17:45 +0000104 // Add custom type for lookup
105 void add_custom_type(std::string name, t_type* type) {
106 custom_types_[name] = type;
107 }
108
Mark Slee31985722006-05-24 21:45:31 +0000109 // Name
110 std::string name_;
111
Mark Slee9cb7c612006-09-01 22:17:45 +0000112 // Namespace
113 std::string namespace_;
114
Mark Slee31985722006-05-24 21:45:31 +0000115 // Components
Mark Slee9cb7c612006-09-01 22:17:45 +0000116 std::vector<t_typedef*> typedefs_;
117 std::vector<t_enum*> enums_;
118 std::vector<t_struct*> structs_;
119 std::vector<t_struct*> xceptions_;
120 std::vector<t_service*> services_;
Mark Sleee8540632006-05-30 09:24:40 +0000121
122 // Type map
123 std::map<std::string, t_type*> custom_types_;
124
Mark Slee31985722006-05-24 21:45:31 +0000125 // Global base types
126 t_type* type_void;
127 t_type* type_string;
Mark Slee78f58e22006-09-02 04:17:07 +0000128 t_type* type_bool;
Mark Slee31985722006-05-24 21:45:31 +0000129 t_type* type_byte;
Mark Slee9cb7c612006-09-01 22:17:45 +0000130 t_type* type_i16;
Mark Slee31985722006-05-24 21:45:31 +0000131 t_type* type_i32;
Mark Slee31985722006-05-24 21:45:31 +0000132 t_type* type_i64;
Mark Slee31985722006-05-24 21:45:31 +0000133};
134
135#endif