]>
git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/util/cast_util.h
c91b6ff1ee4a196043bac265d3140afab74bbe7b
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).
10 #include "rocksdb/rocksdb_namespace.h"
12 namespace ROCKSDB_NAMESPACE
{
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.
15 // It is not recommended to add new code to issue class casting. The preferred
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
));
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
;
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
);
42 } // namespace ROCKSDB_NAMESPACE