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 | |
David Reiss | 47557bc | 2007-09-04 21:31:04 +0000 | [diff] [blame] | 24 | struct FieldMeta { |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame^] | 25 | int16_t tag; |
David Reiss | 47557bc | 2007-09-04 21:31:04 +0000 | [diff] [blame] | 26 | bool is_optional; |
| 27 | }; |
| 28 | |
David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 29 | struct TypeSpec { |
| 30 | // Use an anonymous union here so we can fit two TypeSpecs in one cache line. |
| 31 | union { |
| 32 | struct { |
| 33 | // Use parallel arrays here for denser packing (of the arrays). |
David Reiss | 47557bc | 2007-09-04 21:31:04 +0000 | [diff] [blame] | 34 | FieldMeta* metas; |
David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 35 | TypeSpec** specs; |
David Reiss | 47557bc | 2007-09-04 21:31:04 +0000 | [diff] [blame] | 36 | // n_fields is only used for debugging, but it should only add |
| 37 | // a minimimal amount to the effective size of this structure |
| 38 | // because of alignment restrictions. |
David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 39 | int n_fields; |
| 40 | } tstruct; |
| 41 | struct { |
| 42 | TypeSpec *subtype1; |
| 43 | TypeSpec *subtype2; |
| 44 | } tcontainer; |
| 45 | }; |
| 46 | |
| 47 | // Put this at the end so the 32-bit enum can be crammed up next to the |
| 48 | // 32-bit int (n_fields). |
| 49 | TType ttype; |
| 50 | |
| 51 | |
| 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 | |
| 56 | TypeSpec(TType ttype) : ttype(ttype) {} |
| 57 | |
David Reiss | 47557bc | 2007-09-04 21:31:04 +0000 | [diff] [blame] | 58 | TypeSpec(TType ttype, int n_fields, FieldMeta* metas, TypeSpec** specs) : |
David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 59 | ttype(ttype) |
| 60 | { |
| 61 | tstruct.n_fields = n_fields; |
David Reiss | 47557bc | 2007-09-04 21:31:04 +0000 | [diff] [blame] | 62 | tstruct.metas = metas; |
David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 63 | tstruct.specs = specs; |
| 64 | } |
| 65 | |
| 66 | TypeSpec(TType ttype, TypeSpec* subtype1, TypeSpec* subtype2) : |
| 67 | ttype(ttype) |
| 68 | { |
| 69 | tcontainer.subtype1 = subtype1; |
| 70 | tcontainer.subtype2 = subtype2; |
| 71 | } |
| 72 | |
| 73 | }; |
| 74 | |
| 75 | }}}} // facebook::thrift::reflection::local |
| 76 | |
| 77 | #endif // #ifndef _THRIFT_TREFLECTIONLOCAL_H_ |