Skip to content

Commit

Permalink
Add readonly modifier on struct methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
huoyaoyuan committed Dec 16, 2019
1 parent 4fc4c94 commit 05ce77a
Show file tree
Hide file tree
Showing 34 changed files with 95 additions and 102 deletions.
2 changes: 1 addition & 1 deletion osu.Framework/Allocation/ValueInvokeOnDisposal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace osu.Framework.Allocation
///
/// This is a struct version of <see cref="InvokeOnDisposal"/> to be used when allocations are to be minimised.
/// </summary>
public struct ValueInvokeOnDisposal : IDisposable
public readonly struct ValueInvokeOnDisposal : IDisposable
{
private readonly Action action;

Expand Down
4 changes: 2 additions & 2 deletions osu.Framework/Audio/Track/TrackAmplitudes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ public struct TrackAmplitudes
public float LeftChannel;
public float RightChannel;

public float Maximum => Math.Max(LeftChannel, RightChannel);
public readonly float Maximum => Math.Max(LeftChannel, RightChannel);

public float Average => (LeftChannel + RightChannel) / 2;
public readonly float Average => (LeftChannel + RightChannel) / 2;

/// <summary>
/// 256 length array of bins containing the average frequency of both channels at every ~78Hz step of the audible spectrum (0Hz - 20,000Hz).
Expand Down
18 changes: 9 additions & 9 deletions osu.Framework/Graphics/BlendingParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,55 +138,55 @@ public void ApplyDefaultToInherited()
AlphaEquation = BlendingEquation.Add;
}

public bool Equals(BlendingParameters other) =>
public readonly bool Equals(BlendingParameters other) =>
other.Source == Source
&& other.Destination == Destination
&& other.SourceAlpha == SourceAlpha
&& other.DestinationAlpha == DestinationAlpha
&& other.RGBEquation == RGBEquation
&& other.AlphaEquation == AlphaEquation;

public bool IsDisabled =>
public readonly bool IsDisabled =>
Source == BlendingType.One
&& Destination == BlendingType.Zero
&& SourceAlpha == BlendingType.One
&& DestinationAlpha == BlendingType.Zero
&& RGBEquation == BlendingEquation.Add
&& AlphaEquation == BlendingEquation.Add;

public override string ToString() => $"BlendingParameter: Factor: {Source}/{Destination}/{SourceAlpha}/{DestinationAlpha} RGBEquation: {RGBEquation} AlphaEquation: {AlphaEquation}";
public override readonly string ToString() => $"BlendingParameter: Factor: {Source}/{Destination}/{SourceAlpha}/{DestinationAlpha} RGBEquation: {RGBEquation} AlphaEquation: {AlphaEquation}";

#region GL Type Getters

/// <summary>
/// Gets the <see cref="BlendEquationMode"/> for the currently specified RGB Equation.
/// </summary>
public BlendEquationMode RGBEquationMode => translateEquation(RGBEquation);
public readonly BlendEquationMode RGBEquationMode => translateEquation(RGBEquation);

/// <summary>
/// Gets the <see cref="BlendEquationMode"/> for the currently specified Alpha Equation.
/// </summary>
public BlendEquationMode AlphaEquationMode => translateEquation(AlphaEquation);
public readonly BlendEquationMode AlphaEquationMode => translateEquation(AlphaEquation);

/// <summary>
/// Gets the <see cref="BlendingFactorSrc"/> for the currently specified source blending mode.
/// </summary>
public BlendingFactorSrc SourceBlendingFactor => translateBlendingFactorSrc(Source);
public readonly BlendingFactorSrc SourceBlendingFactor => translateBlendingFactorSrc(Source);

/// <summary>
/// Gets the <see cref="BlendingFactorDest"/> for the currently specified destination blending mode.
/// </summary>
public BlendingFactorDest DestinationBlendingFactor => translateBlendingFactorDest(Destination);
public readonly BlendingFactorDest DestinationBlendingFactor => translateBlendingFactorDest(Destination);

/// <summary>
/// Gets the <see cref="BlendingFactorSrc"/> for the currently specified source alpha mode.
/// </summary>
public BlendingFactorSrc SourceAlphaBlendingFactor => translateBlendingFactorSrc(SourceAlpha);
public readonly BlendingFactorSrc SourceAlphaBlendingFactor => translateBlendingFactorSrc(SourceAlpha);

/// <summary>
/// Gets the <see cref="BlendingFactorDest"/> for the currently specified destination alpha mode.
/// </summary>
public BlendingFactorDest DestinationAlphaBlendingFactor => translateBlendingFactorDest(DestinationAlpha);
public readonly BlendingFactorDest DestinationAlphaBlendingFactor => translateBlendingFactorDest(DestinationAlpha);

