1 | //STARTHEADER |
---|
2 | // $Id: IsBase.hh 2622 2011-10-05 22:13:23Z soyez $ |
---|
3 | // |
---|
4 | // Copyright (c) 2005-2011, Matteo Cacciari, Gavin P. Salam and Gregory Soyez |
---|
5 | // |
---|
6 | //---------------------------------------------------------------------- |
---|
7 | // This file is part of FastJet. |
---|
8 | // |
---|
9 | // FastJet is free software; you can redistribute it and/or modify |
---|
10 | // it under the terms of the GNU General Public License as published by |
---|
11 | // the Free Software Foundation; either version 2 of the License, or |
---|
12 | // (at your option) any later version. |
---|
13 | // |
---|
14 | // The algorithms that underlie FastJet have required considerable |
---|
15 | // development and are described in hep-ph/0512210. If you use |
---|
16 | // FastJet as part of work towards a scientific publication, please |
---|
17 | // include a citation to the FastJet paper. |
---|
18 | // |
---|
19 | // FastJet is distributed in the hope that it will be useful, |
---|
20 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
21 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
22 | // GNU General Public License for more details. |
---|
23 | // |
---|
24 | // You should have received a copy of the GNU General Public License |
---|
25 | // along with FastJet. If not, see <http://www.gnu.org/licenses/>. |
---|
26 | //---------------------------------------------------------------------- |
---|
27 | //ENDHEADER |
---|
28 | |
---|
29 | #ifndef __FASTJET_INTERNAL_IS_BASE_HH__ |
---|
30 | #define __FASTJET_INTERNAL_IS_BASE_HH__ |
---|
31 | |
---|
32 | #include "fastjet/internal/numconsts.hh" |
---|
33 | |
---|
34 | FASTJET_BEGIN_NAMESPACE |
---|
35 | |
---|
36 | //--------------------------------------------------- |
---|
37 | // define a true and a false 'type' |
---|
38 | // Note: |
---|
39 | // we could actually template the type and recover |
---|
40 | // the TR1 integral_constant type. This also |
---|
41 | // includes adding a typedef for the type and a |
---|
42 | // typedef for the struct in the struct below |
---|
43 | // |
---|
44 | // This is going to be helpful to "split" a given |
---|
45 | // call into 2 options based on a type constraint |
---|
46 | // at compilation-time (rather than doing an "if" |
---|
47 | // which would only be resolved at runtime and could |
---|
48 | // thus resutl in compilation errors. |
---|
49 | //--------------------------------------------------- |
---|
50 | |
---|
51 | /// \if internal_doc |
---|
52 | /// \class integral_type |
---|
53 | /// a generic construct that promotes a generic value of a generic type |
---|
54 | /// as a type |
---|
55 | /// |
---|
56 | /// this has 2 template parameters: T, the considered type, and _t, a |
---|
57 | /// value of type T |
---|
58 | /// This object is a basic construct in type traits |
---|
59 | /// \endif |
---|
60 | template<typename T, T _t> |
---|
61 | struct integral_type{ |
---|
62 | static const T value = _t; ///< the value (only member carrying info) |
---|
63 | typedef T value_type; ///< a typedef for the type T |
---|
64 | typedef integral_type<T,_t> type; ///< a typedef for the whole structure |
---|
65 | }; |
---|
66 | |
---|
67 | // definition of the static member in integral_type |
---|
68 | template<typename T, T _t> |
---|
69 | const T integral_type<T, _t>::value; |
---|
70 | |
---|
71 | // shortcuts |
---|
72 | typedef integral_type<bool, true> true_type; ///< the bool 'true' value promoted to a type |
---|
73 | typedef integral_type<bool, false> false_type; ///< the bool 'false' value promoted to a type |
---|
74 | |
---|
75 | |
---|
76 | //--------------------------------------------------- |
---|
77 | // define a yes and a no type (based on their size) |
---|
78 | //--------------------------------------------------- |
---|
79 | typedef char (&__yes_type)[1]; //< the yes type |
---|
80 | typedef char (&__no_type) [2]; //< the no type |
---|
81 | |
---|
82 | |
---|
83 | //--------------------------------------------------- |
---|
84 | // Now deal with inheritance checks |
---|
85 | // |
---|
86 | // We want to provide a IsBaseAndDerived<B,D> type |
---|
87 | // trait that contains a value that is true if D |
---|
88 | // is derived from B and false otherwise. |
---|
89 | // |
---|
90 | // For an explanation of how the code below works, |
---|
91 | // have a look at |
---|
92 | // http://groups.google.com/group/comp.lang.c++.moderated/msg/dd6c4e4d5160bd83 |
---|
93 | // and the links therein |
---|
94 | // |
---|
95 | // WARNING: according to 'boost', this may have some |
---|
96 | // issues with MSVC7.1. See their code for a description |
---|
97 | // of the workaround used below |
---|
98 | //--------------------------------------------------- |
---|
99 | |
---|
100 | /// \if internal_doc |
---|
101 | /// \class __inheritance_helper |
---|
102 | /// helper for IsBasedAndDerived<B,D> |
---|
103 | /// \endif |
---|
104 | template<typename B, typename D> |
---|
105 | struct __inheritance_helper{ |
---|
106 | #if !((_MSC_VER !=0 ) && (_MSC_VER == 1310)) // MSVC 7.1 |
---|
107 | template <typename T> |
---|
108 | static __yes_type check_sig(D const volatile *, T); |
---|
109 | #else |
---|
110 | static __yes_type check_sig(D const volatile *, long); |
---|
111 | #endif |
---|
112 | static __no_type check_sig(B const volatile *, int); |
---|
113 | }; |
---|
114 | |
---|
115 | /// \if internal_doc |
---|
116 | /// \class IsBaseAndDerived |
---|
117 | /// check if the second template argument is derived from the first one |
---|
118 | /// |
---|
119 | /// this class has 2 template dependencies: B and D. It contains a |
---|
120 | /// static boolean value that will be true if D is derived from B and |
---|
121 | /// false otherwise. |
---|
122 | /// |
---|
123 | /// Note: This construct may have a problem with MSVC7.1. See the |
---|
124 | /// boost implementation for a description and workaround |
---|
125 | /// \endif |
---|
126 | template<typename B, typename D> |
---|
127 | struct IsBaseAndDerived{ |
---|
128 | #if ((_MSC_FULL_VER != 0) && (_MSC_FULL_VER >= 140050000)) |
---|
129 | #pragma warning(push) |
---|
130 | #pragma warning(disable:6334) |
---|
131 | #endif |
---|
132 | |
---|
133 | |
---|
134 | /// \if internal_doc |
---|
135 | /// a helper structure that will pick between a casting to B*const |
---|
136 | /// or D. |
---|
137 | /// |
---|
138 | /// precisely how this structure works involves advanced C++ |
---|
139 | /// conversion rules |
---|
140 | /// \endif |
---|
141 | struct Host{ |
---|
142 | #if !((_MSC_VER !=0 ) && (_MSC_VER == 1310)) |
---|
143 | operator B const volatile *() const; |
---|
144 | #else |
---|
145 | operator B const volatile * const&() const; |
---|
146 | #endif |
---|
147 | operator D const volatile *(); |
---|
148 | }; |
---|
149 | |
---|
150 | /// the boolean value being true if D is derived from B |
---|
151 | static const bool value = ((sizeof(B)!=0) && |
---|
152 | (sizeof(D)!=0) && |
---|
153 | (sizeof(__inheritance_helper<B,D>::check_sig(Host(), 0)) == sizeof(__yes_type))); |
---|
154 | |
---|
155 | #if ((_MSC_FULL_VER != 0) && (_MSC_FULL_VER >= 140050000)) |
---|
156 | #pragma warning(pop) |
---|
157 | #endif |
---|
158 | }; |
---|
159 | |
---|
160 | |
---|
161 | /// a little helper that returns a pointer to d of type B* if D is |
---|
162 | /// derived from B and NULL otherwise |
---|
163 | template<class B, class D> |
---|
164 | B* cast_if_derived(D* d){ |
---|
165 | return IsBaseAndDerived<B,D>::value ? (B*)(d) : NULL; |
---|
166 | } |
---|
167 | |
---|
168 | |
---|
169 | FASTJET_END_NAMESPACE |
---|
170 | |
---|
171 | |
---|
172 | #endif // __IS_BASE_OF_HH__ |
---|