blob: 67a2125ce8e0148eee187131c267fd717f787ed3 [file] [log] [blame]
Gavin McDonald0b75e1a2010-10-28 02:12:01 +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 */
19
20#ifndef T_FIELD_H
21#define T_FIELD_H
22
23#include <string>
24#include <boost/lexical_cast.hpp>
25
26#include "t_doc.h"
27
28// Forward declare for xsd_attrs
29class t_struct;
30
31/**
32 * Class to represent a field in a thrift structure. A field has a data type,
33 * a symbolic name, and a numeric identifier.
34 *
35 */
36class t_field : public t_doc {
37 public:
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
47 t_field(t_type* type, std::string name, int32_t key) :
48 type_(type),
49 name_(name),
50 key_(key),
51 req_(T_OPT_IN_REQ_OUT),
52 value_(NULL),
53 xsd_optional_(false),
54 xsd_nillable_(false),
55 xsd_attrs_(NULL) {}
56
57 ~t_field() {}
58
59 t_type* get_type() const {
60 return type_;
61 }
62
63 const std::string& get_name() const {
64 return name_;
65 }
66
67 int32_t get_key() const {
68 return key_;
69 }
70
71 enum e_req {
72 T_REQUIRED,
73 T_OPTIONAL,
74 T_OPT_IN_REQ_OUT,
75 };
76
77 void set_req(e_req req) {
78 req_ = req;
79 }
80
81 e_req get_req() const {
82 return req_;
83 }
84
85 void set_value(t_const_value* value) {
86 value_ = value;
87 }
88
89 t_const_value* get_value() {
90 return value_;
91 }
92
93 void set_xsd_optional(bool xsd_optional) {
94 xsd_optional_ = xsd_optional;
95 }
96
97 bool get_xsd_optional() const {
98 return xsd_optional_;
99 }
100
101 void set_xsd_nillable(bool xsd_nillable) {
102 xsd_nillable_ = xsd_nillable;
103 }
104
105 bool get_xsd_nillable() const {
106 return xsd_nillable_;
107 }
108
109 void set_xsd_attrs(t_struct* xsd_attrs) {
110 xsd_attrs_ = xsd_attrs;
111 }
112
113 t_struct* get_xsd_attrs() {
114 return xsd_attrs_;
115 }
116
117 // This is not the same function as t_type::get_fingerprint_material,
118 // but it does the same thing.
119 std::string get_fingerprint_material() const {
120 return boost::lexical_cast<std::string>(key_) + ":" +
121 ((req_ == T_OPTIONAL) ? "opt-" : "") +
122 type_->get_fingerprint_material();
123 }
124
125 /**
126 * Comparator to sort fields in ascending order by key.
127 * Make this a functor instead of a function to help GCC inline it.
128 * The arguments are (const) references to const pointers to const t_fields.
129 */
130 struct key_compare {
131 bool operator()(t_field const * const & a, t_field const * const & b) {
132 return a->get_key() < b->get_key();
133 }
134 };
135
136
137 private:
138 t_type* type_;
139 std::string name_;
140 int32_t key_;
141 e_req req_;
142 t_const_value* value_;
143
144 bool xsd_optional_;
145 bool xsd_nillable_;
146 t_struct* xsd_attrs_;
147
148};
149
150#endif