blob: aeee112e58a401329e51df18dfb5c4b1c5163071 [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001/*
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 Sleee9ce01c2007-05-16 02:29:53 +000019
Mark Slee31985722006-05-24 21:45:31 +000020#ifndef T_STRUCT_H
21#define T_STRUCT_H
22
Bryan Duxburyff219ac2009-04-10 21:51:00 +000023#include <algorithm>
Mark Slee31985722006-05-24 21:45:31 +000024#include <vector>
Bryan Duxburyff219ac2009-04-10 21:51:00 +000025#include <utility>
Mark Slee31985722006-05-24 21:45:31 +000026#include <string>
27
28#include "t_type.h"
Mark Sleee8540632006-05-30 09:24:40 +000029#include "t_field.h"
Mark Slee31985722006-05-24 21:45:31 +000030
Mark Sleef0712dc2006-10-25 19:03:57 +000031// Forward declare that puppy
32class t_program;
33
Mark Sleef5377b32006-10-10 01:42:59 +000034/**
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 Sleef5377b32006-10-10 01:42:59 +000038 */
Mark Slee31985722006-05-24 21:45:31 +000039class t_struct : public t_type {
40 public:
Bryan Duxburyff219ac2009-04-10 21:51:00 +000041 typedef std::vector<t_field*> members_type;
42
Mark Sleef0712dc2006-10-25 19:03:57 +000043 t_struct(t_program* program) :
44 t_type(program),
Mark Slee782abbb2007-01-19 00:17:02 +000045 is_xception_(false),
David Reissf84b3602007-09-17 21:16:32 +000046 xsd_all_(false) {}
Mark Sleeb15a68b2006-06-07 06:46:24 +000047
Mark Sleef0712dc2006-10-25 19:03:57 +000048 t_struct(t_program* program, const std::string& name) :
49 t_type(program, name),
Mark Slee782abbb2007-01-19 00:17:02 +000050 is_xception_(false),
David Reissf84b3602007-09-17 21:16:32 +000051 xsd_all_(false) {}
Mark Slee31985722006-05-24 21:45:31 +000052
Mark Slee9cb7c612006-09-01 22:17:45 +000053 void set_name(const std::string& name) {
54 name_ = name;
55 }
56
Mark Slee9cb7c612006-09-01 22:17:45 +000057 void set_xception(bool is_xception) {
58 is_xception_ = is_xception;
59 }
Mark Sleee8540632006-05-30 09:24:40 +000060
Bryan Duxburyab3666e2009-09-01 23:03:47 +000061 void set_union(bool is_union) {
62 is_union_ = is_union;
63 }
64
Mark Slee782abbb2007-01-19 00:17:02 +000065 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 Duxburyff219ac2009-04-10 21:51:00 +000073 bool append(t_field* elem) {
David Reissebb6cc42009-04-09 20:02:56 +000074 members_.push_back(elem);
Bryan Duxburyff219ac2009-04-10 21:51:00 +000075
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 Slee9cb7c612006-09-01 22:17:45 +000085 }
Mark Sleee8540632006-05-30 09:24:40 +000086
Bryan Duxburyff219ac2009-04-10 21:51:00 +000087 const members_type& get_members() {
Mark Slee9cb7c612006-09-01 22:17:45 +000088 return members_;
89 }
90
Bryan Duxburyff219ac2009-04-10 21:51:00 +000091 const members_type& get_sorted_members() {
92 return members_in_id_order_;
93 }
94
Mark Slee9cb7c612006-09-01 22:17:45 +000095 bool is_struct() const {
96 return !is_xception_;
97 }
98
99 bool is_xception() const {
100 return is_xception_;
101 }
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000102
103 bool is_union() const {
104 return is_union_;
105 }
Mark Slee31985722006-05-24 21:45:31 +0000106
David Reiss18bf22d2007-08-28 20:49:17 +0000107 virtual std::string get_fingerprint_material() const {
108 std::string rv = "{";
Bryan Duxburyff219ac2009-04-10 21:51:00 +0000109 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 Reiss6ce97e32007-08-30 23:18:31 +0000111 rv += (*m_iter)->get_fingerprint_material();
David Reiss18bf22d2007-08-28 20:49:17 +0000112 rv += ";";
113 }
114 rv += "}";
115 return rv;
116 }
117
David Reiss9885c682007-08-30 23:12:37 +0000118 virtual void generate_fingerprint() {
119 t_type::generate_fingerprint();
Bryan Duxburyff219ac2009-04-10 21:51:00 +0000120 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 Reiss6ce97e32007-08-30 23:18:31 +0000122 (*m_iter)->get_type()->generate_fingerprint();
David Reiss18bf22d2007-08-28 20:49:17 +0000123 }
124 }
125
Bryan Duxbury2d804702009-12-18 19:41:11 +0000126 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 Slee31985722006-05-24 21:45:31 +0000136 private:
David Reiss18bf22d2007-08-28 20:49:17 +0000137
Bryan Duxburyff219ac2009-04-10 21:51:00 +0000138 members_type members_;
139 members_type members_in_id_order_;
Mark Slee9cb7c612006-09-01 22:17:45 +0000140 bool is_xception_;
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000141 bool is_union_;
Mark Slee782abbb2007-01-19 00:17:02 +0000142
143 bool xsd_all_;
Mark Slee31985722006-05-24 21:45:31 +0000144};
145
146#endif