Sign in to follow this  

Code reduction

This topic is 2539 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've boiled this section of my C++ code down to the following:



void hello() {
cout<<"Hello world!";
}

void goodbye() {
cout<<"Goodbye world!";
}

#define scriptRegisterFunction(func) EXPORTCH void func##ChDL(void* varg) { ChInterp_t interp; ChVaList_t ap; Ch_VaStart(interp,ap,varg); func(); Ch_VaEnd(interp,ap); }

#define scriptDeclareFunction(funcString,func) Ch_DeclareFunc(interp,"void "##funcString##"();",func##ChDL);

scriptRegisterFunction(hello);
scriptRegisterFunction(goodbye);

void init() {
Ch_Initialize(&interp,NULL);
scriptDeclareFunction("hello",hello);
scriptDeclareFunction("goodbye",goodbye);
}



I would like to get it to look something like this instead, but don't know how:


void hello() {
cout<<"Hello world!";
}

void goodbye() {
cout<<"Goodbye world!";
}

//..whatever macros & such needed here..

scriptRegister(hello);
scriptRegister(goodbye);


Share this post


Link to post
Share on other sites
You could possibly use static initialization hacks to make this work:

// At global scope
struct SetupFoo
{
SetupFoo() { register_and_declare_foo(); }
} SetupFoo_;



Conversion to macros for general use is left as an exercise to the reader [smile]


However, be aware of the static order of initialization problem before using this too readily. Also, using it in DLLs is not recommended.

Share this post


Link to post
Share on other sites
Not sure if you're being serious - don't think anyone says hash-define or hash-include - sound like hash table methods. In other news, think I've almost figured out a solution. I hope..

Share this post


Link to post
Share on other sites
Who is "they"? I hear pound-define a lot, because it's more specific than just saying define. The interpretter's an object, but written that way in the snippet because it's easier to read.

Share this post


Link to post
Share on other sites
Quote:
Original post by iMalc
# is not a pound sign, it's a hash sign.
You know, I would be just as confused if somebody called the language C-hash as I would with C-pound...

Anyway, in that strange language they call 'American English', the hash symbol is called a 'pound', not to be confused with £ sterling.

Share this post


Link to post
Share on other sites
Quote:
Original post by swiftcoder
Quote:
Original post by iMalc
# is not a pound sign, it's a hash sign.
You know, I would be just as confused if somebody called the language C-hash as I would with C-pound...

Anyway, in that strange language they call 'American English', the hash symbol is called a 'pound', not to be confused with £ sterling.
Interesting! Where I'm from you've just written the actual pound sign: £. Never heard of sterling in the context of a symbol.

Until reading this post I had never heard or read of anyone saying "pound include", my first impression of which is that it sounds absurdly silly, even though I'm sure it makes perfect sense where you come from.
Everyone I know and everything I've ever read says "Hash include", or just "include".

Share this post


Link to post
Share on other sites
Quote:
Original post by iMalc
]Interesting! Where I'm from you've just written the actual pound sign: £. Never heard of sterling in the context of a symbol.
Pound Sterling is the official name of the British currency - I believe it dates back to when pound coins were actually cast from sterling silver.
Quote:
Until reading this post I had never heard or read of anyone saying "pound include", my first impression of which is that it sounds absurdly silly, even though I'm sure it makes perfect sense where you come from. Everyone I know and everything I've ever read says "Hash include", or just "include".
I am a Brit, so I call it 'hash', or 'sharp' if it is a musical context. I hear 'pound' all the time here in the US though...

Share this post


Link to post
Share on other sites
Oh, come on. It's perfectly well understood that there exist regional variations on the English language, including terminology; it isn't "right" or "wrong" to use one term over another, nor is it "absolutely" anything.

People aren't uniform across the planet. Get over it [razz]

Share this post


Link to post
Share on other sites
Heh, I had a vague notion the term had something to do with the symbol being the button you "pound" after pressing a string of numbers on the pad or when trying to navigate a phone kiosk, but apparently it was actually derived from lb.

Share this post


Link to post
Share on other sites
Quote:
Original post by mightypigeon
edit: The reason (I believe, at least) people call the hash a pound is because on UK keyboards the symbol above 3 is a pound, and not a hash.
Yes, and in fact I have no idea how one can type a hash symbol on a UK keyboard. Mac's let you type one with alt-3, but PC's don't have those hand-dandy alt-symbols...

Share this post


Link to post
Share on other sites
Quote:
Original post by swiftcoder
Quote:
Original post by mightypigeon
edit: The reason (I believe, at least) people call the hash a pound is because on UK keyboards the symbol above 3 is a pound, and not a hash.
Yes, and in fact I have no idea how one can type a hash symbol on a UK keyboard. Mac's let you type one with alt-3, but PC's don't have those hand-dandy alt-symbols...


You can do it with an alt code on windows, alt-35 (I assume it will give the same result in the UK ;) )

Share this post


Link to post
Share on other sites

This topic is 2539 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this