Commit be4a3034 authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook Github Bot

down_cast

Summary: [Folly] `down_cast` for polymorphic checked-in-debug down-casts.

Reviewed By: nbronson

Differential Revision: D17650399

fbshipit-source-id: 9cdd224b3f97ba40f88013877f968ed311a3ab3c
parent df012bf4
......@@ -722,6 +722,7 @@ if (BUILD_TESTS)
DIRECTORY lang/test/
TEST aligned_test SOURCES AlignedTest.cpp
TEST bits_test SOURCES BitsTest.cpp
TEST cast_test SOURCES CastTest.cpp
TEST cold_class_test SOURCES ColdClassTest.cpp
TEST safe_assert_test SOURCES SafeAssertTest.cpp
......
/*
* Copyright 2019-present Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <memory>
#include <type_traits>
#include <folly/Portability.h>
#include <folly/Traits.h>
#include <folly/lang/SafeAssert.h>
namespace folly {
// down_cast
//
// Unchecked polymorphic down-cast using static_cast. Only works for pairs of
// types where the cvref-unqualified source type is polymorphic and a base of
// the target type. The target type, which is passed as an explicit template
// param, must be cvref-unqualified. The return type is the target type with
// the same cvref-qualifiers or cvptr-qualifiers as the source type.
//
// Checked with an assertion in debug builds.
template <typename T, typename S>
FOLLY_ERASE like_t<S, T>* down_cast(S* ptr) noexcept {
using Q = std::remove_cv_t<S>;
static_assert(std::is_polymorphic<Q>::value, "not polymorphic");
static_assert(std::is_base_of<Q, T>::value, "not down-castable");
using R = like_t<S, T>;
FOLLY_SAFE_DCHECK(dynamic_cast<R*>(ptr), "not a runtime down-cast");
return static_cast<R*>(ptr);
}
template <typename T, typename S>
FOLLY_ERASE like_t<S&&, T> down_cast(S&& ref) noexcept {
return static_cast<like_t<S&&, T>>(*down_cast<T>(std::addressof(ref)));
}
} // namespace folly
/*
* Copyright 2019-present Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <folly/lang/Cast.h>
#include <folly/Utility.h>
#include <folly/portability/GTest.h>
using folly::as_const;
using folly::down_cast;
class CastTest : public testing::Test {};
template <typename T>
static T& stop(T&& t) {
return t;
}
template <typename T>
static void stop(T& t) = delete;
TEST_F(CastTest, down_cast) {
struct base {
virtual ~base() {}
};
struct derived : public base {};
derived obj;
base& b = obj;
EXPECT_TRUE(&obj == down_cast<derived>(&b));
EXPECT_TRUE(&obj == down_cast<derived>(&as_const(b)));
EXPECT_TRUE(&obj == &stop(down_cast<derived>(std::move(b))));
EXPECT_TRUE(&obj == &stop(down_cast<derived>(std::move(as_const(b)))));
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment