Salome HOME
This commit was generated by cvs2git to create tag 'V1_4_0b2'.
[modules/kernel.git] / doc / KernelResources / kernel_resources-3.html
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
2 <HTML>
3 <HEAD>
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>
8 </HEAD>
9 <BODY>
10 Next
11 <A HREF="kernel_resources-2.html">Previous</A>
12 <A HREF="kernel_resources.html#toc3">Contents</A>
13 <HR>
14 <H2><A NAME="s3">3. Miscellaneous tools</A></H2>
15
16 <H2><A NAME="ss3.1">3.1 Singleton</A>
17 </H2>
18
19 <H3>Definition</H3>
20
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
28 at deletion.
29 <H3>Usage</H3>
30
31 <P>To create a single instance a POINT object :
32 <P>
33 <PRE>
34 # include &quot;Utils_SINGLETON.hxx&quot;
35 ... 
36 POINT *ptrPoint=SINGLETON_&lt;POINT&gt;::Instance() ; 
37 assert(ptrPoint!=NULL) ;
38 </PRE>
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_&lt;TYPE&gt;::Instance()</CODE>, the pointer
42 is returned with the same value even if this is done in different
43 functions (threads ?).
44 <P>
45 <PRE>
46 POINT *p1=SINGLETON_&lt;POINT&gt;::Instance() ;
47 ... 
48 POINT *p2=SINGLETON_&lt;POINT&gt;::Instance() ; 
49 assert(p1==p2)
50 </PRE>
51 <H3>Design description</H3>
52
53 <P>Here are the principles features of the singleton design :
54 <P>
55 <UL>
56 <LI>the user creates an object of class <CODE>TYPE</CODE> by using the class method
57 <CODE>SINGLETON_&lt;TYPE&gt;::Instance()</CODE> which returns a pointer to the
58 single object ;</LI>
59 <LI>to create an object, <CODE>SINGLETON_&lt;TYPE&gt;::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>
69 </UL>
70 <P>
71 <P>
72 <HR>
73 Next
74 <A HREF="kernel_resources-2.html">Previous</A>
75 <A HREF="kernel_resources.html#toc3">Contents</A>
76 </BODY>
77 </HTML>