xref: /aoo4110/main/tools/inc/tools/rtti.hxx (revision b1cdbd2c)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef _RTTI_HXX
25 #define _RTTI_HXX
26 
27 #include <string.h>
28 #include <tools/solar.h>
29 
30 typedef void* (*TypeId)();
31 
32 //-------------------------------------------------------------------------
33 
34 #define TYPEINFO() \
35         static  void*  CreateType(); \
36         static  TypeId StaticType(); \
37         static  sal_Bool   IsOf( TypeId aSameOrSuperType ); \
38         virtual TypeId Type() const; \
39         virtual sal_Bool   IsA( TypeId aSameOrSuperType ) const
40 
41 #define TYPEINFO_VISIBILITY(visibility) \
42         visibility static  void*  CreateType(); \
43         visibility static  TypeId StaticType(); \
44         visibility static  sal_Bool   IsOf( TypeId aSameOrSuperType ); \
45         visibility virtual TypeId Type() const; \
46         visibility virtual sal_Bool   IsA( TypeId aSameOrSuperType ) const
47 
48 #define TYPEINIT_FACTORY(sType, Factory ) \
49         void*  sType::CreateType() { return Factory; } \
50         TypeId sType::StaticType() { return &CreateType; } \
51         TypeId sType::Type() const { return &CreateType; } \
52         sal_Bool sType::IsOf( TypeId aSameOrSuperType ) \
53         { \
54             if ( aSameOrSuperType == StaticType() ) \
55                 return sal_True
56 
57 #define STATICTYPE(sType) (sType::StaticType())
58 
59 //-------------------------------------------------------------------------
60 
61 #define TYPEINIT_AUTOFACTORY(sType) TYPEINIT_FACTORY(sType, new sType)
62 #define TYPEINIT(sType) TYPEINIT_FACTORY(sType, 0)
63 
64 #define SUPERTYPE(sSuper) \
65             if ( sSuper::IsOf(aSameOrSuperType ) ) \
66                 return sal_True
67 
68 #define TYPEINIT_END(sType) \
69             return sal_False; \
70         } \
71         sal_Bool sType::IsA( TypeId aSameOrSuperType ) const \
72 		{ return IsOf( aSameOrSuperType ); }
73 
74 #define TYPEINIT0_FACTORY(sType, Factory) \
75         TYPEINIT_FACTORY(sType, Factory); \
76         TYPEINIT_END(sType)
77 #define TYPEINIT0_AUTOFACTORY(sType) TYPEINIT0_FACTORY(sType, new sType)
78 #define TYPEINIT0(sType) TYPEINIT0_FACTORY(sType, 0)
79 
80 #define TYPEINIT1_FACTORY(sType, sSuper, Factory) \
81         TYPEINIT_FACTORY(sType, Factory); \
82             SUPERTYPE(sSuper); \
83         TYPEINIT_END(sType)
84 #define TYPEINIT1_AUTOFACTORY(sType, sSuper) \
85             TYPEINIT1_FACTORY(sType, sSuper, new sType)
86 #define TYPEINIT1(sType, sSuper) \
87             TYPEINIT1_FACTORY(sType, sSuper, 0)
88 
89 #define TYPEINIT2_FACTORY(sType, sSuper1, sSuper2, Factory) \
90         TYPEINIT_FACTORY(sType, Factory); \
91             SUPERTYPE(sSuper1); \
92             SUPERTYPE(sSuper2); \
93         TYPEINIT_END(sType)
94 #define TYPEINIT2_AUTOFACTORY(sType, sSuper1, sSuper2) \
95             TYPEINIT2_FACTORY(sType, sSuper1, sSuper2, new sType)
96 #define TYPEINIT2(sType, sSuper1, sSuper2) \
97             TYPEINIT2_FACTORY(sType, sSuper1, sSuper2, 0)
98 
99 #define TYPEINIT3_FACTORY(sType, sSuper1, sSuper2, sSuper3, Factory) \
100         TYPEINIT_FACTORY(sType, Factory); \
101             SUPERTYPE(sSuper1); \
102             SUPERTYPE(sSuper2); \
103             SUPERTYPE(sSuper3); \
104         TYPEINIT_END(sType)
105 #define TYPEINIT3_AUTOFACTORY(sType, sSuper1, sSuper2, sSuper3) \
106             TYPEINIT3_FACTORY(sType, sSuper1, sSuper2, sSuper3, new sType)
107 #define TYPEINIT3(sType, sSuper1, sSuper2, sSuper3) \
108             TYPEINIT3_FACTORY(sType, sSuper1, sSuper2, sSuper3, 0)
109 
110 #define TYPE(sType) (sType::StaticType())
111 #define ISA(sType) IsA(sType::StaticType())
112 #define ISOF(sType) IsOf(sType::StaticType())
113 #define CREATE(TypeId) (TypeId())
114 
115 //-------------------------------------------------------------------------
116 // On-Demand-faehige persistent-TypeId Version
117 
118 #define TYPEINFO_ID(id) \
119 		static	TypeId StaticType() { return (TypeId) ( id | 0xF000000L ); } \
120         static  sal_Bool   IsOf( TypeId aSameOrSuperType ); \
121         virtual TypeId Type() const; \
122         virtual sal_Bool   IsA( TypeId aSameOrSuperType ) const
123 
124 #define TYPEINIT_ID(sType) \
125         TypeId sType::Type() const { return StaticType(); } \
126         sal_Bool   sType::IsOf( TypeId aSameOrSuperType ) \
127         { \
128             if ( aSameOrSuperType == StaticType() ) \
129                 return sal_True
130 
131 #define TYPEINIT0_ID(sType) \
132         TYPEINIT_ID(sType); \
133         TYPEINIT_END(sType)
134 
135 #define TYPEINIT1_ID(sType, sSuper) \
136         TYPEINIT_ID(sType); \
137             SUPERTYPE(sSuper); \
138         TYPEINIT_END(sType)
139 
140 #define TYPEINIT2_ID(sType, sSuper1, sSuper2) \
141         TYPEINIT_ID(sType); \
142             SUPERTYPE(sSuper1); \
143             SUPERTYPE(sSuper2); \
144         TYPEINIT_END(sType)
145 
146 //-------------------------------------------------------------------------
147 
148 //      Die (exemplarischen) Makros fuer die Anwendung ( hier fuer
149 //      Pointer, kann aber nach dem gleichen Strickmuster fuer
150 //      Referenzen erweitert werden.
151 //      PTR_CAST: sicheres Casten eines Pointers auf einen Pointer
152 //      einer abgeleiteten Klasse. Liefert im Fehlerfall einen
153 //      Nullpointer (wahrscheinlich die haeufigste Anwendung)
154 //
155 //      T: Typ, auf den gecastet werden soll
156 //      p: Pointer, der gecastet werden soll
157 #define PTR_CAST( T, pObj ) \
158         ( pObj && (pObj)->IsA( TYPE(T) ) ? (T*)(pObj) : 0 )
159 
160 //      Abfrage, ob ein Objekt eine bestimmte Klasse als
161 //      Basisklasse hat (oder genau von dieser Klasse ist)
162 #define HAS_BASE( T, pObj ) \
163         ( pObj && (pObj)->IsA( TYPE(T) ) )
164 
165 //      Abfrage, ob ein Pointer auf ein Objekt eines bestimmten
166 //      Typs zeigt
167 #define IS_TYPE(T,pObj) \
168         ( pObj && (pObj)->Type() == TYPE(T) )
169 
170 #endif // _RTTI_HXX
171