Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/emptycoder/Overloader

Generator for method overloads.
https://github.com/emptycoder/Overloader

Last synced: 2 months ago
JSON representation

Generator for method overloads.

Awesome Lists containing this project

README

        

Overloader

Overloader is open-source generator for method overloads.
Support of xml documentation for overloads.

# Installation
[NuGet](https://www.nuget.org/packages/Overloader/): `dotnet add package overloader`

# Specific types on generics
## Uncompilable code
```csharp
public static class GenericMath
{
public static T Square(T val) where T : double, float => val * val;
}
```

## Solution
```csharp
[TSpecify(typeof(double))]
[Overload(typeof(float), "D", "F")]
public static class GenericMathD
{
public static double Square([T] double val) => val * val;
}
```

## Generated part
```csharp
public static partial class GenericMathF
{
public static double Square(float val) => val * val;
}
```

# Parameter overload creation to avoid additional struct/class allocation
## User template
```csharp
[assembly: Formatter(
"Vector2",
typeof(Vector2<>),
new object[] {"T"},
new object[]
{
"X", "T",
"Y", "T"
})]
[TSpecify(typeof(double), "Vector2")]
[Overload(typeof(float), "2D", "2F")]
public static partial class Vector2DExtension
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[return: T]
public static ref Vector2 Sum([Integrity][T] this ref Vector2 vec1, [T] in Vector2 vec2)
{
vec1.X += vec2.X;
vec1.Y += vec2.Y;

return ref vec1;
}
}
```

## Generated part
```csharp
public static partial class Vector2FExtension
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ref Vector2 Sum(this ref Vector2 vec1, float vec2X, float vec2Y)
{
vec1.X += vec2X;
vec1.Y += vec2Y;
return ref vec1;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ref Vector2 Sum(this ref Vector2 vec1, in Vector2 vec2)
{
vec1.X += vec2.X;
vec1.Y += vec2.Y;
return ref vec1;
}
}
```

# Parameter overload creation with cast
## User template
```csharp
[assembly: Formatter(
"Vector2S",
typeof(Vector2<>),
new object[] {"T"},
new object[]
{
nameof(Vector2.X), "T",
nameof(Vector2.Y), "T"
},
new object[]
{
typeof(Vector2),
"(short) ${Var0}.X, (short) ${Var0}.Y"
})]

[assembly: Formatter(
"Vector2US",
typeof(Vector2<>),
new object[] {"T"},
new object[]
{
nameof(Vector2.X), "T",
nameof(Vector2.Y), "T"
},
new object[]
{
typeof(Vector2),
"(ushort) ${Var0}.X, (ushort) ${Var0}.Y"
})]

[TSpecify(typeof(short), "Vector2S")]
[TOverload(typeof(ushort), "2S", "2US", "Vector2US")]
public static partial class V2SAdd
{
[return: T]
public static ref Vector2 Add(
[T] [Integrity] this ref Vector2 current,
[T] Vector2 vector)
{
current.X += vector.X;
current.Y += vector.Y;
return ref current;
}
}
```

## Generated part
```csharp
public static partial class V2SAdd
{
// Generated by: DecompositionOverload
public static ref Vector2 Add(this ref Vector2 current, short vectorX, short vectorY)
{
current.X += vectorX;
current.Y += vectorY;
return ref current;
}
// Generated by: CastTransitionOverloads
public static ref Vector2 Add(this ref Vector2 current, Vector2 vector0) =>
ref Add(ref current, (short) vector0.X, (short) vector0.Y);
}

public static partial class V2USAdd
{
// Generated by: DecompositionOverload
public static ref Vector2 Add(this ref Vector2 current, ushort vectorX, ushort vectorY)
{
current.X += vectorX;
current.Y += vectorY;
return ref current;
}
// Generated by: IntegrityOverload
public static ref Vector2 Add(this ref Vector2 current, Vector2 vector)
{
current.X += vector.X;
current.Y += vector.Y;
return ref current;
}
// Generated by: CastTransitionOverloads
public static ref Vector2 Add(this ref Vector2 current, Vector2 vector0) =>
ref Add(ref current, (ushort) vector0.X, (ushort) vector0.Y);
}
```

# Parameter overload creation with ref attribute
## User template
```csharp
[assembly: Formatter(
"Vector2S",
typeof(Vector2<>),
new object[] {"T"},
new object[]
{
nameof(Vector2.X), "T",
nameof(Vector2.Y), "T"
})]

[assembly: Formatter(
"Vector2US",
typeof(Vector2<>),
new object[] {"T"},
new object[]
{
nameof(Vector2.X), "T",
nameof(Vector2.Y), "T"
})]

[TSpecify(typeof(short), "Vector2S")]
[TOverload(typeof(ushort), "2S", "2US", "Vector2US")]
public static partial class V2SAdd
{
[return: T]
public static ref Vector2 Add(
[T] [Integrity] this ref Vector2 current,
[T] [Ref] Vector2 vector)
{
current.X += vector.X;
current.Y += vector.Y;
return ref current;
}
}
```

## Generated part
```csharp
public static partial class V2SAdd
{
// Generated by: DecompositionOverload
public static ref Vector2 Add(this ref Vector2 current, short vectorX, short vectorY)
{
current.X += vectorX;
current.Y += vectorY;
return ref current;
}
// Generated by: RefIntegrityOverloads
public static ref Vector2 Add(this ref Vector2 current, ref Vector2 vector)
{
current.X += vector.X;
current.Y += vector.Y;
return ref current;
}
}

public static partial class V2USAdd
{
// Generated by: DecompositionOverload
public static ref Vector2 Add(this ref Vector2 current, ushort vectorX, ushort vectorY)
{
current.X += vectorX;
current.Y += vectorY;
return ref current;
}
// Generated by: IntegrityOverload
public static ref Vector2 Add(this ref Vector2 current, Vector2 vector)
{
current.X += vector.X;
current.Y += vector.Y;
return ref current;
}
// Generated by: RefIntegrityOverloads
public static ref Vector2 Add(this ref Vector2 current, ref Vector2 vector)
{
current.X += vector.X;
current.Y += vector.Y;
return ref current;
}
}
```

# Development
## How to debug?
- Use the [launchSettings.json](Properties/launchSettings.json) profile.
- Debug tests.

## How to see on generated sources?
Add the next PropertyGroup into your .csproj file which uses Overloader:
```xml

true
$(BaseIntermediateOutputPath)\..\..\$(AssemblyName).Generated

```
See on "$(AssemblyName).Generated" folder.

## How can I determine which syntax nodes I should expect?
Consider installing the Roslyn syntax tree viewer plugin [Rossynt](https://plugins.jetbrains.com/plugin/16902-rossynt/).

# License
Overloader distributed under [MIT](./LICENSE) license.