Back to index

salome-kernel  6.5.0
Static Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes
SINGLETON_< TYPE > Class Template Reference

Definition More...

#include <Utils_SINGLETON.hxx>

Collaboration diagram for SINGLETON_< TYPE >:
Collaboration graph
[legend]

List of all members.

Static Public Member Functions

static TYPE * Instance (void)
 Singleton dynamic creation using the default builder.
static bool IsAlreadyExisting (void)
 returns True if the singleton is already existing
static int Destruction (void)
 destroys the Singleton before the end of the application process

Private Member Functions

 SINGLETON_ (void)
 ~SINGLETON_ ()

Private Attributes

TYPE _Instance

Static Private Attributes

static SINGLETON_PtrSingleton = NULL

Detailed Description

template<class TYPE>
class SINGLETON_< TYPE >

Definition

A singleton is a data which is created and deleted only once in the application. The C++ compiler allow the user to create static data before the first executable statement. They are deleted after the last statement.statement.

The SINGLETON_ template class deals with dynamic singleton. It is useful for functor objects. For example, an object which, when created, connects the application to a system and disconnects the application at deletion.

Usage

To create a single instance a POINT_ object :

include "Utils_SINGLETON.hxx"

... ptrPoint = SINGLETON_<POINT_>::Instance() ;

Design description

 -# the user creates an object of class TYPE By using a class method : SINGLETON_<TYPE>::Instance() which
    returns a pointer to the single object ;
 -# this class method uses the default constructor to create an object ;
 -# at the same time, this class method reate a destructor object which is added to the generic list
    of destructors objects to be executed at the end of the application (atexit) ;
 -# at the end of the application process all the deletions are performed by the Nettoyage() C function
    which execute the destructions objects then deletes the destructions objects themselves ;
 -# the Nettoyage() C function is recorded using atexit() C function through the creation of a static
    single object ATEXIT_().

Definition at line 73 of file Utils_SINGLETON.hxx.


Constructor & Destructor Documentation

template<class TYPE >
SINGLETON_< TYPE >::SINGLETON_ ( void  ) [private]

Definition at line 128 of file Utils_SINGLETON.hxx.

{
        //MESSAGE("CREATION d'un SINGLETON_") ;
}
template<class TYPE >
SINGLETON_< TYPE >::~SINGLETON_ ( ) [private]

Definition at line 168 of file Utils_SINGLETON.hxx.

{
  //MESSAGE("passage dans SINGLETON_<TYPE>::~SINGLETON_( void )") ;
}

Member Function Documentation

template<class TYPE >
int SINGLETON_< TYPE >::Destruction ( void  ) [static]

destroys the Singleton before the end of the application process

The method SINGLETON_<TYPE>::Destruction can be called by the user.

If it is not the function nettoyage() calls it atexit.

N.B. : the singleton objects are destroyed in the reverse order of there creation.

Definition at line 142 of file Utils_SINGLETON.hxx.

{
        int k = - 1 ;
        //BEGIN_OF("SINGLETON_<TYPE>::Destruction( void )") ;
        if ( PtrSingleton )
        {
          //MESSAGE("Destruction du SINGLETON_") ;


                std::list<DESTRUCTEUR_GENERIQUE_ *>::iterator k ;
                for( k=DESTRUCTEUR_GENERIQUE_::Destructeurs->begin() ; k!=DESTRUCTEUR_GENERIQUE_::Destructeurs->end();k++)
                {
                        if ( *k == PtrSingleton->_Instance )
                        {
                                DESTRUCTEUR_GENERIQUE_::Destructeurs->erase( k ) ;
                                break ;
                        }
                }
                delete PtrSingleton ;
                PtrSingleton = NULL ;
        }
        //END_OF("SINGLETON_<TYPE>::Destruction( void )") ;
        return k ;
}
template<class TYPE >
TYPE * SINGLETON_< TYPE >::Instance ( void  ) [static]

Singleton dynamic creation using the default builder.

The class method Instance :

  1. creates an object of class TYPE ;
  2. creates a destruction object DESTRUCTEUR_DE_<TYPE> which is appended to the list of destruction objects to be executed ;
  3. returns a pointer to the created object.

Note that the two created objects are deleted at the end of the process in the function Nettoyage().

Definition at line 108 of file Utils_SINGLETON.hxx.

{
        if ( ! PtrSingleton )
        {
                //MESSAGE("SINGLETON_<TYPE>::Instance( void )") ;
                PtrSingleton = new SINGLETON_<TYPE> ;
                new DESTRUCTEUR_DE_<TYPE>( PtrSingleton->_Instance ) ;
        }
        return &PtrSingleton->_Instance ;
}

Here is the caller graph for this function:

template<class TYPE >
bool SINGLETON_< TYPE >::IsAlreadyExisting ( void  ) [static]

returns True if the singleton is already existing

Definition at line 120 of file Utils_SINGLETON.hxx.

{
        return PtrSingleton ? true : false ;
}

Member Data Documentation

template<class TYPE>
TYPE SINGLETON_< TYPE >::_Instance [private]

Definition at line 84 of file Utils_SINGLETON.hxx.

template<class TYPE>
SINGLETON_< TYPE > * SINGLETON_< TYPE >::PtrSingleton = NULL [static, private]

Definition at line 85 of file Utils_SINGLETON.hxx.


The documentation for this class was generated from the following file: