Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add implicit convert operator for all variants of size struct #1540

Merged
merged 5 commits into from
Mar 9, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think Size -> Size2d is OK, but Size2d -> Size should be marked as explicit. This is because there is no implicit conversion from double to int in the case of scalar values. (For example, double d = 1; is OK but int i = 1.23; causes an error.)

public static explicit operator Point(Point2d self) => new((int) self.X, (int) self.Y);

}

public static implicit operator Size(Size2f size)
n0099 marked this conversation as resolved.
Show resolved Hide resolved
{
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 implicit operator Size2f(Size2d size)
n0099 marked this conversation as resolved.
Show resolved Hide resolved
{
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