| 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 | 
|  | 24 | *   Services | 
|  | 25 | * | 
|  | 26 | * @author Mark Slee <mcslee@facebook.com> | 
|  | 27 | */ | 
|  | 28 | class t_program { | 
|  | 29 | public: | 
|  | 30 | t_program(std::string name) : | 
|  | 31 | name_(name) { | 
|  | 32 | type_void   = new t_base_type("void",   t_base_type::TYPE_VOID); | 
|  | 33 | type_string = new t_base_type("string", t_base_type::TYPE_STRING); | 
|  | 34 | type_byte   = new t_base_type("byte",   t_base_type::TYPE_BYTE); | 
|  | 35 | type_i32    = new t_base_type("i32",    t_base_type::TYPE_I32); | 
|  | 36 | type_u32    = new t_base_type("u32",    t_base_type::TYPE_U32); | 
|  | 37 | type_i64    = new t_base_type("i64",    t_base_type::TYPE_I64); | 
|  | 38 | type_u64    = new t_base_type("u64",    t_base_type::TYPE_U64); | 
|  | 39 | } | 
|  | 40 |  | 
|  | 41 | ~t_program() { | 
|  | 42 | delete type_string; | 
|  | 43 | delete type_byte; | 
|  | 44 | delete type_i32; | 
|  | 45 | delete type_u32; | 
|  | 46 | delete type_i64; | 
|  | 47 | delete type_u64; | 
|  | 48 | } | 
|  | 49 |  | 
|  | 50 | // Name accessor | 
|  | 51 | const std::string& get_name() const { return name_; } | 
|  | 52 |  | 
|  | 53 | // Accessors for program elements | 
|  | 54 | const std::vector<t_typedef*>& get_typedefs() const { return typedefs_; } | 
|  | 55 | const std::vector<t_enum*>&    get_enums()    const { return enums_;    } | 
|  | 56 | const std::vector<t_struct*>&  get_structs()  const { return structs_;  } | 
|  | 57 | const std::vector<t_service*>& get_services() const { return services_; } | 
|  | 58 |  | 
| Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 59 | // Accessors for global types | 
|  | 60 | t_type* get_void_type()   const { return type_void;   } | 
|  | 61 | t_type* get_string_type() const { return type_string; } | 
|  | 62 | t_type* get_byte_type()   const { return type_byte;   } | 
|  | 63 | t_type* get_i32_type()    const { return type_i32;    } | 
|  | 64 | t_type* get_u32_type()    const { return type_u32;    } | 
|  | 65 | t_type* get_i64_type()    const { return type_i64;    } | 
|  | 66 | t_type* get_u64_type()    const { return type_u64;    } | 
|  | 67 |  | 
| Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 68 | // Custom data type lookup | 
|  | 69 | void add_custom_type(std::string name, t_type* type) { | 
|  | 70 | custom_types_[name] = type; | 
|  | 71 | } | 
|  | 72 | t_type* get_custom_type(std::string name) { | 
|  | 73 | return custom_types_[name]; | 
|  | 74 | } | 
|  | 75 |  | 
|  | 76 | // New program element addition | 
|  | 77 | void add_typedef(t_typedef* td) { | 
|  | 78 | typedefs_.push_back(td); | 
|  | 79 | add_custom_type(td->get_symbolic(), td); | 
|  | 80 | } | 
|  | 81 | void add_enum(t_enum* te) { | 
|  | 82 | enums_.push_back(te); | 
|  | 83 | add_custom_type(te->get_name(), te); | 
|  | 84 | } | 
|  | 85 | void add_struct(t_struct* ts) { | 
|  | 86 | structs_.push_back(ts); | 
|  | 87 | add_custom_type(ts->get_name(), ts); | 
|  | 88 | } | 
|  | 89 | void add_service(t_service* ts) { | 
|  | 90 | services_.push_back(ts); | 
|  | 91 | } | 
|  | 92 |  | 
| Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 93 | private: | 
|  | 94 | // Name | 
|  | 95 | std::string name_; | 
|  | 96 |  | 
|  | 97 | // Components | 
|  | 98 | std::vector<t_typedef*> typedefs_; | 
|  | 99 | std::vector<t_enum*>    enums_; | 
|  | 100 | std::vector<t_struct*>  structs_; | 
|  | 101 | std::vector<t_service*> services_; | 
| Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 102 |  | 
|  | 103 | // Type map | 
|  | 104 | std::map<std::string, t_type*> custom_types_; | 
|  | 105 |  | 
| Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 106 | // Global base types | 
|  | 107 | t_type* type_void; | 
|  | 108 | t_type* type_string; | 
|  | 109 | t_type* type_byte; | 
|  | 110 | t_type* type_i32; | 
|  | 111 | t_type* type_u32; | 
|  | 112 | t_type* type_i64; | 
|  | 113 | t_type* type_u64; | 
|  | 114 | }; | 
|  | 115 |  | 
|  | 116 | #endif |