Mark Slee | e9ce01c | 2007-05-16 02:29:53 +0000 | [diff] [blame] | 1 | // Copyright (c) 2006- Facebook |
| 2 | // Distributed under the Thrift Software License |
| 3 | // |
| 4 | // See accompanying file LICENSE or visit the Thrift site at: |
| 5 | // http://developers.facebook.com/thrift/ |
| 6 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 7 | #ifndef T_STRUCT_H |
| 8 | #define T_STRUCT_H |
| 9 | |
| 10 | #include <vector> |
| 11 | #include <string> |
| 12 | |
| 13 | #include "t_type.h" |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 14 | #include "t_field.h" |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 15 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 16 | // Forward declare that puppy |
| 17 | class t_program; |
| 18 | |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 19 | /** |
| 20 | * A struct is a container for a set of member fields that has a name. Structs |
| 21 | * are also used to implement exception types. |
| 22 | * |
| 23 | * @author Mark Slee <mcslee@facebook.com> |
| 24 | */ |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 25 | class t_struct : public t_type { |
| 26 | public: |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 27 | t_struct(t_program* program) : |
| 28 | t_type(program), |
Mark Slee | 782abbb | 2007-01-19 00:17:02 +0000 | [diff] [blame] | 29 | is_xception_(false), |
David Reiss | f84b360 | 2007-09-17 21:16:32 +0000 | [diff] [blame] | 30 | xsd_all_(false) {} |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 31 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 32 | t_struct(t_program* program, const std::string& name) : |
| 33 | t_type(program, name), |
Mark Slee | 782abbb | 2007-01-19 00:17:02 +0000 | [diff] [blame] | 34 | is_xception_(false), |
David Reiss | f84b360 | 2007-09-17 21:16:32 +0000 | [diff] [blame] | 35 | xsd_all_(false) {} |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 36 | |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 37 | void set_name(const std::string& name) { |
| 38 | name_ = name; |
| 39 | } |
| 40 | |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 41 | void set_xception(bool is_xception) { |
| 42 | is_xception_ = is_xception; |
| 43 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 44 | |
Mark Slee | 782abbb | 2007-01-19 00:17:02 +0000 | [diff] [blame] | 45 | void set_xsd_all(bool xsd_all) { |
| 46 | xsd_all_ = xsd_all; |
| 47 | } |
| 48 | |
| 49 | bool get_xsd_all() const { |
| 50 | return xsd_all_; |
| 51 | } |
| 52 | |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 53 | void append(t_field* elem) { |
| 54 | members_.push_back(elem); |
| 55 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 56 | |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 57 | const std::vector<t_field*>& get_members() { |
| 58 | return members_; |
| 59 | } |
| 60 | |
| 61 | bool is_struct() const { |
| 62 | return !is_xception_; |
| 63 | } |
| 64 | |
| 65 | bool is_xception() const { |
| 66 | return is_xception_; |
| 67 | } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 68 | |
David Reiss | 18bf22d | 2007-08-28 20:49:17 +0000 | [diff] [blame] | 69 | virtual std::string get_fingerprint_material() const { |
| 70 | std::string rv = "{"; |
| 71 | std::vector<t_field*>::const_iterator m_iter; |
| 72 | for (m_iter = members_.begin(); m_iter != members_.end(); ++m_iter) { |
David Reiss | 6ce97e3 | 2007-08-30 23:18:31 +0000 | [diff] [blame] | 73 | rv += (*m_iter)->get_fingerprint_material(); |
David Reiss | 18bf22d | 2007-08-28 20:49:17 +0000 | [diff] [blame] | 74 | rv += ";"; |
| 75 | } |
| 76 | rv += "}"; |
| 77 | return rv; |
| 78 | } |
| 79 | |
David Reiss | 9885c68 | 2007-08-30 23:12:37 +0000 | [diff] [blame] | 80 | virtual void generate_fingerprint() { |
| 81 | t_type::generate_fingerprint(); |
| 82 | std::vector<t_field*>::const_iterator m_iter; |
| 83 | for (m_iter = members_.begin(); m_iter != members_.end(); ++m_iter) { |
David Reiss | 6ce97e3 | 2007-08-30 23:18:31 +0000 | [diff] [blame] | 84 | (*m_iter)->get_type()->generate_fingerprint(); |
David Reiss | 18bf22d | 2007-08-28 20:49:17 +0000 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | |
Mark Slee | 6f9ac3f | 2007-11-28 22:28:13 +0000 | [diff] [blame] | 88 | bool validate_field(t_field* field) { |
| 89 | int key = field->get_key(); |
| 90 | std::vector<t_field*>::const_iterator m_iter; |
| 91 | for (m_iter = members_.begin(); m_iter != members_.end(); ++m_iter) { |
| 92 | if ((*m_iter)->get_key() == key) { |
| 93 | return false; |
| 94 | } |
| 95 | } |
| 96 | return true; |
| 97 | } |
| 98 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 99 | private: |
David Reiss | 18bf22d | 2007-08-28 20:49:17 +0000 | [diff] [blame] | 100 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 101 | std::vector<t_field*> members_; |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 102 | bool is_xception_; |
Mark Slee | 782abbb | 2007-01-19 00:17:02 +0000 | [diff] [blame] | 103 | |
| 104 | bool xsd_all_; |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | #endif |