473,406 Members | 2,698 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Globals from a namespace

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?

Mar 6 '07 #1
5 1823
jg*******@gmail.com wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Mar 6 '07 #2
jg*******@gmail.com wrote:
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?
// 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?

Mar 6 '07 #3
Victor Bazarov wrote:
jg*******@gmail.com wrote:
>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().

Mar 6 '07 #4
>>
>>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
Mar 6 '07 #5
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 Bazarov wrote:
jg*******@gmail.com wrote:
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
Mar 7 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Nel | last post by:
I have a question related to the "security" issues posed by Globals ON. It is good programming technique IMO to initialise variables, even if it's just $foo = 0; $bar = ""; Surely it would...
3
by: Robert Dodier | last post by:
Hello, I'm interested in introducing new variables into the environment of a Python interpreter or program. In reading through old posts to this newsgroup, I see there is an often-repeating...
2
by: tedsuzman | last post by:
----- def f(): ret = 2 exec "ret += 10" return ret print f() ----- The above prints '12', as expected. However,
45
by: It's me | last post by:
I am new to the Python language. How do I do something like this: I know that a = 3 y = "a" print eval(y)
0
by: Geert Jansen | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, I'm experiencing a weird problem with eval(), providing it a copy of the globals() dictionary. The following code works:
18
by: robert | last post by:
Using global variables in Python often raises chaos. Other languages use a clear prefix for globals. * you forget to declare a global * or you declare a global too much or in conflict * you...
5
by: Steven W. Orr | last post by:
I have two seperate modules doing factory stuff which each have the similar function2: In the ds101 module, def DS101CLASS(mname,data): cname = mname+'DS101' msg_class = globals() msg =...
1
by: cokofreedom | last post by:
if __name__ == '__main__': print "Globals (For Loop):" try: for i in globals(): print "\t%s" % i except RuntimeError: print "Only some globals() printed\n" else: print "All globals()...
0
by: Gabriel Genellina | last post by:
En Sat, 12 Jul 2008 16:15:36 -0300, Akathorn Greyhat <akathorn@gmail.com> escribi�: Welcome! You have to pass in the namespace of the desired module - instead of globals. I'd use an...
7
by: CapnBearbossa | last post by:
hi all, forgive me , but the RTFM and Google search approaches are not yielding an answer on this question. I need to know if there's a top level python interpreter command that clears all user...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.