blob: ae9ac7860e4d434fad5c962a6164e6ab9f7b2731 [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 *
Mark Slee31985722006-05-24 21:45:31 +000022 */
David Reissf011bd42008-10-29 00:07:45 +000023class t_field : public t_doc {
Mark Slee31985722006-05-24 21:45:31 +000024 public:
Mark Sleeb15a68b2006-06-07 06:46:24 +000025 t_field(t_type* type, std::string name) :
Mark Sleef5377b32006-10-10 01:42:59 +000026 type_(type),
27 name_(name),
Mark Slee36bfa2e2007-01-19 20:09:51 +000028 key_(0),
Mark Slee7ff32452007-02-01 05:26:18 +000029 value_(NULL),
Mark Slee7df0e2a2007-02-06 21:03:18 +000030 xsd_optional_(false),
Mark Slee748d83f2007-02-07 01:20:08 +000031 xsd_nillable_(false),
32 xsd_attrs_(NULL) {}
Mark Sleeb15a68b2006-06-07 06:46:24 +000033
Mark Slee9cb7c612006-09-01 22:17:45 +000034 t_field(t_type* type, std::string name, int32_t key) :
Mark Sleef5377b32006-10-10 01:42:59 +000035 type_(type),
36 name_(name),
Mark Slee36bfa2e2007-01-19 20:09:51 +000037 key_(key),
David Reiss204420f2008-01-11 20:59:03 +000038 req_(T_OPT_IN_REQ_OUT),
Mark Slee7ff32452007-02-01 05:26:18 +000039 value_(NULL),
Mark Slee7df0e2a2007-02-06 21:03:18 +000040 xsd_optional_(false),
Mark Slee748d83f2007-02-07 01:20:08 +000041 xsd_nillable_(false),
42 xsd_attrs_(NULL) {}
Mark Slee31985722006-05-24 21:45:31 +000043
44 ~t_field() {}
45
Mark Sleef5377b32006-10-10 01:42:59 +000046 t_type* get_type() const {
47 return type_;
48 }
49
50 const std::string& get_name() const {
51 return name_;
52 }
53
54 int32_t get_key() const {
55 return key_;
56 }
Mark Slee31985722006-05-24 21:45:31 +000057
David Reiss8320a922007-08-14 19:59:26 +000058 enum e_req {
David Reiss204420f2008-01-11 20:59:03 +000059 T_REQUIRED,
60 T_OPTIONAL,
61 T_OPT_IN_REQ_OUT,
David Reiss8320a922007-08-14 19:59:26 +000062 };
63
64 void set_req(e_req req) {
65 req_ = req;
66 }
67
68 e_req get_req() const {
69 return req_;
70 }
71
Mark Slee21135c32007-02-05 21:52:08 +000072 void set_value(t_const_value* value) {
73 value_ = value;
74 }
75
76 t_const_value* get_value() {
77 return value_;
78 }
79
Mark Slee36bfa2e2007-01-19 20:09:51 +000080 void set_xsd_optional(bool xsd_optional) {
81 xsd_optional_ = xsd_optional;
82 }
83
84 bool get_xsd_optional() const {
85 return xsd_optional_;
86 }
87
Mark Slee7df0e2a2007-02-06 21:03:18 +000088 void set_xsd_nillable(bool xsd_nillable) {
89 xsd_nillable_ = xsd_nillable;
90 }
91
92 bool get_xsd_nillable() const {
93 return xsd_nillable_;
94 }
95
Mark Slee748d83f2007-02-07 01:20:08 +000096 void set_xsd_attrs(t_struct* xsd_attrs) {
97 xsd_attrs_ = xsd_attrs;
Mark Slee7ff32452007-02-01 05:26:18 +000098 }
99
Mark Slee748d83f2007-02-07 01:20:08 +0000100 t_struct* get_xsd_attrs() {
Mark Slee21135c32007-02-05 21:52:08 +0000101 return xsd_attrs_;
Mark Slee7ff32452007-02-01 05:26:18 +0000102 }
103
David Reiss18bf22d2007-08-28 20:49:17 +0000104 // This is not the same function as t_type::get_fingerprint_material,
105 // but it does the same thing.
106 std::string get_fingerprint_material() const {
107 return boost::lexical_cast<std::string>(key_) + ":" +
David Reiss204420f2008-01-11 20:59:03 +0000108 ((req_ == T_OPTIONAL) ? "opt-" : "") +
David Reiss18bf22d2007-08-28 20:49:17 +0000109 type_->get_fingerprint_material();
110 }
111
Mark Slee31985722006-05-24 21:45:31 +0000112 private:
113 t_type* type_;
114 std::string name_;
Mark Slee9cb7c612006-09-01 22:17:45 +0000115 int32_t key_;
David Reiss8320a922007-08-14 19:59:26 +0000116 e_req req_;
Mark Slee7ff32452007-02-01 05:26:18 +0000117 t_const_value* value_;
ccheeverf53b5cf2007-02-05 20:33:11 +0000118
Mark Slee36bfa2e2007-01-19 20:09:51 +0000119 bool xsd_optional_;
Mark Slee7df0e2a2007-02-06 21:03:18 +0000120 bool xsd_nillable_;
Mark Slee748d83f2007-02-07 01:20:08 +0000121 t_struct* xsd_attrs_;
Mark Slee36bfa2e2007-01-19 20:09:51 +0000122
Mark Slee31985722006-05-24 21:45:31 +0000123};
124
125#endif