Code reduction

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

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 on other sites
You could possibly use static initialization hacks to make this work:

// At global scopestruct 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 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 on other sites
Quote:
 Original post by DWNJust looked up macro stringification - been wondering how to do that for the past 10 years. Just a pound sign - aaa! :)
# is not a pound sign, it's a hash sign.

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 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 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 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 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 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...

1. 1
2. 2
Rutin
21
3. 3
4. 4
frob
17
5. 5

• 9
• 12
• 9
• 33
• 13
• Forum Statistics

• Total Topics
632591
• Total Posts
3007250

×