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