blob: 172a0f2896ab9a5dd9db2d5867357ee83a93f53c [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
David Reiss566a9872009-03-30 22:24:30 +000023#include <algorithm>
Mark Slee31985722006-05-24 21:45:31 +000024#include <vector>
David Reiss566a9872009-03-30 22:24:30 +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:
David Reiss566a9872009-03-30 22:24:30 +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
Mark Slee782abbb2007-01-19 00:17:02 +000061 void set_xsd_all(bool xsd_all) {
62 xsd_all_ = xsd_all;
63 }
64
65 bool get_xsd_all() const {
66 return xsd_all_;
67 }
68
David Reiss566a9872009-03-30 22:24:30 +000069 bool append(t_field* elem) {
70 typedef members_type::iterator iter_type;
71 std::pair<iter_type, iter_type> bounds = std::equal_range(
72 members_.begin(), members_.end(), elem, t_field::key_compare()
73 );
74 if (bounds.first != bounds.second) {
75 return false;
76 }
77 members_.insert(bounds.second, elem);
78 return true;
Mark Slee9cb7c612006-09-01 22:17:45 +000079 }
Mark Sleee8540632006-05-30 09:24:40 +000080
David Reiss566a9872009-03-30 22:24:30 +000081 const members_type& get_members() {
Mark Slee9cb7c612006-09-01 22:17:45 +000082 return members_;
83 }
84
85 bool is_struct() const {
86 return !is_xception_;
87 }
88
89 bool is_xception() const {
90 return is_xception_;
91 }
Mark Slee31985722006-05-24 21:45:31 +000092
David Reiss18bf22d2007-08-28 20:49:17 +000093 virtual std::string get_fingerprint_material() const {
94 std::string rv = "{";
David Reiss566a9872009-03-30 22:24:30 +000095 members_type::const_iterator m_iter;
David Reiss18bf22d2007-08-28 20:49:17 +000096 for (m_iter = members_.begin(); m_iter != members_.end(); ++m_iter) {
David Reiss6ce97e32007-08-30 23:18:31 +000097 rv += (*m_iter)->get_fingerprint_material();
David Reiss18bf22d2007-08-28 20:49:17 +000098 rv += ";";
99 }
100 rv += "}";
101 return rv;
102 }
103
David Reiss9885c682007-08-30 23:12:37 +0000104 virtual void generate_fingerprint() {
105 t_type::generate_fingerprint();
David Reiss566a9872009-03-30 22:24:30 +0000106 members_type::const_iterator m_iter;
David Reiss9885c682007-08-30 23:12:37 +0000107 for (m_iter = members_.begin(); m_iter != members_.end(); ++m_iter) {
David Reiss6ce97e32007-08-30 23:18:31 +0000108 (*m_iter)->get_type()->generate_fingerprint();
David Reiss18bf22d2007-08-28 20:49:17 +0000109 }
110 }
111
Mark Slee31985722006-05-24 21:45:31 +0000112 private:
David Reiss18bf22d2007-08-28 20:49:17 +0000113
David Reiss566a9872009-03-30 22:24:30 +0000114 members_type members_;
Mark Slee9cb7c612006-09-01 22:17:45 +0000115 bool is_xception_;
Mark Slee782abbb2007-01-19 00:17:02 +0000116
117 bool xsd_all_;
Mark Slee31985722006-05-24 21:45:31 +0000118};
119
120#endif