blob: 7181697602dd8b691236c6cd9f9b30021e5441c9 [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
David Reissf011bd42008-10-29 00:07:45 +000013#include "t_doc.h"
14
Mark Slee748d83f2007-02-07 01:20:08 +000015// Forward declare for xsd_attrs
16class t_struct;
17
Mark Slee31985722006-05-24 21:45:31 +000018/**
Mark Sleef5377b32006-10-10 01:42:59 +000019 * Class to represent a field in a thrift structure. A field has a data type,
20 * a symbolic name, and a numeric identifier.
Mark Slee31985722006-05-24 21:45:31 +000021 *
22 * @author Mark Slee <mcslee@facebook.com>
23 */
David Reissf011bd42008-10-29 00:07:45 +000024class t_field : public t_doc {
Mark Slee31985722006-05-24 21:45:31 +000025 public:
Mark Sleeb15a68b2006-06-07 06:46:24 +000026 t_field(t_type* type, std::string name) :
Mark Sleef5377b32006-10-10 01:42:59 +000027 type_(type),
28 name_(name),
Mark Slee36bfa2e2007-01-19 20:09:51 +000029 key_(0),
Mark Slee7ff32452007-02-01 05:26:18 +000030 value_(NULL),
Mark Slee7df0e2a2007-02-06 21:03:18 +000031 xsd_optional_(false),
Mark Slee748d83f2007-02-07 01:20:08 +000032 xsd_nillable_(false),
33 xsd_attrs_(NULL) {}
Mark Sleeb15a68b2006-06-07 06:46:24 +000034
Mark Slee9cb7c612006-09-01 22:17:45 +000035 t_field(t_type* type, std::string name, int32_t key) :
Mark Sleef5377b32006-10-10 01:42:59 +000036 type_(type),
37 name_(name),
Mark Slee36bfa2e2007-01-19 20:09:51 +000038 key_(key),
David Reiss204420f2008-01-11 20:59:03 +000039 req_(T_OPT_IN_REQ_OUT),
Mark Slee7ff32452007-02-01 05:26:18 +000040 value_(NULL),
Mark Slee7df0e2a2007-02-06 21:03:18 +000041 xsd_optional_(false),
Mark Slee748d83f2007-02-07 01:20:08 +000042 xsd_nillable_(false),
43 xsd_attrs_(NULL) {}
Mark Slee31985722006-05-24 21:45:31 +000044
45 ~t_field() {}
46
Mark Sleef5377b32006-10-10 01:42:59 +000047 t_type* get_type() const {
48 return type_;
49 }
50
51 const std::string& get_name() const {
52 return name_;
53 }
54
55 int32_t get_key() const {
56 return key_;
57 }
Mark Slee31985722006-05-24 21:45:31 +000058
David Reiss8320a922007-08-14 19:59:26 +000059 enum e_req {
David Reiss204420f2008-01-11 20:59:03 +000060 T_REQUIRED,
61 T_OPTIONAL,
62 T_OPT_IN_REQ_OUT,
David Reiss8320a922007-08-14 19:59:26 +000063 };
64
65 void set_req(e_req req) {
66 req_ = req;
67 }
68
69 e_req get_req() const {
70 return req_;
71 }
72
Mark Slee21135c32007-02-05 21:52:08 +000073 void set_value(t_const_value* value) {
74 value_ = value;
75 }
76
77 t_const_value* get_value() {
78 return value_;
79 }
80
Mark Slee36bfa2e2007-01-19 20:09:51 +000081 void set_xsd_optional(bool xsd_optional) {
82 xsd_optional_ = xsd_optional;
83 }
84
85 bool get_xsd_optional() const {
86 return xsd_optional_;
87 }
88
Mark Slee7df0e2a2007-02-06 21:03:18 +000089 void set_xsd_nillable(bool xsd_nillable) {
90 xsd_nillable_ = xsd_nillable;
91 }
92
93 bool get_xsd_nillable() const {
94 return xsd_nillable_;
95 }
96
Mark Slee748d83f2007-02-07 01:20:08 +000097 void set_xsd_attrs(t_struct* xsd_attrs) {
98 xsd_attrs_ = xsd_attrs;
Mark Slee7ff32452007-02-01 05:26:18 +000099 }
100
Mark Slee748d83f2007-02-07 01:20:08 +0000101 t_struct* get_xsd_attrs() {
Mark Slee21135c32007-02-05 21:52:08 +0000102 return xsd_attrs_;
Mark Slee7ff32452007-02-01 05:26:18 +0000103 }
104
David Reiss18bf22d2007-08-28 20:49:17 +0000105 // This is not the same function as t_type::get_fingerprint_material,
106 // but it does the same thing.
107 std::string get_fingerprint_material() const {
108 return boost::lexical_cast<std::string>(key_) + ":" +
David Reiss204420f2008-01-11 20:59:03 +0000109 ((req_ == T_OPTIONAL) ? "opt-" : "") +
David Reiss18bf22d2007-08-28 20:49:17 +0000110 type_->get_fingerprint_material();
111 }
112
Mark Slee31985722006-05-24 21:45:31 +0000113 private:
114 t_type* type_;
115 std::string name_;
Mark Slee9cb7c612006-09-01 22:17:45 +0000116 int32_t key_;
David Reiss8320a922007-08-14 19:59:26 +0000117 e_req req_;
Mark Slee7ff32452007-02-01 05:26:18 +0000118 t_const_value* value_;
ccheeverf53b5cf2007-02-05 20:33:11 +0000119
Mark Slee36bfa2e2007-01-19 20:09:51 +0000120 bool xsd_optional_;
Mark Slee7df0e2a2007-02-06 21:03:18 +0000121 bool xsd_nillable_;
Mark Slee748d83f2007-02-07 01:20:08 +0000122 t_struct* xsd_attrs_;
Mark Slee36bfa2e2007-01-19 20:09:51 +0000123
Mark Slee31985722006-05-24 21:45:31 +0000124};
125
126#endif