mirror of
https://github.com/Ryujinx/Opentk.git
synced 2024-12-23 12:05:33 +00:00
* Source/OpenTK/BlittableValueType.cs: Removed struct constraint. Sometimes consumers of this class may not be able to use struct generic constraints - the class is now usable in this case.
* Source/Examples/OpenTK/Test/BlittableValueTypes.cs: Test BlittableValueType class using several different types.
This commit is contained in:
parent
1f9c9eac44
commit
5991bee09f
93
Source/Examples/OpenTK/Test/BlittableValueTypes.cs
Normal file
93
Source/Examples/OpenTK/Test/BlittableValueTypes.cs
Normal file
|
@ -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<T> { public T Value; }
|
||||
enum Enum { First, Second }
|
||||
struct Complex { public Simple Value; }
|
||||
struct Complex<T> { public Generic<T> Value; }
|
||||
struct Complex2 { public Enum Value; }
|
||||
struct Complex3 { public Class Value; }
|
||||
struct Complex4 : Interface { public Class Value; }
|
||||
class Class { public int Value; }
|
||||
class Class<T> { 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<Simple>());
|
||||
PrintType(new Generic<Enum>());
|
||||
PrintType(new Complex());
|
||||
PrintType(new Complex<Enum>());
|
||||
PrintType(new Complex2());
|
||||
PrintType(new Complex3());
|
||||
PrintType(new Complex4());
|
||||
PrintType(new Class());
|
||||
PrintType(new Class<Simple>());
|
||||
}
|
||||
|
||||
static bool CheckBlittable<T>(T type)
|
||||
{
|
||||
return BlittableValueType.Check(type);
|
||||
}
|
||||
|
||||
static int GetStride<T>(T type)
|
||||
{
|
||||
return BlittableValueType.StrideOf(type);
|
||||
}
|
||||
|
||||
static void PrintType<T>(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("");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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.
|
||||
/// </remarks>
|
||||
public static class BlittableValueType<T> where T : struct
|
||||
public static class BlittableValueType<T>
|
||||
{
|
||||
#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
|
|||
/// <summary>
|
||||
/// Gets the size of the type in bytes.
|
||||
/// </summary>
|
||||
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.
|
||||
/// </summary>
|
||||
/// <param name="type">An instance of the type to check.</param>
|
||||
public static bool Check<T>(T type) where T : struct
|
||||
public static bool Check<T>(T type)
|
||||
{
|
||||
return BlittableValueType<T>.Check();
|
||||
}
|
||||
|
@ -160,7 +170,7 @@ namespace OpenTK
|
|||
/// Checks whether type is a blittable value type.
|
||||
/// </summary>
|
||||
/// <param name="type">An instance of the type to check.</param>
|
||||
public static bool Check<T>(T[] type) where T : struct
|
||||
public static bool Check<T>(T[] type)
|
||||
{
|
||||
return BlittableValueType<T>.Check();
|
||||
}
|
||||
|
@ -169,7 +179,7 @@ namespace OpenTK
|
|||
/// Checks whether type is a blittable value type.
|
||||
/// </summary>
|
||||
/// <param name="type">An instance of the type to check.</param>
|
||||
public static bool Check<T>(T[,] type) where T : struct
|
||||
public static bool Check<T>(T[,] type)
|
||||
{
|
||||
return BlittableValueType<T>.Check();
|
||||
}
|
||||
|
@ -178,7 +188,7 @@ namespace OpenTK
|
|||
/// Checks whether type is a blittable value type.
|
||||
/// </summary>
|
||||
/// <param name="type">An instance of the type to check.</param>
|
||||
public static bool Check<T>(T[, ,] type) where T : struct
|
||||
public static bool Check<T>(T[, ,] type)
|
||||
{
|
||||
return BlittableValueType<T>.Check();
|
||||
}
|
||||
|
@ -188,14 +198,14 @@ namespace OpenTK
|
|||
/// </summary>
|
||||
/// <param name="type">An instance of the type to check.</param>
|
||||
[CLSCompliant(false)]
|
||||
public static bool Check<T>(T[][] type) where T : struct
|
||||
public static bool Check<T>(T[][] type)
|
||||
{
|
||||
return BlittableValueType<T>.Check();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region From
|
||||
#region StrideOf
|
||||
|
||||
/// <summary>
|
||||
/// Returns the size of the specified value type in bytes.
|
||||
|
@ -205,7 +215,6 @@ namespace OpenTK
|
|||
/// <returns>An integer, specifying the size of the type in bytes.</returns>
|
||||
/// <exception cref="System.ArgumentException">Occurs when type is not blittable.</exception>
|
||||
public static int StrideOf<T>(T type)
|
||||
where T : struct
|
||||
{
|
||||
if (!Check(type))
|
||||
throw new ArgumentException("type");
|
||||
|
@ -221,7 +230,6 @@ namespace OpenTK
|
|||
/// <returns>An integer, specifying the size of the type in bytes.</returns>
|
||||
/// <exception cref="System.ArgumentException">Occurs when type is not blittable.</exception>
|
||||
public static int StrideOf<T>(T[] type)
|
||||
where T : struct
|
||||
{
|
||||
if (!Check(type))
|
||||
throw new ArgumentException("type");
|
||||
|
@ -237,7 +245,6 @@ namespace OpenTK
|
|||
/// <returns>An integer, specifying the size of the type in bytes.</returns>
|
||||
/// <exception cref="System.ArgumentException">Occurs when type is not blittable.</exception>
|
||||
public static int StrideOf<T>(T[,] type)
|
||||
where T : struct
|
||||
{
|
||||
if (!Check(type))
|
||||
throw new ArgumentException("type");
|
||||
|
@ -253,7 +260,6 @@ namespace OpenTK
|
|||
/// <returns>An integer, specifying the size of the type in bytes.</returns>
|
||||
/// <exception cref="System.ArgumentException">Occurs when type is not blittable.</exception>
|
||||
public static int StrideOf<T>(T[, ,] type)
|
||||
where T : struct
|
||||
{
|
||||
if (!Check(type))
|
||||
throw new ArgumentException("type");
|
||||
|
|
Loading…
Reference in a new issue