blob: b82933dd64553b987dcc719db89773b4e4ae6f53 [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),
Bryan Duxburyd6203282012-06-26 00:32:57 +000046 is_union_(false),
David Reissf84b3602007-09-17 21:16:32 +000047 xsd_all_(false) {}
Mark Sleeb15a68b2006-06-07 06:46:24 +000048
Mark Sleef0712dc2006-10-25 19:03:57 +000049 t_struct(t_program* program, const std::string& name) :
50 t_type(program, name),
Mark Slee782abbb2007-01-19 00:17:02 +000051 is_xception_(false),
Bryan Duxburyd6203282012-06-26 00:32:57 +000052 is_union_(false),
David Reissf84b3602007-09-17 21:16:32 +000053 xsd_all_(false) {}
Mark Slee31985722006-05-24 21:45:31 +000054
Mark Slee9cb7c612006-09-01 22:17:45 +000055 void set_name(const std::string& name) {
56 name_ = name;
57 }
58
Mark Slee9cb7c612006-09-01 22:17:45 +000059 void set_xception(bool is_xception) {
60 is_xception_ = is_xception;
61 }
Mark Sleee8540632006-05-30 09:24:40 +000062
Bryan Duxburyab3666e2009-09-01 23:03:47 +000063 void set_union(bool is_union) {
64 is_union_ = is_union;
65 }
66
Mark Slee782abbb2007-01-19 00:17:02 +000067 void set_xsd_all(bool xsd_all) {
68 xsd_all_ = xsd_all;
69 }
70
71 bool get_xsd_all() const {
72 return xsd_all_;
73 }
74
Bryan Duxburyff219ac2009-04-10 21:51:00 +000075 bool append(t_field* elem) {
David Reissebb6cc42009-04-09 20:02:56 +000076 members_.push_back(elem);
Bryan Duxburyff219ac2009-04-10 21:51:00 +000077
78 typedef members_type::iterator iter_type;
79 std::pair<iter_type, iter_type> bounds = std::equal_range(
80 members_in_id_order_.begin(), members_in_id_order_.end(), elem, t_field::key_compare()
81 );
82 if (bounds.first != bounds.second) {
83 return false;
84 }
85 members_in_id_order_.insert(bounds.second, elem);
86 return true;
Mark Slee9cb7c612006-09-01 22:17:45 +000087 }
Mark Sleee8540632006-05-30 09:24:40 +000088
Bryan Duxburyff219ac2009-04-10 21:51:00 +000089 const members_type& get_members() {
Mark Slee9cb7c612006-09-01 22:17:45 +000090 return members_;
91 }
92
Bryan Duxburyff219ac2009-04-10 21:51:00 +000093 const members_type& get_sorted_members() {
94 return members_in_id_order_;
95 }
96
Mark Slee9cb7c612006-09-01 22:17:45 +000097 bool is_struct() const {
98 return !is_xception_;
99 }
100
101 bool is_xception() const {
102 return is_xception_;
103 }
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000104
105 bool is_union() const {
106 return is_union_;
107 }
Mark Slee31985722006-05-24 21:45:31 +0000108
David Reiss18bf22d2007-08-28 20:49:17 +0000109 virtual std::string get_fingerprint_material() const {
110 std::string rv = "{";
Bryan Duxburyff219ac2009-04-10 21:51:00 +0000111 members_type::const_iterator m_iter;
112 for (m_iter = members_in_id_order_.begin(); m_iter != members_in_id_order_.end(); ++m_iter) {
David Reiss6ce97e32007-08-30 23:18:31 +0000113 rv += (*m_iter)->get_fingerprint_material();
David Reiss18bf22d2007-08-28 20:49:17 +0000114 rv += ";";
115 }
116 rv += "}";
117 return rv;
118 }
119
David Reiss9885c682007-08-30 23:12:37 +0000120 virtual void generate_fingerprint() {
121 t_type::generate_fingerprint();
Bryan Duxburyff219ac2009-04-10 21:51:00 +0000122 members_type::const_iterator m_iter;
123 for (m_iter = members_in_id_order_.begin(); m_iter != members_in_id_order_.end(); ++m_iter) {
David Reiss6ce97e32007-08-30 23:18:31 +0000124 (*m_iter)->get_type()->generate_fingerprint();
David Reiss18bf22d2007-08-28 20:49:17 +0000125 }
126 }
127
Bryan Duxbury2d804702009-12-18 19:41:11 +0000128 t_field* get_field_by_name(std::string field_name) {
129 members_type::const_iterator m_iter;
130 for (m_iter = members_in_id_order_.begin(); m_iter != members_in_id_order_.end(); ++m_iter) {
131 if ((*m_iter)->get_name() == field_name) {
132 return *m_iter;
133 }
134 }
135 return NULL;
136 }
137
Mark Slee31985722006-05-24 21:45:31 +0000138 private:
David Reiss18bf22d2007-08-28 20:49:17 +0000139
Bryan Duxburyff219ac2009-04-10 21:51:00 +0000140 members_type members_;
141 members_type members_in_id_order_;
Mark Slee9cb7c612006-09-01 22:17:45 +0000142 bool is_xception_;
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000143 bool is_union_;
Mark Slee782abbb2007-01-19 00:17:02 +0000144
145 bool xsd_all_;
Mark Slee31985722006-05-24 21:45:31 +0000146};
147
148#endif