Question about Loki::Singleton

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Krivenok Dmitry

    #1

    Question about Loki::Singleton

    Hello All!

    This is example of code:
    //////////////////////////////////////////////////////////////////////////////////////////////////
    #ifndef A_H_
    #define A_H_

    #include <Loki/Singleton.h>
    #include <Loki/SmartPtr.h>
    #include <iostream>

    class A
    {
    public:
    A() { std::cout << "A constructor" << std::endl;}
    ~A() { std::cout << "A destructor" << std::endl;}
    };

    typedef Loki::Singleton Holder<
    Loki::SmartPtr< A>,
    Loki::CreateUsi ngNew,
    Loki::NoDestroy ,
    Loki::SingleThr eaded[color=blue]
    > APtrSingleton_t ;[/color]
    #endif
    //////////////////////////////////////////////////////////////////////////////////////////////////

    I can use APtrSingleton_t ::Instance() in my program, but it's very
    uneasily.
    How to define (short) alternative name of APtrSingleton_t ::Instance()?

    Thanks!

  • Ben Pope

    #2
    Re: Question about Loki::Singleton

    Krivenok Dmitry wrote:[color=blue]
    > Hello All!
    >
    > This is example of code:
    > //////////////////////////////////////////////////////////////////////////////////////////////////
    > #ifndef A_H_
    > #define A_H_
    >
    > #include <Loki/Singleton.h>
    > #include <Loki/SmartPtr.h>
    > #include <iostream>
    >
    > class A
    > {
    > public:
    > A() { std::cout << "A constructor" << std::endl;}
    > ~A() { std::cout << "A destructor" << std::endl;}
    > };
    >
    > typedef Loki::Singleton Holder<
    > Loki::SmartPtr< A>,
    > Loki::CreateUsi ngNew,
    > Loki::NoDestroy ,
    > Loki::SingleThr eaded[color=green]
    > > APtrSingleton_t ;[/color]
    > #endif
    > //////////////////////////////////////////////////////////////////////////////////////////////////
    >
    > I can use APtrSingleton_t ::Instance() in my program, but it's very
    > uneasily.
    > How to define (short) alternative name of APtrSingleton_t ::Instance()?
    >
    > Thanks![/color]

    A* x() {
    return APtrSingleton_t ::Instance();
    }

    Then whenever you want the instance, just do:
    A* a = x();

    Not very useful if you ask me, though.

    Ben Pope
    --
    I'm not just a number. To many, I'm known as a string...

    Comment

    • mlimber

      #3
      Re: Question about Loki::Singleton

      Krivenok Dmitry wrote:[color=blue]
      > Hello All!
      >
      > This is example of code:
      > //////////////////////////////////////////////////////////////////////////////////////////////////
      > #ifndef A_H_
      > #define A_H_
      >
      > #include <Loki/Singleton.h>
      > #include <Loki/SmartPtr.h>
      > #include <iostream>
      >
      > class A
      > {
      > public:
      > A() { std::cout << "A constructor" << std::endl;}
      > ~A() { std::cout << "A destructor" << std::endl;}
      > };
      >
      > typedef Loki::Singleton Holder<
      > Loki::SmartPtr< A>,
      > Loki::CreateUsi ngNew,
      > Loki::NoDestroy ,
      > Loki::SingleThr eaded[color=green]
      > > APtrSingleton_t ;[/color]
      > #endif
      > //////////////////////////////////////////////////////////////////////////////////////////////////
      >
      > I can use APtrSingleton_t ::Instance() in my program, but it's very
      > uneasily.
      > How to define (short) alternative name of APtrSingleton_t ::Instance()?
      >
      > Thanks![/color]

      You could create a macro (yuck!), an inline function, or a local
      reference (my preference):

      Loki::SmartPtr< A>& ptr = APtrSingleton_t ::Instance();

      Cheers! --M

      Comment

      • Krivenok Dmitry

        #4
        Re: Question about Loki::Singleton

        Of course s/Loki::SmartPtr< A>/A*/g :))

        Comment

        Working...