[Vala] [Suggestion] New class attribute - Singleton




Currently, it is hard to create pure singleton in Vala. Sure, I can implicate myself
get_instance() method and make all constructors private, but outside usage of
the type could call g_object_new(type) and ruin it all.
An example for this would be the libpeas plugin extensions registration(by
type).

There is a solution for that, introduced here:
http://blogs.gnome.org/xclaesse/2010/02/11/how-to-make-a-gobject-singleton/

This attribute[Singleton], shell is relevant for all class types:
* GObject derived
* Non GObject classes
* Compact classes
No classes constructor are allowed(even private).
Only "construct {}" block is allowed.
Of course finalizers would act normal(both for ref_counted and not, see next).

this attribute shell have this properties:
1. "thread_safe"-bool: Whether construction should be thread safe or not.
Default: false.
Exported to Vapi binding.
2. "mutual_lock"-bool: Whether the lock for thread safety is mutual, or int-lock.
Most constructors shell have int-lock(inter lock) since it's non block
operation, but some constructor have blocking operations, so it is better to
have mutual lock for them.
Default: false.

Doesn't exported to Vapi binding.
Ignored if thread_safe=false.
3. "ref_counted"-bool: Whether the instance is ref counted or not(live for ever).
If true, get_instance() shell return owned instance, otherwise, an owned
instance.
Default: false.

Exported to Vapi binding.
Ignored(live forever) if the class is compact and doesn't have ref_function and
unref_function in CCode attribute.
4. "get_instance_func"-string: Name of the generated func for getting the
instance, or null to default method("get_instance")

Default: null.

Exported to Vapi binding.

I think it will be great if Vala would support this. It will makes Singleton more
pure and easier.
I know some of you may think Singletons are bad, but it has good usage(at
least for me).
What do you think?

Tal
                                          


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]