Code reduction

Started by
17 comments, last by mightypigeon 13 years, 3 months ago
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);

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

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

Just looked up macro stringification - been wondering how to do that for the past 10 years. Just a pound sign - aaa! :)
Quote:Original post by DWN
Just 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.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
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..
No, they just say "include".

FWIW, having the interpreter be global smells bad to me. I would just manually call the functions somewhere.
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.
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.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

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".
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
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...

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

This topic is closed to new replies.

Advertisement