Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1452 | chris | 1 | #region License |
| 2 | // |
||
| 3 | // The Open Toolkit Library License |
||
| 4 | // |
||
| 5 | // Copyright (c) 2006 - 2010 the Open Toolkit library. |
||
| 6 | // |
||
| 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy |
||
| 8 | // of this software and associated documentation files (the "Software"), to deal |
||
| 9 | // in the Software without restriction, including without limitation the rights to |
||
| 10 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
||
| 11 | // the Software, and to permit persons to whom the Software is furnished to do |
||
| 12 | // so, subject to the following conditions: |
||
| 13 | // |
||
| 14 | // The above copyright notice and this permission notice shall be included in all |
||
| 15 | // copies or substantial portions of the Software. |
||
| 16 | // |
||
| 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
||
| 18 | // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
||
| 19 | // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
||
| 20 | // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
||
| 21 | // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
||
| 22 | // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
||
| 23 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
||
| 24 | // OTHER DEALINGS IN THE SOFTWARE. |
||
| 25 | // |
||
| 26 | #endregion |
||
| 27 | |||
| 28 | using System; |
||
| 29 | using System.Collections.Generic; |
||
| 30 | using System.Text; |
||
| 31 | using System.Runtime.InteropServices; |
||
| 32 | using System.Diagnostics; |
||
| 33 | using System.Reflection; |
||
| 34 | |||
| 35 | namespace OpenTK |
||
| 36 | { |
||
| 37 | #region BlittableValueType<T> |
||
| 38 | |||
| 39 | /// <summary> |
||
| 40 | /// Checks whether the specified type parameter is a blittable value type. |
||
| 41 | /// </summary> |
||
| 42 | /// <remarks> |
||
| 43 | /// A blittable value type is a struct that only references other value types recursively, |
||
| 44 | /// which allows it to be passed to unmanaged code directly. |
||
| 45 | /// </remarks> |
||
| 46 | public static class BlittableValueType<T> |
||
| 47 | { |
||
| 48 | #region Fields |
||
| 49 | |||
| 50 | static readonly Type Type; |
||
| 51 | static readonly int stride; |
||
| 52 | |||
| 53 | #endregion |
||
| 54 | |||
| 55 | #region Constructors |
||
| 56 | |||
| 57 | static BlittableValueType() |
||
| 58 | { |
||
| 59 | Type = typeof(T); |
||
| 60 | if (Type.IsValueType && !Type.IsGenericType) |
||
| 61 | { |
||
| 62 | // Does this support generic types? On Mono 2.4.3 it does |
||
| 63 | // On .Net it doesn't. |
||
| 64 | // http://msdn.microsoft.com/en-us/library/5s4920fa.aspx |
||
| 65 | stride = Marshal.SizeOf(typeof(T)); |
||
| 66 | } |
||
| 67 | } |
||
| 68 | |||
| 69 | #endregion |
||
| 70 | |||
| 71 | #region Public Members |
||
| 72 | |||
| 73 | /// <summary> |
||
| 74 | /// Gets the size of the type in bytes or 0 for non-blittable types. |
||
| 75 | /// </summary> |
||
| 76 | /// <remarks> |
||
| 77 | /// This property returns 0 for non-blittable types. |
||
| 78 | /// </remarks> |
||
| 79 | public static int Stride { get { return stride; } } |
||
| 80 | |||
| 81 | #region Check |
||
| 82 | |||
| 83 | /// <summary> |
||
| 84 | /// Checks whether the current typename T is blittable. |
||
| 85 | /// </summary> |
||
| 86 | /// <returns>True if T is blittable; false otherwise.</returns> |
||
| 87 | public static bool Check() |
||
| 88 | { |
||
| 89 | return Check(Type); |
||
| 90 | } |
||
| 91 | |||
| 92 | /// <summary> |
||
| 93 | /// Checks whether type is a blittable value type. |
||
| 94 | /// </summary> |
||
| 95 | /// <param name="type">A System.Type to check.</param> |
||
| 96 | /// <returns>True if T is blittable; false otherwise.</returns> |
||
| 97 | public static bool Check(Type type) |
||
| 98 | { |
||
| 99 | if (!CheckStructLayoutAttribute(type)) |
||
| 100 | Debug.Print("Warning: type {0} does not specify a StructLayoutAttribute with Pack=1. The memory layout of the struct may change between platforms.", type.Name); |
||
| 101 | |||
| 102 | return CheckType(type); |
||
| 103 | } |
||
| 104 | |||
| 105 | #endregion |
||
| 106 | |||
| 107 | #endregion |
||
| 108 | |||
| 109 | #region Private Members |
||
| 110 | |||
| 111 | // Checks whether the parameter is a primitive type or consists of primitive types recursively. |
||
| 112 | // Throws a NotSupportedException if it is not. |
||
| 113 | static bool CheckType(Type type) |
||
| 114 | { |
||
| 115 | //Debug.Print("Checking type {0} (size: {1} bytes).", type.Name, Marshal.SizeOf(type)); |
||
| 116 | if (type.IsPrimitive) |
||
| 117 | return true; |
||
| 118 | |||
| 119 | if (!type.IsValueType) |
||
| 120 | return false; |
||
| 121 | |||
| 122 | FieldInfo[] fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); |
||
| 123 | Debug.Indent(); |
||
| 124 | foreach (FieldInfo field in fields) |
||
| 125 | { |
||
| 126 | if (!CheckType(field.FieldType)) |
||
| 127 | return false; |
||
| 128 | } |
||
| 129 | Debug.Unindent(); |
||
| 130 | |||
| 131 | return Stride != 0; |
||
| 132 | } |
||
| 133 | |||
| 134 | // Checks whether the specified struct defines [StructLayout(LayoutKind.Sequential, Pack=1)] |
||
| 135 | // or [StructLayout(LayoutKind.Explicit)] |
||
| 136 | static bool CheckStructLayoutAttribute(Type type) |
||
| 137 | { |
||
| 138 | StructLayoutAttribute[] attr = (StructLayoutAttribute[]) |
||
| 139 | type.GetCustomAttributes(typeof(StructLayoutAttribute), true); |
||
| 140 | |||
| 141 | if ((attr == null) || |
||
| 142 | (attr != null && attr.Length > 0 && attr[0].Value != LayoutKind.Explicit && attr[0].Pack != 1)) |
||
| 143 | return false; |
||
| 144 | |||
| 145 | return true; |
||
| 146 | } |
||
| 147 | |||
| 148 | #endregion |
||
| 149 | } |
||
| 150 | |||
| 151 | #endregion |
||
| 152 | |||
| 153 | #region BlittableValueType |
||
| 154 | |||
| 155 | /// <summary> |
||
| 156 | /// Checks whether the specified type parameter is a blittable value type. |
||
| 157 | /// </summary> |
||
| 158 | /// <remarks> |
||
| 159 | /// A blittable value type is a struct that only references other value types recursively, |
||
| 160 | /// which allows it to be passed to unmanaged code directly. |
||
| 161 | /// </remarks> |
||
| 162 | public static class BlittableValueType |
||
| 163 | { |
||
| 164 | #region Check |
||
| 165 | |||
| 166 | /// <summary> |
||
| 167 | /// Checks whether type is a blittable value type. |
||
| 168 | /// </summary> |
||
| 169 | /// <param name="type">An instance of the type to check.</param> |
||
| 170 | /// <returns>True if T is blittable; false otherwise.</returns> |
||
| 171 | public static bool Check<T>(T type) |
||
| 172 | { |
||
| 173 | return BlittableValueType<T>.Check(); |
||
| 174 | } |
||
| 175 | |||
| 176 | /// <summary> |
||
| 177 | /// Checks whether type is a blittable value type. |
||
| 178 | /// </summary> |
||
| 179 | /// <param name="type">An instance of the type to check.</param> |
||
| 180 | /// <returns>True if T is blittable; false otherwise.</returns> |
||
| 181 | public static bool Check<T>(T[] type) |
||
| 182 | { |
||
| 183 | return BlittableValueType<T>.Check(); |
||
| 184 | } |
||
| 185 | |||
| 186 | /// <summary> |
||
| 187 | /// Checks whether type is a blittable value type. |
||
| 188 | /// </summary> |
||
| 189 | /// <param name="type">An instance of the type to check.</param> |
||
| 190 | /// <returns>True if T is blittable; false otherwise.</returns> |
||
| 191 | public static bool Check<T>(T[,] type) |
||
| 192 | { |
||
| 193 | return BlittableValueType<T>.Check(); |
||
| 194 | } |
||
| 195 | |||
| 196 | /// <summary> |
||
| 197 | /// Checks whether type is a blittable value type. |
||
| 198 | /// </summary> |
||
| 199 | /// <param name="type">An instance of the type to check.</param> |
||
| 200 | /// <returns>True if T is blittable; false otherwise.</returns> |
||
| 201 | public static bool Check<T>(T[, ,] type) |
||
| 202 | { |
||
| 203 | return BlittableValueType<T>.Check(); |
||
| 204 | } |
||
| 205 | |||
| 206 | /// <summary> |
||
| 207 | /// Checks whether type is a blittable value type. |
||
| 208 | /// </summary> |
||
| 209 | /// <param name="type">An instance of the type to check.</param> |
||
| 210 | /// <returns>True if T is blittable; false otherwise.</returns> |
||
| 211 | [CLSCompliant(false)] |
||
| 212 | public static bool Check<T>(T[][] type) |
||
| 213 | { |
||
| 214 | return BlittableValueType<T>.Check(); |
||
| 215 | } |
||
| 216 | |||
| 217 | #endregion |
||
| 218 | |||
| 219 | #region StrideOf |
||
| 220 | |||
| 221 | /// <summary> |
||
| 222 | /// Returns the size of the specified value type in bytes or 0 if the type is not blittable. |
||
| 223 | /// </summary> |
||
| 224 | /// <typeparam name="T">The value type. Must be blittable.</typeparam> |
||
| 225 | /// <param name="type">An instance of the value type.</param> |
||
| 226 | /// <returns>An integer, specifying the size of the type in bytes.</returns> |
||
| 227 | /// <exception cref="System.ArgumentException">Occurs when type is not blittable.</exception> |
||
| 228 | public static int StrideOf<T>(T type) |
||
| 229 | { |
||
| 230 | if (!Check(type)) |
||
| 231 | throw new ArgumentException("type"); |
||
| 232 | |||
| 233 | return BlittableValueType<T>.Stride; |
||
| 234 | } |
||
| 235 | |||
| 236 | /// <summary> |
||
| 237 | /// Returns the size of a single array element in bytes or 0 if the element is not blittable. |
||
| 238 | /// </summary> |
||
| 239 | /// <typeparam name="T">The value type.</typeparam> |
||
| 240 | /// <param name="type">An instance of the value type.</param> |
||
| 241 | /// <returns>An integer, specifying the size of the type in bytes.</returns> |
||
| 242 | /// <exception cref="System.ArgumentException">Occurs when type is not blittable.</exception> |
||
| 243 | public static int StrideOf<T>(T[] type) |
||
| 244 | { |
||
| 245 | if (!Check(type)) |
||
| 246 | throw new ArgumentException("type"); |
||
| 247 | |||
| 248 | return BlittableValueType<T>.Stride; |
||
| 249 | } |
||
| 250 | |||
| 251 | /// <summary> |
||
| 252 | /// Returns the size of a single array element in bytes or 0 if the element is not blittable. |
||
| 253 | /// </summary> |
||
| 254 | /// <typeparam name="T">The value type.</typeparam> |
||
| 255 | /// <param name="type">An instance of the value type.</param> |
||
| 256 | /// <returns>An integer, specifying the size of the type in bytes.</returns> |
||
| 257 | /// <exception cref="System.ArgumentException">Occurs when type is not blittable.</exception> |
||
| 258 | public static int StrideOf<T>(T[,] type) |
||
| 259 | { |
||
| 260 | if (!Check(type)) |
||
| 261 | throw new ArgumentException("type"); |
||
| 262 | |||
| 263 | return BlittableValueType<T>.Stride; |
||
| 264 | } |
||
| 265 | |||
| 266 | /// <summary> |
||
| 267 | /// Returns the size of a single array element in bytes or 0 if the element is not blittable. |
||
| 268 | /// </summary> |
||
| 269 | /// <typeparam name="T">The value type.</typeparam> |
||
| 270 | /// <param name="type">An instance of the value type.</param> |
||
| 271 | /// <returns>An integer, specifying the size of the type in bytes.</returns> |
||
| 272 | /// <exception cref="System.ArgumentException">Occurs when type is not blittable.</exception> |
||
| 273 | public static int StrideOf<T>(T[, ,] type) |
||
| 274 | { |
||
| 275 | if (!Check(type)) |
||
| 276 | throw new ArgumentException("type"); |
||
| 277 | |||
| 278 | return BlittableValueType<T>.Stride; |
||
| 279 | } |
||
| 280 | |||
| 281 | #endregion |
||
| 282 | } |
||
| 283 | |||
| 284 | #endregion |
||
| 285 | } |