blob: bca51f6265c2ac3c73b4c2cfc0c89cf92763dbb0 [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_STRUCT_H
8#define T_STRUCT_H
9
10#include <vector>
11#include <string>
12
13#include "t_type.h"
Mark Sleee8540632006-05-30 09:24:40 +000014#include "t_field.h"
Mark Slee31985722006-05-24 21:45:31 +000015
Mark Sleef0712dc2006-10-25 19:03:57 +000016// Forward declare that puppy
17class t_program;
18
Mark Sleef5377b32006-10-10 01:42:59 +000019/**
20 * A struct is a container for a set of member fields that has a name. Structs
21 * are also used to implement exception types.
22 *
23 * @author Mark Slee <mcslee@facebook.com>
24 */
Mark Slee31985722006-05-24 21:45:31 +000025class t_struct : public t_type {
26 public:
Mark Sleef0712dc2006-10-25 19:03:57 +000027 t_struct(t_program* program) :
28 t_type(program),
Mark Slee782abbb2007-01-19 00:17:02 +000029 is_xception_(false),
David Reissf84b3602007-09-17 21:16:32 +000030 xsd_all_(false) {}
Mark Sleeb15a68b2006-06-07 06:46:24 +000031
Mark Sleef0712dc2006-10-25 19:03:57 +000032 t_struct(t_program* program, const std::string& name) :
33 t_type(program, name),
Mark Slee782abbb2007-01-19 00:17:02 +000034 is_xception_(false),
David Reissf84b3602007-09-17 21:16:32 +000035 xsd_all_(false) {}
Mark Slee31985722006-05-24 21:45:31 +000036
Mark Slee9cb7c612006-09-01 22:17:45 +000037 void set_name(const std::string& name) {
38 name_ = name;
39 }
40
Mark Slee9cb7c612006-09-01 22:17:45 +000041 void set_xception(bool is_xception) {
42 is_xception_ = is_xception;
43 }
Mark Sleee8540632006-05-30 09:24:40 +000044
Mark Slee782abbb2007-01-19 00:17:02 +000045 void set_xsd_all(bool xsd_all) {
46 xsd_all_ = xsd_all;
47 }
48
49 bool get_xsd_all() const {
50 return xsd_all_;
51 }
52
Mark Slee9cb7c612006-09-01 22:17:45 +000053 void append(t_field* elem) {
54 members_.push_back(elem);
55 }
Mark Sleee8540632006-05-30 09:24:40 +000056
Mark Slee9cb7c612006-09-01 22:17:45 +000057 const std::vector<t_field*>& get_members() {
58 return members_;
59 }
60
61 bool is_struct() const {
62 return !is_xception_;
63 }
64
65 bool is_xception() const {
66 return is_xception_;
67 }
Mark Slee31985722006-05-24 21:45:31 +000068
David Reiss18bf22d2007-08-28 20:49:17 +000069 virtual std::string get_fingerprint_material() const {
70 std::string rv = "{";
71 std::vector<t_field*>::const_iterator m_iter;
72 for (m_iter = members_.begin(); m_iter != members_.end(); ++m_iter) {
David Reiss6ce97e32007-08-30 23:18:31 +000073 rv += (*m_iter)->get_fingerprint_material();
David Reiss18bf22d2007-08-28 20:49:17 +000074 rv += ";";
75 }
76 rv += "}";
77 return rv;
78 }
79
David Reiss9885c682007-08-30 23:12:37 +000080 virtual void generate_fingerprint() {
81 t_type::generate_fingerprint();
82 std::vector<t_field*>::const_iterator m_iter;
83 for (m_iter = members_.begin(); m_iter != members_.end(); ++m_iter) {
David Reiss6ce97e32007-08-30 23:18:31 +000084 (*m_iter)->get_type()->generate_fingerprint();
David Reiss18bf22d2007-08-28 20:49:17 +000085 }
86 }
87
Mark Slee6f9ac3f2007-11-28 22:28:13 +000088 bool validate_field(t_field* field) {
89 int key = field->get_key();
90 std::vector<t_field*>::const_iterator m_iter;
91 for (m_iter = members_.begin(); m_iter != members_.end(); ++m_iter) {
92 if ((*m_iter)->get_key() == key) {
93 return false;
94 }
95 }
96 return true;
97 }
98
Mark Slee31985722006-05-24 21:45:31 +000099 private:
David Reiss18bf22d2007-08-28 20:49:17 +0000100
Mark Sleee8540632006-05-30 09:24:40 +0000101 std::vector<t_field*> members_;
Mark Slee9cb7c612006-09-01 22:17:45 +0000102 bool is_xception_;
Mark Slee782abbb2007-01-19 00:17:02 +0000103
104 bool xsd_all_;
Mark Slee31985722006-05-24 21:45:31 +0000105};
106
107#endif