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

Mateusz Łoskot mateusz at loskot.net
Sun Mar 12 06:45:31 EST 2006


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:

template<class T>
class Singleton
{
private:

     class InstancePtr
     {
     public:
         InstancePtr() : m_ptr(0) {}
         ~InstancePtr() { delete m_ptr; }

         T* Get()
         {
             return m_ptr;
         }

         void Set(T* p)
         {
             if(p!= 0)
             {
                 if (m_ptr != 0)
                 {
                     delete m_ptr;
                 }
                 m_ptr = p;
             }
         }

     private:

         T* m_ptr;
     };

     static InstancePtr m_instptr;

     Singleton();

     Singleton(const Singleton&);

     Singleton& operator=(const Singleton&);

public:

     static T* Instance()
     {
         if(m_instptr.Get() == 0)
         {
             m_instptr.Set(new T());
         }
         return m_instptr.Get();
     }
};

But it may be better defined using smart pointers as well.

Cheers
-- 
Mateusz Łoskot
http://mateusz.loskot.net



More information about the geos-devel mailing list