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 | * |
| 19 | * @author David Reiss <dreiss@facebook.com> |
| 20 | */ |
| 21 | |
David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 22 | namespace facebook { namespace thrift { namespace reflection { namespace local { |
| 23 | |
| 24 | using facebook::thrift::protocol::TType; |
| 25 | |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 26 | // We include this many bytes of the structure's fingerprint when serializing |
| 27 | // a top-level structure. Long enough to make collisions unlikely, short |
| 28 | // enough to not significantly affect the amount of memory used. |
| 29 | const int FP_PREFIX_LEN = 4; |
David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 30 | |
David Reiss | 47557bc | 2007-09-04 21:31:04 +0000 | [diff] [blame] | 31 | struct FieldMeta { |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 32 | int16_t tag; |
David Reiss | 47557bc | 2007-09-04 21:31:04 +0000 | [diff] [blame] | 33 | bool is_optional; |
| 34 | }; |
| 35 | |
David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 36 | struct TypeSpec { |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 37 | TType ttype; |
| 38 | uint8_t fp_prefix[FP_PREFIX_LEN]; |
| 39 | |
David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 40 | // Use an anonymous union here so we can fit two TypeSpecs in one cache line. |
| 41 | union { |
| 42 | struct { |
| 43 | // Use parallel arrays here for denser packing (of the arrays). |
David Reiss | 47557bc | 2007-09-04 21:31:04 +0000 | [diff] [blame] | 44 | FieldMeta* metas; |
David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 45 | TypeSpec** specs; |
David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 46 | } tstruct; |
| 47 | struct { |
| 48 | TypeSpec *subtype1; |
| 49 | TypeSpec *subtype2; |
| 50 | } tcontainer; |
| 51 | }; |
| 52 | |
David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 53 | // Static initialization of unions isn't really possible, |
| 54 | // so take the plunge and use constructors. |
| 55 | // Hopefully they'll be evaluated at compile time. |
| 56 | |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 57 | TypeSpec(TType ttype) : ttype(ttype) { |
| 58 | std::memset(fp_prefix, 0, FP_PREFIX_LEN); |
| 59 | } |
David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 60 | |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 61 | TypeSpec(TType ttype, |
David Reiss | 2b9ddab | 2007-10-17 03:39:55 +0000 | [diff] [blame^] | 62 | const uint8_t* fingerprint, |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 63 | FieldMeta* metas, |
| 64 | TypeSpec** specs) : |
David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 65 | ttype(ttype) |
| 66 | { |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 67 | std::memcpy(fp_prefix, fingerprint, FP_PREFIX_LEN); |
David Reiss | 47557bc | 2007-09-04 21:31:04 +0000 | [diff] [blame] | 68 | tstruct.metas = metas; |
David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 69 | tstruct.specs = specs; |
| 70 | } |
| 71 | |
| 72 | TypeSpec(TType ttype, TypeSpec* subtype1, TypeSpec* subtype2) : |
| 73 | ttype(ttype) |
| 74 | { |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 75 | std::memset(fp_prefix, 0, FP_PREFIX_LEN); |
David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 76 | tcontainer.subtype1 = subtype1; |
| 77 | tcontainer.subtype2 = subtype2; |
| 78 | } |
| 79 | |
| 80 | }; |
| 81 | |
| 82 | }}}} // facebook::thrift::reflection::local |
| 83 | |
| 84 | #endif // #ifndef _THRIFT_TREFLECTIONLOCAL_H_ |