diff --git a/Source/Examples/OpenTK/Test/BlittableValueTypes.cs b/Source/Examples/OpenTK/Test/BlittableValueTypes.cs new file mode 100644 index 00000000..706ead06 --- /dev/null +++ b/Source/Examples/OpenTK/Test/BlittableValueTypes.cs @@ -0,0 +1,93 @@ +#region License +// +// The Open Toolkit Library License +// +// Copyright (c) 2006 - 2008 the Open Toolkit library, except where noted. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights to +// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +// the Software, and to permit persons to whom the Software is furnished to do +// so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// +#endregion + +using System; +using System.Diagnostics; +using System.Reflection; +using OpenTK; + +namespace Examples.Tests +{ + struct Simple { public int Value; } + struct Generic { public T Value; } + enum Enum { First, Second } + struct Complex { public Simple Value; } + struct Complex { public Generic Value; } + struct Complex2 { public Enum Value; } + struct Complex3 { public Class Value; } + struct Complex4 : Interface { public Class Value; } + class Class { public int Value; } + class Class { public T Value; } + interface Interface { } + + [Example("Blittable Value Types", ExampleCategory.OpenTK, "Test", Documentation="BlittableValueTypes")] + public class BlittableValueTypes + { + public static void Main() + { + PrintType(new Simple()); + PrintType(new Generic()); + PrintType(new Generic()); + PrintType(new Complex()); + PrintType(new Complex()); + PrintType(new Complex2()); + PrintType(new Complex3()); + PrintType(new Complex4()); + PrintType(new Class()); + PrintType(new Class()); + } + + static bool CheckBlittable(T type) + { + return BlittableValueType.Check(type); + } + + static int GetStride(T type) + { + return BlittableValueType.StrideOf(type); + } + + static void PrintType(T instance) + { + Type type = typeof(T); + string typename = type.GetFields()[0].FieldType.ToString(); + + Trace.Write(type.IsClass ? "class " : type.IsEnum ? "enum " : type.IsInterface ? "interface " : "struct "); + Trace.Write(type.Name); + if (type.IsGenericType) + Trace.Write(String.Format("<{0}>", type.GetGenericArguments()[0].Name)); + Trace.Write(" { "); + + Trace.Write(typename.Substring(typename.LastIndexOf('.') + 1)); + Trace.Write(" } "); + Trace.Write(CheckBlittable(instance) ? "is blittable " : "is not blittable "); + try { Trace.Write(String.Format("({0} bytes)", GetStride(instance))); } + catch (Exception e) { Trace.Write(String.Format("({0})", e.GetType().Name)); } + Trace.WriteLine(""); + } + } +} diff --git a/Source/OpenTK/BlittableValueType.cs b/Source/OpenTK/BlittableValueType.cs index 06c202d8..8b89a889 100644 --- a/Source/OpenTK/BlittableValueType.cs +++ b/Source/OpenTK/BlittableValueType.cs @@ -1,4 +1,4 @@ -#region License +#region License // // The Open Toolkit Library License // @@ -43,11 +43,12 @@ namespace OpenTK /// A blittable value type is a struct that only references other value types recursively, /// which allows it to be passed to unmanaged code directly. /// - public static class BlittableValueType where T : struct + public static class BlittableValueType { #region Fields static readonly Type Type; + static readonly int stride; #endregion @@ -56,6 +57,12 @@ namespace OpenTK static BlittableValueType() { Type = typeof(T); + if (Type.IsValueType) + { + // Does this support generic types? On Mono 2.4.3 it does + // http://msdn.microsoft.com/en-us/library/5s4920fa.aspx + stride = Marshal.SizeOf(typeof(T)); + } } #endregion @@ -65,7 +72,7 @@ namespace OpenTK /// /// Gets the size of the type in bytes. /// - public static readonly int Stride = Marshal.SizeOf(typeof(T)); + public static int Stride { get { return stride; } } #region Check @@ -103,6 +110,9 @@ namespace OpenTK if (type.IsPrimitive) return true; + if (!type.IsValueType) + return false; + FieldInfo[] fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); Debug.Indent(); foreach (FieldInfo field in fields) @@ -151,7 +161,7 @@ namespace OpenTK /// Checks whether type is a blittable value type. /// /// An instance of the type to check. - public static bool Check(T type) where T : struct + public static bool Check(T type) { return BlittableValueType.Check(); } @@ -160,7 +170,7 @@ namespace OpenTK /// Checks whether type is a blittable value type. /// /// An instance of the type to check. - public static bool Check(T[] type) where T : struct + public static bool Check(T[] type) { return BlittableValueType.Check(); } @@ -169,7 +179,7 @@ namespace OpenTK /// Checks whether type is a blittable value type. /// /// An instance of the type to check. - public static bool Check(T[,] type) where T : struct + public static bool Check(T[,] type) { return BlittableValueType.Check(); } @@ -178,7 +188,7 @@ namespace OpenTK /// Checks whether type is a blittable value type. /// /// An instance of the type to check. - public static bool Check(T[, ,] type) where T : struct + public static bool Check(T[, ,] type) { return BlittableValueType.Check(); } @@ -188,14 +198,14 @@ namespace OpenTK /// /// An instance of the type to check. [CLSCompliant(false)] - public static bool Check(T[][] type) where T : struct + public static bool Check(T[][] type) { return BlittableValueType.Check(); } #endregion - #region From + #region StrideOf /// /// Returns the size of the specified value type in bytes. @@ -205,7 +215,6 @@ namespace OpenTK /// An integer, specifying the size of the type in bytes. /// Occurs when type is not blittable. public static int StrideOf(T type) - where T : struct { if (!Check(type)) throw new ArgumentException("type"); @@ -221,7 +230,6 @@ namespace OpenTK /// An integer, specifying the size of the type in bytes. /// Occurs when type is not blittable. public static int StrideOf(T[] type) - where T : struct { if (!Check(type)) throw new ArgumentException("type"); @@ -237,7 +245,6 @@ namespace OpenTK /// An integer, specifying the size of the type in bytes. /// Occurs when type is not blittable. public static int StrideOf(T[,] type) - where T : struct { if (!Check(type)) throw new ArgumentException("type"); @@ -253,7 +260,6 @@ namespace OpenTK /// An integer, specifying the size of the type in bytes. /// Occurs when type is not blittable. public static int StrideOf(T[, ,] type) - where T : struct { if (!Check(type)) throw new ArgumentException("type");