Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 1 | #ifndef T_PROGRAM_H |
| 2 | #define T_PROGRAM_H |
| 3 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 4 | #include <map> |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 5 | #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 Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 13 | #include "t_list.h" |
| 14 | #include "t_map.h" |
| 15 | #include "t_set.h" |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 16 | |
| 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 Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame^] | 24 | * Exceptions |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 25 | * Services |
| 26 | * |
| 27 | * @author Mark Slee <mcslee@facebook.com> |
| 28 | */ |
| 29 | class t_program { |
| 30 | public: |
| 31 | t_program(std::string name) : |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame^] | 32 | name_(name), namespace_() { |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 33 | 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 Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame^] | 36 | type_i16 = new t_base_type("i16", t_base_type::TYPE_I16); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 37 | type_i32 = new t_base_type("i32", t_base_type::TYPE_I32); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 38 | type_i64 = new t_base_type("i64", t_base_type::TYPE_I64); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | ~t_program() { |
| 42 | delete type_string; |
| 43 | delete type_byte; |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame^] | 44 | delete type_i16; |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 45 | delete type_i32; |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 46 | delete type_i64; |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | // Name accessor |
| 50 | const std::string& get_name() const { return name_; } |
| 51 | |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame^] | 52 | // Namespace |
| 53 | const std::string& get_namespace() const { return namespace_; } |
| 54 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 55 | // Accessors for program elements |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame^] | 56 | 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 Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 61 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 62 | // 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 Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame^] | 66 | t_type* get_i16_type() const { return type_i16; } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 67 | t_type* get_i32_type() const { return type_i32; } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 68 | t_type* get_i64_type() const { return type_i64; } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 69 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 70 | // Custom data type lookup |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 71 | t_type* get_custom_type(std::string name) { |
| 72 | return custom_types_[name]; |
| 73 | } |
| 74 | |
| 75 | // New program element addition |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame^] | 76 | void set_namespace(std::string name) { |
| 77 | namespace_ = name; |
| 78 | } |
| 79 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 80 | 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 Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame^] | 92 | void add_xception(t_struct* tx) { |
| 93 | xceptions_.push_back(tx); |
| 94 | add_custom_type(tx->get_name(), tx); |
| 95 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 96 | void add_service(t_service* ts) { |
| 97 | services_.push_back(ts); |
| 98 | } |
| 99 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 100 | private: |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame^] | 101 | // Add custom type for lookup |
| 102 | void add_custom_type(std::string name, t_type* type) { |
| 103 | custom_types_[name] = type; |
| 104 | } |
| 105 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 106 | // Name |
| 107 | std::string name_; |
| 108 | |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame^] | 109 | // Namespace |
| 110 | std::string namespace_; |
| 111 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 112 | // Components |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame^] | 113 | 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 Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 118 | |
| 119 | // Type map |
| 120 | std::map<std::string, t_type*> custom_types_; |
| 121 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 122 | // Global base types |
| 123 | t_type* type_void; |
| 124 | t_type* type_string; |
| 125 | t_type* type_byte; |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame^] | 126 | t_type* type_i16; |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 127 | t_type* type_i32; |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 128 | t_type* type_i64; |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 129 | }; |
| 130 | |
| 131 | #endif |