473,659 Members | 3,494 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 =^.^=
Jun 27 '08 #1
7 1440
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*********@nt world.NOSPAM.co mwrote in message
news:bA******** ***********@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 =^.^=

Jun 27 '08 #2
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.netNoS pamMwrote in
message news:OJ******** ******@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
http://gregorybeamer.spaces.live.com/lists/feed.rss

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

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


Jun 27 '08 #3
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
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**********@g mail.comwrote in message
news:78******** *************** ***********@d26 g2000prg.google groups.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
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; }
}
Jun 27 '08 #6
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
"Arne Vajhøj" <ar**@vajhoej.d kwrote in message
news:48******** *************** @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 =^.^=
Jun 27 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

51
5254
by: Mudge | last post by:
Please, someone, tell me why OO in PHP is better than procedural.
8
3714
by: Max M | last post by:
Yesterday there was an article on Slashdot: http://books.slashdot.org/article.pl?sid=03/09/04/1415210&mode=flat&tid=108&tid=126&tid=156 It is about automatic code generation. I got interrested in the subject, did a web search, and it seems kind of powerfull. My main interrest is web development in Zope/CMF/Plone, where there is a lot of repeated code in the products. So automated code generation seems
0
2440
by: Rasmus Fogh | last post by:
Someone raised the question of automatic code generation a few weeks back. And yes, we (CCPN) are using automatic Python code generation in a major way. Basically we are making data models in UML, and using automatic code generation to make Python APIs, XML I/O etc. (more below). We can be found at http://www.ccpn.ac.uk/index.html As a general point, automtic code generation would seem like a good idea in special cases where:
1
9500
by: Gina | last post by:
I need to add the cell generation to a templated program. I am using graphics magician, but my problem is with the math. I cannot figure out my cell generations. I do know that I need two different arrays. One array is the original grid, and one is the copy of that grid. But, I am stuck..here is my code, if anyone could please help me I would greatly appreciate it. >// John Horton Conway's "Game of Life" #include "GraphicsMagician.h"
12
1979
by: Francois Grieu | last post by:
Can #include safely use a preprocessing token, as in #define HEADERFILE "stdio.h" #include HEADERFILE int main(void) {return printf("Hello, world\n")*0;} TIA, François Grieu
27
2500
by: Ken Human | last post by:
I want to generate every possible 16 character combination of the characters 0-9, A-Z, and a-z programatically. My current code follows: #include <stdio.h> #include <ctype.h> int main() { char strBuf = {0}; int i, j;
5
2025
by: aarklon | last post by:
Hi all, the following is a question which i found on a book,the reader is asked to predict the output #include<stdio.h> #define SUM(F_NAME,DATA_TYPE,L)\ void F_NAME(DATA_TYPE x,DATA_TYPE y)\ {\
10
2521
by: =?Utf-8?B?Tm90cmUgUG91YmVsbGU=?= | last post by:
Hello, I have a legacy MFC application that has a lot of editor functionality associated with a class that extends CFrameWnd. This editor watches for key down events by using the OnKeyDown handler in the message map. In my legacy MFC application, my OnKeyDown handler is always called, as I would expect. I'm calling the legacy application's editor window from within a C# application. I've written a little C++/CLI application that...
5
6290
by: Francois Grieu | last post by:
One of the C compiler that I use <OT>(Keil's CX51)</OTbarks at #define a(b) b int main(void){return a( #if 0 #endif 0);} More generally, this compiler seems confused by any preprocessing directive in the middle of macro arguments, which comes handy e.g. to
0
8337
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8748
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7359
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6181
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5650
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2754
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1978
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1739
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.