mirror of
https://github.com/BlackMATov/meta.hpp.git
synced 2025-12-15 03:45:30 +07:00
add is_virtual_base_of trait
This commit is contained in:
@@ -1180,6 +1180,38 @@ namespace meta_hpp::detail
|
|||||||
inline constexpr bool is_in_place_type_v = is_in_place_type<T>::value;
|
inline constexpr bool is_in_place_type_v = is_in_place_type<T>::value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
namespace meta_hpp::detail
|
||||||
|
{
|
||||||
|
namespace impl
|
||||||
|
{
|
||||||
|
template < typename From, typename To >
|
||||||
|
constexpr bool is_virtual_base_of_impl(...) noexcept {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <
|
||||||
|
typename From,
|
||||||
|
typename To,
|
||||||
|
decltype(static_cast<const volatile To*>(std::declval<const volatile From*>())) = nullptr >
|
||||||
|
constexpr bool is_virtual_base_of_impl(int) noexcept {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// clang-format off
|
||||||
|
|
||||||
|
template < typename Base, typename Derived >
|
||||||
|
struct is_virtual_base_of : std::integral_constant<bool,
|
||||||
|
std::is_base_of_v<Base, Derived> &&
|
||||||
|
impl::is_virtual_base_of_impl<Derived, Base>(0) &&
|
||||||
|
!impl::is_virtual_base_of_impl<Base, Derived>(0)> {};
|
||||||
|
|
||||||
|
// clang-format on
|
||||||
|
|
||||||
|
template < typename Base, typename Derived >
|
||||||
|
inline constexpr bool is_virtual_base_of_v = is_virtual_base_of<Base, Derived>::value;
|
||||||
|
}
|
||||||
|
|
||||||
namespace meta_hpp::detail
|
namespace meta_hpp::detail
|
||||||
{
|
{
|
||||||
class memory_buffer final {
|
class memory_buffer final {
|
||||||
|
|||||||
77
develop/untests/meta_base/is_virtual_base_of_tests.cpp
Normal file
77
develop/untests/meta_base/is_virtual_base_of_tests.cpp
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
/*******************************************************************************
|
||||||
|
* This file is part of the "https://github.com/blackmatov/meta.hpp"
|
||||||
|
* For conditions of distribution and use, see copyright notice in LICENSE.md
|
||||||
|
* Copyright (C) 2021-2023, by Matvey Cherevko (blackmatov@gmail.com)
|
||||||
|
******************************************************************************/
|
||||||
|
|
||||||
|
#include <meta.hpp/meta_all.hpp>
|
||||||
|
#include <doctest/doctest.h>
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
struct A {};
|
||||||
|
struct B : virtual A {};
|
||||||
|
struct C : virtual A {};
|
||||||
|
struct D : B, C {};
|
||||||
|
struct E {};
|
||||||
|
|
||||||
|
struct I {};
|
||||||
|
struct J : virtual I {};
|
||||||
|
struct K : J {};
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("meta/meta_base/is_virtual_base_of") {
|
||||||
|
namespace meta = meta_hpp;
|
||||||
|
using meta::detail::is_virtual_base_of;
|
||||||
|
using meta::detail::is_virtual_base_of_v;
|
||||||
|
|
||||||
|
{
|
||||||
|
static_assert(!is_virtual_base_of_v<A, A>);
|
||||||
|
static_assert(is_virtual_base_of_v<A, B>);
|
||||||
|
static_assert(is_virtual_base_of_v<A, C>);
|
||||||
|
|
||||||
|
static_assert(is_virtual_base_of_v<A, D>);
|
||||||
|
static_assert(!is_virtual_base_of_v<B, D>);
|
||||||
|
static_assert(!is_virtual_base_of_v<C, D>);
|
||||||
|
|
||||||
|
static_assert(!is_virtual_base_of_v<A, E>);
|
||||||
|
static_assert(!is_virtual_base_of_v<E, A>);
|
||||||
|
|
||||||
|
static_assert(!is_virtual_base_of_v<E, D>);
|
||||||
|
static_assert(!is_virtual_base_of_v<D, E>);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
static_assert(!is_virtual_base_of_v<I, I>);
|
||||||
|
static_assert(is_virtual_base_of_v<I, J>);
|
||||||
|
static_assert(is_virtual_base_of_v<I, K>);
|
||||||
|
|
||||||
|
static_assert(!is_virtual_base_of_v<J, I>);
|
||||||
|
static_assert(!is_virtual_base_of_v<J, J>);
|
||||||
|
static_assert(!is_virtual_base_of_v<J, K>);
|
||||||
|
|
||||||
|
static_assert(!is_virtual_base_of_v<K, I>);
|
||||||
|
static_assert(!is_virtual_base_of_v<K, J>);
|
||||||
|
static_assert(!is_virtual_base_of_v<K, K>);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
static_assert(is_virtual_base_of_v<I, J>);
|
||||||
|
|
||||||
|
static_assert(is_virtual_base_of_v<const I, J>);
|
||||||
|
static_assert(is_virtual_base_of_v<volatile I, J>);
|
||||||
|
static_assert(is_virtual_base_of_v<const volatile I, J>);
|
||||||
|
|
||||||
|
static_assert(is_virtual_base_of_v<const I, const J>);
|
||||||
|
static_assert(is_virtual_base_of_v<volatile I, const J>);
|
||||||
|
static_assert(is_virtual_base_of_v<const volatile I, const J>);
|
||||||
|
|
||||||
|
static_assert(is_virtual_base_of_v<const I, volatile J>);
|
||||||
|
static_assert(is_virtual_base_of_v<volatile I, volatile J>);
|
||||||
|
static_assert(is_virtual_base_of_v<const volatile I, volatile J>);
|
||||||
|
|
||||||
|
static_assert(is_virtual_base_of_v<const I, const volatile J>);
|
||||||
|
static_assert(is_virtual_base_of_v<volatile I, const volatile J>);
|
||||||
|
static_assert(is_virtual_base_of_v<const volatile I, const volatile J>);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -19,6 +19,7 @@
|
|||||||
#include "meta_base/insert_or_assign.hpp"
|
#include "meta_base/insert_or_assign.hpp"
|
||||||
#include "meta_base/intrusive_ptr.hpp"
|
#include "meta_base/intrusive_ptr.hpp"
|
||||||
#include "meta_base/is_in_place_type.hpp"
|
#include "meta_base/is_in_place_type.hpp"
|
||||||
|
#include "meta_base/is_virtual_base_of.hpp"
|
||||||
#include "meta_base/memory_buffer.hpp"
|
#include "meta_base/memory_buffer.hpp"
|
||||||
#include "meta_base/noncopyable.hpp"
|
#include "meta_base/noncopyable.hpp"
|
||||||
#include "meta_base/nonesuch.hpp"
|
#include "meta_base/nonesuch.hpp"
|
||||||
|
|||||||
41
headers/meta.hpp/meta_base/is_virtual_base_of.hpp
Normal file
41
headers/meta.hpp/meta_base/is_virtual_base_of.hpp
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
/*******************************************************************************
|
||||||
|
* This file is part of the "https://github.com/blackmatov/meta.hpp"
|
||||||
|
* For conditions of distribution and use, see copyright notice in LICENSE.md
|
||||||
|
* Copyright (C) 2021-2023, by Matvey Cherevko (blackmatov@gmail.com)
|
||||||
|
******************************************************************************/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "base.hpp"
|
||||||
|
|
||||||
|
namespace meta_hpp::detail
|
||||||
|
{
|
||||||
|
namespace impl
|
||||||
|
{
|
||||||
|
template < typename From, typename To >
|
||||||
|
constexpr bool is_virtual_base_of_impl(...) noexcept {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <
|
||||||
|
typename From,
|
||||||
|
typename To,
|
||||||
|
decltype(static_cast<const volatile To*>(std::declval<const volatile From*>())) = nullptr >
|
||||||
|
constexpr bool is_virtual_base_of_impl(int) noexcept {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// clang-format off
|
||||||
|
|
||||||
|
template < typename Base, typename Derived >
|
||||||
|
struct is_virtual_base_of : std::integral_constant<bool,
|
||||||
|
std::is_base_of_v<Base, Derived> &&
|
||||||
|
impl::is_virtual_base_of_impl<Derived, Base>(0) &&
|
||||||
|
!impl::is_virtual_base_of_impl<Base, Derived>(0)> {};
|
||||||
|
|
||||||
|
// clang-format on
|
||||||
|
|
||||||
|
template < typename Base, typename Derived >
|
||||||
|
inline constexpr bool is_virtual_base_of_v = is_virtual_base_of<Base, Derived>::value;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user