Line data Source code
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 : */
19 :
20 : #ifndef T_FIELD_H
21 : #define T_FIELD_H
22 :
23 : #include <string>
24 : #include <sstream>
25 :
26 : #include "t_doc.h"
27 :
28 : // Forward declare for xsd_attrs
29 : class t_struct;
30 :
31 : /**
32 : * Class to represent a field in a thrift structure. A field has a data type,
33 : * a symbolic name, and a numeric identifier.
34 : *
35 : */
36 : class t_field : public t_doc {
37 : public:
38 13673 : t_field(t_type* type, std::string name) :
39 13673 : type_(type),
40 13673 : name_(name),
41 13673 : key_(0),
42 : #ifdef SANDESH
43 13673 : req_(T_OPT_IN_REQ_OUT),
44 : #endif
45 13673 : value_(NULL),
46 13673 : xsd_optional_(false),
47 13673 : xsd_nillable_(false),
48 13673 : xsd_attrs_(NULL)
49 : #ifdef SANDESH
50 13673 : , auto_generated_(false)
51 : #endif
52 13673 : {}
53 :
54 88724 : t_field(t_type* type, std::string name, int32_t key) :
55 88724 : type_(type),
56 88724 : name_(name),
57 88724 : key_(key),
58 88724 : req_(T_OPT_IN_REQ_OUT),
59 88724 : value_(NULL),
60 88724 : xsd_optional_(false),
61 88724 : xsd_nillable_(false),
62 88724 : xsd_attrs_(NULL)
63 : #ifdef SANDESH
64 88724 : , auto_generated_(false)
65 : #endif
66 88724 : {}
67 :
68 : #ifdef SANDESH
69 12180 : t_field(t_type* type, std::string name, int32_t key, bool auto_generated) :
70 12180 : type_(type),
71 12180 : name_(name),
72 12180 : key_(key),
73 12180 : req_(T_OPT_IN_REQ_OUT),
74 12180 : value_(NULL),
75 12180 : xsd_optional_(false),
76 12180 : xsd_nillable_(false),
77 12180 : xsd_attrs_(NULL),
78 24360 : auto_generated_(auto_generated){}
79 :
80 4326 : static std::string auto_string_name(int32_t key) {
81 4326 : std::string str;
82 : char buf[20];
83 4326 : snprintf(buf, 20, "str%d", key);
84 4326 : str = buf;
85 8652 : return str;
86 0 : }
87 : #endif
88 :
89 13673 : virtual ~t_field() {}
90 :
91 619397 : t_type* get_type() const {
92 619397 : return type_;
93 : }
94 :
95 780557 : const std::string& get_name() const {
96 780557 : return name_;
97 : }
98 :
99 504285 : int32_t get_key() const {
100 504285 : return key_;
101 : }
102 :
103 : enum e_req {
104 : T_REQUIRED,
105 : T_OPTIONAL,
106 : T_OPT_IN_REQ_OUT
107 : };
108 :
109 88724 : void set_req(e_req req) {
110 88724 : req_ = req;
111 88724 : }
112 :
113 421867 : e_req get_req() const {
114 421867 : return req_;
115 : }
116 :
117 4494 : void set_value(t_const_value* value) {
118 4494 : value_ = value;
119 4494 : }
120 :
121 29899 : t_const_value* get_value() {
122 29899 : return value_;
123 : }
124 :
125 84398 : void set_xsd_optional(bool xsd_optional) {
126 84398 : xsd_optional_ = xsd_optional;
127 84398 : }
128 :
129 0 : bool get_xsd_optional() const {
130 0 : return xsd_optional_;
131 : }
132 :
133 84398 : void set_xsd_nillable(bool xsd_nillable) {
134 84398 : xsd_nillable_ = xsd_nillable;
135 84398 : }
136 :
137 0 : bool get_xsd_nillable() const {
138 0 : return xsd_nillable_;
139 : }
140 :
141 0 : void set_xsd_attrs(t_struct* xsd_attrs) {
142 0 : xsd_attrs_ = xsd_attrs;
143 0 : }
144 :
145 0 : t_struct* get_xsd_attrs() {
146 0 : return xsd_attrs_;
147 : }
148 :
149 : #ifdef SANDESH
150 104070 : bool get_auto_generated() const {
151 104070 : return auto_generated_;
152 : }
153 :
154 : void set_auto_generated(bool auto_generated) {
155 : auto_generated_ = auto_generated;
156 : }
157 : #endif
158 :
159 : // This is not the same function as t_type::get_fingerprint_material,
160 : // but it does the same thing.
161 182010 : std::string get_fingerprint_material() const {
162 182010 : std::ostringstream keystm;
163 182010 : keystm << key_;
164 364020 : return keystm.str() + ":" +
165 182010 : ((req_ == T_OPTIONAL) ? "opt-" : "") +
166 728040 : type_->get_fingerprint_material();
167 182010 : }
168 :
169 : /**
170 : * Comparator to sort fields in ascending order by key.
171 : * Make this a functor instead of a function to help GCC inline it.
172 : * The arguments are (const) references to const pointers to const t_fields.
173 : */
174 : struct key_compare {
175 219192 : bool operator()(t_field const * const & a, t_field const * const & b) const {
176 219192 : return a->get_key() < b->get_key();
177 : }
178 : };
179 :
180 : #ifdef SANDESH
181 11279 : virtual bool has_key_annotation() const {
182 11279 : std::map<std::string, std::string>::const_iterator it;
183 11279 : it = annotations_.find("key");
184 11279 : if (it != annotations_.end()) {
185 765 : return true;
186 : }
187 10514 : return false;
188 : }
189 0 : virtual bool has_hidden_annotation() const {
190 0 : std::map<std::string, std::string>::const_iterator it;
191 0 : it = annotations_.find("hidden");
192 0 : if (it != annotations_.end()) {
193 0 : return true;
194 : }
195 0 : return false;
196 : }
197 : #endif
198 :
199 : std::map<std::string, std::string> annotations_;
200 :
201 : private:
202 : t_type* type_;
203 : std::string name_;
204 : int32_t key_;
205 : e_req req_;
206 : t_const_value* value_;
207 :
208 : bool xsd_optional_;
209 : bool xsd_nillable_;
210 : t_struct* xsd_attrs_;
211 : #ifdef SANDESH
212 : bool auto_generated_;
213 : #endif
214 :
215 : };
216 :
217 : /**
218 : * A simple struct for the parser to use to store a field ID, and whether or
219 : * not it was specified by the user or automatically chosen.
220 : */
221 : struct t_field_id {
222 : int64_t value;
223 : bool auto_assigned;
224 : };
225 :
226 : #endif
|