David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 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 | */ |
Mark Slee | e9ce01c | 2007-05-16 02:29:53 +0000 | [diff] [blame] | 19 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 20 | #ifndef T_FIELD_H |
| 21 | #define T_FIELD_H |
| 22 | |
| 23 | #include <string> |
David Reiss | aca320d | 2010-08-31 16:51:26 +0000 | [diff] [blame] | 24 | #include <sstream> |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 25 | |
David Reiss | f011bd4 | 2008-10-29 00:07:45 +0000 | [diff] [blame] | 26 | #include "t_doc.h" |
| 27 | |
Mark Slee | 748d83f | 2007-02-07 01:20:08 +0000 | [diff] [blame] | 28 | // Forward declare for xsd_attrs |
| 29 | class t_struct; |
| 30 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 31 | /** |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 32 | * Class to represent a field in a thrift structure. A field has a data type, |
| 33 | * a symbolic name, and a numeric identifier. |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 34 | * |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 35 | */ |
David Reiss | f011bd4 | 2008-10-29 00:07:45 +0000 | [diff] [blame] | 36 | class t_field : public t_doc { |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 37 | public: |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 38 | t_field(t_type* type, std::string name) : |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 39 | type_(type), |
| 40 | name_(name), |
Mark Slee | 36bfa2e | 2007-01-19 20:09:51 +0000 | [diff] [blame] | 41 | key_(0), |
Mark Slee | 7ff3245 | 2007-02-01 05:26:18 +0000 | [diff] [blame] | 42 | value_(NULL), |
Mark Slee | 7df0e2a | 2007-02-06 21:03:18 +0000 | [diff] [blame] | 43 | xsd_optional_(false), |
Mark Slee | 748d83f | 2007-02-07 01:20:08 +0000 | [diff] [blame] | 44 | xsd_nillable_(false), |
Jens Geyer | 885c679 | 2014-05-02 21:31:55 +0200 | [diff] [blame] | 45 | xsd_attrs_(NULL), |
| 46 | reference_(false) {} |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 47 | |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 48 | t_field(t_type* type, std::string name, int32_t key) : |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 49 | type_(type), |
| 50 | name_(name), |
Mark Slee | 36bfa2e | 2007-01-19 20:09:51 +0000 | [diff] [blame] | 51 | key_(key), |
David Reiss | 204420f | 2008-01-11 20:59:03 +0000 | [diff] [blame] | 52 | req_(T_OPT_IN_REQ_OUT), |
Mark Slee | 7ff3245 | 2007-02-01 05:26:18 +0000 | [diff] [blame] | 53 | value_(NULL), |
Mark Slee | 7df0e2a | 2007-02-06 21:03:18 +0000 | [diff] [blame] | 54 | xsd_optional_(false), |
Mark Slee | 748d83f | 2007-02-07 01:20:08 +0000 | [diff] [blame] | 55 | xsd_nillable_(false), |
Jens Geyer | 885c679 | 2014-05-02 21:31:55 +0200 | [diff] [blame] | 56 | xsd_attrs_(NULL), |
| 57 | reference_(false) {} |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 58 | |
| 59 | ~t_field() {} |
| 60 | |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 61 | 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 Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 72 | |
David Reiss | 8320a92 | 2007-08-14 19:59:26 +0000 | [diff] [blame] | 73 | enum e_req { |
David Reiss | 204420f | 2008-01-11 20:59:03 +0000 | [diff] [blame] | 74 | T_REQUIRED, |
| 75 | T_OPTIONAL, |
Roger Meier | 0069cc4 | 2010-10-13 18:10:18 +0000 | [diff] [blame] | 76 | T_OPT_IN_REQ_OUT |
David Reiss | 8320a92 | 2007-08-14 19:59:26 +0000 | [diff] [blame] | 77 | }; |
| 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 Slee | 21135c3 | 2007-02-05 21:52:08 +0000 | [diff] [blame] | 87 | void set_value(t_const_value* value) { |
| 88 | value_ = value; |
| 89 | } |
| 90 | |
| 91 | t_const_value* get_value() { |
| 92 | return value_; |
| 93 | } |
| 94 | |
Mark Slee | 36bfa2e | 2007-01-19 20:09:51 +0000 | [diff] [blame] | 95 | 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 Slee | 7df0e2a | 2007-02-06 21:03:18 +0000 | [diff] [blame] | 103 | 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 Slee | 748d83f | 2007-02-07 01:20:08 +0000 | [diff] [blame] | 111 | void set_xsd_attrs(t_struct* xsd_attrs) { |
| 112 | xsd_attrs_ = xsd_attrs; |
Mark Slee | 7ff3245 | 2007-02-01 05:26:18 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Mark Slee | 748d83f | 2007-02-07 01:20:08 +0000 | [diff] [blame] | 115 | t_struct* get_xsd_attrs() { |
Mark Slee | 21135c3 | 2007-02-05 21:52:08 +0000 | [diff] [blame] | 116 | return xsd_attrs_; |
Mark Slee | 7ff3245 | 2007-02-01 05:26:18 +0000 | [diff] [blame] | 117 | } |
| 118 | |
David Reiss | 18bf22d | 2007-08-28 20:49:17 +0000 | [diff] [blame] | 119 | // 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 Reiss | aca320d | 2010-08-31 16:51:26 +0000 | [diff] [blame] | 122 | std::ostringstream keystm; |
| 123 | keystm << key_; |
| 124 | return keystm.str() + ":" + |
David Reiss | 204420f | 2008-01-11 20:59:03 +0000 | [diff] [blame] | 125 | ((req_ == T_OPTIONAL) ? "opt-" : "") + |
David Reiss | 18bf22d | 2007-08-28 20:49:17 +0000 | [diff] [blame] | 126 | type_->get_fingerprint_material(); |
| 127 | } |
| 128 | |
Bryan Duxbury | ff219ac | 2009-04-10 21:51:00 +0000 | [diff] [blame] | 129 | /** |
| 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 Reiss | 53c10e0 | 2010-03-05 07:51:51 +0000 | [diff] [blame] | 140 | std::map<std::string, std::string> annotations_; |
Bryan Duxbury | ff219ac | 2009-04-10 21:51:00 +0000 | [diff] [blame] | 141 | |
Jens Geyer | 885c679 | 2014-05-02 21:31:55 +0200 | [diff] [blame] | 142 | bool get_reference() { |
| 143 | return reference_; |
| 144 | } |
| 145 | |
| 146 | void set_reference(bool reference) { |
| 147 | reference_ = reference; |
| 148 | } |
| 149 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 150 | private: |
| 151 | t_type* type_; |
| 152 | std::string name_; |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 153 | int32_t key_; |
David Reiss | 8320a92 | 2007-08-14 19:59:26 +0000 | [diff] [blame] | 154 | e_req req_; |
Mark Slee | 7ff3245 | 2007-02-01 05:26:18 +0000 | [diff] [blame] | 155 | t_const_value* value_; |
ccheever | f53b5cf | 2007-02-05 20:33:11 +0000 | [diff] [blame] | 156 | |
Mark Slee | 36bfa2e | 2007-01-19 20:09:51 +0000 | [diff] [blame] | 157 | bool xsd_optional_; |
Mark Slee | 7df0e2a | 2007-02-06 21:03:18 +0000 | [diff] [blame] | 158 | bool xsd_nillable_; |
Mark Slee | 748d83f | 2007-02-07 01:20:08 +0000 | [diff] [blame] | 159 | t_struct* xsd_attrs_; |
Jens Geyer | 885c679 | 2014-05-02 21:31:55 +0200 | [diff] [blame] | 160 | bool reference_; |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 161 | }; |
| 162 | |
Bryan Duxbury | c7206a4 | 2011-08-17 23:17:04 +0000 | [diff] [blame] | 163 | /** |
| 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 | */ |
| 167 | struct t_field_id { |
Ben Craig | e957675 | 2013-10-11 08:19:16 -0500 | [diff] [blame] | 168 | int32_t value; |
Bryan Duxbury | c7206a4 | 2011-08-17 23:17:04 +0000 | [diff] [blame] | 169 | bool auto_assigned; |
| 170 | }; |
| 171 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 172 | #endif |