blob: c05fdf37e2ba5188dca8a6b025eed3a0bbcb3176 [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_FIELD_H
21#define T_FIELD_H
22
23#include <string>
David Reissaca320d2010-08-31 16:51:26 +000024#include <sstream>
Mark Slee31985722006-05-24 21:45:31 +000025
David Reissf011bd42008-10-29 00:07:45 +000026#include "t_doc.h"
27
Mark Slee748d83f2007-02-07 01:20:08 +000028// Forward declare for xsd_attrs
29class t_struct;
30
Mark Slee31985722006-05-24 21:45:31 +000031/**
Mark Sleef5377b32006-10-10 01:42:59 +000032 * Class to represent a field in a thrift structure. A field has a data type,
33 * a symbolic name, and a numeric identifier.
Mark Slee31985722006-05-24 21:45:31 +000034 *
Mark Slee31985722006-05-24 21:45:31 +000035 */
David Reissf011bd42008-10-29 00:07:45 +000036class t_field : public t_doc {
Mark Slee31985722006-05-24 21:45:31 +000037 public:
Mark Sleeb15a68b2006-06-07 06:46:24 +000038 t_field(t_type* type, std::string name) :
Mark Sleef5377b32006-10-10 01:42:59 +000039 type_(type),
40 name_(name),
Mark Slee36bfa2e2007-01-19 20:09:51 +000041 key_(0),
Mark Slee7ff32452007-02-01 05:26:18 +000042 value_(NULL),
Mark Slee7df0e2a2007-02-06 21:03:18 +000043 xsd_optional_(false),
Mark Slee748d83f2007-02-07 01:20:08 +000044 xsd_nillable_(false),
Jens Geyer885c6792014-05-02 21:31:55 +020045 xsd_attrs_(NULL),
46 reference_(false) {}
Mark Sleeb15a68b2006-06-07 06:46:24 +000047
Mark Slee9cb7c612006-09-01 22:17:45 +000048 t_field(t_type* type, std::string name, int32_t key) :
Mark Sleef5377b32006-10-10 01:42:59 +000049 type_(type),
50 name_(name),
Mark Slee36bfa2e2007-01-19 20:09:51 +000051 key_(key),
David Reiss204420f2008-01-11 20:59:03 +000052 req_(T_OPT_IN_REQ_OUT),
Mark Slee7ff32452007-02-01 05:26:18 +000053 value_(NULL),
Mark Slee7df0e2a2007-02-06 21:03:18 +000054 xsd_optional_(false),
Mark Slee748d83f2007-02-07 01:20:08 +000055 xsd_nillable_(false),
Jens Geyer885c6792014-05-02 21:31:55 +020056 xsd_attrs_(NULL),
57 reference_(false) {}
Mark Slee31985722006-05-24 21:45:31 +000058
59 ~t_field() {}
60
Mark Sleef5377b32006-10-10 01:42:59 +000061 t_type* get_type() const {
62 return type_;
63 }
64
65 const std::string& get_name() const {
66 return name_;
67 }
68
69 int32_t get_key() const {
70 return key_;
71 }
Mark Slee31985722006-05-24 21:45:31 +000072
David Reiss8320a922007-08-14 19:59:26 +000073 enum e_req {
David Reiss204420f2008-01-11 20:59:03 +000074 T_REQUIRED,
75 T_OPTIONAL,
Roger Meier0069cc42010-10-13 18:10:18 +000076 T_OPT_IN_REQ_OUT
David Reiss8320a922007-08-14 19:59:26 +000077 };
78
79 void set_req(e_req req) {
80 req_ = req;
81 }
82
83 e_req get_req() const {
84 return req_;
85 }
86
Mark Slee21135c32007-02-05 21:52:08 +000087 void set_value(t_const_value* value) {
88 value_ = value;
89 }
90
91 t_const_value* get_value() {
92 return value_;
93 }
94
Mark Slee36bfa2e2007-01-19 20:09:51 +000095 void set_xsd_optional(bool xsd_optional) {
96 xsd_optional_ = xsd_optional;
97 }
98
99 bool get_xsd_optional() const {
100 return xsd_optional_;
101 }
102
Mark Slee7df0e2a2007-02-06 21:03:18 +0000103 void set_xsd_nillable(bool xsd_nillable) {
104 xsd_nillable_ = xsd_nillable;
105 }
106
107 bool get_xsd_nillable() const {
108 return xsd_nillable_;
109 }
110
Mark Slee748d83f2007-02-07 01:20:08 +0000111 void set_xsd_attrs(t_struct* xsd_attrs) {
112 xsd_attrs_ = xsd_attrs;
Mark Slee7ff32452007-02-01 05:26:18 +0000113 }
114
Mark Slee748d83f2007-02-07 01:20:08 +0000115 t_struct* get_xsd_attrs() {
Mark Slee21135c32007-02-05 21:52:08 +0000116 return xsd_attrs_;
Mark Slee7ff32452007-02-01 05:26:18 +0000117 }
118
David Reiss18bf22d2007-08-28 20:49:17 +0000119 // This is not the same function as t_type::get_fingerprint_material,
120 // but it does the same thing.
121 std::string get_fingerprint_material() const {
David Reissaca320d2010-08-31 16:51:26 +0000122 std::ostringstream keystm;
123 keystm << key_;
124 return keystm.str() + ":" +
David Reiss204420f2008-01-11 20:59:03 +0000125 ((req_ == T_OPTIONAL) ? "opt-" : "") +
David Reiss18bf22d2007-08-28 20:49:17 +0000126 type_->get_fingerprint_material();
127 }
128
Bryan Duxburyff219ac2009-04-10 21:51:00 +0000129 /**
130 * Comparator to sort fields in ascending order by key.
131 * Make this a functor instead of a function to help GCC inline it.
132 * The arguments are (const) references to const pointers to const t_fields.
133 */
134 struct key_compare {
135 bool operator()(t_field const * const & a, t_field const * const & b) {
136 return a->get_key() < b->get_key();
137 }
138 };
139
David Reiss53c10e02010-03-05 07:51:51 +0000140 std::map<std::string, std::string> annotations_;
Bryan Duxburyff219ac2009-04-10 21:51:00 +0000141
Jens Geyer885c6792014-05-02 21:31:55 +0200142 bool get_reference() {
143 return reference_;
144 }
145
146 void set_reference(bool reference) {
147 reference_ = reference;
148 }
149
Mark Slee31985722006-05-24 21:45:31 +0000150 private:
151 t_type* type_;
152 std::string name_;
Mark Slee9cb7c612006-09-01 22:17:45 +0000153 int32_t key_;
David Reiss8320a922007-08-14 19:59:26 +0000154 e_req req_;
Mark Slee7ff32452007-02-01 05:26:18 +0000155 t_const_value* value_;
ccheeverf53b5cf2007-02-05 20:33:11 +0000156
Mark Slee36bfa2e2007-01-19 20:09:51 +0000157 bool xsd_optional_;
Mark Slee7df0e2a2007-02-06 21:03:18 +0000158 bool xsd_nillable_;
Mark Slee748d83f2007-02-07 01:20:08 +0000159 t_struct* xsd_attrs_;
Jens Geyer885c6792014-05-02 21:31:55 +0200160 bool reference_;
Mark Slee31985722006-05-24 21:45:31 +0000161};
162
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000163/**
164 * A simple struct for the parser to use to store a field ID, and whether or
165 * not it was specified by the user or automatically chosen.
166 */
167struct t_field_id {
Ben Craige9576752013-10-11 08:19:16 -0500168 int32_t value;
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000169 bool auto_assigned;
170};
171
Mark Slee31985722006-05-24 21:45:31 +0000172#endif