0024975: Improve of preprocessor directives in header files to be equal to the file...
[occt.git] / src / Interface / Interface_Statics.hxx
1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
2 //
3 // This file is part of Open CASCADE Technology software library.
4 //
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
10 //
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
13
14 #ifndef Interface_Statics_HeaderFile
15 #define Interface_Statics_HeaderFile
16
17 //  Macros to help static Handles not to be "constructed" before main run
18 //  In order to avoid it, the Handle to be statically reserved is encapsulated
19 //  in a structure itself designated through a Null Pointer :
20 //  Only the pointer is declared static, and initialized to NULL : then,
21 //  there is no routine to call for static construction
22
23 //  Remember that the object designated by a static Handle should not be created
24 //  in the static declaration, but must anyway be created, during main run,
25 //  once before its first use : this is the initialization step.
26
27
28 //  This set of macros allows user to simply declare and use "static" Handles.
29 //  It is available once having included this file :
30 //  ***************************************************
31 //         #include <Interface_Statics.hxx>
32
33 //  Static construction is replaced by using the macro StaticHandle :
34 //  ***************************************************
35 //    Old statement  :  static Handle(pk_class) object;
36 //    Is replaced by :  StaticHandle(pk_class,object);
37 //    which creates a null pointer called 'object_s' and typed 'pk_class_struc'
38
39 //  For first initialisation and use, several ways are available, all of them
40 //  give an access to the Handle through a reference.
41 //  It is required to initialize the static structure once, the macros Init*
42 //  assume that it is created once and only once, even if they are called
43 //  more than once.
44 //  It is possible : to create the object at initialization time by a macro,
45 //  or to create it after the macro call through its reference :
46
47 //  ***************************************************
48 //  Old statement (in a routine, not static) :
49 //          if (object.IsNull()) object = new pk_class (..args if any..);
50 //  can be maintained, but preceded by an initialization :
51 //          InitHandle(pk_class,object);         // -> Null Handle
52
53 //  ***************************************************
54 //  or it can be replaced by a direct formula (creation called only once) :
55 //          InitHandleVoid(pk_class,object);     // for a void constructor
56 //   or     InitHandleArgs(pk_class,object,(..args..));
57 //               (the arglist between embedded parentheses)
58 //   or     InitHandleVal(pk_class,object,val);  // i.e. object = val;
59
60 //  To simply use this pseudo-static object, consider
61 //  either the static variable  object_s->H
62 //  ***************************************************
63 //  or take it by the macro (which does not initialize it)
64 //          UseHandle(pk_class,object);
65
66
67 //  Declaration of a static Handle : first use for a given type
68 #define StaticHandle(type,var) static struct type##_struc { Handle(type) H; } *var##_s = NULL
69
70 //  Another declaration for an already declared type (with StaticHandle)
71 #define StaticHandleA(type,var) static struct type##_struc *var##_s = NULL
72
73 //  Using it (IT MUST HAVE BEEN FORMERLY INITIALIZED)
74 #define UseHandle(type,var) Handle(type)& var = var##_s->H
75
76 //  Initializing it (as Null Handle)
77 #define InitHandle(type,var) \
78 if(!var##_s) { var##_s=new type##_struc;  }\
79 Handle(type)& var = var##_s->H;
80
81 //  Initializing it and Creating it by a Void Constructor
82 #define InitHandleVoid(type,var) \
83 if(!var##_s) { var##_s=new type##_struc; var##_s->H=new type; }\
84 Handle(type)& var = var##_s->H;
85
86 //  Initializing it and Creating it by a Constructor with Arguments
87 //    (give them grouped in their parentheses)
88 #define InitHandleArgs(type,var,args) \
89 if(!var##_s) { var##_s=new type##_struc; var##_s->H=new type args; }\
90 Handle(type)& var = var##_s->H;
91
92 //  Initializing it from an already determined Value
93 #define InitHandleVal(type,var,value) \
94 if(!var##_s) { var##_s=new type##_struc; var##_s->H=value; }\
95 Handle(type)& var = var##_s->H;
96
97 #endif