blob: e83e47530b14d9cff43e4892539c4fd8eb792a8a [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +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 */
David Reissd779cbe2007-08-31 01:42:55 +000019
20#ifndef _THRIFT_TREFLECTIONLOCAL_H_
21#define _THRIFT_TREFLECTIONLOCAL_H_ 1
22
23#include <stdint.h>
David Reissce161a92007-09-11 22:09:42 +000024#include <cstring>
David Reissd779cbe2007-08-31 01:42:55 +000025#include <protocol/TProtocol.h>
26
David Reissce161a92007-09-11 22:09:42 +000027/**
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 *
David Reissce161a92007-09-11 22:09:42 +000032 */
33
T Jake Lucianib5e62212009-01-31 22:36:20 +000034namespace apache { namespace thrift { namespace reflection { namespace local {
David Reissd779cbe2007-08-31 01:42:55 +000035
T Jake Lucianib5e62212009-01-31 22:36:20 +000036using apache::thrift::protocol::TType;
David Reissd779cbe2007-08-31 01:42:55 +000037
David Reissce161a92007-09-11 22:09:42 +000038// 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;
David Reissd779cbe2007-08-31 01:42:55 +000042
David Reiss47557bc2007-09-04 21:31:04 +000043struct FieldMeta {
David Reiss4e7530d2007-09-04 21:49:53 +000044 int16_t tag;
David Reiss47557bc2007-09-04 21:31:04 +000045 bool is_optional;
46};
47
David Reissd779cbe2007-08-31 01:42:55 +000048struct TypeSpec {
David Reissce161a92007-09-11 22:09:42 +000049 TType ttype;
50 uint8_t fp_prefix[FP_PREFIX_LEN];
51
David Reissd779cbe2007-08-31 01:42:55 +000052 // 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).
David Reiss47557bc2007-09-04 21:31:04 +000056 FieldMeta* metas;
David Reissd779cbe2007-08-31 01:42:55 +000057 TypeSpec** specs;
David Reissd779cbe2007-08-31 01:42:55 +000058 } tstruct;
59 struct {
60 TypeSpec *subtype1;
61 TypeSpec *subtype2;
62 } tcontainer;
63 };
64
David Reissd779cbe2007-08-31 01:42:55 +000065 // 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
David Reissce161a92007-09-11 22:09:42 +000069 TypeSpec(TType ttype) : ttype(ttype) {
70 std::memset(fp_prefix, 0, FP_PREFIX_LEN);
71 }
David Reissd779cbe2007-08-31 01:42:55 +000072
David Reissce161a92007-09-11 22:09:42 +000073 TypeSpec(TType ttype,
David Reiss2b9ddab2007-10-17 03:39:55 +000074 const uint8_t* fingerprint,
David Reissce161a92007-09-11 22:09:42 +000075 FieldMeta* metas,
76 TypeSpec** specs) :
David Reissd779cbe2007-08-31 01:42:55 +000077 ttype(ttype)
78 {
David Reissce161a92007-09-11 22:09:42 +000079 std::memcpy(fp_prefix, fingerprint, FP_PREFIX_LEN);
David Reiss47557bc2007-09-04 21:31:04 +000080 tstruct.metas = metas;
David Reissd779cbe2007-08-31 01:42:55 +000081 tstruct.specs = specs;
82 }
83
84 TypeSpec(TType ttype, TypeSpec* subtype1, TypeSpec* subtype2) :
85 ttype(ttype)
86 {
David Reissce161a92007-09-11 22:09:42 +000087 std::memset(fp_prefix, 0, FP_PREFIX_LEN);
David Reissd779cbe2007-08-31 01:42:55 +000088 tcontainer.subtype1 = subtype1;
89 tcontainer.subtype2 = subtype2;
90 }
91
92};
93
T Jake Lucianib5e62212009-01-31 22:36:20 +000094}}}} // apache::thrift::reflection::local
David Reissd779cbe2007-08-31 01:42:55 +000095
96#endif // #ifndef _THRIFT_TREFLECTIONLOCAL_H_