From 0a121d913d18e16c7217a8c862213088039d9ae4 Mon Sep 17 00:00:00 2001 From: Mat Sutcliffe Date: Sun, 3 Oct 2021 21:39:43 +0100 Subject: [PATCH] Own implementation of std::tuple with reduced functionality To reduce compile time. --- src/blackmisc/metaclass.h | 45 +++++++---------------------------- src/blackmisc/tuple.h | 50 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 37 deletions(-) create mode 100644 src/blackmisc/tuple.h diff --git a/src/blackmisc/metaclass.h b/src/blackmisc/metaclass.h index 55c2c1b74..220fd8958 100644 --- a/src/blackmisc/metaclass.h +++ b/src/blackmisc/metaclass.h @@ -12,6 +12,7 @@ #define BLACKMISC_METACLASS_H #include "blackmisc/invoke.h" +#include "blackmisc/tuple.h" #include #include #include @@ -120,9 +121,8 @@ namespace BlackMisc constexpr operator QLatin1String() const { return m_latin1; } }; - // *INDENT-OFF* /*! - * Type wrapper for passing MetaFlag to CMetaClassIntrospector::with and CMetaClassIntrospector::without. + * Type wrapper for passing MetaFlag to CMetaMember::has. * \ingroup MetaClass */ template @@ -189,19 +189,11 @@ namespace BlackMisc struct CMetaMemberList { //! Tuple of CMetaMember. - const std::tuple m_members; + const Private::tuple m_members; //! Number of members. static constexpr size_t c_size = sizeof...(Members); - - //! Convenience method returning the member at index I. - template - constexpr auto at(std::integral_constant = {}) const - { - return std::get(m_members); - } }; - // *INDENT-ON* /*! * Metadata flags attached to members of a meta class. @@ -242,7 +234,7 @@ namespace BlackMisc template constexpr static CMetaMemberList makeMetaMemberList(Members... members) { - return { std::tuple(members...) }; + return { { { { members }... } } }; } //! Return a CMetaMethod of type deduced from the type of the member. @@ -255,13 +247,12 @@ namespace BlackMisc } }; - // *INDENT-OFF* /*! - * Implementation of an introspector for the metaclass of T. + * An introspector for a metaclass. * Obtain an instance of this class via BlackMisc::introspect. * \ingroup MetaClass */ - template + template class CMetaClassIntrospector { public: @@ -269,33 +260,20 @@ namespace BlackMisc template static void forEachMember(F &&visitor) { - (static_cast(std::forward(visitor)(MetaClass::getMemberList().at(std::integral_constant()))), ...); + MetaClass::getMemberList().m_members.for_each(std::forward(visitor)); } }; - // *INDENT-ON* namespace Private { //! \private Friend class of all value classes, so it can access the private nested class. struct CMetaClassAccessor { - template - constexpr static auto getIntrospector(std::index_sequence) - { - return CMetaClassIntrospector(); - } - template constexpr static auto getIntrospector() { - return getIntrospector(std::make_index_sequence()); + return CMetaClassIntrospector(); } - - template - constexpr static std::true_type hasMetaClass(int, typename T::MetaClass * = nullptr) { return {}; } - - template - constexpr static std::false_type hasMetaClass(...) { return {}; } }; } @@ -310,13 +288,6 @@ namespace BlackMisc return Private::CMetaClassAccessor::getIntrospector(); } - /*! - * Trait that is true if T has a metaclass. - * \ingroup MetaClass - */ - template - struct THasMetaClass : public decltype(Private::CMetaClassAccessor::hasMetaClass(0)) {}; - } // namespace #endif diff --git a/src/blackmisc/tuple.h b/src/blackmisc/tuple.h new file mode 100644 index 000000000..cf7ba16ab --- /dev/null +++ b/src/blackmisc/tuple.h @@ -0,0 +1,50 @@ +/* Copyright (C) 2021 + * swift Project Community / Contributors + * + * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution. No part of swift project, including this file, may be copied, modified, propagated, + * or distributed except according to the terms contained in the LICENSE file. + */ + +//! \file + +#ifndef BLACKMISC_TUPLE_H +#define BLACKMISC_TUPLE_H + +#include +#include + +//! \cond +namespace BlackMisc::Private +{ + /* + * Own minimal implementation of std::tuple with very limited functionality. + * Only used to reduce compile time for the metaclass system. + */ + + template + struct tuple_part + { + T value; + }; + + template + struct tuple_impl; + + template + struct tuple_impl, Ts...> : public tuple_part... + { + template + void for_each(F &&visitor) const + { + (static_cast(visitor(static_cast&>(*this).value)), ...); + } + }; + + template + struct tuple : public tuple_impl, Ts...> + {}; +} +//! \endcond + +#endif