blob: 7980f803f2248b1176cfd906273485bb5b94bff9 [file] [log] [blame]
Gavin McDonald0b75e1a2010-10-28 02:12:01 +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 */
19
20#ifndef T_STRUCT_H
21#define T_STRUCT_H
22
23#include <algorithm>
24#include <vector>
25#include <utility>
26#include <string>
27
28#include "t_type.h"
29#include "t_field.h"
30
31// Forward declare that puppy
32class t_program;
33
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 *
38 */
39class t_struct : public t_type {
40 public:
41 typedef std::vector<t_field*> members_type;
42
43 t_struct(t_program* program) :
44 t_type(program),
45 is_xception_(false),
46 xsd_all_(false) {}
47
48 t_struct(t_program* program, const std::string& name) :
49 t_type(program, name),
50 is_xception_(false),
51 xsd_all_(false) {}
52
53 void set_name(const std::string& name) {
54 name_ = name;
55 }
56
57 void set_xception(bool is_xception) {
58 is_xception_ = is_xception;
59 }
60
61 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
69 bool append(t_field* elem) {
70 members_.push_back(elem);
71
72 typedef members_type::iterator iter_type;
73 std::pair<iter_type, iter_type> bounds = std::equal_range(
74 members_in_id_order_.begin(), members_in_id_order_.end(), elem, t_field::key_compare()
75 );
76 if (bounds.first != bounds.second) {
77 return false;
78 }
79 members_in_id_order_.insert(bounds.second, elem);
80 return true;
81 }
82
83 const members_type& get_members() {
84 return members_;
85 }
86
87 const members_type& get_sorted_members() {
88 return members_in_id_order_;
89 }
90
91 bool is_struct() const {
92 return !is_xception_;
93 }
94
95 bool is_xception() const {
96 return is_xception_;
97 }
98
99 virtual std::string get_fingerprint_material() const {
100 std::string rv = "{";
101 members_type::const_iterator m_iter;
102 for (m_iter = members_in_id_order_.begin(); m_iter != members_in_id_order_.end(); ++m_iter) {
103 rv += (*m_iter)->get_fingerprint_material();
104 rv += ";";
105 }
106 rv += "}";
107 return rv;
108 }
109
110 virtual void generate_fingerprint() {
111 t_type::generate_fingerprint();
112 members_type::const_iterator m_iter;
113 for (m_iter = members_in_id_order_.begin(); m_iter != members_in_id_order_.end(); ++m_iter) {
114 (*m_iter)->get_type()->generate_fingerprint();
115 }
116 }
117
118 private:
119
120 members_type members_;
121 members_type members_in_id_order_;
122 bool is_xception_;
123
124 bool xsd_all_;
125};
126
127#endif