1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 // This file is part of Open CASCADE Technology software library.
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.
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
14 // Macros to help static Handles not to be "constructed" before main run
15 // In order to avoid it, the Handle to be statically reserved is encapsulated
16 // in a structure itself designated through a Null Pointer :
17 // Only the pointer is declared static, and initialized to NULL : then,
18 // there is no routine to call for static construction
20 // Remember that the objet designated by a static Handle should not be created
21 // in the static declaration, but must anyway be created, during main run,
22 // once before its first use : this is the initialization step.
25 // This set of macros allows user to simply declare and use "static" Handles.
26 // It is available once having included this file :
27 // ***************************************************
28 // #include <Interface_Statics.hxx>
30 // Static construction is replaced by using the macro StaticHandle :
31 // ***************************************************
32 // Old statement : static Handle(pk_class) object;
33 // Is replaced by : StaticHandle(pk_class,object);
34 // which creates a null pointer called 'object_s' and typed 'pk_class_struc'
36 // For first initialisation and use, several ways are available, all of them
37 // give an access to the Handle through a reference.
38 // It is required to initialize the static structure once, the macros Init*
39 // assume that it is created once and only once, even if they are called
41 // It is possible : to create the object at initialization time by a macro,
42 // or to create it after the macro call through its reference :
44 // ***************************************************
45 // Old statement (in a routine, not static) :
46 // if (object.IsNull()) object = new pk_class (..args if any..);
47 // can be maintained, but preceeded by an initialization :
48 // InitHandle(pk_class,object); // -> Null Handle
50 // ***************************************************
51 // or it can be replaced by a direct formula (creation called only once) :
52 // InitHandleVoid(pk_class,object); // for a void constructor
53 // or InitHandleArgs(pk_class,object,(..args..));
54 // (the arglist between embedded parentheses)
55 // or InitHandleVal(pk_class,object,val); // i.e. object = val;
57 // To simply use this pseudo-static object, consider
58 // either the static variable object_s->H
59 // ***************************************************
60 // or take it by the macro (which does not initialize it)
61 // UseHandle(pk_class,object);
64 // Declaration of a static Handle : first use for a given type
65 #define StaticHandle(type,var) static struct type##_struc { Handle(type) H; } *var##_s = NULL
67 // Another declaration for an already declared type (with StaticHandle)
68 #define StaticHandleA(type,var) static struct type##_struc *var##_s = NULL
70 // Using it (IT MUST HAVE BEEN FORMERLY INITIALIZED)
71 #define UseHandle(type,var) Handle(type)& var = var##_s->H
73 // Initializing it (as Null Handle)
74 #define InitHandle(type,var) \
75 if(!var##_s) { var##_s=new type##_struc; }\
76 Handle(type)& var = var##_s->H;
78 // Initializing it and Creating it by a Void Constructor
79 #define InitHandleVoid(type,var) \
80 if(!var##_s) { var##_s=new type##_struc; var##_s->H=new type; }\
81 Handle(type)& var = var##_s->H;
83 // Initializing it and Creating it by a Constructor with Arguments
84 // (give them grouped in their parentheses)
85 #define InitHandleArgs(type,var,args) \
86 if(!var##_s) { var##_s=new type##_struc; var##_s->H=new type args; }\
87 Handle(type)& var = var##_s->H;
89 // Initializing it from an already determined Value
90 #define InitHandleVal(type,var,value) \
91 if(!var##_s) { var##_s=new type##_struc; var##_s->H=value; }\
92 Handle(type)& var = var##_s->H;