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> |
David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 25 | #include <protocol/TProtocol.h> |
| 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 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 34 | namespace apache { namespace thrift { namespace reflection { namespace local { |
David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 35 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 36 | using apache::thrift::protocol::TType; |
David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 37 | |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 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. |
| 41 | const int FP_PREFIX_LEN = 4; |
David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 42 | |
David Reiss | 47557bc | 2007-09-04 21:31:04 +0000 | [diff] [blame] | 43 | struct FieldMeta { |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 44 | int16_t tag; |
David Reiss | 47557bc | 2007-09-04 21:31:04 +0000 | [diff] [blame] | 45 | bool is_optional; |
| 46 | }; |
| 47 | |
David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 48 | struct TypeSpec { |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 49 | TType ttype; |
| 50 | uint8_t fp_prefix[FP_PREFIX_LEN]; |
| 51 | |
David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 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). |
David Reiss | 47557bc | 2007-09-04 21:31:04 +0000 | [diff] [blame] | 56 | FieldMeta* metas; |
David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 57 | TypeSpec** specs; |
David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 58 | } tstruct; |
| 59 | struct { |
| 60 | TypeSpec *subtype1; |
| 61 | TypeSpec *subtype2; |
| 62 | } tcontainer; |
| 63 | }; |
| 64 | |
David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 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 | |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 69 | TypeSpec(TType ttype) : ttype(ttype) { |
| 70 | std::memset(fp_prefix, 0, FP_PREFIX_LEN); |
| 71 | } |
David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 72 | |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 73 | TypeSpec(TType ttype, |
David Reiss | 2b9ddab | 2007-10-17 03:39:55 +0000 | [diff] [blame] | 74 | const uint8_t* fingerprint, |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 75 | FieldMeta* metas, |
| 76 | TypeSpec** specs) : |
David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 77 | ttype(ttype) |
| 78 | { |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 79 | std::memcpy(fp_prefix, fingerprint, FP_PREFIX_LEN); |
David Reiss | 47557bc | 2007-09-04 21:31:04 +0000 | [diff] [blame] | 80 | tstruct.metas = metas; |
David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 81 | tstruct.specs = specs; |
| 82 | } |
| 83 | |
| 84 | TypeSpec(TType ttype, TypeSpec* subtype1, TypeSpec* subtype2) : |
| 85 | ttype(ttype) |
| 86 | { |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 87 | std::memset(fp_prefix, 0, FP_PREFIX_LEN); |
David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 88 | tcontainer.subtype1 = subtype1; |
| 89 | tcontainer.subtype2 = subtype2; |
| 90 | } |
| 91 | |
| 92 | }; |
| 93 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 94 | }}}} // apache::thrift::reflection::local |
David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 95 | |
| 96 | #endif // #ifndef _THRIFT_TREFLECTIONLOCAL_H_ |