1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #ifndef INCLUDED_TOOLS_RTTI_HXX
21 #define INCLUDED_TOOLS_RTTI_HXX
24 #include <tools/solar.h>
26 typedef void* (*TypeId
)();
29 static void* CreateType(); \
30 static TypeId StaticType(); \
31 static bool IsOf( TypeId aSameOrSuperType ); \
32 virtual TypeId Type() const; \
33 virtual bool IsA( TypeId aSameOrSuperType ) const
35 #define TYPEINFO_OVERRIDE() \
36 static void* CreateType(); \
37 static TypeId StaticType(); \
38 static bool IsOf( TypeId aSameOrSuperType ); \
39 virtual TypeId Type() const SAL_OVERRIDE; \
40 virtual bool IsA( TypeId aSameOrSuperType ) const SAL_OVERRIDE
42 #define TYPEINFO_VISIBILITY(visibility) \
43 visibility static void* CreateType(); \
44 visibility static TypeId StaticType(); \
45 visibility static bool IsOf( TypeId aSameOrSuperType ); \
46 visibility virtual TypeId Type() const; \
47 visibility virtual bool IsA( TypeId aSameOrSuperType ) const
49 #define TYPEINFO_VISIBILITY_OVERRIDE(visibility) \
50 visibility static void* CreateType(); \
51 visibility static TypeId StaticType(); \
52 visibility static bool IsOf( TypeId aSameOrSuperType ); \
53 visibility virtual TypeId Type() const SAL_OVERRIDE; \
54 visibility virtual bool IsA( TypeId aSameOrSuperType ) const SAL_OVERRIDE
56 #define TYPEINIT_FACTORY(sType, Factory ) \
57 void* sType::CreateType() { return Factory; } \
58 TypeId sType::StaticType() { return &CreateType; } \
59 TypeId sType::Type() const { return &CreateType; } \
60 bool sType::IsOf( TypeId aSameOrSuperType ) \
62 if ( aSameOrSuperType == StaticType() ) \
65 #define STATICTYPE(sType) (sType::StaticType())
67 #define SUPERTYPE(sSuper) \
68 if ( sSuper::IsOf(aSameOrSuperType ) ) \
71 #define TYPEINIT_END(sType) \
74 bool sType::IsA( TypeId aSameOrSuperType ) const \
75 { return IsOf( aSameOrSuperType ); }
77 #define TYPEINIT0_FACTORY(sType, Factory) \
78 TYPEINIT_FACTORY(sType, Factory); \
80 #define TYPEINIT0_AUTOFACTORY(sType) TYPEINIT0_FACTORY(sType, new sType)
81 #define TYPEINIT0(sType) TYPEINIT0_FACTORY(sType, 0)
83 #define TYPEINIT1_FACTORY(sType, sSuper, Factory) \
84 TYPEINIT_FACTORY(sType, Factory); \
87 #define TYPEINIT1_AUTOFACTORY(sType, sSuper) \
88 TYPEINIT1_FACTORY(sType, sSuper, new sType)
89 #define TYPEINIT1(sType, sSuper) \
90 TYPEINIT1_FACTORY(sType, sSuper, 0)
92 #define TYPEINIT2_FACTORY(sType, sSuper1, sSuper2, Factory) \
93 TYPEINIT_FACTORY(sType, Factory); \
97 #define TYPEINIT2_AUTOFACTORY(sType, sSuper1, sSuper2) \
98 TYPEINIT2_FACTORY(sType, sSuper1, sSuper2, new sType)
99 #define TYPEINIT2(sType, sSuper1, sSuper2) \
100 TYPEINIT2_FACTORY(sType, sSuper1, sSuper2, 0)
102 #define TYPEINIT3_FACTORY(sType, sSuper1, sSuper2, sSuper3, Factory) \
103 TYPEINIT_FACTORY(sType, Factory); \
104 SUPERTYPE(sSuper1); \
105 SUPERTYPE(sSuper2); \
106 SUPERTYPE(sSuper3); \
108 #define TYPEINIT3(sType, sSuper1, sSuper2, sSuper3) \
109 TYPEINIT3_FACTORY(sType, sSuper1, sSuper2, sSuper3, 0)
111 #define TYPE(sType) (sType::StaticType())
112 #define ISA(sType) IsA(sType::StaticType())
113 #define ISOF(sType) IsOf(sType::StaticType())
114 #define CREATE(TypeId) (TypeId())
116 /** Exemplary application macros for pointers
117 (can be extended for use with references)
119 PTR_CAST: Safe pointer casting to a derived class.
120 Returns NULL pointer on cast error
122 T: Target type to cast into
123 p: Pointer to be cast into T
125 #define PTR_CAST( T, pObj ) rttiCast<T>(pObj, TYPE(T))
127 template<class T1
, class T2
>
128 inline T1
* rttiCast(T2
* pObj
, const TypeId
& rTypeId
) {
129 return (pObj
&& pObj
->IsA( rTypeId
)) ? static_cast<T1
*>(pObj
) : 0;
132 template<class T1
, class T2
>
133 inline const T1
* rttiCast(const T2
* pObj
, const TypeId
& rTypeId
) {
134 return (pObj
&& pObj
->IsA( rTypeId
)) ? static_cast<const T1
*>(pObj
) : 0;
137 /** Check whether object pObj has a Base Class T
138 (or if pObj is an instance of T) */
139 #define HAS_BASE( T, pObj ) \
140 ( pObj && (pObj)->IsA( TYPE(T) ) )
142 /** Check whether a pointer is targeting an object of type T. */
143 #define IS_TYPE(T,pObj) \
144 ( pObj && (pObj)->Type() == TYPE(T) )
148 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */