]> SALOME platform Git repositories - modules/kernel.git/blob - doc/salome/kernel_resources.dox
Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[modules/kernel.git] / doc / salome / kernel_resources.dox
1 /*!
2
3 \page kernel_resources SALOME Kernel resources for developer
4
5 <b>WORK in PROGRESS, INCOMPLETE DOCUMENT</b>
6
7
8 \section S1_kernel_res Abstract
9
10 This document describes the development environment for 
11 C++ and Python. Makefiles generation and usage are 
12 introduced in another document: "using the %SALOME 
13 configuration and building system environment". 
14 Development environment is intended here as: trace and 
15 debug macros usage; %SALOME exceptions usage, in C++ and 
16 Python; user CORBA exceptions usage, in C++ and Python, 
17 with and without Graphical User Interface; some general 
18 purpose services such as singleton, used for CORBA 
19 connection and disconnection.
20
21 \section S2_kernel_res Trace and debug Utilities
22
23 During the development process, an execution log is 
24 useful to identify problems. This log contains 
25 messages, variables values, source files names and line 
26 numbers. It is recommended to verify assertions on 
27 variables values and if necessary, to stop the 
28 execution at debug time, in order to validate all parts 
29 of code.
30
31 <ol>
32 <li>
33 <b>Two modes: debug and release</b>
34
35 The goal of debug mode is to check as many features as 
36 possible during the early stages of the development 
37 process. The purpose of the utilities provided in 
38 %SALOME is to help the developer to add detailed traces 
39 and check variables values, without writing a lot of code.
40
41 When the code is assumed to be valid, the release mode 
42 optimizes execution, in terms of speed, memory, and 
43 display only user level messages.
44
45 But, some informations must always be displayed in both 
46 modes: especially messages concerning environment or 
47 internal errors, with version identification. When an 
48 end user is confronted to such a message, he may refer 
49 to a configuration documentation or send the message to 
50 the people in charge of %SALOME installation, or to the 
51 development team, following the kind of error.
52 </li>
53 <li>
54 <b>C++ Macros for trace and debug</b>
55
56 %SALOME provides C++ macros for trace and debug. These 
57 macros are in:
58
59 \code
60 KERNEL_SRC/src/SALOMELocalTrace/utilities.h
61 \endcode
62
63 This file must be included in C++ source. Some 
64 macros are activated only in debug mode, others are 
65 always activated. To activate the debug mode, ``_DEBUG_``
66 must be defined, which is the case when %SALOME 
67 Makefiles are generated from configure, without 
68 options. When ``_DEBUG_`` is undefined (release mode: 
69 ``configure --disable-debug --enable-production``), the 
70 debug mode macros are defined empty (they do nothing). 
71 So, when switching from debug to release, it is 
72 possible (and recommended) to let the macro calls 
73 unchanged in the source.
74
75 All the macros generate trace messages, stored in a 
76 circular buffer pool. %A separate %thread reads the 
77 messages in the buffer pool, and, depending on options 
78 given at %SALOME start, writes the messages on the 
79 standard output, a file, or send them via CORBA, in 
80 case of a multi machine configuration.
81
82 Three informations are systematically added in front of 
83 the information displayed:
84
85 - the %thread number from which the message come from;
86
87 - the name of the source file in which the macros is set;
88
89 - the line number of the source file at which the macro 
90   is set.
91
92 <ol>  
93 <li>
94 <b>Macros defined in debug and release modes</b>
95 \n
96 <b>INFOS_COMPILATION</b>
97
98    The C++ macro INFOS_COMPILATION writes on the trace 
99    buffer pool informations about the compiling process: 
100
101    - the name of the compiler : g++, KCC, CC, pgCC;
102
103    - the date and the time of the compiling processing process.
104
105    This macro INFOS_COMPILATION does not have any 
106    argument. Moreover, it is defined in both compiling 
107    mode : _DEBUG_ and _RELEASE_.
108
109    Example:
110
111    \code
112 #include "utilities.h"
113 int main(int argc , char **argv) 
114
115   INFOS_COMPILATION;
116   ...
117 }
118 INFOS(str)
119   \endcode
120 \n
121 <b>INFOS</b>
122
123    In both compiling mode _DEBUG_ and _RELEASE_, The C++ 
124    macro INFOS writes on the trace buffer pool %the string 
125    which has been passed in argument by the user.
126
127    Example:
128
129    \code
130 #include "utilities.h"
131 int main(int argc , char **argv)
132
133   ... 
134   INFOS("NORMAL END OF THE PROCESS"); 
135   return 0; 
136 }
137    \endcode
138
139    Displays:
140
141    \code
142 main.cxx [5] : NORMAL END OF THE PROCESS
143    \endcode
144 \n
145 <b>INTERRUPTION(str)</b>
146
147    In both compiling mode _DEBUG_ and _RELEASE_, The C++ 
148    macro INTERRUPTION writes on the trace buffer pool the 
149    %string, with a special ABORT type. When the %thread in 
150    charge of collecting messages finds this message, it 
151    terminates the application, after message treatment.
152
153 <b>IMMEDIATE_ABORT(str)</b>
154
155    In both compiling mode _DEBUG_ and _RELEASE_, The C++ 
156    macro IMMEDIATE_ABORT writes the message str immediately on 
157    standard error and exits the application. Remaining 
158    messages not treated by the message collector %thread 
159    are lost.
160
161 </li>
162 <li>
163 <b>Macros defined only in debug mode</b>
164 \n 
165 <b>MESSAGE(str)</b>
166
167    In _DEBUG_ compiling mode only, the C++ macro MESSAGE 
168    writes on the trace buffer pool the %string which has 
169    been passed in argument by the user. In _RELEASE_ 
170    compiling mode, this macro is blank.
171
172    Example:
173
174    \code
175 #include "utilities.h" 
176 #include <string> 
177
178 using namespace std; 
179
180 int main(int argc , char **argv) 
181
182   ... 
183   const char *str = "Salome";
184   MESSAGE(str);
185   ... const string st; 
186   st = "Aster"; 
187   MESSAGE(c_str(st+" and CASTEM")); 
188   return 0;
189 }
190
191    \endcode
192
193    Displays:
194
195    \code
196 - Trace main.cxx [8] : Salome
197 - Trace main.cxx [12] : Aster and CASTEM
198    \endcode
199
200 \n
201 <b>BEGIN_OF(func_name)</b>
202
203    In _DEBUG_ compiling mode, The C++ macro BEGIN_OF 
204    appends the %string "Begin of " to the one passed in 
205    argument by the user and displays the result on the 
206    trace buffer pool. In _RELEASE_ compiling mode, this 
207    macro is blank.
208
209    Example:
210
211    \code
212 #include "utilities.h" 
213 int main(int argc , char **argv) 
214
215   BEGIN_OF(argv[0]);
216   return 0;
217 }
218    \endcode
219
220    Displays:
221
222    \code
223      - Trace main.cxx [3] : Begin of a.out
224    \endcode
225 \n
226 <b>END_OF(func_name)</b>
227
228    In _DEBUG_ compiling mode, The C++ macro END_OF appends 
229    the %string "Normal end of " to the one passed in 
230    argument by the user and displays the result on the 
231    trace buffer pool. In _RELEASE_ compiling mode, this 
232    macro is blank.
233
234    Example:
235
236    \code
237 #include "utilities.h" 
238 int main(int argc , char **argv) 
239
240   END_OF(argv[0]);
241   return 0; 
242 }
243   \endcode
244
245    Displays:
246
247    \code
248 - Trace main.cxx [4] : Normal end of a.out
249    \endcode
250 \n
251 <b>SCRUTE(var)</b>
252
253    In _DEBUG_ compiling mode, The C++ macro SCRUTE 
254    displays its argument which is an application variable 
255    followed by the value of the variable. In _RELEASE_ 
256    compiling mode, this macro is blank.
257
258    Example:
259
260    \code
261 #include "utilities.h"
262 int main(int argc , char **argv) 
263
264   const int i=999;
265   if( i > 0 ) SCRUTE(i) ; i=i+1;
266   return 0;
267 }
268    \endcode
269
270    Displays:
271
272    \code
273 - Trace main.cxx [5] : i=999
274    \endcode
275 \n
276 <b>ASSERT(condition)</b>
277
278    In _DEBUG_ compiling mode only, The C++ macro ASSERT 
279    checks the expression passed in argument to be not 
280    NULL. If it is NULL the condition is written with the 
281    macro INTERRUPTION (see above). The process exits after 
282    trace of this last message. In _RELEASE_ compiling 
283    mode, this macro is blank. N.B. : if ASSERT is already 
284    defined, this macro is ignored.
285
286    Example:
287
288    \code
289 #include "utilities.h" 
290 ... 
291 const char *ptrS = fonc();
292 ASSERT(ptrS!=NULL); 
293 cout << strlen(ptrS); 
294 float table[10];
295 int k;
296 ... 
297 ASSERT(k<10);
298 cout << table[k];
299    \endcode
300
301 </li>
302 </ol>
303 </li>
304 </ol>
305
306 \section S3_kernel_res Exceptions
307
308 <ol>
309 <li>
310 <b>C++ exceptions: class SALOME_Exception</b>
311
312 <ol>
313 <li>
314 <b>definition</b>
315
316 The class SALOME_Exception provides a generic method to 
317 send a message, with optional source file name and line 
318 number. This class is intended to serve as a base class 
319 for all kinds of exceptions %SALOME code. All the 
320 exceptions derived from SALOME_Exception could be 
321 handled in a single catch, in which the message 
322 associated to the exception is displayed, or sent to a 
323 log file.
324
325 The class SALOME_Exception inherits its behavior from 
326 the STL class exception.
327 </li>
328 <li>
329 <b>usage</b>
330
331 The header %SALOME/src/utils/utils_SALOME_Exception.hxx 
332 must be included in the C++ source, when raised or trapped:
333
334 \code
335 #include "utils_SALOME_Exception.hxx"
336 \endcode
337
338 The SALOME_Exception constructor is:
339
340 \code
341 SALOME_Exception( const char *text,
342                   const char *fileName=0, 
343                   const unsigned int lineNumber=0 );
344 \endcode
345
346 The exception is raised like this:
347
348 \code
349 throw SALOME_Exception("my pertinent message");
350 \endcode
351
352 or like this:
353
354 \code
355 throw SALOME_Exception(LOCALIZED("my pertinent message"));
356 \endcode
357
358 where LOCALIZED is a macro provided with 
359 ``utils_SALOME_Exception.hxx`` which gives file name and 
360 line number.
361
362 The exception is handled like this:
363
364 \code
365    try
366 {
367   ...
368 }
369 catch (const SALOME_Exception &ex)
370 {
371   cerr << ex.what() <<endl;
372 }
373 \endcode
374
375 The what() method overrides the one defined in the STL 
376 exception class.
377 </li>
378 </ol>
379 </li>
380 <li>
381 <b>CORBA exceptions</b>
382
383 <ol>
384 <li>
385 <b>definition</b>
386
387 The idl SALOME_Exception provides a generic CORBA 
388 exception for %SALOME, with an attribute that gives an 
389 exception type,a message, plus optional source file 
390 name and line number. 
391
392 This idl is intended to serve for all user CORBA 
393 exceptions raised in %SALOME code, as IDL specification 
394 does not support exception inheritance. So, all the 
395 user CORBA exceptions from %SALOME could be handled in a 
396 single catch.
397
398 The exception types defined in idl are:
399
400   - COMM CORBA communication problem,
401
402   - BAD_PARAM Bad User parameters,
403
404   - INTERNAL_ERROR application level problem (often irrecoverable).
405
406 CORBA system and user exceptions already defined in the 
407 packages used within %SALOME, such as OmniORB 
408 exceptions, must be handled separately.
409
410 </li>
411 <li>
412 <b>usage</b>
413 <ol>
414 <li>
415 <b>CORBA servant, C++</b>
416
417    The CORBA Server header for SALOME_Exception and a 
418    macro to throw the exception are provided with the 
419    header ``KERNEL_SRC/src/Utils/Utils_CorbaException.hxx``:
420
421    \code
422 #include "Utils_CorbaException.hxx"
423    \endcode
424
425    The exception is raised with a macro which appends file 
426    name and line number:
427
428    \code
429 if (myStudyName.size() == 0)
430   THROW_SALOME_CORBA_EXCEPTION("No Study Name given", 
431                                SALOME::BAD_PARAM);
432    \endcode
433
434 </li>
435 <li>
436 <b>CORBA Client, GUI Qt C++</b>
437
438    <b>NO MORE AVAILABLE in %SALOME 3.x</b>
439
440    The CORBA Client header for SALOME_Exception and a Qt 
441    function header that displays a message box are 
442    provided in:
443
444      ``KERNEL_SRC/src/SALOMEGUI/SALOMEGUI_QtCatchCorbaException.hxx``
445
446    \code
447 #include "SALOMEGUI_QtCatchCorbaException.hxx"
448    \endcode
449
450    %A typical exchange with a CORBA Servant will be:
451
452    \code
453 try
454 {
455  ... // one ore more CORBA calls
456 }
457
458 catch (const SALOME::SALOME_Exception & S_ex)
459 {
460   QtCatchCorbaException(S_ex);
461 }
462    \endcode
463
464 </li>
465 <li>
466 <b>CORBA Client, C++, without GUI</b>
467
468   Nothing specific has been provided to the developer 
469   yet. See the idl or the Qt function 
470   SALOMEGUI_QtCatchCorbaException.hxx to see how to get 
471   the information given by the exception %object.
472
473 </li>
474 </ol>
475 </li>
476 </ol>
477 </ol>
478
479 \section S4_kernel_res Miscellaneous tools
480
481 <ol>
482 <li>
483 <b>Singleton</b>
484 <ol>
485 <li>
486 <b>Definition</b>
487
488 %A singleton is an application data which is created and 
489 deleted only once at the end of the application 
490 process. The C++ compiler allows the user to create a 
491 static singleton data before the first executable 
492 statement. They are deleted after the last statement execution.
493
494 The ``SINGLETON_`` template class deals with dynamic 
495 singleton. It is useful for functor objects. For 
496 example, an %object that connects the application to a 
497 system at creation and disconnects the application at deletion.
498
499 </li>
500 <li>
501 <b>Usage</b>
502
503 To create a single instance of a POINT %object:
504
505 \code
506 # include "Utils_SINGLETON.hxx"
507 ... 
508 POINT *ptrPoint=SINGLETON_<POINT>::Instance() ; 
509 assert(ptrPoint!=NULL) ;
510 \endcode
511
512 No need to delete ptrPoint. Deletion is achieved 
513 automatically at exit. If the user tries to create more 
514 than one singleton by using the class method 
515 SINGLETON_<TYPE>::Instance(), the pointer is returned 
516 with the same value even if this is done in different 
517 functions (threads ?):
518
519 \code
520 POINT *p1=SINGLETON_<POINT>::Instance() ;
521 ... 
522 POINT *p2=SINGLETON_<POINT>::Instance() ; 
523
524 assert(p1==p2)
525 \endcode
526
527 </li>
528 <li>
529 <b>Design description</b>
530
531 Here are the principles features of the singleton 
532 design:
533
534 - the user creates an %object of class TYPE by using the 
535   class method ``SINGLETON_<TYPE>::Instance()`` which 
536   returns a pointer to the single %object ;
537
538 - to create an %object, ``SINGLETON_<TYPE>::Instance()`` 
539   uses the default constructor of class TYPE ;
540
541 - at the same time, this class method creates a 
542   destructor %object which is added to the generic list 
543   of destructor objects to be executed at the end of 
544   the application (atexit) ;
545
546 - at the end of the application process all the 
547   deletions are performed by the ``Nettoyage()`` C function 
548   which executes the destruction objects end then 
549   deletes the destructions objects themselves ;
550
551 - the ``Nettoyage()`` C  function using ``atexit()`` C  function 
552   is embedded in a static single %object ``ATEXIT_()``.
553
554 </li>
555 </ol>
556 </li>
557 </ol>
558
559 */