[geos-devel] Question about geos::io::Unload::Release()
Mateusz Łoskot
mateusz at loskot.net
Mon Mar 13 07:19:12 EST 2006
strk at refractions.net wrote:
> On Sun, Mar 12, 2006 at 12:45:31PM +0100, Mateusz Å?oskot wrote:
>
>>strk at refractions.net wrote:
>>
>>>static lifetime should be ok. I dunno if it's used by client code,
>>>anyway it might be useful to have a 'default' factory to use instead
>>>of having to construct (and maintain alive) a custom one. I'd move
>>>access to it to GeometryFactory::defaultInstance()
>>
>>I see the idea behind default factory.
>>So, I'd make it a singleton, constructed on first request of its
>>instance. As I understand defaultInstance() is sucha a solution.
>>The only problem is to properly define Singleton class.
>>
>>Here is some simple proposal:
>
>
> ... mmm .. simple ? :)
IMHO it's simple and extensible.
This template class can be used to implement singleton for any type of
our classes.
> How about:
>
> class PrecisionModel {
> public:
> .....
> const PrecisionModel* defaultInstance() {
> static PrecisionModel defInstance(PrecisionModel::defaultInstance());
> return &defInstance;
> }
> };
>
> class GeometryFactory {
> public:
> .....
> const GeometryFactory* defaultInstance() {
> static GeometryFactory defInstance(PrecisionModel::defaultInstance());
> return &defInstance;
> }
> };
It's OK, but slightly different idea.
I mean, the first I gave is for global objects representation.
For less typing, I'd suggest to use a little modified version ;-)
template <typename T>
class Singleton
{
~Singleton() {}
public:
static T* instance();
};
template <typename T>
T* Singleton<T>::instance()
{
static T single;
return(&single);
}
//////////////////////
// Example:
struct A
{
friend class Singleton<A>;
void doit() { std::cout << "DO IT\n"; }
};
int main()
{
A* ptr = Singleton<A>::instance();
ptr->doit();
return 0;
}
Another option, I'd say recommended, is to return reference instead of
pointer. I'm only not sure if GCC will handle it well.
I mean, if gcc can destroy object earlier, when still holding by
reference - behaviour undefined. Though, this would be bug in gcc.
Here is example of usage with reference:
A a = Singleton<A>::instance();
a.doit();
A b = Singleton<A>::instance();
b.doit();
a and b refer to the same instance.
Cheers
--
Mateusz Łoskot
http://mateusz.loskot.net
More information about the geos-devel
mailing list