Re: [Vala] Singleton and reference count



public class Singleton<G>
{
  public G instance {get; private set; }

  public Singleton(owned G instance)
  {
    _instance = (owned) instance;
  }
}

public class Prefs
{
  internal Prefs ()
  {
    stdout.printf ("constructor\n");
  }

  public void do_something()
  {
    stdout.printf("Doing something...\n");
  }

  ~Prefs ()
  {
    stdout.printf ("destruction\n");
  }
}

Singleton<Prefs> preferences;

public static void main (string[] args)
{
  preferences = new Singleton<Prefs>(new Prefs());

  preferences.instance.do_something();
  preferences.unref();
}


What's the point of this code? The Singleton class is redundant, you can
do exactly the same just by storing a single instance of Prefs in a
global variable. That's not a singleton.
Note: you should use preferences = null, rather than unref()

I guess is a way to have a generic way to add the singleton behavior
to a class, just as this proposed (and rejected) utility in boost:
http://torjo.com/tobias/

vmjl



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