blob: c4e30e37d3e68403984cb55b0a7e6d3d797f685f [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 {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010037public:
38 t_field(t_type* type, std::string name)
39 : type_(type),
40 name_(name),
41 key_(0),
42 value_(NULL),
43 xsd_optional_(false),
44 xsd_nillable_(false),
45 xsd_attrs_(NULL),
46 reference_(false) {}
Mark Sleeb15a68b2006-06-07 06:46:24 +000047
Konrad Grochowski16a23a62014-11-13 15:33:38 +010048 t_field(t_type* type, std::string name, int32_t key)
49 : type_(type),
50 name_(name),
51 key_(key),
52 req_(T_OPT_IN_REQ_OUT),
53 value_(NULL),
54 xsd_optional_(false),
55 xsd_nillable_(false),
56 xsd_attrs_(NULL),
57 reference_(false) {}
Mark Slee31985722006-05-24 21:45:31 +000058
59 ~t_field() {}
60
Konrad Grochowski16a23a62014-11-13 15:33:38 +010061 t_type* get_type() const { return type_; }
Mark Sleef5377b32006-10-10 01:42:59 +000062
Konrad Grochowski16a23a62014-11-13 15:33:38 +010063 const std::string& get_name() const { return name_; }
Mark Sleef5377b32006-10-10 01:42:59 +000064
Konrad Grochowski16a23a62014-11-13 15:33:38 +010065 int32_t get_key() const { return key_; }
Mark Slee31985722006-05-24 21:45:31 +000066
Konrad Grochowski16a23a62014-11-13 15:33:38 +010067 enum e_req { T_REQUIRED, T_OPTIONAL, T_OPT_IN_REQ_OUT };
David Reiss8320a922007-08-14 19:59:26 +000068
Konrad Grochowski16a23a62014-11-13 15:33:38 +010069 void set_req(e_req req) { req_ = req; }
David Reiss8320a922007-08-14 19:59:26 +000070
Konrad Grochowski16a23a62014-11-13 15:33:38 +010071 e_req get_req() const { return req_; }
David Reiss8320a922007-08-14 19:59:26 +000072
Konrad Grochowski16a23a62014-11-13 15:33:38 +010073 void set_value(t_const_value* value) { value_ = value; }
Mark Slee21135c32007-02-05 21:52:08 +000074
Konrad Grochowski16a23a62014-11-13 15:33:38 +010075 t_const_value* get_value() { return value_; }
Mark Slee21135c32007-02-05 21:52:08 +000076
Konrad Grochowski16a23a62014-11-13 15:33:38 +010077 void set_xsd_optional(bool xsd_optional) { xsd_optional_ = xsd_optional; }
Mark Slee36bfa2e2007-01-19 20:09:51 +000078
Konrad Grochowski16a23a62014-11-13 15:33:38 +010079 bool get_xsd_optional() const { return xsd_optional_; }
Mark Slee36bfa2e2007-01-19 20:09:51 +000080
Konrad Grochowski16a23a62014-11-13 15:33:38 +010081 void set_xsd_nillable(bool xsd_nillable) { xsd_nillable_ = xsd_nillable; }
Mark Slee7df0e2a2007-02-06 21:03:18 +000082
Konrad Grochowski16a23a62014-11-13 15:33:38 +010083 bool get_xsd_nillable() const { return xsd_nillable_; }
Mark Slee7df0e2a2007-02-06 21:03:18 +000084
Konrad Grochowski16a23a62014-11-13 15:33:38 +010085 void set_xsd_attrs(t_struct* xsd_attrs) { xsd_attrs_ = xsd_attrs; }
Mark Slee7ff32452007-02-01 05:26:18 +000086
Konrad Grochowski16a23a62014-11-13 15:33:38 +010087 t_struct* get_xsd_attrs() { return xsd_attrs_; }
Mark Slee7ff32452007-02-01 05:26:18 +000088
David Reiss18bf22d2007-08-28 20:49:17 +000089 // This is not the same function as t_type::get_fingerprint_material,
90 // but it does the same thing.
91 std::string get_fingerprint_material() const {
David Reissaca320d2010-08-31 16:51:26 +000092 std::ostringstream keystm;
93 keystm << key_;
Konrad Grochowski16a23a62014-11-13 15:33:38 +010094 return keystm.str() + ":" + ((req_ == T_OPTIONAL) ? "opt-" : "")
95 + type_->get_fingerprint_material();
David Reiss18bf22d2007-08-28 20:49:17 +000096 }
97
Bryan Duxburyff219ac2009-04-10 21:51:00 +000098 /**
99 * Comparator to sort fields in ascending order by key.
100 * Make this a functor instead of a function to help GCC inline it.
101 * The arguments are (const) references to const pointers to const t_fields.
102 */
103 struct key_compare {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100104 bool operator()(t_field const* const& a, t_field const* const& b) {
Bryan Duxburyff219ac2009-04-10 21:51:00 +0000105 return a->get_key() < b->get_key();
106 }
107 };
108
David Reiss53c10e02010-03-05 07:51:51 +0000109 std::map<std::string, std::string> annotations_;
Bryan Duxburyff219ac2009-04-10 21:51:00 +0000110
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100111 bool get_reference() { return reference_; }
Jens Geyer885c6792014-05-02 21:31:55 +0200112
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100113 void set_reference(bool reference) { reference_ = reference; }
Jens Geyer885c6792014-05-02 21:31:55 +0200114
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100115private:
Mark Slee31985722006-05-24 21:45:31 +0000116 t_type* type_;
117 std::string name_;
Mark Slee9cb7c612006-09-01 22:17:45 +0000118 int32_t key_;
David Reiss8320a922007-08-14 19:59:26 +0000119 e_req req_;
Mark Slee7ff32452007-02-01 05:26:18 +0000120 t_const_value* value_;
ccheeverf53b5cf2007-02-05 20:33:11 +0000121
Mark Slee36bfa2e2007-01-19 20:09:51 +0000122 bool xsd_optional_;
Mark Slee7df0e2a2007-02-06 21:03:18 +0000123 bool xsd_nillable_;
Mark Slee748d83f2007-02-07 01:20:08 +0000124 t_struct* xsd_attrs_;
Jens Geyer885c6792014-05-02 21:31:55 +0200125 bool reference_;
Mark Slee31985722006-05-24 21:45:31 +0000126};
127
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000128/**
129 * A simple struct for the parser to use to store a field ID, and whether or
130 * not it was specified by the user or automatically chosen.
131 */
132struct t_field_id {
Ben Craige9576752013-10-11 08:19:16 -0500133 int32_t value;
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000134 bool auto_assigned;
135};
136
Mark Slee31985722006-05-24 21:45:31 +0000137#endif