Skip to content

Commit

Permalink
add implicit convert operator for all variants of size struct (#1540)
Browse files Browse the repository at this point in the history
* add implicit convert operator for `Size->Size2f`

* more implicit conversion operators for size structs

* Update src/OpenCvSharp/Modules/core/Struct/Size.cs

* Update src/OpenCvSharp/Modules/core/Struct/Size.cs

Co-authored-by: shimat <schimatk@gmail.com>

* Update src/OpenCvSharp/Modules/core/Struct/Size2f.cs

Co-authored-by: shimat <schimatk@gmail.com>

---------

Co-authored-by: shimat <schimatk@gmail.com>
  • Loading branch information
n0099 and shimat authored Mar 9, 2023
1 parent 0498156 commit a14400b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 18 deletions.
22 changes: 16 additions & 6 deletions src/OpenCvSharp/Modules/core/Struct/Size.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@
namespace OpenCvSharp;

/// <summary>
///
///
/// </summary>
[Serializable]
[StructLayout(LayoutKind.Sequential)]
[SuppressMessage("Design", "CA1051: Do not declare visible instance fields")]
public struct Size : IEquatable<Size>
{
/// <summary>
///
///
/// </summary>
public int Width;

/// <summary>
///
///
/// </summary>
public int Height;

Expand Down Expand Up @@ -73,22 +73,32 @@ public Size(double width, double height)
return !lhs.Equals(rhs);
}

public static explicit operator Size(Size2d size)
{
return new(size.Width, size.Height);
}

public static explicit operator Size(Size2f size)
{
return new(size.Width, size.Height);
}

#endregion

#region Override

/// <inheritdoc />
public readonly bool Equals(Size other)
{
return Width == other.Width && Height == other.Height;
}

/// <inheritdoc />
public override readonly bool Equals(object? obj)
{
return obj is Size other && Equals(other);
}

/// <inheritdoc />
public override readonly int GetHashCode()
{
Expand Down
22 changes: 16 additions & 6 deletions src/OpenCvSharp/Modules/core/Struct/Size2d.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@
namespace OpenCvSharp;

/// <summary>
///
///
/// </summary>
[Serializable]
[StructLayout(LayoutKind.Sequential)]
public struct Size2d : IEquatable<Size2d>
{
/// <summary>
///
///
/// </summary>
public double Width;

/// <summary>
///
///
/// </summary>
public double Height;

Expand Down Expand Up @@ -68,22 +68,32 @@ public Size2d(double width, double height)
return !lhs.Equals(rhs);
}

public static implicit operator Size2d(Size size)
{
return new(size.Width, size.Height);
}

public static implicit operator Size2d(Size2f size)
{
return new(size.Width, size.Height);
}

#endregion

#region Override

/// <inheritdoc />
public readonly bool Equals(Size2d other)
{
return Width.Equals(other.Width) && Height.Equals(other.Height);
}

/// <inheritdoc />
public override readonly bool Equals(object? obj)
{
return obj is Size2d other && Equals(other);
}

/// <inheritdoc />
public override readonly int GetHashCode()
{
Expand Down
22 changes: 16 additions & 6 deletions src/OpenCvSharp/Modules/core/Struct/Size2f.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@
namespace OpenCvSharp;

/// <summary>
///
///
/// </summary>
[Serializable]
[StructLayout(LayoutKind.Sequential)]
// ReSharper disable once InconsistentNaming
public struct Size2f : IEquatable<Size2f>
{
/// <summary>
///
///
/// </summary>
public float Width;

/// <summary>
///
///
/// </summary>
public float Height;

Expand Down Expand Up @@ -69,22 +69,32 @@ public Size2f(double width, double height)
return !lhs.Equals(rhs);
}

public static implicit operator Size2f(Size size)
{
return new(size.Width, size.Height);
}

public static explicit operator Size2f(Size2d size)
{
return new(size.Width, size.Height);
}

#endregion

#region Override

/// <inheritdoc />
public readonly bool Equals(Size2f other)
{
return Width.Equals(other.Width) && Height.Equals(other.Height);
}

/// <inheritdoc />
public override readonly bool Equals(object? obj)
{
return obj is Size2f other && Equals(other);
}

/// <inheritdoc />
public override readonly int GetHashCode()
{
Expand Down

0 comments on commit a14400b

Please sign in to comment.