csharptest | d9c59e6 | 2010-11-04 19:36:28 -0500 | [diff] [blame] | 1 | using System; |
csharptest | 804b6d8 | 2010-11-07 10:49:33 -0600 | [diff] [blame^] | 2 | using Google.ProtocolBuffers.Descriptors; |
csharptest | d9c59e6 | 2010-11-04 19:36:28 -0500 | [diff] [blame] | 3 | |
| 4 | namespace Google.ProtocolBuffers { |
| 5 | |
| 6 | public interface IGeneratedExtensionLite { |
| 7 | int Number { get; } |
| 8 | object ContainingType { get; } |
| 9 | IMessageLite MessageDefaultInstance { get; } |
| 10 | } |
csharptest | 804b6d8 | 2010-11-07 10:49:33 -0600 | [diff] [blame^] | 11 | |
| 12 | public class ExtensionDescriptorLite { |
| 13 | private readonly EnumLiteMap enumTypeMap; |
| 14 | private readonly int number; |
| 15 | private readonly FieldType type; |
| 16 | private readonly bool isRepeated; |
| 17 | private readonly bool isPacked; |
| 18 | |
| 19 | public ExtensionDescriptorLite(EnumLiteMap enumTypeMap, int number, FieldType type, bool isRepeated, bool isPacked) { |
| 20 | this.enumTypeMap = enumTypeMap; |
| 21 | this.number = number; |
| 22 | this.type = type; |
| 23 | this.isRepeated = isRepeated; |
| 24 | this.isPacked = isPacked; |
| 25 | } |
| 26 | |
| 27 | public int Number { |
| 28 | get { return number; } |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | public class EnumLiteMap { } |
| 33 | |
| 34 | public class GeneratedExtensionLite<TContainingType, TExtensionType> : IGeneratedExtensionLite |
| 35 | where TContainingType : IMessageLite { |
| 36 | |
| 37 | private readonly TContainingType containingTypeDefaultInstance; |
| 38 | private readonly TExtensionType defaultValue; |
| 39 | private readonly IMessageLite messageDefaultInstance; |
| 40 | private readonly ExtensionDescriptorLite descriptor; |
| 41 | |
| 42 | // We can't always initialize a GeneratedExtension when we first construct |
| 43 | // it due to initialization order difficulties (namely, the default |
| 44 | // instances may not have been constructed yet). So, we construct an |
| 45 | // uninitialized GeneratedExtension once, then call internalInit() on it |
| 46 | // later. Generated code will always call internalInit() on all extensions |
| 47 | // as part of the static initialization code, and internalInit() throws an |
| 48 | // exception if called more than once, so this method is useless to users. |
| 49 | protected GeneratedExtensionLite( |
| 50 | TContainingType containingTypeDefaultInstance, |
| 51 | TExtensionType defaultValue, |
| 52 | IMessageLite messageDefaultInstance, |
| 53 | ExtensionDescriptorLite descriptor) { |
| 54 | this.containingTypeDefaultInstance = containingTypeDefaultInstance; |
| 55 | this.messageDefaultInstance = messageDefaultInstance; |
| 56 | this.defaultValue = defaultValue; |
| 57 | this.descriptor = descriptor; |
| 58 | } |
| 59 | |
| 60 | /** For use by generated code only. */ |
| 61 | public GeneratedExtensionLite( |
| 62 | TContainingType containingTypeDefaultInstance, |
| 63 | TExtensionType defaultValue, |
| 64 | IMessageLite messageDefaultInstance, |
| 65 | EnumLiteMap enumTypeMap, |
| 66 | int number, |
| 67 | FieldType type) |
| 68 | : this(containingTypeDefaultInstance, defaultValue, messageDefaultInstance, |
| 69 | new ExtensionDescriptorLite(enumTypeMap, number, type, |
| 70 | false /* isRepeated */, false /* isPacked */)) { |
| 71 | } |
| 72 | |
| 73 | /** For use by generated code only. */ |
| 74 | public GeneratedExtensionLite( |
| 75 | TContainingType containingTypeDefaultInstance, |
| 76 | TExtensionType defaultValue, |
| 77 | IMessageLite messageDefaultInstance, |
| 78 | EnumLiteMap enumTypeMap, |
| 79 | int number, |
| 80 | FieldType type, |
| 81 | bool isPacked) |
| 82 | : this(containingTypeDefaultInstance, defaultValue, messageDefaultInstance, |
| 83 | new ExtensionDescriptorLite(enumTypeMap, number, type, |
| 84 | true /* isRepeated */, isPacked)) { |
| 85 | } |
| 86 | |
| 87 | /// <summary> |
| 88 | /// used for the extension registry |
| 89 | /// </summary> |
| 90 | object IGeneratedExtensionLite.ContainingType { |
| 91 | get { return ContainingTypeDefaultInstance; } |
| 92 | } |
| 93 | /** |
| 94 | * Default instance of the type being extended, used to identify that type. |
| 95 | */ |
| 96 | public TContainingType ContainingTypeDefaultInstance { |
| 97 | get { |
| 98 | return containingTypeDefaultInstance; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | /** Get the field number. */ |
| 103 | public int Number { |
| 104 | get { |
| 105 | return descriptor.Number; |
| 106 | } |
| 107 | } |
| 108 | /** |
| 109 | * If the extension is an embedded message, this is the default instance of |
| 110 | * that type. |
| 111 | */ |
| 112 | public IMessageLite MessageDefaultInstance { |
| 113 | get { |
| 114 | return messageDefaultInstance; |
| 115 | } |
| 116 | } |
| 117 | } |
csharptest | d9c59e6 | 2010-11-04 19:36:28 -0500 | [diff] [blame] | 118 | } |