diff options
| author | Jean-Philippe Bruyère <jp_bruyere@hotmail.com> | 2022-11-01 21:13:46 +0100 |
|---|---|---|
| committer | Jean-Philippe Bruyère <jp_bruyere@hotmail.com> | 2022-11-01 21:13:46 +0100 |
| commit | b65719d05fc71b2b631334fbc06da58963769063 (patch) | |
| tree | 07e35f6e033b2328b9ce511f1dd12e5e22275294 /vulkan/PNext.cs | |
| parent | fb18b96960692f760419324e18bd4eee61e31d3e (diff) | |
PNext<T>, structs with stype passed by refv0.5.0
Diffstat (limited to 'vulkan/PNext.cs')
| -rw-r--r-- | vulkan/PNext.cs | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/vulkan/PNext.cs b/vulkan/PNext.cs new file mode 100644 index 0000000..783e757 --- /dev/null +++ b/vulkan/PNext.cs @@ -0,0 +1,46 @@ +// This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) +// +// Copyright (c) 2022 JP Bruyère +using System; +using System.Linq; +using System.Runtime.InteropServices; + +namespace Vulkan +{ + /// <summary> + /// Helper class to construct pnext chain + /// </summary> + public class PNext<T> : IDisposable where T : struct + { + IntPtr handle; + /// <summary> + /// Allocate on HGlobal a new structure of type T, ensure instance is Disposed. + /// </summary> + public PNext () { + init(); + } + /// <summary> + /// Allocate on HGlobal a new structure of type T and set the pNext field, + /// ensure instance is Disposed. + /// </summary> + public PNext (IntPtr pNext) { + init(); + Marshal.WriteIntPtr(handle + 4, pNext); + } + void init() { + handle = Marshal.AllocHGlobal(Marshal.SizeOf<T>()); + Type t = typeof(T); + StructureTypeAttribute st = (StructureTypeAttribute)t.GetCustomAttributes(typeof(StructureTypeAttribute), false).FirstOrDefault(); + Marshal.WriteInt32(handle, st.StructureType); + } + /// <summary> + /// Get value of T + /// </summary> + public T Val => Marshal.PtrToStructure<T> (handle); + public static implicit operator IntPtr(PNext<T> pn) => pn.handle; + public void Dispose() + { + Marshal.FreeHGlobal(handle); + } + } +} |