private static BlendingFactorSrc translateBlendingFactorSrc(BlendingType factor)
{
Expand Down
18 changes: 9 additions & 9 deletions osu.Framework/Graphics/Colour/ColourInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static ColourInfo GradientVertical(SRGBColour c1, SRGBColour c2)

private SRGBColour singleColour
{
get
readonly get
{
if (!HasSingleColour)
throw new InvalidOperationException("Attempted to read single colour from multi-colour ColourInfo.");
Expand All @@ -81,7 +81,7 @@ private SRGBColour singleColour
}
}

public SRGBColour Interpolate(Vector2 interp) => SRGBColour.FromVector(
public readonly SRGBColour Interpolate(Vector2 interp) => SRGBColour.FromVector(
(1 - interp.Y) * ((1 - interp.X) * TopLeft.ToVector() + interp.X * TopRight.ToVector()) +
interp.Y * ((1 - interp.X) * BottomLeft.ToVector() + interp.X * BottomRight.ToVector()));

Expand Down Expand Up @@ -128,7 +128,7 @@ public void ApplyChild(ColourInfo childColour, Quad interp)
/// </summary>
/// <param name="alpha">The alpha parameter to multiply the alpha values of all vertices with.</param>
/// <returns>The new ColourInfo.</returns>
public ColourInfo MultiplyAlpha(float alpha)
public readonly ColourInfo MultiplyAlpha(float alpha)
{
if (alpha == 1.0)
return this;
Expand All @@ -148,7 +148,7 @@ public ColourInfo MultiplyAlpha(float alpha)
return result;
}

public bool Equals(ColourInfo other)
public readonly bool Equals(ColourInfo other)
{
if (!HasSingleColour)
{
Expand All @@ -165,12 +165,12 @@ public bool Equals(ColourInfo other)
return other.HasSingleColour && TopLeft.Equals(other.TopLeft);
}

public bool Equals(SRGBColour other) => HasSingleColour && TopLeft.Equals(other);
public readonly bool Equals(SRGBColour other) => HasSingleColour && TopLeft.Equals(other);

/// <summary>
/// The average colour of all corners.
/// </summary>
public SRGBColour AverageColour
public readonly SRGBColour AverageColour
{
get
{
Expand All @@ -185,7 +185,7 @@ public SRGBColour AverageColour
/// <summary>
/// The maximum alpha value of all four corners.
/// </summary>
public float MaxAlpha
public readonly float MaxAlpha
{
get
{
Expand All @@ -201,7 +201,7 @@ public float MaxAlpha
/// <summary>
/// The minimum alpha value of all four corners.
/// </summary>
public float MinAlpha
public readonly float MinAlpha
{
get
{
Expand All @@ -214,7 +214,7 @@ public float MinAlpha
}
}

public override string ToString() => HasSingleColour ? $@"{TopLeft} (Single)" : $@"{TopLeft}, {TopRight}, {BottomLeft}, {BottomRight}";
public override readonly string ToString() => HasSingleColour ? $@"{TopLeft} (Single)" : $@"{TopLeft}, {TopRight}, {BottomLeft}, {BottomRight}";

public static implicit operator ColourInfo(SRGBColour colour) => SingleColour(colour);
public static implicit operator SRGBColour(ColourInfo colour) => colour.singleColour;
Expand Down
6 changes: 3 additions & 3 deletions osu.Framework/Graphics/Colour/SRGBColour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public struct SRGBColour : IEquatable<SRGBColour>
first.Linear.A + second.Linear.A),
};

public Vector4 ToVector() => new Vector4(Linear.R, Linear.G, Linear.B, Linear.A);
public readonly Vector4 ToVector() => new Vector4(Linear.R, Linear.G, Linear.B, Linear.A);
public static SRGBColour FromVector(Vector4 v) => new SRGBColour { Linear = new Color4(v.X, v.Y, v.Z, v.W) };

/// <summary>
Expand All @@ -65,7 +65,7 @@ public struct SRGBColour : IEquatable<SRGBColour>
/// <param name="alpha">The alpha factor to multiply with.</param>
public void MultiplyAlpha(float alpha) => Linear.A *= alpha;

public bool Equals(SRGBColour other) => Linear.Equals(other.Linear);
public override string ToString() => Linear.ToString();
public readonly bool Equals(SRGBColour other) => Linear.Equals(other.Linear);
public override readonly string ToString() => Linear.ToString();
}
}
4 changes: 2 additions & 2 deletions osu.Framework/Graphics/Containers/Container.cs
Original file line number Diff line number Diff line change
Expand Up @@ -449,9 +449,9 @@ internal Enumerator(Container<T> container)

public void Reset() => currentIndex = -1;

public T Current => container[currentIndex];
public readonly T Current => container[currentIndex];

object IEnumerator.Current => Current;
readonly object IEnumerator.Current => Current;

public void Dispose()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,6 @@ public LifetimeBoundaryCrossedEvent(Drawable child, LifetimeBoundaryKind kind, L
Direction = direction;
}

public override string ToString() => $"({Child.ChildID}, {Kind}, {Direction})";
public override readonly string ToString() => $"({Child.ChildID}, {Kind}, {Direction})";
}
}
2 changes: 1 addition & 1 deletion osu.Framework/Graphics/DrawColourInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ public DrawColourInfo(ColourInfo? colour = null, BlendingParameters? blending =
Blending = blending ?? BlendingParameters.Inherit;
}

public bool Equals(DrawColourInfo other) => Colour.Equals(other.Colour) && Blending.Equals(other.Blending);
public readonly bool Equals(DrawColourInfo other) => Colour.Equals(other.Colour) && Blending.Equals(other.Blending);
}
}
4 changes: 2 additions & 2 deletions osu.Framework/Graphics/DrawInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ public void ApplyTransform(Vector2 translation, Vector2 scale, float rotation, V
//MatrixExtensions.FastInvert(ref target.MatrixInverse);
}

