blob: 2fc53c8827c88f17b87a55f46cad8bba8ddeb25b [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>
Roger Meier49ff8b12012-04-13 09:12:31 +000025#include <thrift/protocol/TProtocol.h>
David Reissd779cbe2007-08-31 01:42:55 +000026
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
Konrad Grochowski16a23a62014-11-13 15:33:38 +010034namespace apache {
35namespace thrift {
36namespace reflection {
37namespace local {
David Reissd779cbe2007-08-31 01:42:55 +000038
T Jake Lucianib5e62212009-01-31 22:36:20 +000039using apache::thrift::protocol::TType;
David Reissd779cbe2007-08-31 01:42:55 +000040
David Reissce161a92007-09-11 22:09:42 +000041// We include this many bytes of the structure's fingerprint when serializing
42// a top-level structure. Long enough to make collisions unlikely, short
43// enough to not significantly affect the amount of memory used.
44const int FP_PREFIX_LEN = 4;
David Reissd779cbe2007-08-31 01:42:55 +000045
David Reiss47557bc2007-09-04 21:31:04 +000046struct FieldMeta {
David Reiss4e7530d2007-09-04 21:49:53 +000047 int16_t tag;
David Reiss47557bc2007-09-04 21:31:04 +000048 bool is_optional;
49};
50
David Reissd779cbe2007-08-31 01:42:55 +000051struct TypeSpec {
David Reissce161a92007-09-11 22:09:42 +000052 TType ttype;
Konrad Grochowski16a23a62014-11-13 15:33:38 +010053 uint8_t fp_prefix[FP_PREFIX_LEN];
David Reissce161a92007-09-11 22:09:42 +000054
David Reissd779cbe2007-08-31 01:42:55 +000055 // Use an anonymous union here so we can fit two TypeSpecs in one cache line.
56 union {
57 struct {
58 // Use parallel arrays here for denser packing (of the arrays).
David Reiss47557bc2007-09-04 21:31:04 +000059 FieldMeta* metas;
David Reissd779cbe2007-08-31 01:42:55 +000060 TypeSpec** specs;
David Reissd779cbe2007-08-31 01:42:55 +000061 } tstruct;
62 struct {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010063 TypeSpec* subtype1;
64 TypeSpec* subtype2;
David Reissd779cbe2007-08-31 01:42:55 +000065 } tcontainer;
66 };
67
David Reissd779cbe2007-08-31 01:42:55 +000068 // Static initialization of unions isn't really possible,
69 // so take the plunge and use constructors.
70 // Hopefully they'll be evaluated at compile time.
71
Konrad Grochowski16a23a62014-11-13 15:33:38 +010072 TypeSpec(TType ttype) : ttype(ttype) { std::memset(fp_prefix, 0, FP_PREFIX_LEN); }
David Reissd779cbe2007-08-31 01:42:55 +000073
Konrad Grochowski16a23a62014-11-13 15:33:38 +010074 TypeSpec(TType ttype, const uint8_t* fingerprint, FieldMeta* metas, TypeSpec** specs)
75 : ttype(ttype) {
David Reissce161a92007-09-11 22:09:42 +000076 std::memcpy(fp_prefix, fingerprint, FP_PREFIX_LEN);
David Reiss47557bc2007-09-04 21:31:04 +000077 tstruct.metas = metas;
David Reissd779cbe2007-08-31 01:42:55 +000078 tstruct.specs = specs;
79 }
80
Konrad Grochowski16a23a62014-11-13 15:33:38 +010081 TypeSpec(TType ttype, TypeSpec* subtype1, TypeSpec* subtype2) : ttype(ttype) {
David Reissce161a92007-09-11 22:09:42 +000082 std::memset(fp_prefix, 0, FP_PREFIX_LEN);
David Reissd779cbe2007-08-31 01:42:55 +000083 tcontainer.subtype1 = subtype1;
84 tcontainer.subtype2 = subtype2;
85 }
David Reissd779cbe2007-08-31 01:42:55 +000086};
Konrad Grochowski16a23a62014-11-13 15:33:38 +010087}
88}
89}
90} // apache::thrift::reflection::local
David Reissd779cbe2007-08-31 01:42:55 +000091
92#endif // #ifndef _THRIFT_TREFLECTIONLOCAL_H_