From fb9010516546a056c203b8a936502e2d234dd925 Mon Sep 17 00:00:00 2001 From: dahall Date: Wed, 20 May 2020 12:25:55 -0600 Subject: [PATCH] Added SafeGuidPtr for instances where GUID is optional. --- Core/InteropServices/StrPtr.cs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/Core/InteropServices/StrPtr.cs b/Core/InteropServices/StrPtr.cs index 713b7a01..5da75913 100644 --- a/Core/InteropServices/StrPtr.cs +++ b/Core/InteropServices/StrPtr.cs @@ -311,4 +311,28 @@ namespace Vanara.InteropServices /// public override string ToString() => Value?.ToString("B") ?? ""; } + + /// + /// Represents a GUID point, or REFGUID, that will automatically dispose the memory to which it points at the end of scope. + /// You must use the value, or the parameter-less constructor to pass the equivalent of . + /// + public class SafeGuidPtr : SafeMemStruct + { + /// + /// Initializes a new instance of the class. This value is equivalent to a pointer. + /// + public SafeGuidPtr() : base(IntPtr.Zero, true, 0) { } + + /// Initializes a new instance of the class. + /// The unique identifier to assign to the pointer. + public SafeGuidPtr(in Guid guid) : base(guid) { } + + /// Performs an implicit conversion from to . + /// The unique identifier. + /// The resulting instance from the conversion. + public static implicit operator SafeGuidPtr(Guid? guid) => guid.HasValue ? new SafeGuidPtr(guid.Value) : Null; + + /// The equivalent of a pointer to a GUID value. + public static readonly SafeGuidPtr Null = new SafeGuidPtr(); + } } \ No newline at end of file