public bool Equals(DrawInfo other) => Matrix.Equals(other.Matrix);
public readonly bool Equals(DrawInfo other) => Matrix.Equals(other.Matrix);

public override string ToString() => $@"{GetType().ReadableName().Replace(@"DrawInfo", string.Empty)} DrawInfo";
public override readonly string ToString() => $@"{GetType().ReadableName().Replace(@"DrawInfo", string.Empty)} DrawInfo";
}
}
4 changes: 2 additions & 2 deletions osu.Framework/Graphics/Effects/EdgeEffectParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ public struct EdgeEffectParameters : IEquatable<EdgeEffectParameters>
/// </summary>
public bool Hollow;

public bool Equals(EdgeEffectParameters other) =>
public readonly bool Equals(EdgeEffectParameters other) =>
Colour.Equals(other.Colour) &&
Offset == other.Offset &&
Type == other.Type &&
Roundness == other.Roundness &&
Radius == other.Radius;

public override string ToString() => Type != EdgeEffectType.None ? $@"{Radius} {Type}EdgeEffect" : @"EdgeEffect (Disabled)";
public override readonly string ToString() => Type != EdgeEffectType.None ? $@"{Radius} {Type}EdgeEffect" : @"EdgeEffect (Disabled)";
}
}
10 changes: 5 additions & 5 deletions osu.Framework/Graphics/MarginPadding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public struct MarginPadding : IEquatable<MarginPadding>
/// the absolute size of the space left empty from the right and left of the container if used as padding.
/// Effectively <see cref="Right"/> + <see cref="Left"/>.
/// </summary>
public float TotalHorizontal => Left + Right;
public readonly float TotalHorizontal => Left + Right;

/// <summary>
/// Sets the values of both <see cref="Left"/> and <see cref="Right"/> to the assigned value.
Expand All @@ -58,7 +58,7 @@ public float Horizontal
/// the absolute size of the space left empty from the top and bottom of the container if used as padding.
/// Effectively <see cref="Top"/> + <see cref="Bottom"/>.
/// </summary>
public float TotalVertical => Top + Bottom;
public readonly float TotalVertical => Top + Bottom;

/// <summary>
/// Sets the values of both <see cref="Top"/> and <see cref="Bottom"/> to the assigned value.
Expand All @@ -71,7 +71,7 @@ public float Vertical
/// <summary>
/// Gets the total absolute size of the empty space horizontally (x coordinate) and vertically (y coordinate) around the <see cref="Drawable"/> or inside the container if used as padding.
/// </summary>
public Vector2 Total => new Vector2(TotalHorizontal, TotalVertical);
public readonly Vector2 Total => new Vector2(TotalHorizontal, TotalVertical);

/// <summary>
/// Initializes all four sides (<see cref="Left"/>, <see cref="Right"/>, <see cref="Top"/> and <see cref="Bottom"/>) to the given value.
Expand All @@ -82,9 +82,9 @@ public MarginPadding(float allSides)
Top = Left = Bottom = Right = allSides;
}

public bool Equals(MarginPadding other) => Top == other.Top && Left == other.Left && Bottom == other.Bottom && Right == other.Right;
public readonly bool Equals(MarginPadding other) => Top == other.Top && Left == other.Left && Bottom == other.Bottom && Right == other.Right;

public override string ToString() => $@"({Top}, {Left}, {Bottom}, {Right})";
public override readonly string ToString() => $@"({Top}, {Left}, {Bottom}, {Right})";

public static MarginPadding operator -(MarginPadding mp) =>
new MarginPadding
Expand Down
2 changes: 1 addition & 1 deletion osu.Framework/Graphics/OpenGL/GLWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,7 @@ public struct MaskingInfo : IEquatable<MaskingInfo>
public bool Hollow;
public float HollowCornerRadius;

