1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
4 <META NAME="GENERATOR" CONTENT="SGML-Tools 1.0.9">
5 <TITLE>SALOME Kernel resources for developer: Miscellaneous tools</TITLE>
6 <LINK HREF="kernel_resources-2.html" REL=previous>
7 <LINK HREF="kernel_resources.html#toc3" REL=contents>
11 <A HREF="kernel_resources-2.html">Previous</A>
12 <A HREF="kernel_resources.html#toc3">Contents</A>
14 <H2><A NAME="s3">3. Miscellaneous tools</A></H2>
16 <H2><A NAME="ss3.1">3.1 Singleton</A>
21 <P>A singleton is an application data which is created and deleted
22 only once at the end of the application process. The C++ compiler
23 allows the user to create a static singleton data before the first
24 executable statement. They are deleted after the last statement execution.
25 <P>The <CODE>SINGLETON_</CODE> template class deals with dynamic singleton. It
26 is useful for functor objects. For example, an object that connects
27 the application to a system at creation and disconnects the application
31 <P>To create a single instance a POINT object :
34 # include "Utils_SINGLETON.hxx"
36 POINT *ptrPoint=SINGLETON_<POINT>::Instance() ;
37 assert(ptrPoint!=NULL) ;
39 <P>No need to delete ptrPoint. Deletion is achieved automatically
40 at exit. If the user tries to create more than one singleton by using
41 the class method <CODE>SINGLETON_<TYPE>::Instance()</CODE>, the pointer
42 is returned with the same value even if this is done in different
43 functions (threads ?).
46 POINT *p1=SINGLETON_<POINT>::Instance() ;
48 POINT *p2=SINGLETON_<POINT>::Instance() ;
51 <H3>Design description</H3>
53 <P>Here are the principles features of the singleton design :
56 <LI>the user creates an object of class <CODE>TYPE</CODE> by using the class method
57 <CODE>SINGLETON_<TYPE>::Instance()</CODE> which returns a pointer to the
59 <LI>to create an object, <CODE>SINGLETON_<TYPE>::Instance()</CODE> uses
60 the default constructor of class <CODE>TYPE</CODE> ;</LI>
61 <LI>at the same time, this class method creates a destructor object
62 which is added to the generic list of destructor objects to be executed
63 at the end of the application (<CODE>atexit</CODE>) ;</LI>
64 <LI>at the end of the application process all the deletions are performed
65 by the <CODE>Nettoyage()</CODE> C function which executes the destruction objects
66 end then deletes the destructions objects themselves ;</LI>
67 <LI>the <CODE>Nettoyage()</CODE> C function using atexit() C function is embedded
68 in a static single object <CODE>ATEXIT_()</CODE>.</LI>
74 <A HREF="kernel_resources-2.html">Previous</A>
75 <A HREF="kernel_resources.html#toc3">Contents</A>