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 <string>
25 #include <sstream>
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       value_(nullptr),
45       xsd_optional_(false),
46       xsd_nillable_(false),
47       xsd_attrs_(nullptr),
48       reference_(false) {}
49 
t_field(t_type * type,std::string name,int32_t key)50   t_field(t_type* type, std::string name, int32_t key)
51     : type_(type),
52       name_(name),
53       key_(key),
54       req_(T_OPT_IN_REQ_OUT),
55       value_(nullptr),
56       xsd_optional_(false),
57       xsd_nillable_(false),
58       xsd_attrs_(nullptr),
59       reference_(false) {}
60 
61   ~t_field() override = default;
62 
get_type()63   t_type* get_type() { return type_; }
64 
get_type()65   const t_type* get_type() const { return type_; }
66 
get_name()67   const std::string& get_name() const { return name_; }
68 
get_key()69   int32_t get_key() const { return key_; }
70 
71   enum e_req { T_REQUIRED, T_OPTIONAL, T_OPT_IN_REQ_OUT };
72 
set_req(e_req req)73   void set_req(e_req req) { req_ = req; }
74 
get_req()75   e_req get_req() const { return req_; }
76 
set_value(t_const_value * value)77   void set_value(t_const_value* value) { value_ = value; }
78 
get_value()79   t_const_value* get_value() { return value_; }
80 
get_value()81   const t_const_value* get_value() const { return value_; }
82 
set_xsd_optional(bool xsd_optional)83   void set_xsd_optional(bool xsd_optional) { xsd_optional_ = xsd_optional; }
84 
get_xsd_optional()85   bool get_xsd_optional() const { return xsd_optional_; }
86 
set_xsd_nillable(bool xsd_nillable)87   void set_xsd_nillable(bool xsd_nillable) { xsd_nillable_ = xsd_nillable; }
88 
get_xsd_nillable()89   bool get_xsd_nillable() const { return xsd_nillable_; }
90 
set_xsd_attrs(t_struct * xsd_attrs)91   void set_xsd_attrs(t_struct* xsd_attrs) { xsd_attrs_ = xsd_attrs; }
92 
get_xsd_attrs()93   t_struct* get_xsd_attrs() { return xsd_attrs_; }
94 
get_xsd_attrs()95   const t_struct* get_xsd_attrs() const { return xsd_attrs_; }
96 
97   /**
98    * Comparator to sort fields in ascending order by key.
99    * Make this a functor instead of a function to help GCC inline it.
100    * The arguments are (const) references to const pointers to const t_fields.
101    */
102   struct key_compare {
operatorkey_compare103     bool operator()(t_field const* const& a, t_field const* const& b) {
104       return a->get_key() < b->get_key();
105     }
106   };
107 
108   std::map<std::string, std::string> annotations_;
109 
get_reference()110   bool get_reference() const { return reference_; }
111 
set_reference(bool reference)112   void set_reference(bool reference) { reference_ = reference; }
113 
114 private:
115   t_type* type_;
116   std::string name_;
117   int32_t key_;
118   e_req req_;
119   t_const_value* value_;
120 
121   bool xsd_optional_;
122   bool xsd_nillable_;
123   t_struct* xsd_attrs_;
124   bool reference_;
125 };
126 
127 /**
128  * A simple struct for the parser to use to store a field ID, and whether or
129  * not it was specified by the user or automatically chosen.
130  */
131 struct t_field_id {
132   int32_t value;
133   bool auto_assigned;
134 };
135 
136 #endif
137