[geos-devel] Question about geos::io::Unload::Release()

strk at refractions.net strk at refractions.net
Mon Mar 13 07:48:32 EST 2006


On Mon, Mar 13, 2006 at 01:19:12PM +0100, Mateusz Å?oskot wrote:
> 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);
> }

Yes, this was my idea.

> //////////////////////
> // Example:
> struct A
> {
>     friend class Singleton<A>;
>     void doit() { std::cout << "DO IT\n"; }
> };
> 
> int main()
> {
>     A* ptr = Singleton<A>::instance();

I'd like to keep doing

	A* ptr = A::instance():

template should only be known by class A.

class A {
	Singleton<A> defaultInstance;
public:
	A* getDefaultInstance() { defaultInstance.instance(); }
};

The template itself could return by ref, to make it clean.
The template users (classes that want to provide default instances)
will keep returning pointers if this is what client code expects
(we can always add a return-ref version for new code written, anyway
so far pointers are most used to signal NULLS to class taking
it - those NULL will usually trigger use of the default instance).

--strk;



More information about the geos-devel mailing list