Skip to main content
GameDev.net gamedev.net
🔒 Locked

Singleton with dynamicalli linked methods ?

Started by MrKaktus May 18, 2011 at 9:45 AM 3 replies 1.3k views
Original Post
MrKaktus
MrKaktus
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:



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;
}




But during compilation I get this error :



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'




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

rip-off
rip-off
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.
MrKaktus
MrKaktus

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.


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.
rip-off
rip-off
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.
MrKaktus
MrKaktus
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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.