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

Mateusz Łoskot mateusz at loskot.net
Mon Mar 13 08:20:11 EST 2006


strk at refractions.net wrote:
> 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.

I don't agree.
Template is not a type.
It's a template for new type.
Template is not a memory or objects, etc.
You can use same tempalate to generate many similar objects:
use template for singleton to make types A, B, C, etc. behaving as a 
singleton.

Just like having a one form and use it to make e.g. 100 cakes.

> class A {
> 	Singleton<A> defaultInstance;
> public:
> 	A* getDefaultInstance() { defaultInstance.instance(); }
> };
> 
> The template itself could return by ref, to make it clean.

Yes, but there are still problems explained in my latest post about 
breaking Meyers' singleton.

> 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).

I don't agree.
You MUST NOT expect that singleton will be null, because it's always 
instantiated! That's how singleton works.

I'd also propose to not to double interfaces for references and 
pointers. In example, this does not to seem resonable:

struct CoordinateLessThen {
	bool operator()(const Coordinate* a, const Coordinate* b) const;
	bool operator()(const Coordinate& a, const Coordinate& b) const;
};

Why not to keep it simple

struct CoordinateLessThen {
	bool operator()(const Coordinate& a, const Coordinate& b) const;
};

This interface can work well with pointers (by dereferencing) and 
references.

Simply, I'm trying to say that references are much more recommended 
where pointers can be used.

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



More information about the geos-devel mailing list