xref: /AOO41X/main/tools/inc/tools/rtti.hxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef _RTTI_HXX
29 #define _RTTI_HXX
30 
31 #include <string.h>
32 #include <tools/solar.h>
33 
34 typedef void* (*TypeId)();
35 
36 //-------------------------------------------------------------------------
37 
38 #define TYPEINFO() \
39         static  void*  CreateType(); \
40         static  TypeId StaticType(); \
41         static  sal_Bool   IsOf( TypeId aSameOrSuperType ); \
42         virtual TypeId Type() const; \
43         virtual sal_Bool   IsA( TypeId aSameOrSuperType ) const
44 
45 #define TYPEINFO_VISIBILITY(visibility) \
46         visibility static  void*  CreateType(); \
47         visibility static  TypeId StaticType(); \
48         visibility static  sal_Bool   IsOf( TypeId aSameOrSuperType ); \
49         visibility virtual TypeId Type() const; \
50         visibility virtual sal_Bool   IsA( TypeId aSameOrSuperType ) const
51 
52 #define TYPEINIT_FACTORY(sType, Factory ) \
53         void*  sType::CreateType() { return Factory; } \
54         TypeId sType::StaticType() { return &CreateType; } \
55         TypeId sType::Type() const { return &CreateType; } \
56         sal_Bool sType::IsOf( TypeId aSameOrSuperType ) \
57         { \
58             if ( aSameOrSuperType == StaticType() ) \
59                 return sal_True
60 
61 #define STATICTYPE(sType) (sType::StaticType())
62 
63 //-------------------------------------------------------------------------
64 
65 #define TYPEINIT_AUTOFACTORY(sType) TYPEINIT_FACTORY(sType, new sType)
66 #define TYPEINIT(sType) TYPEINIT_FACTORY(sType, 0)
67 
68 #define SUPERTYPE(sSuper) \
69             if ( sSuper::IsOf(aSameOrSuperType ) ) \
70                 return sal_True
71 
72 #define TYPEINIT_END(sType) \
73             return sal_False; \
74         } \
75         sal_Bool sType::IsA( TypeId aSameOrSuperType ) const \
76 		{ return IsOf( aSameOrSuperType ); }
77 
78 #define TYPEINIT0_FACTORY(sType, Factory) \
79         TYPEINIT_FACTORY(sType, Factory); \
80         TYPEINIT_END(sType)
81 #define TYPEINIT0_AUTOFACTORY(sType) TYPEINIT0_FACTORY(sType, new sType)
82 #define TYPEINIT0(sType) TYPEINIT0_FACTORY(sType, 0)
83 
84 #define TYPEINIT1_FACTORY(sType, sSuper, Factory) \
85         TYPEINIT_FACTORY(sType, Factory); \
86             SUPERTYPE(sSuper); \
87         TYPEINIT_END(sType)
88 #define TYPEINIT1_AUTOFACTORY(sType, sSuper) \
89             TYPEINIT1_FACTORY(sType, sSuper, new sType)
90 #define TYPEINIT1(sType, sSuper) \
91             TYPEINIT1_FACTORY(sType, sSuper, 0)
92 
93 #define TYPEINIT2_FACTORY(sType, sSuper1, sSuper2, Factory) \
94         TYPEINIT_FACTORY(sType, Factory); \
95             SUPERTYPE(sSuper1); \
96             SUPERTYPE(sSuper2); \
97         TYPEINIT_END(sType)
98 #define TYPEINIT2_AUTOFACTORY(sType, sSuper1, sSuper2) \
99             TYPEINIT2_FACTORY(sType, sSuper1, sSuper2, new sType)
100 #define TYPEINIT2(sType, sSuper1, sSuper2) \
101             TYPEINIT2_FACTORY(sType, sSuper1, sSuper2, 0)
102 
103 #define TYPEINIT3_FACTORY(sType, sSuper1, sSuper2, sSuper3, Factory) \
104         TYPEINIT_FACTORY(sType, Factory); \
105             SUPERTYPE(sSuper1); \
106             SUPERTYPE(sSuper2); \
107             SUPERTYPE(sSuper3); \
108         TYPEINIT_END(sType)
109 #define TYPEINIT3_AUTOFACTORY(sType, sSuper1, sSuper2, sSuper3) \
110             TYPEINIT3_FACTORY(sType, sSuper1, sSuper2, sSuper3, new sType)
111 #define TYPEINIT3(sType, sSuper1, sSuper2, sSuper3) \
112             TYPEINIT3_FACTORY(sType, sSuper1, sSuper2, sSuper3, 0)
113 
114 #define TYPE(sType) (sType::StaticType())
115 #define ISA(sType) IsA(sType::StaticType())
116 #define ISOF(sType) IsOf(sType::StaticType())
117 #define CREATE(TypeId) (TypeId())
118 
119 //-------------------------------------------------------------------------
120 // On-Demand-faehige persistent-TypeId Version
121 
122 #define TYPEINFO_ID(id) \
123 		static	TypeId StaticType() { return (TypeId) ( id | 0xF000000L ); } \
124         static  sal_Bool   IsOf( TypeId aSameOrSuperType ); \
125         virtual TypeId Type() const; \
126         virtual sal_Bool   IsA( TypeId aSameOrSuperType ) const
127 
128 #define TYPEINIT_ID(sType) \
129         TypeId sType::Type() const { return StaticType(); } \
130         sal_Bool   sType::IsOf( TypeId aSameOrSuperType ) \
131         { \
132             if ( aSameOrSuperType == StaticType() ) \
133                 return sal_True
134 
135 #define TYPEINIT0_ID(sType) \
136         TYPEINIT_ID(sType); \
137         TYPEINIT_END(sType)
138 
139 #define TYPEINIT1_ID(sType, sSuper) \
140         TYPEINIT_ID(sType); \
141             SUPERTYPE(sSuper); \
142         TYPEINIT_END(sType)
143 
144 #define TYPEINIT2_ID(sType, sSuper1, sSuper2) \
145         TYPEINIT_ID(sType); \
146             SUPERTYPE(sSuper1); \
147             SUPERTYPE(sSuper2); \
148         TYPEINIT_END(sType)
149 
150 //-------------------------------------------------------------------------
151 
152 //      Die (exemplarischen) Makros fuer die Anwendung ( hier fuer
153 //      Pointer, kann aber nach dem gleichen Strickmuster fuer
154 //      Referenzen erweitert werden.
155 //      PTR_CAST: sicheres Casten eines Pointers auf einen Pointer
156 //      einer abgeleiteten Klasse. Liefert im Fehlerfall einen
157 //      Nullpointer (wahrscheinlich die haeufigste Anwendung)
158 //
159 //      T: Typ, auf den gecastet werden soll
160 //      p: Pointer, der gecastet werden soll
161 #define PTR_CAST( T, pObj ) \
162         ( pObj && (pObj)->IsA( TYPE(T) ) ? (T*)(pObj) : 0 )
163 
164 //      Abfrage, ob ein Objekt eine bestimmte Klasse als
165 //      Basisklasse hat (oder genau von dieser Klasse ist)
166 #define HAS_BASE( T, pObj ) \
167         ( pObj && (pObj)->IsA( TYPE(T) ) )
168 
169 //      Abfrage, ob ein Pointer auf ein Objekt eines bestimmten
170 //      Typs zeigt
171 #define IS_TYPE(T,pObj) \
172         ( pObj && (pObj)->Type() == TYPE(T) )
173 
174 #endif // _RTTI_HXX
175