]>
Commit | Line | Data |
---|---|---|
11fdf7f2 TL |
1 | // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. |
2 | // This source code is licensed under both the GPLv2 (found in the | |
3 | // COPYING file in the root directory) and Apache 2.0 License | |
4 | // (found in the LICENSE.Apache file in the root directory). | |
5 | ||
6 | #pragma once | |
1e59de90 TL |
7 | |
8 | #include <type_traits> | |
9 | ||
10 | #include "rocksdb/rocksdb_namespace.h" | |
11 | ||
f67539c2 | 12 | namespace ROCKSDB_NAMESPACE { |
11fdf7f2 TL |
13 | // The helper function to assert the move from dynamic_cast<> to |
14 | // static_cast<> is correct. This function is to deal with legacy code. | |
20effc67 | 15 | // It is not recommended to add new code to issue class casting. The preferred |
11fdf7f2 TL |
16 | // solution is to implement the functionality without a need of casting. |
17 | template <class DestClass, class SrcClass> | |
18 | inline DestClass* static_cast_with_check(SrcClass* x) { | |
19 | DestClass* ret = static_cast<DestClass*>(x); | |
20 | #ifdef ROCKSDB_USE_RTTI | |
21 | assert(ret == dynamic_cast<DestClass*>(x)); | |
22 | #endif | |
23 | return ret; | |
24 | } | |
1e59de90 TL |
25 | |
26 | // A wrapper around static_cast for lossless conversion between integral | |
27 | // types, including enum types. For example, this can be used for converting | |
28 | // between signed/unsigned or enum type and underlying type without fear of | |
29 | // stripping away data, now or in the future. | |
30 | template <typename To, typename From> | |
31 | inline To lossless_cast(From x) { | |
32 | using FromValue = typename std::remove_reference<From>::type; | |
33 | static_assert( | |
34 | std::is_integral<FromValue>::value || std::is_enum<FromValue>::value, | |
35 | "Only works on integral types"); | |
36 | static_assert(std::is_integral<To>::value || std::is_enum<To>::value, | |
37 | "Only works on integral types"); | |
38 | static_assert(sizeof(To) >= sizeof(FromValue), "Must be lossless"); | |
39 | return static_cast<To>(x); | |
40 | } | |
41 | ||
f67539c2 | 42 | } // namespace ROCKSDB_NAMESPACE |