public bool Equals(MaskingInfo other) =>
public readonly bool Equals(MaskingInfo other) =>
ScreenSpaceAABB == other.ScreenSpaceAABB &&
MaskingRect == other.MaskingRect &&
ToMaskingSpace == other.ToMaskingSpace &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal struct DepthWrappingVertex<TVertex> : IVertex, IEquatable<DepthWrapping
[VertexMember(1, VertexAttribPointerType.Float)]
public float BackbufferDrawDepth;

public bool Equals(DepthWrappingVertex<TVertex> other)
public readonly bool Equals(DepthWrappingVertex<TVertex> other)
=> Vertex.Equals(other.Vertex)
&& BackbufferDrawDepth.Equals(other.BackbufferDrawDepth);
}
Expand Down
2 changes: 1 addition & 1 deletion osu.Framework/Graphics/OpenGL/Vertices/ParticleVertex2D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ public struct ParticleVertex2D : IEquatable<ParticleVertex2D>, IVertex
[VertexMember(2, VertexAttribPointerType.Float)]
public Vector2 Direction;

public bool Equals(ParticleVertex2D other) => Position.Equals(other.Position) && TexturePosition.Equals(other.TexturePosition) && Colour.Equals(other.Colour) && Time.Equals(other.Time) && Direction.Equals(other.Direction);
public readonly bool Equals(ParticleVertex2D other) => Position.Equals(other.Position) && TexturePosition.Equals(other.TexturePosition) && Colour.Equals(other.Colour) && Time.Equals(other.Time) && Direction.Equals(other.Direction);
}
}
2 changes: 1 addition & 1 deletion osu.Framework/Graphics/OpenGL/Vertices/TexturedVertex2D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public struct TexturedVertex2D : IEquatable<TexturedVertex2D>, IVertex
[VertexMember(2, VertexAttribPointerType.Float)]
public Vector2 BlendRange;

public bool Equals(TexturedVertex2D other) =>
public readonly bool Equals(TexturedVertex2D other) =>
Position.Equals(other.Position)
&& TexturePosition.Equals(other.TexturePosition)
&& Colour.Equals(other.Colour)
Expand Down
2 changes: 1 addition & 1 deletion osu.Framework/Graphics/OpenGL/Vertices/TexturedVertex3D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ public struct TexturedVertex3D : IEquatable<TexturedVertex3D>, IVertex
[VertexMember(2, VertexAttribPointerType.Float)]
public Vector2 TexturePosition;

public bool Equals(TexturedVertex3D other) => Position.Equals(other.Position) && TexturePosition.Equals(other.TexturePosition) && Colour.Equals(other.Colour);
public readonly bool Equals(TexturedVertex3D other) => Position.Equals(other.Position) && TexturePosition.Equals(other.TexturePosition) && Colour.Equals(other.Colour);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ public struct TimedTexturedVertex2D : IEquatable<TimedTexturedVertex2D>, IVertex
[VertexMember(1, VertexAttribPointerType.Float)]
public float Time;

public bool Equals(TimedTexturedVertex2D other) => Position.Equals(other.Position) && TexturePosition.Equals(other.TexturePosition) && Colour.Equals(other.Colour) && Time.Equals(other.Time);
public readonly bool Equals(TimedTexturedVertex2D other) => Position.Equals(other.Position) && TexturePosition.Equals(other.TexturePosition) && Colour.Equals(other.Colour) && Time.Equals(other.Time);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ public struct UncolouredVertex2D : IEquatable<UncolouredVertex2D>, IVertex
[VertexMember(2, VertexAttribPointerType.Float)]
public Vector2 Position;

public bool Equals(UncolouredVertex2D other) => Position.Equals(other.Position);
public readonly bool Equals(UncolouredVertex2D other) => Position.Equals(other.Position);
}
}
2 changes: 1 addition & 1 deletion osu.Framework/Graphics/OpenGL/Vertices/Vertex2D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ public struct Vertex2D : IEquatable<Vertex2D>, IVertex
[VertexMember(4, VertexAttribPointerType.Float)]
public Color4 Colour;

public bool Equals(Vertex2D other) => Position.Equals(other.Position) && Colour.Equals(other.Colour);
public readonly bool Equals(Vertex2D other) => Position.Equals(other.Position) && Colour.Equals(other.Colour);
}
}
2 changes: 1 addition & 1 deletion osu.Framework/Graphics/Primitives/ProjectionRange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace osu.Framework.Graphics.Primitives
/// A structure that tells how "far" along an axis
/// the projection of vertices onto the axis would be.
/// </summary>
internal struct ProjectionRange
internal readonly struct ProjectionRange
{
/// <summary>
/// The minimum projected value.
Expand Down
Loading

0 comments on commit 05ce77a

Please sign in to comment.