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 | |
Bryan Duxbury | ff219ac | 2009-04-10 21:51:00 +0000 | [diff] [blame] | 23 | #include <algorithm> |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 24 | #include <vector> |
Bryan Duxbury | ff219ac | 2009-04-10 21:51:00 +0000 | [diff] [blame] | 25 | #include <utility> |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 26 | #include <string> |
| 27 | |
| 28 | #include "t_type.h" |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 29 | #include "t_field.h" |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 30 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 31 | // Forward declare that puppy |
| 32 | class t_program; |
| 33 | |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 34 | /** |
| 35 | * A struct is a container for a set of member fields that has a name. Structs |
| 36 | * are also used to implement exception types. |
| 37 | * |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 38 | */ |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 39 | class t_struct : public t_type { |
| 40 | public: |
Bryan Duxbury | ff219ac | 2009-04-10 21:51:00 +0000 | [diff] [blame] | 41 | typedef std::vector<t_field*> members_type; |
| 42 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 43 | t_struct(t_program* program) : |
| 44 | t_type(program), |
Mark Slee | 782abbb | 2007-01-19 00:17:02 +0000 | [diff] [blame] | 45 | is_xception_(false), |
David Reiss | f84b360 | 2007-09-17 21:16:32 +0000 | [diff] [blame] | 46 | xsd_all_(false) {} |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 47 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 48 | t_struct(t_program* program, const std::string& name) : |
| 49 | t_type(program, name), |
Mark Slee | 782abbb | 2007-01-19 00:17:02 +0000 | [diff] [blame] | 50 | is_xception_(false), |
David Reiss | f84b360 | 2007-09-17 21:16:32 +0000 | [diff] [blame] | 51 | xsd_all_(false) {} |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 52 | |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 53 | void set_name(const std::string& name) { |
| 54 | name_ = name; |
| 55 | } |
| 56 | |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 57 | void set_xception(bool is_xception) { |
| 58 | is_xception_ = is_xception; |
| 59 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 60 | |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 61 | void set_union(bool is_union) { |
| 62 | is_union_ = is_union; |
| 63 | } |
| 64 | |
Mark Slee | 782abbb | 2007-01-19 00:17:02 +0000 | [diff] [blame] | 65 | void set_xsd_all(bool xsd_all) { |
| 66 | xsd_all_ = xsd_all; |
| 67 | } |
| 68 | |
| 69 | bool get_xsd_all() const { |
| 70 | return xsd_all_; |
| 71 | } |
| 72 | |
Bryan Duxbury | ff219ac | 2009-04-10 21:51:00 +0000 | [diff] [blame] | 73 | bool append(t_field* elem) { |
David Reiss | ebb6cc4 | 2009-04-09 20:02:56 +0000 | [diff] [blame] | 74 | members_.push_back(elem); |
Bryan Duxbury | ff219ac | 2009-04-10 21:51:00 +0000 | [diff] [blame] | 75 | |
| 76 | typedef members_type::iterator iter_type; |
| 77 | std::pair<iter_type, iter_type> bounds = std::equal_range( |
| 78 | members_in_id_order_.begin(), members_in_id_order_.end(), elem, t_field::key_compare() |
| 79 | ); |
| 80 | if (bounds.first != bounds.second) { |
| 81 | return false; |
| 82 | } |
| 83 | members_in_id_order_.insert(bounds.second, elem); |
| 84 | return true; |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 85 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 86 | |
Bryan Duxbury | ff219ac | 2009-04-10 21:51:00 +0000 | [diff] [blame] | 87 | const members_type& get_members() { |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 88 | return members_; |
| 89 | } |
| 90 | |
Bryan Duxbury | ff219ac | 2009-04-10 21:51:00 +0000 | [diff] [blame] | 91 | const members_type& get_sorted_members() { |
| 92 | return members_in_id_order_; |
| 93 | } |
| 94 | |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 95 | bool is_struct() const { |
| 96 | return !is_xception_; |
| 97 | } |
| 98 | |
| 99 | bool is_xception() const { |
| 100 | return is_xception_; |
| 101 | } |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 102 | |
| 103 | bool is_union() const { |
| 104 | return is_union_; |
| 105 | } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 106 | |
David Reiss | 18bf22d | 2007-08-28 20:49:17 +0000 | [diff] [blame] | 107 | virtual std::string get_fingerprint_material() const { |
| 108 | std::string rv = "{"; |
Bryan Duxbury | ff219ac | 2009-04-10 21:51:00 +0000 | [diff] [blame] | 109 | members_type::const_iterator m_iter; |
| 110 | for (m_iter = members_in_id_order_.begin(); m_iter != members_in_id_order_.end(); ++m_iter) { |
David Reiss | 6ce97e3 | 2007-08-30 23:18:31 +0000 | [diff] [blame] | 111 | rv += (*m_iter)->get_fingerprint_material(); |
David Reiss | 18bf22d | 2007-08-28 20:49:17 +0000 | [diff] [blame] | 112 | rv += ";"; |
| 113 | } |
| 114 | rv += "}"; |
| 115 | return rv; |
| 116 | } |
| 117 | |
David Reiss | 9885c68 | 2007-08-30 23:12:37 +0000 | [diff] [blame] | 118 | virtual void generate_fingerprint() { |
| 119 | t_type::generate_fingerprint(); |
Bryan Duxbury | ff219ac | 2009-04-10 21:51:00 +0000 | [diff] [blame] | 120 | members_type::const_iterator m_iter; |
| 121 | for (m_iter = members_in_id_order_.begin(); m_iter != members_in_id_order_.end(); ++m_iter) { |
David Reiss | 6ce97e3 | 2007-08-30 23:18:31 +0000 | [diff] [blame] | 122 | (*m_iter)->get_type()->generate_fingerprint(); |
David Reiss | 18bf22d | 2007-08-28 20:49:17 +0000 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
Bryan Duxbury | 2d80470 | 2009-12-18 19:41:11 +0000 | [diff] [blame^] | 126 | t_field* get_field_by_name(std::string field_name) { |
| 127 | members_type::const_iterator m_iter; |
| 128 | for (m_iter = members_in_id_order_.begin(); m_iter != members_in_id_order_.end(); ++m_iter) { |
| 129 | if ((*m_iter)->get_name() == field_name) { |
| 130 | return *m_iter; |
| 131 | } |
| 132 | } |
| 133 | return NULL; |
| 134 | } |
| 135 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 136 | private: |
David Reiss | 18bf22d | 2007-08-28 20:49:17 +0000 | [diff] [blame] | 137 | |
Bryan Duxbury | ff219ac | 2009-04-10 21:51:00 +0000 | [diff] [blame] | 138 | members_type members_; |
| 139 | members_type members_in_id_order_; |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 140 | bool is_xception_; |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 141 | bool is_union_; |
Mark Slee | 782abbb | 2007-01-19 00:17:02 +0000 | [diff] [blame] | 142 | |
| 143 | bool xsd_all_; |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 144 | }; |
| 145 | |
| 146 | #endif |