Sign in to follow this  
MrKaktus

Singleton with dynamicalli linked methods ?

Recommended Posts

Hi there I am trying to create singleton class that will handle interface of my module with the programmer.The trick is that I want it to dynamically bind best posisible functionc to it's methods declarations. Currently I have almost done it but mu function's don't have access to class protected variables.

This is my current code example:

[code]

typedef float(*PFNENRCPOINTSIZEMINPROC)(void); // Func ptr typedef

float pointSizeMin_gl20();
float pointSizeMin_gl30();
PFNENRCPOINTSIZEMINPROC enGpuPointSizeMin = NULL;

class layout
{
protected:
static float var;

public:
layout();

float pointSizeMin(void);


friend PFNENRCPOINTSIZEMINPROC;
};

float layout::var;

layout::layout()
{
var = 1.0f;

enGpuPointSizeMin = pointSizeMin_gl30;
}

float layout::pointSizeMin(void)
{
return enGpuPointSizeMin();
}

float pointSizeMin_gl20(void)
{
return 2.0f + layout::var;
}

float pointSizeMin_gl30(void)
{
return 3.0f + layout::var;
}


int main(int argc, char* argv[])
{
layout renderer;
float result = renderer.pointSizeMin();

return 0;
}


[/code]

But during compilation I get this error :

[code]

1>src\main.cpp(48): error C2248: 'layout::var' : cannot access protected member declared in class 'layout'
1> src\main.cpp(19) : see declaration of 'layout::var'
1> src\main.cpp(17) : see declaration of 'layout'


[/code]

It refers to bodies of pointSizeMin_gl20 and pointSizeMin_gl30.

So how to allow these functions to work with private/protected data? Is there any way at all?

Thanks,

MrKaktus

Share this post


Link to post
Share on other sites
You must "friend" individual functions, you cannot friend a function pointer. Why not write the function pointer to take a float as an argument, and have the layout pass the value to the function? Or if all possible implementations are going to add "layout::var" to some constant, you could write them to return their "modifier", and then layout::pointSizeMin could add the modifier to the "var" value.

Also consider not using singletons, they are widely considered to be a bad idea, a glorified global variable. Your layout::var could be a member variable instead of a static. Also, it could have a much better name.

Share this post


Link to post
Share on other sites
[quote name='rip-off' timestamp='1305714632' post='4812449']
You must "friend" individual functions, you cannot friend a function pointer. Why not write the function pointer to take a float as an argument, and have the layout pass the value to the function? Or if all possible implementations are going to add "layout::var" to some constant, you could write them to return their "modifier", and then layout::pointSizeMin could add the modifier to the "var" value.

Also consider not using singletons, they are widely considered to be a bad idea, a glorified global variable. Your layout::var could be a member variable instead of a static. Also, it could have a much better name.
[/quote]

This is just code sample, not the actual code implementation. Layout class is inheriting after singleton template which makes it singleton , to make it simpler I just set "var" as static to give a point of singleton. I want to create layout for my rendering context that could choose which functions to use depending on which version and/or extensions of OpenGL are supported. From the programmer way I will always have the same layout though.

Share this post


Link to post
Share on other sites
You could add a public accessor for the value, if you don't need these functions to ever write to it.

Another option would be to hide everything inside a particular source file. You would have your global data and custom functions in an anonymous namespace in the file. In this way, the "layout" class becomes more of a monostate than a singleton. The upside (and downside) of this is that all direct access to that variable will be limited to a single source file. Or do you want the user to be able to provide their own custom function pointers?

Without knowing the "big picture" it is hard to give more concrete advise. It is possible that the overall design of your class could be re-worked to facilitate such flexibility.

Share this post


Link to post
Share on other sites
The idea is to have this functions to behave like normal methods of singleton. So they can operate on protected variables etc. without using getters and setters methods. I want to have interface that will be thesame from the programmer point of view but be implemented by specific functions depending on which version or extension of OpenGL is supported. When you want to use OpenGL greater than 1.1 or any extension under Windows you first need to bind each OpenGL function to it's pointer. When you do this you check if each version or extension is supported before binding functions corresponding to it. I would like to have singleton with abstract interface that would cover all that versions and extensions with one, intermediate and abstract API supported by it's methods. Therefore for each method I want to have set of specified functions that will cover it's behavior in the best way on different OpenGL versions. This is why I am searching for such mechanism to allow external functions to work on protected stuctures and data of Rendering Context singleton (or some alternative to that singleton).

I think idea of covering all global data in some source file would be great but I think it will be too much code to hide it in only one cpp.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this