By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,948 Members | 1,692 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,948 IT Pros & Developers. It's quick & easy.

code generation/preprocessing

P: n/a
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 =^.^=
Jun 27 '08 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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
http://gregorybeamer.spaces.live.com/lists/feed.rss

or just read it:
http://gregorybeamer.spaces.live.com/

*************************************************
| Think outside the box!
|
*************************************************
"colin" <co*********@ntworld.NOSPAM.comwrote in message
news:bA*******************@newsfe5-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 =^.^=

Jun 27 '08 #2

P: n/a
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)" <No************@comcast.netNoSpamMwrote in
message news:OJ**************@TK2MSFTNGP05.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
http://gregorybeamer.spaces.live.com/lists/feed.rss

or just read it:
http://gregorybeamer.spaces.live.com/

*************************************************
| Think outside the box! |
*************************************************
"colin" <co*********@ntworld.NOSPAM.comwrote in message
news:bA*******************@newsfe5-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 =^.^=


Jun 27 '08 #3

P: n/a
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:
http://www.pobox.com/~skeet/csharp/m...operators.html
http://www.pobox.com/~skeet/csharp/g...operators.html
download from:
http://www.pobox.com/~skeet/csharp/miscutil/

Marc
Jun 27 '08 #4

P: n/a
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" <ma**********@gmail.comwrote in message
news:78**********************************@d26g2000 prg.googlegroups.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:
http://www.pobox.com/~skeet/csharp/m...operators.html
http://www.pobox.com/~skeet/csharp/g...operators.html
download from:
http://www.pobox.com/~skeet/csharp/miscutil/

Marc

Jun 27 '08 #5

P: n/a
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<TValue>.Instance = calc;
CalcCache<TValue?>.Instance = calc;
}
public static T Add<T>(T x, T y) {
return CalcCache<T>.Instance.Add(x, y);
}
public static T Multiply<T>(T x, T y)
{
return CalcCache<T>.Instance.Multiply(x, y);
}
public static T Negate<T>(T x)
{
return CalcCache<T>.Instance.Negate(x);
}
static class CalcCache<T>
{
private static ICalc<Tinstance = new CalcNotSupported<T>();
public static ICalc<TInstance
{
get
{
return instance;
}
set
{
if (instance == null) throw new
ArgumentNullException("Instance");
instance = value;
}
}
}
}
interface ICalc<T>
{
T Add(T x, T y);
T Multiply(T x, T y);
T Negate(T x);
// etc
}

sealed class CalcNotSupported<T: ICalc<T>
{
public T Add(T x, T y) { throw new NotSupportedException(); }
public T Multiply(T x, T y) { throw new NotSupportedException(); }
public T Negate(T x) { throw new NotSupportedException(); }
}
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; }
}
Jun 27 '08 #6

P: n/a
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
Jun 27 '08 #7

P: n/a
"Arne Vajhøj" <ar**@vajhoej.dkwrote in message
news:48***********************@news.sunsite.dk...
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 =^.^=
Jun 27 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.