blob: 414a0ebfe31e272164b8e6c4aae447c1698ce2e9 [file] [log] [blame]
David Reissd779cbe2007-08-31 01:42:55 +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
7#ifndef _THRIFT_TREFLECTIONLOCAL_H_
8#define _THRIFT_TREFLECTIONLOCAL_H_ 1
9
10#include <stdint.h>
11#include <protocol/TProtocol.h>
12
13namespace facebook { namespace thrift { namespace reflection { namespace local {
14
15using facebook::thrift::protocol::TType;
16
17/**
18 * A local reflection is a representation of a Thrift structure.
19 * (It is called local because it cannot be serialized by Thrift).
20 *
21 * @author David Reiss <dreiss@facebook.com>
22 */
23
David Reiss47557bc2007-09-04 21:31:04 +000024struct FieldMeta {
25 int16_t tags;
26 bool is_optional;
27};
28
David Reissd779cbe2007-08-31 01:42:55 +000029struct TypeSpec {
30 // Use an anonymous union here so we can fit two TypeSpecs in one cache line.
31 union {
32 struct {
33 // Use parallel arrays here for denser packing (of the arrays).
David Reiss47557bc2007-09-04 21:31:04 +000034 FieldMeta* metas;
David Reissd779cbe2007-08-31 01:42:55 +000035 TypeSpec** specs;
David Reiss47557bc2007-09-04 21:31:04 +000036 // n_fields is only used for debugging, but it should only add
37 // a minimimal amount to the effective size of this structure
38 // because of alignment restrictions.
David Reissd779cbe2007-08-31 01:42:55 +000039 int n_fields;
40 } tstruct;
41 struct {
42 TypeSpec *subtype1;
43 TypeSpec *subtype2;
44 } tcontainer;
45 };
46
47 // Put this at the end so the 32-bit enum can be crammed up next to the
48 // 32-bit int (n_fields).
49 TType ttype;
50
51
52 // Static initialization of unions isn't really possible,
53 // so take the plunge and use constructors.
54 // Hopefully they'll be evaluated at compile time.
55
56 TypeSpec(TType ttype) : ttype(ttype) {}
57
David Reiss47557bc2007-09-04 21:31:04 +000058 TypeSpec(TType ttype, int n_fields, FieldMeta* metas, TypeSpec** specs) :
David Reissd779cbe2007-08-31 01:42:55 +000059 ttype(ttype)
60 {
61 tstruct.n_fields = n_fields;
David Reiss47557bc2007-09-04 21:31:04 +000062 tstruct.metas = metas;
David Reissd779cbe2007-08-31 01:42:55 +000063 tstruct.specs = specs;
64 }
65
66 TypeSpec(TType ttype, TypeSpec* subtype1, TypeSpec* subtype2) :
67 ttype(ttype)
68 {
69 tcontainer.subtype1 = subtype1;
70 tcontainer.subtype2 = subtype2;
71 }
72
73};
74
75}}}} // facebook::thrift::reflection::local
76
77#endif // #ifndef _THRIFT_TREFLECTIONLOCAL_H_