Archive

Monthly Archives: September 2012

 

Get it here:
https://github.com/SuperV1234/VeeSharpTemplate/downloads

Outdated video:
http://www.youtube.com/watch?v=Uz868MuVvTY


This is especially useful when dealing with numeric types.
It can be easily used to generate vector classes for every numeric type, and also utility classes that can allow you to add/subtract the generated.
The possibilities are endless – the generated code is easy to update and recompile, and since it’s like you’ve written it, there is no performance loss.

After being fed up with some limitations of generics (why isn’t there an IArithmetic interface?), I decided to make a code generator that creates C# code by simply reading templates made in C#.

It’s hard to explain with words, but I have made a video with voice commentary that will explain how it works and show a simple result.

With this code…

var types = new []{"int", "float", "double", "long"};

foreach(var type in types)
{
    $$("public class Vector2_" + type)
    $${         
        $$("public Vector2_" + type + "(" + type + " mX, " + type + " mY)")
        $${
            $$ X = mX;
            $$ Y = mY;
        $$}

        $$("public " + type + " X { get; set; }")
        $$("public " + type + " Y { get; set; }")

        $$("public " + type + " ComponentSum()")
        $${
            $$ return X + Y;
        $$}
    $$}
}        

…my program automatically generates and automatically adds to a .csproj four new classes: Vector2_int, Vector2_float, Vector2_double and Vector2_long.

Watch the video for a better explanation! Thank you :)