Globals from a namespace

J

jgraveskc

I know this sounds nasty, but let me explain.
I have what was a class that contains a whole buncha function pointers
(for OpenGL extensions) that are used in the app. I was porting this
app to macs, where the extensions are properly present (just normal
functions) so the class is not needed. I was wanting a way to make
the same code work on both. For example, if I could do this:
glGenBuffersARB(bla); on both. On the mac its just accessing the
OpenGL function, on the Windows machine it would have a using
namespace whatever; at the top and is actually accessing those
function pointers of the same name.

This seemed like it would work fine for my purposes, but I can't seem
to get the namespace pointers to be truly shared across the app. It
seems like every function that wants to use them has to assign them
itself.

Right now it looks like this (this isn't a copy paste [hopefully
obviously] just an example):
namespace whatever
{
static PtrType func1;
static PtrType2 func2;
// etc

InitPointers(); // defined in cpp; assignes all the function pointers
with wglGetProcAddress
};


Any ideas to make this work without having to do a whole lot of #ifdef
WIN32 #else's in the code?
 
V

Victor Bazarov

I know this sounds nasty, but let me explain.
I have what was a class that contains a whole buncha function pointers
(for OpenGL extensions) that are used in the app. I was porting this
app to macs, where the extensions are properly present (just normal
functions) so the class is not needed. I was wanting a way to make
the same code work on both. For example, if I could do this:
glGenBuffersARB(bla); on both. On the mac its just accessing the
OpenGL function, on the Windows machine it would have a using
namespace whatever; at the top and is actually accessing those
function pointers of the same name.

This seemed like it would work fine for my purposes, but I can't seem
to get the namespace pointers to be truly shared across the app. It
seems like every function that wants to use them has to assign them
itself.

Right now it looks like this (this isn't a copy paste [hopefully
obviously] just an example):
namespace whatever
{
static PtrType func1;
static PtrType2 func2;
// etc

Why 'static'? What if you drop 'static'? If you have this in the
header, you need to add 'extern'. Then in one of the C++ modules
you need to actually initialise them with something (NULL should be
fine), and then make 'InitPointers()' be called from another object's
initialisation:

int dummy = (InitPointer(), 42);
InitPointers(); // defined in cpp; assignes all the function pointers
with wglGetProcAddress
};


Any ideas to make this work without having to do a whole lot of #ifdef
WIN32 #else's in the code?

See above.

V
 
R

Rolf Magnus

I know this sounds nasty, but let me explain.
I have what was a class that contains a whole buncha function pointers
(for OpenGL extensions) that are used in the app. I was porting this
app to macs, where the extensions are properly present (just normal
functions) so the class is not needed. I was wanting a way to make
the same code work on both. For example, if I could do this:
glGenBuffersARB(bla); on both. On the mac its just accessing the
OpenGL function, on the Windows machine it would have a using
namespace whatever; at the top and is actually accessing those
function pointers of the same name.

This seemed like it would work fine for my purposes, but I can't seem
to get the namespace pointers to be truly shared across the app. It
seems like every function that wants to use them has to assign them
itself.

Right now it looks like this (this isn't a copy paste [hopefully
obviously] just an example):
namespace whatever
{
static PtrType func1;
static PtrType2 func2;

Why are those static? That means every translation unit that wants to use
them needs its own set of pointers. Maybe that's where your problem is
coming from?
 
R

Rolf Magnus

Victor said:
I know this sounds nasty, but let me explain.
I have what was a class that contains a whole buncha function pointers
(for OpenGL extensions) that are used in the app. I was porting this
app to macs, where the extensions are properly present (just normal
functions) so the class is not needed. I was wanting a way to make
the same code work on both. For example, if I could do this:
glGenBuffersARB(bla); on both. On the mac its just accessing the
OpenGL function, on the Windows machine it would have a using
namespace whatever; at the top and is actually accessing those
function pointers of the same name.

This seemed like it would work fine for my purposes, but I can't seem
to get the namespace pointers to be truly shared across the app. It
seems like every function that wants to

Right now it looks like this (this isn't a copy paste [hopefully
obviously] just an example):
namespace whatever
{
static PtrType func1;
static PtrType2 func2;
// etc

Why 'static'? What if you drop 'static'? If you have this in the
header, you need to add 'extern'. Then in one of the C++ modules
you need to actually initialise them with something (NULL should be
fine),

Globals are automatically initialized (to zero in case of PODs).
and then make 'InitPointers()' be called from another object's
initialisation:

int dummy = (InitPointer(), 42);

I'm not sure that's possible in this case. The function might itself need
some initialization of the OpenGL before, and I'm not sure if that is safe
before main().
 
J

John Harrison

Right now it looks like this (this isn't a copy paste [hopefully
obviously] just an example):
namespace whatever
{
static PtrType func1;
static PtrType2 func2;


Why are those static? That means every translation unit that wants to use
them needs its own set of pointers. Maybe that's where your problem is
coming from?

I would guess because originally 'whatever' was a class, which at some
point got changed to a namespace.

john
 
J

jgraveskc

extern fixed it, thank you! I was unaware of this (probably
obviously).
By the way, I did require explicitly initializing them in a CPP file
to prevent linker errors in response to those mentioning it.

Thanks a lot guys!

- Justin

Victor said:
I know this sounds nasty, but let me explain.
I have what was a class that contains a whole buncha function pointers
(for OpenGL extensions) that are used in the app. I was porting this
app to macs, where the extensions are properly present (just normal
functions) so the class is not needed. I was wanting a way to make
the same code work on both. For example, if I could do this:
glGenBuffersARB(bla); on both. On the mac its just accessing the
OpenGL function, on the Windows machine it would have a using
namespace whatever; at the top and is actually accessing those
function pointers of the same name.

This seemed like it would work fine for my purposes, but I can't seem
to get the namespace pointers to be truly shared across the app. It
seems like every function that wants to use them has to assign them
itself.

Right now it looks like this (this isn't a copy paste [hopefully
obviously] just an example):
namespace whatever
{
static PtrType func1;
static PtrType2 func2;
// etc

Why 'static'? What if you drop 'static'? If you have this in the
header, you need to add 'extern'. Then in one of the C++ modules
you need to actually initialise them with something (NULL should be
fine), and then make 'InitPointers()' be called from another object's
initialisation:

int dummy = (InitPointer(), 42);
InitPointers(); // defined in cpp; assignes all the function pointers
with wglGetProcAddress
};


Any ideas to make this work without having to do a whole lot of #ifdef
WIN32 #else's in the code?

See above.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top