blob: 25c50a56cb02636fbb0abf48b82aa302638bd861 [file] [log] [blame]
Mark Sleee9ce01c2007-05-16 02:29:53 +00001// Copyright (c) 2006- Facebook
2// Distributed under the Thrift Software License
3//
4// See accompanying file LICENSE or visit the Thrift site at:
5// http://developers.facebook.com/thrift/
6
Mark Slee31985722006-05-24 21:45:31 +00007#ifndef T_FIELD_H
8#define T_FIELD_H
9
10#include <string>
David Reiss18bf22d2007-08-28 20:49:17 +000011#include <boost/lexical_cast.hpp>
Mark Slee31985722006-05-24 21:45:31 +000012
Mark Slee748d83f2007-02-07 01:20:08 +000013// Forward declare for xsd_attrs
14class t_struct;
15
Mark Slee31985722006-05-24 21:45:31 +000016/**
Mark Sleef5377b32006-10-10 01:42:59 +000017 * Class to represent a field in a thrift structure. A field has a data type,
18 * a symbolic name, and a numeric identifier.
Mark Slee31985722006-05-24 21:45:31 +000019 *
20 * @author Mark Slee <mcslee@facebook.com>
21 */
22class t_field {
23 public:
Mark Sleeb15a68b2006-06-07 06:46:24 +000024 t_field(t_type* type, std::string name) :
Mark Sleef5377b32006-10-10 01:42:59 +000025 type_(type),
26 name_(name),
Mark Slee36bfa2e2007-01-19 20:09:51 +000027 key_(0),
Mark Slee7ff32452007-02-01 05:26:18 +000028 value_(NULL),
Mark Slee7df0e2a2007-02-06 21:03:18 +000029 xsd_optional_(false),
Mark Slee748d83f2007-02-07 01:20:08 +000030 xsd_nillable_(false),
31 xsd_attrs_(NULL) {}
Mark Sleeb15a68b2006-06-07 06:46:24 +000032
Mark Slee9cb7c612006-09-01 22:17:45 +000033 t_field(t_type* type, std::string name, int32_t key) :
Mark Sleef5377b32006-10-10 01:42:59 +000034 type_(type),
35 name_(name),
Mark Slee36bfa2e2007-01-19 20:09:51 +000036 key_(key),
David Reiss8320a922007-08-14 19:59:26 +000037 req_(OPT_IN_REQ_OUT),
Mark Slee7ff32452007-02-01 05:26:18 +000038 value_(NULL),
Mark Slee7df0e2a2007-02-06 21:03:18 +000039 xsd_optional_(false),
Mark Slee748d83f2007-02-07 01:20:08 +000040 xsd_nillable_(false),
41 xsd_attrs_(NULL) {}
Mark Slee31985722006-05-24 21:45:31 +000042
43 ~t_field() {}
44
Mark Sleef5377b32006-10-10 01:42:59 +000045 t_type* get_type() const {
46 return type_;
47 }
48
49 const std::string& get_name() const {
50 return name_;
51 }
52
53 int32_t get_key() const {
54 return key_;
55 }
Mark Slee31985722006-05-24 21:45:31 +000056
David Reiss8320a922007-08-14 19:59:26 +000057 enum e_req {
58 REQUIRED,
59 OPTIONAL,
60 OPT_IN_REQ_OUT
61 };
62
63 void set_req(e_req req) {
64 req_ = req;
65 }
66
67 e_req get_req() const {
68 return req_;
69 }
70
Mark Slee21135c32007-02-05 21:52:08 +000071 void set_value(t_const_value* value) {
72 value_ = value;
73 }
74
75 t_const_value* get_value() {
76 return value_;
77 }
78
Mark Slee36bfa2e2007-01-19 20:09:51 +000079 void set_xsd_optional(bool xsd_optional) {
80 xsd_optional_ = xsd_optional;
81 }
82
83 bool get_xsd_optional() const {
84 return xsd_optional_;
85 }
86
Mark Slee7df0e2a2007-02-06 21:03:18 +000087 void set_xsd_nillable(bool xsd_nillable) {
88 xsd_nillable_ = xsd_nillable;
89 }
90
91 bool get_xsd_nillable() const {
92 return xsd_nillable_;
93 }
94
Mark Slee748d83f2007-02-07 01:20:08 +000095 void set_xsd_attrs(t_struct* xsd_attrs) {
96 xsd_attrs_ = xsd_attrs;
Mark Slee7ff32452007-02-01 05:26:18 +000097 }
98
Mark Slee748d83f2007-02-07 01:20:08 +000099 t_struct* get_xsd_attrs() {
Mark Slee21135c32007-02-05 21:52:08 +0000100 return xsd_attrs_;
Mark Slee7ff32452007-02-01 05:26:18 +0000101 }
102
ccheeverf53b5cf2007-02-05 20:33:11 +0000103 const std::string& get_doc() const {
104 return doc_;
105 }
106
107 bool has_doc() {
108 return has_doc_;
109 }
110
111 void set_doc(const std::string& doc) {
112 doc_ = doc;
113 has_doc_ = true;
114 }
115
David Reiss18bf22d2007-08-28 20:49:17 +0000116 // This is not the same function as t_type::get_fingerprint_material,
117 // but it does the same thing.
118 std::string get_fingerprint_material() const {
119 return boost::lexical_cast<std::string>(key_) + ":" +
120 (req_ == OPTIONAL ? "opt-" : "") +
121 type_->get_fingerprint_material();
122 }
123
Mark Slee31985722006-05-24 21:45:31 +0000124 private:
125 t_type* type_;
126 std::string name_;
Mark Slee9cb7c612006-09-01 22:17:45 +0000127 int32_t key_;
David Reiss8320a922007-08-14 19:59:26 +0000128 e_req req_;
Mark Slee7ff32452007-02-01 05:26:18 +0000129 t_const_value* value_;
ccheeverf53b5cf2007-02-05 20:33:11 +0000130
Mark Slee36bfa2e2007-01-19 20:09:51 +0000131 bool xsd_optional_;
Mark Slee7df0e2a2007-02-06 21:03:18 +0000132 bool xsd_nillable_;
Mark Slee748d83f2007-02-07 01:20:08 +0000133 t_struct* xsd_attrs_;
Mark Slee36bfa2e2007-01-19 20:09:51 +0000134
ccheeverf53b5cf2007-02-05 20:33:11 +0000135 std::string doc_;
136 bool has_doc_;
137
Mark Slee31985722006-05-24 21:45:31 +0000138};
139
140#endif