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

type_info_of

Summary: [Folly] `type_info_of`, which returns `typeid` when RTTI is available, otherwise `nullptr`.

Reviewed By: swolchok

Differential Revision: D14159675

fbshipit-source-id: b64ab770f12c7385bb286f658627d085e561def6
parent 12314eb7
main v2022.02.14.00 v2022.02.07.00 v2022.01.31.00 v2022.01.24.00 v2022.01.17.00 v2022.01.10.00 v2022.01.03.00 v2021.12.27.00 v2021.12.20.00 v2021.12.13.00 v2021.12.06.00 v2021.11.29.00 v2021.11.15.00 v2021.11.08.00 v2021.11.01.00 v2021.10.25.00 v2021.10.18.00 v2021.10.11.00 v2021.10.04.00 v2021.09.27.00 v2021.09.20.00 v2021.09.13.00 v2021.09.06.00 v2021.08.30.00 v2021.08.23.00 v2021.08.02.00 v2021.07.22.00 v2021.07.20.01 v2021.07.20.00 v2021.06.28.00 v2021.06.14.00 v2021.06.07.00 v2021.05.31.00 v2021.05.24.00 v2021.05.17.00 v2021.05.10.00 v2021.05.03.00 v2021.04.26.00 v2021.04.19.00 v2021.04.12.00 v2021.04.05.00 v2021.03.29.00 v2021.03.22.00 v2021.03.15.00 v2021.03.08.00 v2021.03.01.00 v2021.02.22.00 v2021.02.15.00 v2021.02.08.00 v2021.02.01.00 v2021.01.25.00 v2021.01.18.01 v2021.01.18.00 v2021.01.11.00 v2021.01.04.00 v2020.12.28.00 v2020.12.21.00 v2020.12.14.00 v2020.12.07.00 v2020.11.30.00 v2020.11.23.00 v2020.11.16.00 v2020.11.09.00 v2020.11.02.00 v2020.10.26.00 v2020.10.19.00 v2020.10.12.00 v2020.10.05.00 v2020.09.28.00 v2020.09.21.00 v2020.09.14.00 v2020.09.07.00 v2020.08.31.00 v2020.08.24.00 v2020.08.17.00 v2020.08.10.00 v2020.08.03.00 v2020.07.27.00 v2020.07.20.00 v2020.07.13.00 v2020.07.06.00 v2020.06.29.00 v2020.06.15.00 v2020.06.08.00 v2020.06.01.00 v2020.05.25.00 v2020.05.18.00 v2020.05.11.00 v2020.05.04.00 v2020.04.27.00 v2020.04.20.00 v2020.04.13.00 v2020.04.06.00 v2020.03.30.00 v2020.03.23.00 v2020.03.16.00 v2020.03.09.00 v2020.03.02.00 v2020.02.24.00 v2020.02.17.00 v2020.02.10.00 v2020.02.03.00 v2020.01.27.00 v2020.01.20.00 v2020.01.13.00 v2020.01.06.00 v2019.12.30.00 v2019.12.23.00 v2019.12.16.00 v2019.12.09.00 v2019.12.06.00 v2019.12.02.00 v2019.11.11.00 v2019.11.04.00 v2019.10.28.00 v2019.10.21.00 v2019.10.14.00 v2019.10.07.00 v2019.09.30.00 v2019.09.23.00 v2019.09.16.00 v2019.09.09.00 v2019.09.02.00 v2019.08.26.00 v2019.08.19.00 v2019.08.12.00 v2019.08.05.00 v2019.07.29.00 v2019.07.22.00 v2019.06.17.00 v2019.06.10.00 v2019.06.03.00 v2019.05.27.00 v2019.05.20.00 v2019.05.13.00 v2019.05.06.00 v2019.04.29.00 v2019.04.22.00 v2019.04.15.00 v2019.04.08.00 v2019.04.01.00 v2019.03.25.00 v2019.03.18.00 v2019.03.04.00 v2019.02.25.00
No related merge requests found
......@@ -24,6 +24,7 @@
#include <folly/Likely.h>
#include <folly/detail/Singleton.h>
#include <folly/lang/Exception.h>
#include <folly/lang/TypeInfo.h>
namespace folly {
namespace detail {
......@@ -75,12 +76,12 @@ class StaticSingletonManagerWithRtti {
}
template <typename T, typename Tag>
FOLLY_ALWAYS_INLINE static std::type_info const& key_() {
#if FOLLY_HAS_RTTI
return typeid(TypeTuple<T, Tag>);
#else
throw_exception<std::logic_error>("rtti unavailable");
#endif
FOLLY_ALWAYS_INLINE static Key const& key_() {
auto const key = type_info_of<TypeTuple<T, Tag>>();
if (!key) {
throw_exception<std::logic_error>("rtti unavailable");
}
return *key;
}
FOLLY_NOINLINE static void* create_(Key const& key, Make& make, Cache& cache);
......
/*
* 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 <typeinfo>
#include <folly/Portability.h>
namespace folly {
// type_info_of
//
// Returns &typeid(T) if RTTI is available, nullptr otherwise.
//
// This overload works on the static type of the template parameter.
template <typename T>
FOLLY_ALWAYS_INLINE static std::type_info const* type_info_of() {
#if FOLLY_HAS_RTTI
return &typeid(T);
#else
return nullptr;
#endif
}
// type_info_of
//
// Returns &typeid(t) if RTTI is available, nullptr otherwise.
//
// This overload works on the dynamic type of the non-template parameter.
template <typename T>
FOLLY_ALWAYS_INLINE static std::type_info const* type_info_of(T const& t) {
#if FOLLY_HAS_RTTI
return &typeid(t);
#else
return nullptr;
#endif
}
} // 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/TypeInfo.h>
#include <folly/portability/GTest.h>
class TypeInfoTest : public testing::Test {};
namespace std {
static void PrintTo(type_info const& ti, std::ostream* out) {
*out << ti.name();
}
} // namespace std
namespace {
struct Foo {};
struct Bar : Foo {
virtual ~Bar() {}
};
struct Toc : Bar {};
} // namespace
TEST_F(TypeInfoTest, exanples) {
EXPECT_EQ(typeid(Foo), *folly::type_info_of<Foo>());
EXPECT_EQ(typeid(Foo), *folly::type_info_of(Foo()));
EXPECT_EQ(typeid(Foo), *folly::type_info_of(static_cast<Foo const&>(Bar())));
EXPECT_EQ(typeid(Bar), *folly::type_info_of(static_cast<Bar const&>(Bar())));
EXPECT_EQ(typeid(Toc), *folly::type_info_of(static_cast<Bar const&>(Toc())));
}
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