From 4c73c298780c98577be2b433e7be36ba09076748 Mon Sep 17 00:00:00 2001 From: Alex Corrado Date: Wed, 18 May 2011 15:47:56 -0400 Subject: [PATCH] Make certain CppTypeInfo properties read-only --- src/Mono.VisualC.Interop/CppTypeInfo.cs | 54 ++++++++++++++++--------- 1 file changed, 34 insertions(+), 20 deletions(-) diff --git a/src/Mono.VisualC.Interop/CppTypeInfo.cs b/src/Mono.VisualC.Interop/CppTypeInfo.cs index fecfe4ae..91957139 100644 --- a/src/Mono.VisualC.Interop/CppTypeInfo.cs +++ b/src/Mono.VisualC.Interop/CppTypeInfo.cs @@ -30,6 +30,7 @@ using System; using System.Linq; using System.Reflection; using System.Collections.Generic; +using System.Collections.ObjectModel; using System.Runtime.InteropServices; @@ -45,11 +46,17 @@ namespace Mono.VisualC.Interop { public CppAbi Abi { get; private set; } public Type NativeLayout {get; private set; } - public IList VirtualMethods { get; private set; } - public LazyGeneratedList VTableDelegateTypes { get; private set; } - public LazyGeneratedList VTableOverrides { get; private set; } + public IList VirtualMethods { get; private set; } // read only version + protected List virtual_methods; - public IList BaseClasses { get; private set; } + public IList VTableDelegateTypes { get; private set; } // read only version + protected LazyGeneratedList vt_delegate_types; + + public IList VTableOverrides { get; private set; } // read only version + protected LazyGeneratedList vt_overrides; + + public IList BaseClasses { get; private set; } // read only version + protected List base_classes; // returns the number of vtable slots reserved for the // base class(es) @@ -67,11 +74,18 @@ namespace Mono.VisualC.Interop { Abi = abi; NativeLayout = nativeLayout; - VirtualMethods = new List (virtualMethods); - VTableDelegateTypes = new LazyGeneratedList (VirtualMethods.Count, i => DelegateTypeCache.GetDelegateType (VirtualMethods [i])); - VTableOverrides = new LazyGeneratedList (VirtualMethods.Count, i => Abi.GetManagedOverrideTrampoline (this, i)); + virtual_methods = new List (virtualMethods); + VirtualMethods = new ReadOnlyCollection (virtual_methods); + + vt_delegate_types = new LazyGeneratedList (virtual_methods.Count, i => DelegateTypeCache.GetDelegateType (virtual_methods [i])); + VTableDelegateTypes = new ReadOnlyCollection (vt_delegate_types); + + vt_overrides = new LazyGeneratedList (virtual_methods.Count, i => Abi.GetManagedOverrideTrampoline (this, i)); + VTableOverrides = new ReadOnlyCollection (vt_overrides); + + base_classes = new List (); + BaseClasses = new ReadOnlyCollection (base_classes); - BaseClasses = new List (); BaseVTableSlots = 0; TypeComplete = false; @@ -93,19 +107,19 @@ namespace Mono.VisualC.Interop { if (TypeComplete) return; - BaseClasses.Add (baseType); + base_classes.Add (baseType); if (!addVTablePointer) { // If we're not adding a vtptr, then all this base class's virtual methods go in primary vtable // Skew the offsets of this subclass's vmethods to account for the new base vmethods. - int newVirtualMethodCount = baseType.VirtualMethods.Count; + int newVirtualMethodCount = baseType.virtual_methods.Count; for (int i = 0; i < newVirtualMethodCount; i++) - VirtualMethods.Insert (BaseVTableSlots + i, baseType.VirtualMethods [i]); + virtual_methods.Insert (BaseVTableSlots + i, baseType.virtual_methods [i]); BaseVTableSlots += newVirtualMethodCount; - VTableDelegateTypes.PrependLast (baseType.VTableDelegateTypes); - VTableOverrides.PrependLast (baseType.VTableOverrides); + vt_delegate_types.PrependLast (baseType.vt_delegate_types); + vt_overrides.PrependLast (baseType.vt_overrides); } field_offset_padding_without_vtptr += baseType.native_size + @@ -115,7 +129,7 @@ namespace Mono.VisualC.Interop { public int CountBases (Func predicate) { int count = 0; - foreach (var baseClass in BaseClasses) { + foreach (var baseClass in base_classes) { count += baseClass.CountBases (predicate); count += predicate (baseClass)? 1 : 0; } @@ -127,7 +141,7 @@ namespace Mono.VisualC.Interop { if (TypeComplete) return; - foreach (var baseClass in BaseClasses) + foreach (var baseClass in base_classes) baseClass.CompleteType (); TypeComplete = true; @@ -139,14 +153,14 @@ namespace Mono.VisualC.Interop { // check that any virtual methods overridden in a subclass are only included once var vsignatures = new HashSet (); - for (int i = 0; i < VirtualMethods.Count; i++) { - var sig = VirtualMethods [i]; + for (int i = 0; i < virtual_methods.Count; i++) { + var sig = virtual_methods [i]; if (sig == null) continue; if (vsignatures.Contains (sig)) { if (pred (sig)) - VirtualMethods.RemoveAt (i--); + virtual_methods.RemoveAt (i--); } else { vsignatures.Add (sig); } @@ -163,7 +177,7 @@ namespace Mono.VisualC.Interop { public virtual VTable VTable { get { CompleteType (); - if (!VirtualMethods.Any ()) + if (!virtual_methods.Any ()) return null; if (lazy_vtable == null) @@ -183,7 +197,7 @@ namespace Mono.VisualC.Interop { // the extra padding to allocate at the top of the class before the fields begin // (by default, just the vtable pointer) public virtual int FieldOffsetPadding { - get { return field_offset_padding_without_vtptr + (VirtualMethods.Any ()? Marshal.SizeOf (typeof (IntPtr)) : 0); } + get { return field_offset_padding_without_vtptr + (virtual_methods.Any ()? Marshal.SizeOf (typeof (IntPtr)) : 0); } } // the padding in the data pointed to by the vtable pointer before the list of function pointers starts