code generation/preprocessing

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • colin

    code generation/preprocessing

    Hi,

    Is there a way of doing simple code generation inside visual c# express
    such as similar to preprocessing in c++ ?

    I need to generate a library for some vector maths,
    but I need to implement it for different types wich would be difficult to do
    with
    generic types and/or interfaces for numerous reasons
    and would probaly sufer a considerable performance hit.

    many thanks
    Colin =^.^=


  • Cowboy \(Gregory A. Beamer\)

    #2
    Re: code generation/preprocessing

    You can do code generation in .NET, using the Reflection bits. There is an
    Emit() method that will compile on the fly, if that is what you are looking
    for.

    I believe you will find this method has an initial perf hit, as well, so I
    would consider saving the emitted bits after they are created for a type.
    You can then dynamically add references at startup for any you have already
    generated.

    --
    Gregory A. Beamer
    MVP, MCP: +I, SE, SD, DBA

    Subscribe to my blog


    or just read it:


    *************** *************** *************** ****
    | Think outside the box!
    |
    *************** *************** *************** ****
    "colin" <colin.rowe1@nt world.NOSPAM.co mwrote in message
    news:bAMMj.4297 7$Ff4.24062@new sfe5-win.ntli.net...
    Hi,
    >
    Is there a way of doing simple code generation inside visual c# express
    such as similar to preprocessing in c++ ?
    >
    I need to generate a library for some vector maths,
    but I need to implement it for different types wich would be difficult to
    do with
    generic types and/or interfaces for numerous reasons
    and would probaly sufer a considerable performance hit.
    >
    many thanks
    Colin =^.^=
    >

    Comment

    • colin

      #3
      Re: code generation/preprocessing

      thanks, I realy need to do this as part of the build process,
      as other code needs to use it. I just need some simple c++
      macro/definition style expansion.

      I could probably write some code to do this but was wondering if theres a
      commonly used method.

      many thanks
      Colin =^.^=

      "Cowboy (Gregory A. Beamer)" <NoSpamMgbworld @comcast.netNoS pamMwrote in
      message news:OJWpLcmnIH A.3532@TK2MSFTN GP05.phx.gbl...
      You can do code generation in .NET, using the Reflection bits. There is an
      Emit() method that will compile on the fly, if that is what you are
      looking for.
      >
      I believe you will find this method has an initial perf hit, as well, so I
      would consider saving the emitted bits after they are created for a type.
      You can then dynamically add references at startup for any you have
      already generated.
      >
      --
      Gregory A. Beamer
      MVP, MCP: +I, SE, SD, DBA
      >
      Subscribe to my blog

      >
      or just read it:

      >
      *************** *************** *************** ****
      | Think outside the box! |
      *************** *************** *************** ****
      "colin" <colin.rowe1@nt world.NOSPAM.co mwrote in message
      news:bAMMj.4297 7$Ff4.24062@new sfe5-win.ntli.net...
      >Hi,
      >>
      >Is there a way of doing simple code generation inside visual c# express
      >such as similar to preprocessing in c++ ?
      >>
      >I need to generate a library for some vector maths,
      >but I need to implement it for different types wich would be difficult to
      >do with
      >generic types and/or interfaces for numerous reasons
      >and would probaly sufer a considerable performance hit.
      >>
      >many thanks
      >Colin =^.^=
      >>
      >
      >

      Comment

      • Marc Gravell

        #4
        Re: code generation/preprocessing

        I need to generate a library for some vector maths,
        but I need to implement it for different types wich would be difficult to do
        with
        generic types and/or interfaces for numerous reasons
        and would probaly sufer a considerable performance hit.
        Would it help if I posted a generic-maths implementation? I have
        demonstrated this with Complex<Tetc, and an implementation of the
        LINQ "Sum", "Average" etc operators - and a few other things. It works
        faster than you would expect! I have a 2.0 version too, but I haven't
        fully unit tested it

        Here is the 3.5 version:
        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

        download from:
        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.


        Marc

        Comment

        • colin

          #5
          Re: code generation/preprocessing

          thanks thats very interesting indeed, im only using c# 2.0
          I had got part way to making a struct/interface wich wrapped all
          the operators needed, but decided that generating code for the clases
          its used on would be preferable, as its likly that only one of the many
          diferent types would be used in any one project anyway.

          basically there are many different libraries wich use 3d vectors,
          such as xna and the managed directx, but there are countless other libraries
          such as for opengl and managed directx wrappers, but also 3d engines,
          each of wich all use their own Vector consiting of 3 floats.

          I also need to do be able to do the same calculations but at higher
          precision with doubles.

          My aim is to come up with a standard vector library wich can be used with
          any 3d module.

          I already have a math library wich ive compiled from numerous web soruces
          for the different
          functions, ive done a copy/paste/replace to produce one for float and one
          for double,
          but as I add to it and find bugs having to do it on 2 copies is undesirable.

          As I dont have acces to the vector classes to add a common interface I would
          also have to do some conversion too.

          so im realy interested if theres any commonly used method to generate code
          prior to compiling,
          before I write a simple program to do some simple token replacement.

          I find the lack of flexibility in constraints frustratingly limiting
          compared to c++ templates.

          many thanks
          Colin =^.^=


          "Marc Gravell" <marc.gravell@g mail.comwrote in message
          news:78ca3fca-74bd-472e-9d35-74b931190c3b@d2 6g2000prg.googl egroups.com...
          >I need to generate a library for some vector maths,
          >but I need to implement it for different types wich would be difficult to
          >do
          >with
          >generic types and/or interfaces for numerous reasons
          >and would probaly sufer a considerable performance hit.
          >
          Would it help if I posted a generic-maths implementation? I have
          demonstrated this with Complex<Tetc, and an implementation of the
          LINQ "Sum", "Average" etc operators - and a few other things. It works
          faster than you would expect! I have a 2.0 version too, but I haven't
          fully unit tested it
          >
          Here is the 3.5 version:
          Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

          Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

          download from:
          Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

          >
          Marc

          Comment

          • Marc Gravell

            #6
            Re: code generation/preprocessing

            im only using c# 2.0
            I had got part way to making a struct/interface wich wrapped all
            the operators needed, but decided that generating code for the clases
            its used on would be preferable, as its likly that only one of the many
            diferent types would be used in any one project anyway.
            Indeed. Well, let me know if you want a copy of the .NET 2.0 version -
            but otherwise the interface apprach can be quite useful if you only
            need to cope with known types (the version I have will work even with
            custom structs).

            Anyway - a simplified (few operators, few supported types) version of
            the interface approach might look like below; note I've used a static
            wrapper class in the middle (Calc) with generic methods; this means
            you don't need to keep passing ICalc<Tinstance around, and type-
            inference makes calling simple (i.e. note no explicit generics
            mentioned in the body of Test<T>)

            using System;
            static class Program
            {
            static void Main()
            {
            int x = 5, y = 6;
            int z = Test(x, y);
            }
            static T Test<T>(T x, T y)
            {
            T val = Calc.Add(x, y);
            val = Calc.Multiply(x , Calc.Negate(val ));
            return val;
            }
            }
            public static class Calc
            {
            static Calc()
            {
            // known types
            Register<int, CalcInt32>();
            Register<float, CalcSingle>();

            }
            static void Register<TValue , TCalc>()
            where TValue : struct
            where TCalc : ICalc<TValue>, ICalc<TValue?>, new()
            {
            // handle both T and T?
            TCalc calc = new TCalc();
            CalcCache<TValu e>.Instance = calc;
            CalcCache<TValu e?>.Instance = calc;
            }
            public static T Add<T>(T x, T y) {
            return CalcCache<T>.In stance.Add(x, y);
            }
            public static T Multiply<T>(T x, T y)
            {
            return CalcCache<T>.In stance.Multiply (x, y);
            }
            public static T Negate<T>(T x)
            {
            return CalcCache<T>.In stance.Negate(x );
            }
            static class CalcCache<T>
            {
            private static ICalc<Tinstance = new CalcNotSupporte d<T>();
            public static ICalc<TInstance
            {
            get
            {
            return instance;
            }
            set
            {
            if (instance == null) throw new
            ArgumentNullExc eption("Instanc e");
            instance = value;
            }
            }
            }
            }
            interface ICalc<T>
            {
            T Add(T x, T y);
            T Multiply(T x, T y);
            T Negate(T x);
            // etc
            }

            sealed class CalcNotSupporte d<T: ICalc<T>
            {
            public T Add(T x, T y) { throw new NotSupportedExc eption(); }
            public T Multiply(T x, T y) { throw new NotSupportedExc eption(); }
            public T Negate(T x) { throw new NotSupportedExc eption(); }
            }
            sealed class CalcInt32 : ICalc<Int32>, ICalc<Int32?>
            {
            public int Add(int x, int y) { return x + y; }
            public int Multiply(int x, int y) {return x * y;}
            public int Negate(int x) { return -x; }

            public int? Add(int? x, int? y) { return x + y; }
            public int? Multiply(int? x, int? y) { return x * y; }
            public int? Negate(int? x) { return -x; }
            }
            sealed class CalcSingle : ICalc<Single>, ICalc<Single?>
            {
            public float Add(float x, float y) { return x + y; }
            public float Multiply(float x, float y) { return x * y; }
            public float Negate(float x) { return -x; }

            public float? Add(float? x, float? y) { return x + y; }
            public float? Multiply(float? x, float? y) { return x * y; }
            public float? Negate(float? x) { return -x; }
            }

            Comment

            • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

              #7
              Re: code generation/preprocessing

              colin wrote:
              Is there a way of doing simple code generation inside visual c# express
              such as similar to preprocessing in c++ ?
              >
              I need to generate a library for some vector maths,
              but I need to implement it for different types wich would be difficult to do
              with
              generic types and/or interfaces for numerous reasons
              and would probaly sufer a considerable performance hit.
              Generics is usually very fast, so you should not worry about
              performance.

              But if you can not use it and really need the preprocessor,
              then use the C++ preprocessor !

              For VC++ the command is cl /E or cl /P.

              Arne

              Comment

              • colin

                #8
                Re: code generation/preprocessing

                "Arne Vajhøj" <arne@vajhoej.d kwrote in message
                news:48055452$0 $90265$14726298 @news.sunsite.d k...
                colin wrote:
                >Is there a way of doing simple code generation inside visual c# express
                >such as similar to preprocessing in c++ ?
                >>
                >I need to generate a library for some vector maths,
                >but I need to implement it for different types wich would be difficult to
                >do with
                >generic types and/or interfaces for numerous reasons
                >and would probaly sufer a considerable performance hit.
                >
                Generics is usually very fast, so you should not worry about
                performance.
                >
                But if you can not use it and really need the preprocessor,
                then use the C++ preprocessor !
                >
                For VC++ the command is cl /E or cl /P.
                thanks, ofc the cpp preprocessor why didnt I think of that !
                can I run it inside the VS c# express I wonder ?
                il give it a try ...

                I have used generics and even reflection,
                and generics CAN make things simple and fast,
                but ive found to my cost that for some situations they are awkward and slow.

                some of the methods suggested look like they are very clever to get
                round the limitations, and might run fast in release mode,
                but I hate to think how slow they might make debugging,
                and they realy need c#3.5 to do it effectivly.

                Im still not sure exactly how to best make the library interface,
                but if I have just the basic formulea wich gets converted to use
                whatever interface necessary then that solves a lot of unknowns.

                many thanks
                Colin =^.^=


                Comment

                Working...