Skip to content

Commit

Permalink
Add alternative overloads for operator overloads
Browse files Browse the repository at this point in the history
  • Loading branch information
Archomeda committed Dec 22, 2020
1 parent 32695fe commit f3977fa
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 4 deletions.
13 changes: 13 additions & 0 deletions Gw2Sharp/ChatLinks/Internal/UInt24.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,24 @@ public UInt24(int value)
public static implicit operator UInt24(int value) =>
new UInt24(value);

/// <summary>
/// Gets the <see cref="UInt24"/> value of an integer.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>The value.</returns>
public static UInt24 FromInt32(int value) => value;

/// <summary>
/// Gets the integer value of <see cref="UInt24"/>.
/// </summary>
/// <param name="value">The value.</param>
public static implicit operator int(UInt24 value) =>
value.Value;

/// <summary>
/// Gets the integer value of <see cref="UInt24"/>.
/// </summary>
/// <returns>The value.</returns>
public int ToInt32() => this;
}
}
6 changes: 6 additions & 0 deletions Gw2Sharp/WebApi/RenderUrl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ public static implicit operator string(RenderUrl renderUrl) =>
public static implicit operator Uri(RenderUrl renderUrl) =>
new Uri(renderUrl.Url.AbsoluteUri, UriKind.Absolute);

/// <summary>
/// Gets the URL as <see cref="Uri"/>.
/// </summary>
/// <returns>The URL as <see cref="Uri"/>.</returns>
public Uri ToUri() => this;

#endregion
}
}
28 changes: 26 additions & 2 deletions Gw2Sharp/WebApi/V2/Models/ApiEnum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,28 +107,52 @@ internal ApiEnum(ulong value)
public static implicit operator ApiEnum<T>(T e) =>
new ApiEnum<T>(e, e.ToString());

/// <summary>
/// Converts an enum to an API enum.
/// </summary>
/// <param name="e">The enum.</param>
public static ApiEnum<T> From(T e) => e;

/// <summary>
/// Converts a string to an API enum.
/// </summary>
/// <param name="e">The enum.</param>
public static implicit operator ApiEnum<T>(string e) =>
new ApiEnum<T>(GetValue(e), e);

#pragma warning disable CA1062 // Validate arguments of public methods
/// <summary>
/// Converts an enum to an API enum.
/// </summary>
/// <param name="e">The enum.</param>
public static ApiEnum<T> From(string e) => e;

/// <summary>
/// Converts an API enum to its normal enum.
/// </summary>
/// <param name="e">The API enum.</param>
#pragma warning disable CA2225 // Operator overloads have named alternates
public static implicit operator T(ApiEnum<T> e) =>
e.Value;
#pragma warning restore CA2225 // Operator overloads have named alternates

/// <summary>
/// Converts an API enum to its normal enum.
/// </summary>
/// <returns>The enum.</returns>
public T ToEnum() => this;

/// <summary>
/// Converts an API enum to its raw string form.
/// </summary>
/// <param name="e">The API enum.</param>
public static implicit operator string?(ApiEnum<T> e) =>
e.RawValue;
#pragma warning restore CA1062 // Validate arguments of public methods

/// <summary>
/// Converts an API enum to its normal enum.
/// </summary>
/// <returns>The enum.</returns>
public string? ToEnumString() => this;

/// <inheritdoc />
public override bool Equals(object? obj) =>
Expand Down
40 changes: 38 additions & 2 deletions Gw2Sharp/WebApi/V2/Models/ApiFlags.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,42 +64,78 @@ internal ApiFlags(IEnumerable<object> list) :
public static implicit operator ApiFlags<T>(ApiEnum<T>[] l) =>
new ApiFlags<T>(l);

/// <summary>
/// Converts an array of enums to API flags.
/// </summary>
/// <param name="l">The list of enums.</param>
public static ApiFlags<T> From(ApiEnum<T>[] l) => l;

/// <summary>
/// Converts a list of enums to API flags.
/// </summary>
/// <param name="l">The list of enums.</param>
public static implicit operator ApiFlags<T>(List<ApiEnum<T>> l) =>
new ApiFlags<T>(l);

/// <summary>
/// Converts an array of enums to API flags.
/// </summary>
/// <param name="l">The list of enums.</param>
public static ApiFlags<T> From(List<ApiEnum<T>> l) => l;

/// <summary>
/// Converts a collection of enums to API flags.
/// </summary>
/// <param name="l">The collection of enums.</param>
public static implicit operator ApiFlags<T>(Collection<ApiEnum<T>> l) =>
new ApiFlags<T>(l);

/// <summary>
/// Converts a collection of enums to API flags.
/// </summary>
/// <param name="l">The list of enums.</param>
public static ApiFlags<T> From(Collection<ApiEnum<T>> l) => l;

/// <summary>
/// Converts a set of enums to API flags.
/// </summary>
/// <param name="l">The set of enums.</param>
public static implicit operator ApiFlags<T>(HashSet<ApiEnum<T>> l) =>
new ApiFlags<T>(l);

#pragma warning disable CA1062 // Validate arguments of public methods
/// <summary>
/// Converts a set of enums to API flags.
/// </summary>
/// <param name="l">The list of enums.</param>
public static ApiFlags<T> From(HashSet<ApiEnum<T>> l) => l;

/// <summary>
/// Converts API flags to an array of enums.
/// </summary>
/// <param name="e">The API enum.</param>
#pragma warning disable CA2225 // Operator overloads have named alternates
public static implicit operator ApiEnum<T>[](ApiFlags<T> e) =>
e.List.ToArray();
#pragma warning restore CA2225 // Operator overloads have named alternates

/// <summary>
/// Converts API flags to an array of enums.
/// </summary>
/// <returns>The enum.</returns>
public ApiEnum<T>[] ToArray() => this;

/// <summary>
/// Converts API flags to a list of enums.
/// </summary>
/// <param name="e">The API enum.</param>
public static implicit operator List<ApiEnum<T>>(ApiFlags<T> e) =>
e.List.ToList();
#pragma warning restore CA1062 // Validate arguments of public methods

/// <summary>
/// Converts API flags to a list of enums.
/// </summary>
/// <returns>The enum.</returns>
public List<ApiEnum<T>> ToList() => this;

/// <inheritdoc />
public override bool Equals(object? obj) =>
Expand Down

0 comments on commit f3977fa

Please sign in to comment.