David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 1 | /* |
| 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 Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 19 | |
| 20 | #ifndef _THRIFT_TREFLECTIONLOCAL_H_ |
| 21 | #define _THRIFT_TREFLECTIONLOCAL_H_ 1 |
| 22 | |
| 23 | #include <stdint.h> |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 24 | #include <cstring> |
Roger Meier | 49ff8b1 | 2012-04-13 09:12:31 +0000 | [diff] [blame] | 25 | #include <thrift/protocol/TProtocol.h> |
David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 26 | |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 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 | * |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 32 | */ |
| 33 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 34 | namespace apache { |
| 35 | namespace thrift { |
| 36 | namespace reflection { |
| 37 | namespace local { |
David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 38 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 39 | using apache::thrift::protocol::TType; |
David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 40 | |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 41 | // 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. |
| 44 | const int FP_PREFIX_LEN = 4; |
David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 45 | |
David Reiss | 47557bc | 2007-09-04 21:31:04 +0000 | [diff] [blame] | 46 | struct FieldMeta { |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 47 | int16_t tag; |
David Reiss | 47557bc | 2007-09-04 21:31:04 +0000 | [diff] [blame] | 48 | bool is_optional; |
| 49 | }; |
| 50 | |
David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 51 | struct TypeSpec { |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 52 | TType ttype; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 53 | uint8_t fp_prefix[FP_PREFIX_LEN]; |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 54 | |
David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 55 | // 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 Reiss | 47557bc | 2007-09-04 21:31:04 +0000 | [diff] [blame] | 59 | FieldMeta* metas; |
David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 60 | TypeSpec** specs; |
David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 61 | } tstruct; |
| 62 | struct { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 63 | TypeSpec* subtype1; |
| 64 | TypeSpec* subtype2; |
David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 65 | } tcontainer; |
| 66 | }; |
| 67 | |
David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 68 | // 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 Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 72 | TypeSpec(TType ttype) : ttype(ttype) { std::memset(fp_prefix, 0, FP_PREFIX_LEN); } |
David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 73 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 74 | TypeSpec(TType ttype, const uint8_t* fingerprint, FieldMeta* metas, TypeSpec** specs) |
| 75 | : ttype(ttype) { |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 76 | std::memcpy(fp_prefix, fingerprint, FP_PREFIX_LEN); |
David Reiss | 47557bc | 2007-09-04 21:31:04 +0000 | [diff] [blame] | 77 | tstruct.metas = metas; |
David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 78 | tstruct.specs = specs; |
| 79 | } |
| 80 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 81 | TypeSpec(TType ttype, TypeSpec* subtype1, TypeSpec* subtype2) : ttype(ttype) { |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 82 | std::memset(fp_prefix, 0, FP_PREFIX_LEN); |
David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 83 | tcontainer.subtype1 = subtype1; |
| 84 | tcontainer.subtype2 = subtype2; |
| 85 | } |
David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 86 | }; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | } |
| 90 | } // apache::thrift::reflection::local |
David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 91 | |
| 92 | #endif // #ifndef _THRIFT_TREFLECTIONLOCAL_H_ |