David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
Mark Slee | e9ce01c | 2007-05-16 02:29:53 +0000 | [diff] [blame] | 19 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 20 | #ifndef T_STRUCT_H |
| 21 | #define T_STRUCT_H |
| 22 | |
| 23 | #include <vector> |
| 24 | #include <string> |
| 25 | |
| 26 | #include "t_type.h" |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 27 | #include "t_field.h" |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 28 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 29 | // Forward declare that puppy |
| 30 | class t_program; |
| 31 | |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 32 | /** |
| 33 | * A struct is a container for a set of member fields that has a name. Structs |
| 34 | * are also used to implement exception types. |
| 35 | * |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 36 | */ |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 37 | class t_struct : public t_type { |
| 38 | public: |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 39 | t_struct(t_program* program) : |
| 40 | t_type(program), |
Mark Slee | 782abbb | 2007-01-19 00:17:02 +0000 | [diff] [blame] | 41 | is_xception_(false), |
David Reiss | f84b360 | 2007-09-17 21:16:32 +0000 | [diff] [blame] | 42 | xsd_all_(false) {} |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 43 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 44 | t_struct(t_program* program, const std::string& name) : |
| 45 | t_type(program, name), |
Mark Slee | 782abbb | 2007-01-19 00:17:02 +0000 | [diff] [blame] | 46 | is_xception_(false), |
David Reiss | f84b360 | 2007-09-17 21:16:32 +0000 | [diff] [blame] | 47 | xsd_all_(false) {} |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 48 | |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 49 | void set_name(const std::string& name) { |
| 50 | name_ = name; |
| 51 | } |
| 52 | |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 53 | void set_xception(bool is_xception) { |
| 54 | is_xception_ = is_xception; |
| 55 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 56 | |
Mark Slee | 782abbb | 2007-01-19 00:17:02 +0000 | [diff] [blame] | 57 | void set_xsd_all(bool xsd_all) { |
| 58 | xsd_all_ = xsd_all; |
| 59 | } |
| 60 | |
| 61 | bool get_xsd_all() const { |
| 62 | return xsd_all_; |
| 63 | } |
| 64 | |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 65 | void append(t_field* elem) { |
| 66 | members_.push_back(elem); |
| 67 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 68 | |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 69 | const std::vector<t_field*>& get_members() { |
| 70 | return members_; |
| 71 | } |
| 72 | |
| 73 | bool is_struct() const { |
| 74 | return !is_xception_; |
| 75 | } |
| 76 | |
| 77 | bool is_xception() const { |
| 78 | return is_xception_; |
| 79 | } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 80 | |
David Reiss | 18bf22d | 2007-08-28 20:49:17 +0000 | [diff] [blame] | 81 | virtual std::string get_fingerprint_material() const { |
| 82 | std::string rv = "{"; |
| 83 | std::vector<t_field*>::const_iterator m_iter; |
| 84 | for (m_iter = members_.begin(); m_iter != members_.end(); ++m_iter) { |
David Reiss | 6ce97e3 | 2007-08-30 23:18:31 +0000 | [diff] [blame] | 85 | rv += (*m_iter)->get_fingerprint_material(); |
David Reiss | 18bf22d | 2007-08-28 20:49:17 +0000 | [diff] [blame] | 86 | rv += ";"; |
| 87 | } |
| 88 | rv += "}"; |
| 89 | return rv; |
| 90 | } |
| 91 | |
David Reiss | 9885c68 | 2007-08-30 23:12:37 +0000 | [diff] [blame] | 92 | virtual void generate_fingerprint() { |
| 93 | t_type::generate_fingerprint(); |
| 94 | std::vector<t_field*>::const_iterator m_iter; |
| 95 | for (m_iter = members_.begin(); m_iter != members_.end(); ++m_iter) { |
David Reiss | 6ce97e3 | 2007-08-30 23:18:31 +0000 | [diff] [blame] | 96 | (*m_iter)->get_type()->generate_fingerprint(); |
David Reiss | 18bf22d | 2007-08-28 20:49:17 +0000 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | |
Mark Slee | 6f9ac3f | 2007-11-28 22:28:13 +0000 | [diff] [blame] | 100 | bool validate_field(t_field* field) { |
| 101 | int key = field->get_key(); |
| 102 | std::vector<t_field*>::const_iterator m_iter; |
| 103 | for (m_iter = members_.begin(); m_iter != members_.end(); ++m_iter) { |
| 104 | if ((*m_iter)->get_key() == key) { |
| 105 | return false; |
| 106 | } |
| 107 | } |
| 108 | return true; |
| 109 | } |
| 110 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 111 | private: |
David Reiss | 18bf22d | 2007-08-28 20:49:17 +0000 | [diff] [blame] | 112 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 113 | std::vector<t_field*> members_; |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 114 | bool is_xception_; |
Mark Slee | 782abbb | 2007-01-19 00:17:02 +0000 | [diff] [blame] | 115 | |
| 116 | bool xsd_all_; |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | #endif |