diff options
author | robot-piglet <robot-piglet@yandex-team.com> | 2024-12-24 18:04:59 +0300 |
---|---|---|
committer | robot-piglet <robot-piglet@yandex-team.com> | 2024-12-24 18:24:16 +0300 |
commit | b821606f7bd364dc755d37b5bcb3559130675364 (patch) | |
tree | 4e6dfde67ea1e9c5178c3ab28e35707d89e61d1f /library | |
parent | 8317ba3835dc7e9c854f6472cb686b1e3e3dd7c8 (diff) | |
download | ydb-b821606f7bd364dc755d37b5bcb3559130675364.tar.gz |
Intermediate changes
commit_hash:41c16027e2f796197b98307419a63da9fa3f1a88
Diffstat (limited to 'library')
-rw-r--r-- | library/cpp/yt/misc/typeid-inl.h | 49 | ||||
-rw-r--r-- | library/cpp/yt/misc/typeid.h | 27 | ||||
-rw-r--r-- | library/cpp/yt/misc/unittests/typeid_sample.cpp | 14 | ||||
-rw-r--r-- | library/cpp/yt/misc/unittests/typeid_sample.h | 17 | ||||
-rw-r--r-- | library/cpp/yt/misc/unittests/typeid_ut.cpp | 25 | ||||
-rw-r--r-- | library/cpp/yt/misc/unittests/ya.make | 2 |
6 files changed, 134 insertions, 0 deletions
diff --git a/library/cpp/yt/misc/typeid-inl.h b/library/cpp/yt/misc/typeid-inl.h new file mode 100644 index 0000000000..a4518cfa46 --- /dev/null +++ b/library/cpp/yt/misc/typeid-inl.h @@ -0,0 +1,49 @@ +#ifndef TYPEID_INL_H_ +#error "Direct inclusion of this file is not allowed, include typeid.h" +// For the sake of sane code completion. +#include "typeid.h" +#endif + +#include "port.h" + +#include <util/system/compiler.h> + +namespace NYT { + +//////////////////////////////////////////////////////////////////////////////// + +namespace NDetail { + +template <class T> +class TTypeidTag +{ }; + +} // namespace NDetail + +template <class T> +const std::type_info& Typeid() +{ + if constexpr (requires { TypeidImpl(NDetail::TTypeidTag<T>()); }) { + return TypeidImpl(NDetail::TTypeidTag<T>()); + } else { + return typeid(T); + } +} + +//////////////////////////////////////////////////////////////////////////////// + +#undef YT_DECLARE_TYPEID +#undef YT_DEFINE_TYPEID + +#define YT_DECLARE_TYPEID(type) \ + [[maybe_unused]] YT_ATTRIBUTE_USED const std::type_info& TypeidImpl(::NYT::NDetail::TTypeidTag<type>); + +#define YT_DEFINE_TYPEID(type) \ + [[maybe_unused]] YT_ATTRIBUTE_USED Y_FORCE_INLINE const std::type_info& TypeidImpl(::NYT::NDetail::TTypeidTag<type>) \ + { \ + return typeid(type); \ + } + +//////////////////////////////////////////////////////////////////////////////// + +} // namespace NYT diff --git a/library/cpp/yt/misc/typeid.h b/library/cpp/yt/misc/typeid.h new file mode 100644 index 0000000000..d4584e64f2 --- /dev/null +++ b/library/cpp/yt/misc/typeid.h @@ -0,0 +1,27 @@ +#pragma once + +#include <typeinfo> + +namespace NYT { + +//////////////////////////////////////////////////////////////////////////////// +// Enables accessing type_info for incomplete types. + +//! Place this macro in header file after forward-declaring a type. +#define YT_DECLARE_TYPEID(type) + +//! Place this macro in header or source file after fully defining a type. +#define YT_DEFINE_TYPEID(type) + +//! Equivalent to |typeid(T)| but also works for incomplete types +//! annotated with YT_DECLARE_TYPEID. +template <class T> +const std::type_info& Typeid(); + +//////////////////////////////////////////////////////////////////////////////// + +} // namespace NYT + +#define TYPEID_INL_H_ +#include "typeid-inl.h" +#undef TYPEID_INL_H_ diff --git a/library/cpp/yt/misc/unittests/typeid_sample.cpp b/library/cpp/yt/misc/unittests/typeid_sample.cpp new file mode 100644 index 0000000000..9a3184e458 --- /dev/null +++ b/library/cpp/yt/misc/unittests/typeid_sample.cpp @@ -0,0 +1,14 @@ +#include "typeid_sample.h" + +namespace NYT { + +//////////////////////////////////////////////////////////////////////////////// + +struct TTypeidIncomplete +{ }; + +YT_DEFINE_TYPEID(TTypeidIncomplete); + +//////////////////////////////////////////////////////////////////////////////// + +} // namespace NYT diff --git a/library/cpp/yt/misc/unittests/typeid_sample.h b/library/cpp/yt/misc/unittests/typeid_sample.h new file mode 100644 index 0000000000..d6b5a288b5 --- /dev/null +++ b/library/cpp/yt/misc/unittests/typeid_sample.h @@ -0,0 +1,17 @@ +#pragma once + +#include <library/cpp/yt/misc/typeid.h> + +namespace NYT { + +//////////////////////////////////////////////////////////////////////////////// + +struct TTypeidIncomplete; +YT_DECLARE_TYPEID(TTypeidIncomplete); + +struct TTypeidComplete +{ }; + +//////////////////////////////////////////////////////////////////////////////// + +} // namespace NYT diff --git a/library/cpp/yt/misc/unittests/typeid_ut.cpp b/library/cpp/yt/misc/unittests/typeid_ut.cpp new file mode 100644 index 0000000000..3400c832e2 --- /dev/null +++ b/library/cpp/yt/misc/unittests/typeid_ut.cpp @@ -0,0 +1,25 @@ +#include "typeid_sample.h" + +#include <library/cpp/yt/misc/typeid.h> + +#include <library/cpp/testing/gtest/gtest.h> + +namespace NYT { +namespace { + +//////////////////////////////////////////////////////////////////////////////// + +TEST(TTypeidTest, Complete) +{ + EXPECT_NE(std::string(Typeid<TTypeidComplete>().name()).find("TTypeidComplete"), std::string::npos); +} + +TEST(TTypeidTest, Incomplete) +{ + EXPECT_NE(std::string(Typeid<TTypeidIncomplete>().name()).find("TTypeidIncomplete"), std::string::npos); +} + +//////////////////////////////////////////////////////////////////////////////// + +} // namespace +} // namespace NYT diff --git a/library/cpp/yt/misc/unittests/ya.make b/library/cpp/yt/misc/unittests/ya.make index c914ca3061..ba7525f66a 100644 --- a/library/cpp/yt/misc/unittests/ya.make +++ b/library/cpp/yt/misc/unittests/ya.make @@ -12,6 +12,8 @@ SRCS( strong_typedef_ut.cpp tag_invoke_cpo_ut.cpp tag_invoke_impl_ut.cpp + typeid_sample.cpp + typeid_ut.cpp ) PEERDIR( |