Mark Slee | e9ce01c | 2007-05-16 02:29:53 +0000 | [diff] [blame] | 1 | // 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 Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 7 | #ifndef T_FIELD_H |
| 8 | #define T_FIELD_H |
| 9 | |
| 10 | #include <string> |
David Reiss | 18bf22d | 2007-08-28 20:49:17 +0000 | [diff] [blame] | 11 | #include <boost/lexical_cast.hpp> |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 12 | |
David Reiss | f011bd4 | 2008-10-29 00:07:45 +0000 | [diff] [blame] | 13 | #include "t_doc.h" |
| 14 | |
Mark Slee | 748d83f | 2007-02-07 01:20:08 +0000 | [diff] [blame] | 15 | // Forward declare for xsd_attrs |
| 16 | class t_struct; |
| 17 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 18 | /** |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 19 | * Class to represent a field in a thrift structure. A field has a data type, |
| 20 | * a symbolic name, and a numeric identifier. |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 21 | * |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 22 | */ |
David Reiss | f011bd4 | 2008-10-29 00:07:45 +0000 | [diff] [blame] | 23 | class t_field : public t_doc { |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 24 | public: |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 25 | t_field(t_type* type, std::string name) : |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 26 | type_(type), |
| 27 | name_(name), |
Mark Slee | 36bfa2e | 2007-01-19 20:09:51 +0000 | [diff] [blame] | 28 | key_(0), |
Mark Slee | 7ff3245 | 2007-02-01 05:26:18 +0000 | [diff] [blame] | 29 | value_(NULL), |
Mark Slee | 7df0e2a | 2007-02-06 21:03:18 +0000 | [diff] [blame] | 30 | xsd_optional_(false), |
Mark Slee | 748d83f | 2007-02-07 01:20:08 +0000 | [diff] [blame] | 31 | xsd_nillable_(false), |
| 32 | xsd_attrs_(NULL) {} |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 33 | |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 34 | t_field(t_type* type, std::string name, int32_t key) : |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 35 | type_(type), |
| 36 | name_(name), |
Mark Slee | 36bfa2e | 2007-01-19 20:09:51 +0000 | [diff] [blame] | 37 | key_(key), |
David Reiss | 204420f | 2008-01-11 20:59:03 +0000 | [diff] [blame] | 38 | req_(T_OPT_IN_REQ_OUT), |
Mark Slee | 7ff3245 | 2007-02-01 05:26:18 +0000 | [diff] [blame] | 39 | value_(NULL), |
Mark Slee | 7df0e2a | 2007-02-06 21:03:18 +0000 | [diff] [blame] | 40 | xsd_optional_(false), |
Mark Slee | 748d83f | 2007-02-07 01:20:08 +0000 | [diff] [blame] | 41 | xsd_nillable_(false), |
| 42 | xsd_attrs_(NULL) {} |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 43 | |
| 44 | ~t_field() {} |
| 45 | |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 46 | 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 Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 57 | |
David Reiss | 8320a92 | 2007-08-14 19:59:26 +0000 | [diff] [blame] | 58 | enum e_req { |
David Reiss | 204420f | 2008-01-11 20:59:03 +0000 | [diff] [blame] | 59 | T_REQUIRED, |
| 60 | T_OPTIONAL, |
| 61 | T_OPT_IN_REQ_OUT, |
David Reiss | 8320a92 | 2007-08-14 19:59:26 +0000 | [diff] [blame] | 62 | }; |
| 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 Slee | 21135c3 | 2007-02-05 21:52:08 +0000 | [diff] [blame] | 72 | void set_value(t_const_value* value) { |
| 73 | value_ = value; |
| 74 | } |
| 75 | |
| 76 | t_const_value* get_value() { |
| 77 | return value_; |
| 78 | } |
| 79 | |
Mark Slee | 36bfa2e | 2007-01-19 20:09:51 +0000 | [diff] [blame] | 80 | 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 Slee | 7df0e2a | 2007-02-06 21:03:18 +0000 | [diff] [blame] | 88 | 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 Slee | 748d83f | 2007-02-07 01:20:08 +0000 | [diff] [blame] | 96 | void set_xsd_attrs(t_struct* xsd_attrs) { |
| 97 | xsd_attrs_ = xsd_attrs; |
Mark Slee | 7ff3245 | 2007-02-01 05:26:18 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Mark Slee | 748d83f | 2007-02-07 01:20:08 +0000 | [diff] [blame] | 100 | t_struct* get_xsd_attrs() { |
Mark Slee | 21135c3 | 2007-02-05 21:52:08 +0000 | [diff] [blame] | 101 | return xsd_attrs_; |
Mark Slee | 7ff3245 | 2007-02-01 05:26:18 +0000 | [diff] [blame] | 102 | } |
| 103 | |
David Reiss | 18bf22d | 2007-08-28 20:49:17 +0000 | [diff] [blame] | 104 | // 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 Reiss | 204420f | 2008-01-11 20:59:03 +0000 | [diff] [blame] | 108 | ((req_ == T_OPTIONAL) ? "opt-" : "") + |
David Reiss | 18bf22d | 2007-08-28 20:49:17 +0000 | [diff] [blame] | 109 | type_->get_fingerprint_material(); |
| 110 | } |
| 111 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 112 | private: |
| 113 | t_type* type_; |
| 114 | std::string name_; |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 115 | int32_t key_; |
David Reiss | 8320a92 | 2007-08-14 19:59:26 +0000 | [diff] [blame] | 116 | e_req req_; |
Mark Slee | 7ff3245 | 2007-02-01 05:26:18 +0000 | [diff] [blame] | 117 | t_const_value* value_; |
ccheever | f53b5cf | 2007-02-05 20:33:11 +0000 | [diff] [blame] | 118 | |
Mark Slee | 36bfa2e | 2007-01-19 20:09:51 +0000 | [diff] [blame] | 119 | bool xsd_optional_; |
Mark Slee | 7df0e2a | 2007-02-06 21:03:18 +0000 | [diff] [blame] | 120 | bool xsd_nillable_; |
Mark Slee | 748d83f | 2007-02-07 01:20:08 +0000 | [diff] [blame] | 121 | t_struct* xsd_attrs_; |
Mark Slee | 36bfa2e | 2007-01-19 20:09:51 +0000 | [diff] [blame] | 122 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | #endif |