11

I recently came across this sort of code in someone's opengl shader class and am not sure of its use.

As I understand it from reading IBM's documentation, the #define ONEWORD will remove any occurence of ONEWORD in the subsequent text.

What is the purpose of having ONEWORD in this code at all if all occurrences are removed? What does having a token like that, after a class keyword but before a class name, really mean?
I've only used #define for include guards in the past so this is entirely new for me.

#define ONEWORD

class ONEWORD FooClass
{
    FooClass();
    ~FooClass();
};

The code I saw this in is here: https://dl.dropbox.com/u/104992465/glsl.h
Just in case I've made its context too abstract.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Katawa Tenshu
  • 117
  • 1
  • 5
  • 1
    Often I've seen two-legged animals walking into some mysterious square enclosures, what's that all about? [Well, to answer it myself, it is about removing critical information about what I've observed, and generalizing my own impression, and then asking about that.] – Cheers and hth. - Alf Apr 04 '13 at 06:31

2 Answers2

7

It's to allow you to easily add compiler specific keywords to your class declaration. For instance with Visual Studio, if you wanted to put this class in a DLL, you would change your definition to

#define ONEWORD __declspec( dllexport )

See here for another example

john
  • 85,011
  • 4
  • 57
  • 81
6

Oh, so after looking at the actual code, it's not ONEWORD, but rather GLSAPI. These XYZ_API macros are often used for conditionally specifying platform-specific linkage, such as some __attributes__ which require different treatment on, for example, Windows and Unixes. So you can expect GLSAPI to be defined in one of the header files (maybe in config.h) like this:

#ifdef WIN32
#    define GLSAPI __dllimport
#elif defined __linux__
#    define GLSAPI __attribute__((visibility("visible")))
#else
#    define GLSAPI
#endif

(Pseudo-code, I'm not sure about all the attributes and linkage "qualifiers", but you can look them up in the code.)

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • 1
    The #else if line is dubious. – Jonathan Leffler Apr 04 '13 at 06:24
  • In the actual code, it wasn't defined like this. There was just the line `#define GLSAPI` – maditya Apr 04 '13 at 06:24
  • @maditya I'm not saying how it **was** defined, I was trying to explain how it **tends to be** defined, and what's the reason for that. –  Apr 04 '13 at 06:27
  • @H2CO3: the actual code appears to be a quick-n-dirty adaption for static linking of the library. – Cheers and hth. - Alf Apr 04 '13 at 06:30
  • @Cheersandhth.-Alf Nice. Nevertheless, I'm still right, aren't I? –  Apr 04 '13 at 06:31
  • yeah, or it can be defined in the compiler invocation – Cheers and hth. - Alf Apr 04 '13 at 06:33
  • @Cheersandhth.-Alf Could you explain the static linking thing? I've never seen it before ... – maditya Apr 04 '13 at 06:34
  • @madity: that's the comment that's attached to the definition of `GLSAPI` as nothing. presumably for linking as a shared library this symbol is defined as `_declspec( dllexport )` on Windows. so on. google it – Cheers and hth. - Alf Apr 04 '13 at 06:36
  • I thought it might be that, but in that case why not define it the way @H2CO3 has done, i.e. wrap the #define with an #ifdef? – maditya Apr 04 '13 at 06:41
  • @maditya: i can only speculate, but often in my own work i have found that people tend to do what's least work for themselves and most impractical (up to a limit) for the next guy. not only does that make one able to finish on time, but compared to next guy one looks good. -> promotion follows. ;-) – Cheers and hth. - Alf Apr 04 '13 at 06:43
  • Very enlightening replies for this question, I appreciate the responses and am sorry for not proving enough context in the code given. I now know what this pattern was used for and what to look for to learn more about it. – Katawa Tenshu Apr 04 '13 at 07:26