Good design?

Discussion in 'C++' started by Pelle Beckman, Apr 20, 2005.

  1. Hi,

    I've just finished writing a template (whew!) and
    would like some opinions on the design.

    The goal was to be able to do fairly simple
    singletons (no excplicit thread-safety, etc)
    and keep it simple.
    It's also supposted to give the ability to
    do simple derived singletons - which is not
    possible with my other attempts that were based
    on the classical "global pointer to an object
    which is set using 'this' in the objects constructor"
    (I hope you understood that...).

    Anyway, comments? suggestions?
    I'm a bit unsure of the ()-operator - could
    that get in the way of other functions, operators, etc?

    template<class T>
    class Singleton {
    public:
    T* Instance ();
    void Release ();
    T* operator() ();

    Singleton () {}

    private:
    static T* m_singleton;
    };

    template<class T> T* Singleton<T>::m_singleton = 0;

    template<class T>
    T* Singleton<T>::eek:perator() () {
    return this->Instance ();
    }

    template<class T>
    T* Singleton<T>::Instance () {
    if (m_singleton == 0) {
    m_singleton = new T;
    }
    return m_singleton;
    }

    template<class T>
    void Singleton<T>::Release () {
    if (m_singleton == 0)
    return;
    delete m_singleton;
    m_singleton = 0;
    }

    The code allows me to do this:

    Singleton<MyClass> mysingleton;
    mysingleton ()-> AFunction();
    mysingleton.Instance ()->AFunction();

    which I find is rather nice.

    -- Pelle
     
    Pelle Beckman, Apr 20, 2005
    #1
    1. Advertising

  2. Pelle Beckman

    Phlip Guest

    Pelle Beckman wrote:

    > I've just finished writing a template (whew!) and
    > would like some opinions on the design.


    > Anyway, comments? suggestions?


    Where are the unit tests?

    --
    Phlip
    http://www.c2.com/cgi/wiki?ZeekLand
     
    Phlip, Apr 20, 2005
    #2
    1. Advertising

  3. Phlip skrev:
    > Pelle Beckman wrote:
    >
    >
    >>I've just finished writing a template (whew!) and
    >>would like some opinions on the design.

    >
    >
    >>Anyway, comments? suggestions?

    >
    >
    > Where are the unit tests?
    >


    I'll guess you'll laugh you head off...

    I don't know.
    What's a unit test?

    -- Pelle
     
    Pelle Beckman, Apr 20, 2005
    #3
  4. Pelle Beckman wrote:
    > I've just finished writing a template (whew!) and
    > would like some opinions on the design.
    > [...]


    Two things are missing: parameterized construction of the instance
    or construction of the instance by a factory. I'd probably try to
    have a special construction policy, and by default it would just
    invoke the default c-tor.

    V
     
    Victor Bazarov, Apr 20, 2005
    #4
  5. Pelle Beckman

    Howard Guest

    "Phlip" <> wrote in message
    news:e7w9e.1449$...
    > Pelle Beckman wrote:
    >
    >> I've just finished writing a template (whew!) and
    >> would like some opinions on the design.

    >
    >> Anyway, comments? suggestions?

    >
    > Where are the unit tests?
    >


    I have them, and I'm not letting you look at them unless you say "please".
    :)

    I think he was looking for suggestions on the *design*, actually. (There
    isn't even a "main" there, so talking about unit tests is going a wee bit
    beyond the question, I think.)

    -Howard
     
    Howard, Apr 20, 2005
    #5
  6. Victor Bazarov skrev:
    > Pelle Beckman wrote:
    >
    >> I've just finished writing a template (whew!) and
    >> would like some opinions on the design.
    >> [...]

    >
    >
    > Two things are missing: parameterized construction of the instance
    > or construction of the instance by a factory. I'd probably try to
    > have a special construction policy, and by default it would just
    > invoke the default c-tor.
    >
    > V


    Good point.
    How you I go about implementing c-tor parameter passing?
    Hints, solutions, hyperlinks?

    -- Pelle
     
    Pelle Beckman, Apr 20, 2005
    #6
  7. Pelle Beckman

    Phlip Guest

    Pelle Beckman wrote:

    > > Where are the unit tests?

    >
    > I'll guess you'll laugh you head off...
    >
    > I don't know.
    > What's a unit test?


    Google for it.

    Contrary to Howard's laugh, tests are a design thing. In the big C++ shops,
    a request for a code review shall be accompanied by tests.

    Here's one for your thing (with made-up code):

    struct Foo{};

    TEST_(TestCase, singular)
    {
    Singleton<Foo> mysingleton;
    Singleton<Foo> yoursingleton;
    Foo * p1 = mysingleton ();
    Foo * p2 = yoursingleton.Instance ();
    CHECK_EQUAL(p1, p2);
    }

    That test shows the Foo really is singular; the system did not allocate two
    of them, at different addresses.

    One runs all tests after every few edits. If a test fails, you have the
    option to either hit Undo until they all pass, or you can debug. Without
    tests, you don't know as soon as possible when you broke something, and you
    can't use Undo. The only choice is to debug.

    Now about style, if your system either throws an exception if new Foo fails,
    then your operators can never return NULL. So maybe your system should
    return a reference. Always use references without a reason to use pointers.

    --
    Phlip
    http://www.c2.com/cgi/wiki?ZeekLand
     
    Phlip, Apr 20, 2005
    #7
  8. Pelle Beckman wrote:
    > Victor Bazarov skrev:
    >
    >> Pelle Beckman wrote:
    >>
    >>> I've just finished writing a template (whew!) and
    >>> would like some opinions on the design.
    >>> [...]

    >>
    >>
    >>
    >> Two things are missing: parameterized construction of the instance
    >> or construction of the instance by a factory. I'd probably try to
    >> have a special construction policy, and by default it would just
    >> invoke the default c-tor.
    >>
    >> V

    >
    >
    > Good point.
    > How you I go about implementing c-tor parameter passing?
    > Hints, solutions, hyperlinks?


    Andrei Alexandrescu, "Modern C++ Design", chapter 6, "Implementing
    Singletons".

    V
     
    Victor Bazarov, Apr 20, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sikka noel
    Replies:
    8
    Views:
    421
    Mike Wahler
    Aug 5, 2003
  2. vlsidesign
    Replies:
    26
    Views:
    984
    Keith Thompson
    Jan 2, 2007
  3. Cliff  Martin
    Replies:
    1
    Views:
    3,041
    Larry Smith
    Jan 31, 2007
  4. SM
    Replies:
    9
    Views:
    506
  5. Casey Hawthorne
    Replies:
    18
    Views:
    626
    Beej Jorgensen
    Nov 6, 2009
Loading...

Share This Page