Jump to content
  • Advertisement
Sign in to follow this  
DWN

Code reduction

This topic is 2904 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
Advertisement
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
Just looked up macro stringification - been wondering how to do that for the past 10 years. Just a pound sign - aaa! :)

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
No, they just say "include".

FWIW, having the interpreter be global smells bad to me. I would just manually call the functions somewhere.

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 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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!