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 <map> 24 #include <sstream> 25 #include <string> 26 27 #include "thrift/parse/t_doc.h" 28 #include "thrift/parse/t_type.h" 29 30 // Forward declare for xsd_attrs 31 class t_struct; 32 33 /** 34 * Class to represent a field in a thrift structure. A field has a data type, 35 * a symbolic name, and a numeric identifier. 36 * 37 */ 38 class t_field : public t_doc { 39 public: t_field(t_type * type,std::string name)40 t_field(t_type* type, std::string name) 41 : type_(type), 42 name_(name), 43 key_(0), 44 req_(T_OPT_IN_REQ_OUT), 45 value_(nullptr), 46 xsd_optional_(false), 47 xsd_nillable_(false), 48 xsd_attrs_(nullptr), 49 reference_(false) {} 50 t_field(t_type * type,std::string name,int32_t key)51 t_field(t_type* type, std::string name, int32_t key) 52 : type_(type), 53 name_(name), 54 key_(key), 55 req_(T_OPT_IN_REQ_OUT), 56 value_(nullptr), 57 xsd_optional_(false), 58 xsd_nillable_(false), 59 xsd_attrs_(nullptr), 60 reference_(false) {} 61 62 ~t_field() override = default; 63 get_type()64 t_type* get_type() { return type_; } 65 get_type()66 const t_type* get_type() const { return type_; } 67 get_name()68 const std::string& get_name() const { return name_; } 69 get_key()70 int32_t get_key() const { return key_; } 71 72 enum e_req { T_REQUIRED, T_OPTIONAL, T_OPT_IN_REQ_OUT }; 73 set_req(e_req req)74 void set_req(e_req req) { req_ = req; } 75 get_req()76 e_req get_req() const { return req_; } 77 set_value(t_const_value * value)78 void set_value(t_const_value* value) { value_ = value; } 79 get_value()80 t_const_value* get_value() { return value_; } 81 get_value()82 const t_const_value* get_value() const { return value_; } 83 set_xsd_optional(bool xsd_optional)84 void set_xsd_optional(bool xsd_optional) { xsd_optional_ = xsd_optional; } 85 get_xsd_optional()86 bool get_xsd_optional() const { return xsd_optional_; } 87 set_xsd_nillable(bool xsd_nillable)88 void set_xsd_nillable(bool xsd_nillable) { xsd_nillable_ = xsd_nillable; } 89 get_xsd_nillable()90 bool get_xsd_nillable() const { return xsd_nillable_; } 91 set_xsd_attrs(t_struct * xsd_attrs)92 void set_xsd_attrs(t_struct* xsd_attrs) { xsd_attrs_ = xsd_attrs; } 93 get_xsd_attrs()94 t_struct* get_xsd_attrs() { return xsd_attrs_; } 95 get_xsd_attrs()96 const t_struct* get_xsd_attrs() const { return xsd_attrs_; } 97 98 /** 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 { operatorkey_compare104 bool operator()(t_field const* const& a, t_field const* const& b) { 105 return a->get_key() < b->get_key(); 106 } 107 }; 108 109 std::map<std::string, std::vector<std::string>> annotations_; 110 get_reference()111 bool get_reference() const { return reference_; } 112 set_reference(bool reference)113 void set_reference(bool reference) { reference_ = reference; } 114 115 private: 116 t_type* type_; 117 std::string name_; 118 int32_t key_; 119 e_req req_; 120 t_const_value* value_; 121 122 bool xsd_optional_; 123 bool xsd_nillable_; 124 t_struct* xsd_attrs_; 125 bool reference_; 126 }; 127 128 /** 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 */ 132 struct t_field_id { 133 int32_t value; 134 bool auto_assigned; 135 }; 136 137 #endif 138