blob: d296473f6c54d6c7a63b7f6cb15323d5ccf01de8 [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>
David Reiss18bf22d2007-08-28 20:49:17 +000012#include <cstring>
Mark Slee31985722006-05-24 21:45:31 +000013
14#include "t_type.h"
Mark Sleee8540632006-05-30 09:24:40 +000015#include "t_field.h"
Mark Slee31985722006-05-24 21:45:31 +000016
Mark Sleef0712dc2006-10-25 19:03:57 +000017// Forward declare that puppy
18class t_program;
19
Mark Sleef5377b32006-10-10 01:42:59 +000020/**
21 * A struct is a container for a set of member fields that has a name. Structs
22 * are also used to implement exception types.
23 *
24 * @author Mark Slee <mcslee@facebook.com>
25 */
Mark Slee31985722006-05-24 21:45:31 +000026class t_struct : public t_type {
27 public:
Mark Sleef0712dc2006-10-25 19:03:57 +000028 t_struct(t_program* program) :
29 t_type(program),
Mark Slee782abbb2007-01-19 00:17:02 +000030 is_xception_(false),
David Reiss18bf22d2007-08-28 20:49:17 +000031 xsd_all_(false)
32 {
33 memset(fingerprint_, 0, sizeof(fingerprint_));
34 }
Mark Sleeb15a68b2006-06-07 06:46:24 +000035
Mark Sleef0712dc2006-10-25 19:03:57 +000036 t_struct(t_program* program, const std::string& name) :
37 t_type(program, name),
Mark Slee782abbb2007-01-19 00:17:02 +000038 is_xception_(false),
David Reiss18bf22d2007-08-28 20:49:17 +000039 xsd_all_(false)
40 {
41 memset(fingerprint_, 0, sizeof(fingerprint_));
42 }
Mark Slee31985722006-05-24 21:45:31 +000043
Mark Slee9cb7c612006-09-01 22:17:45 +000044 void set_name(const std::string& name) {
45 name_ = name;
46 }
47
Mark Slee9cb7c612006-09-01 22:17:45 +000048 void set_xception(bool is_xception) {
49 is_xception_ = is_xception;
50 }
Mark Sleee8540632006-05-30 09:24:40 +000051
Mark Slee782abbb2007-01-19 00:17:02 +000052 void set_xsd_all(bool xsd_all) {
53 xsd_all_ = xsd_all;
54 }
55
56 bool get_xsd_all() const {
57 return xsd_all_;
58 }
59
Mark Slee9cb7c612006-09-01 22:17:45 +000060 void append(t_field* elem) {
61 members_.push_back(elem);
62 }
Mark Sleee8540632006-05-30 09:24:40 +000063
Mark Slee9cb7c612006-09-01 22:17:45 +000064 const std::vector<t_field*>& get_members() {
65 return members_;
66 }
67
68 bool is_struct() const {
69 return !is_xception_;
70 }
71
72 bool is_xception() const {
73 return is_xception_;
74 }
Mark Slee31985722006-05-24 21:45:31 +000075
David Reiss18bf22d2007-08-28 20:49:17 +000076 virtual std::string get_fingerprint_material() const {
77 std::string rv = "{";
78 std::vector<t_field*>::const_iterator m_iter;
79 for (m_iter = members_.begin(); m_iter != members_.end(); ++m_iter) {
80 rv += (**m_iter).get_fingerprint_material();
81 rv += ";";
82 }
83 rv += "}";
84 return rv;
85 }
86
David Reiss9885c682007-08-30 23:12:37 +000087 virtual void generate_fingerprint() {
88 t_type::generate_fingerprint();
89 std::vector<t_field*>::const_iterator m_iter;
90 for (m_iter = members_.begin(); m_iter != members_.end(); ++m_iter) {
91 (**m_iter).get_type()->generate_fingerprint();
David Reiss18bf22d2007-08-28 20:49:17 +000092 }
93 }
94
Mark Slee31985722006-05-24 21:45:31 +000095 private:
David Reiss18bf22d2007-08-28 20:49:17 +000096
Mark Sleee8540632006-05-30 09:24:40 +000097 std::vector<t_field*> members_;
Mark Slee9cb7c612006-09-01 22:17:45 +000098 bool is_xception_;
Mark Slee782abbb2007-01-19 00:17:02 +000099
100 bool xsd_all_;
Mark Slee31985722006-05-24 21:45:31 +0000101};
102
103#endif