r1292@dev030 (orig r64037): dreiss | 2007-10-16 20:39:55 -0700
[amiethrift.git] / lib / cpp / src / TReflectionLocal.h
blob8e39117060c2b1dd8d3588fa82c9bc8928af966f
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/
7 #ifndef _THRIFT_TREFLECTIONLOCAL_H_
8 #define _THRIFT_TREFLECTIONLOCAL_H_ 1
10 #include <stdint.h>
11 #include <cstring>
12 #include <protocol/TProtocol.h>
14 /**
15 * Local Reflection is a blanket term referring to the the structure
16 * and generation of this particular representation of Thrift types.
17 * (It is called local because it cannot be serialized by Thrift).
19 * @author David Reiss <dreiss@facebook.com>
22 namespace facebook { namespace thrift { namespace reflection { namespace local {
24 using facebook::thrift::protocol::TType;
26 // We include this many bytes of the structure's fingerprint when serializing
27 // a top-level structure. Long enough to make collisions unlikely, short
28 // enough to not significantly affect the amount of memory used.
29 const int FP_PREFIX_LEN = 4;
31 struct FieldMeta {
32 int16_t tag;
33 bool is_optional;
36 struct TypeSpec {
37 TType ttype;
38 uint8_t fp_prefix[FP_PREFIX_LEN];
40 // Use an anonymous union here so we can fit two TypeSpecs in one cache line.
41 union {
42 struct {
43 // Use parallel arrays here for denser packing (of the arrays).
44 FieldMeta* metas;
45 TypeSpec** specs;
46 } tstruct;
47 struct {
48 TypeSpec *subtype1;
49 TypeSpec *subtype2;
50 } tcontainer;
53 // Static initialization of unions isn't really possible,
54 // so take the plunge and use constructors.
55 // Hopefully they'll be evaluated at compile time.
57 TypeSpec(TType ttype) : ttype(ttype) {
58 std::memset(fp_prefix, 0, FP_PREFIX_LEN);
61 TypeSpec(TType ttype,
62 const uint8_t* fingerprint,
63 FieldMeta* metas,
64 TypeSpec** specs) :
65 ttype(ttype)
67 std::memcpy(fp_prefix, fingerprint, FP_PREFIX_LEN);
68 tstruct.metas = metas;
69 tstruct.specs = specs;
72 TypeSpec(TType ttype, TypeSpec* subtype1, TypeSpec* subtype2) :
73 ttype(ttype)
75 std::memset(fp_prefix, 0, FP_PREFIX_LEN);
76 tcontainer.subtype1 = subtype1;
77 tcontainer.subtype2 = subtype2;
82 }}}} // facebook::thrift::reflection::local
84 #endif // #ifndef _THRIFT_TREFLECTIONLOCAL_H_