/src/s2geometry/src/s2/base/casts.h
Line | Count | Source |
1 | | // Copyright Google Inc. All Rights Reserved. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS-IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | // |
15 | | |
16 | | #ifndef S2_BASE_CASTS_H_ |
17 | | #define S2_BASE_CASTS_H_ |
18 | | |
19 | | #include <cassert> |
20 | | #include <type_traits> |
21 | | |
22 | | #include "absl/base/config.h" |
23 | | |
24 | | template <typename To, typename From> |
25 | 0 | inline To down_cast(From* f) { |
26 | 0 | static_assert((std::is_base_of<From, std::remove_pointer_t<To>>::value), |
27 | 0 | "target type not derived from source type"); |
28 | | |
29 | | // We depend on abseil-cpp internals, but if this doesn't get defined, |
30 | | // we will just not have the assert. |
31 | 0 | #if ABSL_INTERNAL_HAS_RTTI |
32 | 0 | assert(f == nullptr || dynamic_cast<To>(f) != nullptr); |
33 | 0 | #endif |
34 | | |
35 | 0 | return static_cast<To>(f); |
36 | 0 | } Unexecuted instantiation: S2Loop::Shape const* down_cast<S2Loop::Shape const*, S2Shape const>(S2Shape const*) Unexecuted instantiation: S2Polygon::Shape const* down_cast<S2Polygon::Shape const*, S2Shape const>(S2Shape const*) |
37 | | |
38 | | // Overload of down_cast for references. Use like this: down_cast<T&>(foo). |
39 | | // The code is slightly convoluted because we're still using the pointer |
40 | | // form of dynamic cast. (The reference form throws an exception if it |
41 | | // fails.) |
42 | | // |
43 | | // There's no need for a special const overload either for the pointer |
44 | | // or the reference form. If you call down_cast with a const T&, the |
45 | | // compiler will just bind From to const T. |
46 | | template <typename To, typename From> |
47 | | inline To down_cast(From& f) { |
48 | | static_assert(std::is_lvalue_reference<To>::value, |
49 | | "target type not a reference"); |
50 | | static_assert((std::is_base_of<From, std::remove_reference_t<To>>::value), |
51 | | "target type not derived from source type"); |
52 | | |
53 | | // We skip the assert and hence the dynamic_cast if RTTI is disabled. |
54 | | #if ABSL_INTERNAL_HAS_RTTI |
55 | | assert(dynamic_cast<std::remove_reference_t<To>*>(&f) != nullptr); |
56 | | #endif |
57 | | |
58 | | return static_cast<To>(f); |
59 | | } |
60 | | |
61 | | #endif // S2_BASE_CASTS_H_ |