FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Singletons
Goto page 1, 2  Next
 
Post new topic   Reply to topic     Forum Index -> Sinbad
View previous topic :: View next topic  
Author Message
larsivi
Site Admin


Joined: 27 Mar 2004
Posts: 453
Location: Trondheim, Norway

PostPosted: Mon Apr 26, 2004 8:58 am    Post subject: Singletons Reply with quote

There are several singletons in OGRE. I think it is okay to keep 'em as such in Sinbad too, but how should we implement them?

As a module is probably out of the questions, since many of the singletons inherits Root.
Then it needs to be some class. I'll look into the possibilities. As long as we're aware of the 'problem', it's probably no rush.
Back to top
View user's profile Send private message
csauls



Joined: 27 Mar 2004
Posts: 278

PostPosted: Mon Apr 26, 2004 10:26 am    Post subject: Reply with quote

I've been thinking about the same thing, and how to deal with it. There's probably a number of ways to do it. One possibility is using private/protected constructors and static instances, probably living at module scope.
_________________
Chris Nicholson-Sauls
Back to top
View user's profile Send private message AIM Address Yahoo Messenger
larsivi
Site Admin


Joined: 27 Mar 2004
Posts: 453
Location: Trondheim, Norway

PostPosted: Tue Apr 27, 2004 1:54 am    Post subject: Reply with quote

I thought a second that the template approach used in OGRE also could work in D, but at some point it will lead to MI. Since Root seems to THE most important class, I now believe that this is one of the problems we should solve first.
Back to top
View user's profile Send private message
csauls



Joined: 27 Mar 2004
Posts: 278

PostPosted: Tue Apr 27, 2004 12:25 pm    Post subject: Reply with quote

larsivi wrote:
I thought a second that the template approach used in OGRE also could work in D, but at some point it will lead to MI.


Aka something like:
Code:

class Root : Singleton!(Root) {
}


*nods* It might've worked, if only. Maybe we could do something similar, just at the class level instead of a singleton parent class? Just write them along the lines of:

Code:

class Root {
  static this() {
    _singleton = new Root();
  }
  public:
    static Root singleton() {
      return _singleton;
    }
  protected:
    this() {
      // ...
    }
  private:
    static Root _singleton;
}

// ... user code

Root root = Root.singleton;
// ... do stuff with root


Its pretty short and simple, wouldn't be a hassle to write in where needed.
_________________
Chris Nicholson-Sauls
Back to top
View user's profile Send private message AIM Address Yahoo Messenger
larsivi
Site Admin


Joined: 27 Mar 2004
Posts: 453
Location: Trondheim, Norway

PostPosted: Wed Apr 28, 2004 9:59 am    Post subject: Reply with quote

Why the 'static this()'? Just have an if in the singleton getter:

Code:

if (_singleton is null) {
  _singleton = new Root();
}
Back to top
View user's profile Send private message
csauls



Joined: 27 Mar 2004
Posts: 278

PostPosted: Wed Apr 28, 2004 11:08 am    Post subject: Reply with quote

Serves the same purpose, really, except the if(){...} will occur later... but it does look cleaner, I'll agree on that. If only interfaces could have static members... then we could have an iSingleton requiring the .singleton() method.
_________________
Chris Nicholson-Sauls
Back to top
View user's profile Send private message AIM Address Yahoo Messenger
csauls



Joined: 27 Mar 2004
Posts: 278

PostPosted: Sun May 16, 2004 9:39 am    Post subject: Mixins Reply with quote

If Walter's mixins spec flies, and becomes implemented anytime soon, I daresay that would be the way to have our Singletons done. As it is I'm having to copy-paste the same code into each Singleton class, when we could (now hopefully) just do:

Code:

module sinbad.singleton;

template Singleton(alias T)
{
  public static T singleton()
  {
    if (_singleton is null) {
      _singleton = new T;
    }
    return _singleton;
  }

  private static T _singleton;
}

Code:

module sinbad.root;

private import sinbad.singleton;

class Root
{
mixin Singleton!(Root);

protected:
  this()
  {
  }
}

_________________
Chris Nicholson-Sauls
Back to top
View user's profile Send private message AIM Address Yahoo Messenger
larsivi
Site Admin


Joined: 27 Mar 2004
Posts: 453
Location: Trondheim, Norway

PostPosted: Sun May 16, 2004 12:07 pm    Post subject: Reply with quote

That would be a great feature! Mixins could be used for many of the cases where C/C++ use macros.
Back to top
View user's profile Send private message
csauls



Joined: 27 Mar 2004
Posts: 278

PostPosted: Sun May 16, 2004 12:18 pm    Post subject: Reply with quote

larsivi wrote:
Mixins could be used for many of the cases where C/C++ use macros.

