Skip to content

Commit

Permalink
Add ImplicitNullable type.
Browse files Browse the repository at this point in the history
  • Loading branch information
pluskal committed Jul 3, 2020
1 parent 628f31d commit 7c10982
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions Kaitai.Struct.Runtime/ImplicitNullable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Kaitai
{
public struct ImplicitNullable<T> where T : struct
{
public bool HasValue { get { return this._value.HasValue; } }
public T Value { get { return this._value.Value; } }

public ImplicitNullable(T value) : this() { this._value = value; }
public ImplicitNullable(T? value) : this() { this._value = value; }

public static implicit operator ImplicitNullable<T>(T value) { return new ImplicitNullable<T>(value); }
public static implicit operator ImplicitNullable<T>(T? value) { return new ImplicitNullable<T>(value); }

public static implicit operator T(ImplicitNullable<T> value) { return value._value ?? default(T); }
public static implicit operator T?(ImplicitNullable<T> value) { return value._value; }

private T? _value { get; set; }

// Should define other Nullable<T> members, especially
// Equals and GetHashCode to avoid boxing
}
}

0 comments on commit 7c10982

Please sign in to comment.