blob: e83e47530b14d9cff43e4892539c4fd8eb792a8a [file] [log] [blame]
Gavin McDonald0b75e1a2010-10-28 02:12:01 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20#ifndef _THRIFT_TREFLECTIONLOCAL_H_
21#define _THRIFT_TREFLECTIONLOCAL_H_ 1
22
23#include <stdint.h>
24#include <cstring>
25#include <protocol/TProtocol.h>
26
27/**
28 * Local Reflection is a blanket term referring to the the structure
29 * and generation of this particular representation of Thrift types.
30 * (It is called local because it cannot be serialized by Thrift).
31 *
32 */
33
34namespace apache { namespace thrift { namespace reflection { namespace local {
35
36using apache::thrift::protocol::TType;
37
38// We include this many bytes of the structure's fingerprint when serializing
39// a top-level structure. Long enough to make collisions unlikely, short
40// enough to not significantly affect the amount of memory used.
41const int FP_PREFIX_LEN = 4;
42
43struct FieldMeta {
44 int16_t tag;
45 bool is_optional;
46};
47
48struct TypeSpec {
49 TType ttype;
50 uint8_t fp_prefix[FP_PREFIX_LEN];
51
52 // Use an anonymous union here so we can fit two TypeSpecs in one cache line.
53 union {
54 struct {
55 // Use parallel arrays here for denser packing (of the arrays).
56 FieldMeta* metas;
57 TypeSpec** specs;
58 } tstruct;
59 struct {
60 TypeSpec *subtype1;
61 TypeSpec *subtype2;
62 } tcontainer;
63 };
64
65 // Static initialization of unions isn't really possible,
66 // so take the plunge and use constructors.
67 // Hopefully they'll be evaluated at compile time.
68
69 TypeSpec(TType ttype) : ttype(ttype) {
70 std::memset(fp_prefix, 0, FP_PREFIX_LEN);
71 }
72
73 TypeSpec(TType ttype,
74 const uint8_t* fingerprint,
75 FieldMeta* metas,
76 TypeSpec** specs) :
77 ttype(ttype)
78 {
79 std::memcpy(fp_prefix, fingerprint, FP_PREFIX_LEN);
80 tstruct.metas = metas;
81 tstruct.specs = specs;
82 }
83
84 TypeSpec(TType ttype, TypeSpec* subtype1, TypeSpec* subtype2) :
85 ttype(ttype)
86 {
87 std::memset(fp_prefix, 0, FP_PREFIX_LEN);
88 tcontainer.subtype1 = subtype1;
89 tcontainer.subtype2 = subtype2;
90 }
91
92};
93
94}}}} // apache::thrift::reflection::local
95
96#endif // #ifndef _THRIFT_TREFLECTIONLOCAL_H_