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> |
| 11 | #include <protocol/TProtocol.h> |
| 12 | |
| 13 | namespace facebook { namespace thrift { namespace reflection { namespace local { |
| 14 | |
| 15 | using facebook::thrift::protocol::TType; |
| 16 | |
| 17 | /** |
| 18 | * A local reflection is a representation of a Thrift structure. |
| 19 | * (It is called local because it cannot be serialized by Thrift). |
| 20 | * |
| 21 | * @author David Reiss <dreiss@facebook.com> |
| 22 | */ |
| 23 | |
| 24 | struct TypeSpec { |
| 25 | // Use an anonymous union here so we can fit two TypeSpecs in one cache line. |
| 26 | union { |
| 27 | struct { |
| 28 | // Use parallel arrays here for denser packing (of the arrays). |
| 29 | int16_t* ftags; |
| 30 | TypeSpec** specs; |
| 31 | int n_fields; |
| 32 | } tstruct; |
| 33 | struct { |
| 34 | TypeSpec *subtype1; |
| 35 | TypeSpec *subtype2; |
| 36 | } tcontainer; |
| 37 | }; |
| 38 | |
| 39 | // Put this at the end so the 32-bit enum can be crammed up next to the |
| 40 | // 32-bit int (n_fields). |
| 41 | TType ttype; |
| 42 | |
| 43 | |
| 44 | // Static initialization of unions isn't really possible, |
| 45 | // so take the plunge and use constructors. |
| 46 | // Hopefully they'll be evaluated at compile time. |
| 47 | |
| 48 | TypeSpec(TType ttype) : ttype(ttype) {} |
| 49 | |
| 50 | TypeSpec(TType ttype, int n_fields, int16_t* ftags, TypeSpec** specs) : |
| 51 | ttype(ttype) |
| 52 | { |
| 53 | tstruct.n_fields = n_fields; |
| 54 | tstruct.ftags = ftags; |
| 55 | tstruct.specs = specs; |
| 56 | } |
| 57 | |
| 58 | TypeSpec(TType ttype, TypeSpec* subtype1, TypeSpec* subtype2) : |
| 59 | ttype(ttype) |
| 60 | { |
| 61 | tcontainer.subtype1 = subtype1; |
| 62 | tcontainer.subtype2 = subtype2; |
| 63 | } |
| 64 | |
| 65 | }; |
| 66 | |
| 67 | }}}} // facebook::thrift::reflection::local |
| 68 | |
| 69 | #endif // #ifndef _THRIFT_TREFLECTIONLOCAL_H_ |