My thoughts exactly. As soon as I read the mixin spec, I started picturing templates getting used much as #define's are in C++. Only with a little filtering on their parameters. And hopefully not as often. I'd hate to see this turn D into a mixin-jungle the way C++ is a preproc-jungle. But I guess we're going off-topic. Smile
_________________
Chris Nicholson-Sauls
Back to top
View user's profile Send private message AIM Address Yahoo Messenger
kris



Joined: 27 Mar 2004
Posts: 1494
Location: South Pacific

PostPosted: Thu May 20, 2004 11:03 am    Post subject: Reply with quote

larsivi wrote:
Why the 'static this()'? Just have an if in the singleton getter:

Code:

if (_singleton is null) {
  _singleton = new Root();
}

the use of "static this" avoids issues related to thread contention over singleton construction. An entire subculture grew up around this issue within Java <g>
Back to top
View user's profile Send private message
larsivi
Site Admin


Joined: 27 Mar 2004
Posts: 453
Location: Trondheim, Norway

PostPosted: Sun May 30, 2004 2:01 am    Post subject: Reply with quote

The indentation of the singleton module seems to be messed up in my Vim (on Windows). Edit: This is also the case with the copy in the repository, see web svn.

Also, why do we use 'alias clazz' (corny) instead of T? (Won't argue, just wondering.)
Back to top
View user's profile Send private message
csauls



Joined: 27 Mar 2004
Posts: 278

PostPosted: Thu Jun 17, 2004 2:44 pm    Post subject: Reply with quote

The indentation is indeed screwy, and its DIDE's fault. No, seriously, it is, honest. I'm switching back to EditPlus for the time being so that should be fixed shortly. I'll also be fixing a curious pseudo-error of mine, the reasoning behind which eludes even me, and I'm the one that typed the darn things...

And I used clazz because A) the parameter is supposed to be a class, so its descriptive, B) I guess its just one of those things I learned from Java.
_________________
Chris Nicholson-Sauls
Back to top
View user's profile Send private message AIM Address Yahoo Messenger
larsivi
Site Admin


Joined: 27 Mar 2004
Posts: 453
Location: Trondheim, Norway

PostPosted: Sat Jan 29, 2005 10:27 am    Post subject: Reply with quote

Funny thing; I just noticed that none of the constructors in the OGRE classes inheriting Singleton are protected. Instead all of them are public. I suppose this means that there are uses of these classes where the singleton approach don't fit. The common base for the samples use the EventProcessor constructor directly, even if it derives from Singleton. Well, it's easy to fix. Just switching 'protected' to 'public' in the Singleton mixin Smile

Edit: NOT that easy to fix as the constructor isn't part of the mixin. Well, I'll get by.
Back to top
View user's profile Send private message
csauls



Joined: 27 Mar 2004
Posts: 278

PostPosted: Sat Jan 29, 2005 2:31 pm    Post subject: Reply with quote

Hmf.. strangeness. Okay, then the "Singletons have protected constructors" policy is bunk.

You know... and you may shoot me for saying this if you wish... but it might be good to scrap the current SVN image and start from scratch. Its not like we have anything significant done. I just think it would be good to start over and better plan the layout of the code, and plus its just been a good long while and we've all changed a little. Smile

Glad to see Sinbad isn't forgotten... I just haven't been able to tend it, but that might change soon (now that I'm employed again). I also was hoping to wait for D 1.0 but that looks further and further away.
_________________
Chris Nicholson-Sauls
Back to top
View user's profile Send private message AIM Address Yahoo Messenger
larsivi
Site Admin


Joined: 27 Mar 2004
Posts: 453
Location: Trondheim, Norway

PostPosted: Sat Jan 29, 2005 5:09 pm    Post subject: Reply with quote

csauls wrote:

You know... and you may shoot me for saying this if you wish... but it might be good to scrap the current SVN image and start from scratch. Its not like we have anything significant done. I just think it would be good to start over and better plan the layout of the code, and plus its just been a good long while and we've all changed a little. Smile


Hmm, that depends quite a lot on the direction we want Sinbad to follow. It's beginning to dawn on me that; either you design everything from scratch (API, functionality, implementation, etc) or you follow through, accepting the design choices made by the OGRE team. The only part that we might be able to filter out for a D redesign is the event system as it somewhat independent of the rest. The rest follow something that looks like a really careful design and messing with it probably destroy everything. It's just to damn big and complex to redesign in any significant way. I would really like to create my own engine from scratch, but it would probably never get off the ground. To have any sort of impact, it HAS to be big and complex and that takes time, which I don't know where to find. If you think it's doable for 2+ and want to try I might consider joining in, otherwise I'll continue to plug away (slowly).

csauls wrote:

Glad to see Sinbad isn't forgotten... I just haven't been able to tend it, but that might change soon (now that I'm employed again). I also was hoping to wait for D 1.0 but that looks further and further away.


Good for you! (The job, that is) I think D is close enough, there has been no significant changes to the language in a long time.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> Sinbad All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group