David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 1 | // Copyright (c) 2006- Facebook |
| 2 | // Distributed under the Thrift Software License |
| 3 | // |
| 4 | // See accompanying file LICENSE or visit the Thrift site at: |
| 5 | // http://developers.facebook.com/thrift/ |
| 6 | |
| 7 | #ifndef _THRIFT_TREFLECTIONLOCAL_H_ |
| 8 | #define _THRIFT_TREFLECTIONLOCAL_H_ 1 |
| 9 | |
| 10 | #include <stdint.h> |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 11 | #include <cstring> |
David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 12 | #include <protocol/TProtocol.h> |
| 13 | |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 14 | /** |
| 15 | * Local Reflection is a blanket term referring to the the structure |
| 16 | * and generation of this particular representation of Thrift types. |
| 17 | * (It is called local because it cannot be serialized by Thrift). |
| 18 | * |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 19 | */ |
| 20 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 21 | namespace apache { namespace thrift { namespace reflection { namespace local { |
David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 22 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 23 | using apache::thrift::protocol::TType; |
David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 24 | |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 25 | // We include this many bytes of the structure's fingerprint when serializing |
| 26 | // a top-level structure. Long enough to make collisions unlikely, short |
| 27 | // enough to not significantly affect the amount of memory used. |
| 28 | const int FP_PREFIX_LEN = 4; |
David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 29 | |
David Reiss | 47557bc | 2007-09-04 21:31:04 +0000 | [diff] [blame] | 30 | struct FieldMeta { |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 31 | int16_t tag; |
David Reiss | 47557bc | 2007-09-04 21:31:04 +0000 | [diff] [blame] | 32 | bool is_optional; |
| 33 | }; |
| 34 | |
David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 35 | struct TypeSpec { |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 36 | TType ttype; |
| 37 | uint8_t fp_prefix[FP_PREFIX_LEN]; |
| 38 | |
David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 39 | // Use an anonymous union here so we can fit two TypeSpecs in one cache line. |
| 40 | union { |
| 41 | struct { |
| 42 | // Use parallel arrays here for denser packing (of the arrays). |
David Reiss | 47557bc | 2007-09-04 21:31:04 +0000 | [diff] [blame] | 43 | FieldMeta* metas; |
David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 44 | TypeSpec** specs; |
David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 45 | } tstruct; |
| 46 | struct { |
| 47 | TypeSpec *subtype1; |
| 48 | TypeSpec *subtype2; |
| 49 | } tcontainer; |
| 50 | }; |
| 51 | |
David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 52 | // Static initialization of unions isn't really possible, |
| 53 | // so take the plunge and use constructors. |
| 54 | // Hopefully they'll be evaluated at compile time. |
| 55 | |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 56 | TypeSpec(TType ttype) : ttype(ttype) { |
| 57 | std::memset(fp_prefix, 0, FP_PREFIX_LEN); |
| 58 | } |
David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 59 | |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 60 | TypeSpec(TType ttype, |
David Reiss | 2b9ddab | 2007-10-17 03:39:55 +0000 | [diff] [blame] | 61 | const uint8_t* fingerprint, |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 62 | FieldMeta* metas, |
| 63 | TypeSpec** specs) : |
David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 64 | ttype(ttype) |
| 65 | { |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 66 | std::memcpy(fp_prefix, fingerprint, FP_PREFIX_LEN); |
David Reiss | 47557bc | 2007-09-04 21:31:04 +0000 | [diff] [blame] | 67 | tstruct.metas = metas; |
David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 68 | tstruct.specs = specs; |
| 69 | } |
| 70 | |
| 71 | TypeSpec(TType ttype, TypeSpec* subtype1, TypeSpec* subtype2) : |
| 72 | ttype(ttype) |
| 73 | { |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 74 | std::memset(fp_prefix, 0, FP_PREFIX_LEN); |
David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 75 | tcontainer.subtype1 = subtype1; |
| 76 | tcontainer.subtype2 = subtype2; |
| 77 | } |
| 78 | |
| 79 | }; |
| 80 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 81 | }}}} // apache::thrift::reflection::local |
David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 82 | |
| 83 | #endif // #ifndef _THRIFT_TREFLECTIONLOCAL_H_ |