• Create Account

# Using Preprocessor Directives to achieve a form of specialisation

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

### #1gretty  Members   -  Reputation: 217

Like
0Likes
Like

Posted 01 October 2012 - 03:00 AM

Hello

The following is an attempt to challenge myself to achieve specialisation by using Preprocessor Directives and is not a serious project. The aim is to attempt to see if its possible so its best to avoid asking why I am doing this and instead see if what I am attempting to do is possible out of fun and interest.

I have an idea to achieve a form of specialisation(well mainly abstraction and virtualisation) in pure C(* See Note at end). The idea comes slightly out of necessity but mostly for fun and I dont think I will pursue this in a real project but I am interested to see if it can be sucessfully implemented.

Example of how I use Preprocessor Directive Macro Functions to achieve 'abstract' functions:

// This is 4dm not pure C. 4dm is a language very similar to C
// except it doesn't have structs, pointers & you cannot declare
// new data types. The language is used to wrtie macros for a civil engineering application.

// Global Variables //
{
Draw_Box windows[3]; // a widget that you can draw text & images to

Integer WINDOW_TEXT;
Integer WINDOW_IMAGE;
}

#define WRITE(str,param1) str(param1)
#define draw_generic_window(windowIndex, type) \
if (type==WINDOW_TEXT)			  WRITE(draw_text_window,windowIndex) \
else if (type==WINDOW_IMAGE)		WRITE(draw_image_window,windowIndex)

void draw_text_window(Integer windowIndex)
{
print("draw_text_window():", windowIndex);
}

{
}

void draw_image_window(Integer windowIndex)
{
print("draw_image_window():", windowIndex);
}

void main()
{
windows[1] = Create_draw_box(100,100,0);
windows[2] = Create_draw_box(100,100,0);
windows[3] = Create_draw_box(100,100,0);

draw_generic_window(1, WINDOW_TEXT);
draw_generic_window(3, WINDOW_IMAGE);

/* At compile time the above should be converted to the following
draw_text_window(1);
draw_image_window(3);*/
}


My Problem: Whilst I can sucessfully use "virtual" functions, ie, use draw_generic_window() it requires me to always pass the type of the window as a parameter in the function. Can you think of a way where I wont have to pass the windows' type and the program will figure it out on its own? I hope this makes sence, if you look at the next example you will see where my problem is.

// More globals
{
Integer WINDOW_NUM = 0;
Integer windowTypes[3];
}

void create_window(Integer type)
{
if (WINDOW_NUM >= 3)
return;

WINDOW_NUM++;
windows[WINDOW_NUM]	 = Create_draw_box(100,100,0);
windowTypes[WINDOW_NUM] = type;
}

void draw_all_windows()
{
// arrays start at 1 in 4dm. 3 is the number of Draw_Boxes(windows) I have
for (Integer i=1; i<=3; i++)
{
// PROBLEM HERE: when I call the following macro windowTypes[i] will always be 0
// Any ideas?
draw_generic_window(i, windowTypes[i]);
}
}

void main()
{
create_window(1);
create_window(2);
create_window(2);

while (1) {
draw_all_windows();
}
}


Any ideas how I can overcome this next issue?

Note: This is actually programmed in a language very similar to C called 4dm. Its the same except it has no pointers, structs and you cannot define new data types. But it does have pass by reference and preprocessor instructions. So that may provide a bit more of a reason why I am trying to achieve specialisation in this way. http://www.12dmodel....n/12d_progm.pdf

Another of my reasons is that I want to avoid the following method that also achieves specialisation but its inefficient:

void draw_all_windows()
{
// Every draw loop I am checking what type the window is which can be inefficient and make the program not scalable
// So you can see how coming up with a preprocessor solution would be efficient aswell as a little elegant.

// arrays start at 1 in 4dm. 3 is the number of Draw_Boxes(windows) I have
for (Integer i=1; i<=3; i++)
{
switch (windowTypes[i])
{
case WINDOW_TEXT:
{
draw_text_windiw(i);
}
break;
case WINDOW_IMAGE:
{
draw_image_windiw(i);
}
break;
....
}
}
}


Edited by gretty, 01 October 2012 - 03:05 AM.

### #2gretty  Members   -  Reputation: 217

Like
0Likes
Like

Posted 01 October 2012 - 08:29 PM