Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: provide an implementation that does not requires boost.pfr on C++26. yay !!! #65

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/build_cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ jobs:
}
- {
name: "macOS",
os: macos-13,
os: macos-latest,
common_flags: "",
debug_flags: "-DCMAKE_BUILD_TYPE=Debug",
release_flags: "-DCMAKE_BUILD_TYPE=Release",
build_flags: "",
sdk: "/opt/ossia-sdk-x86_64",
pre_build: "sudo xcode-select -s /Applications/Xcode_15.0.app"
pre_build: "sudo xcode-select -s /Applications/Xcode_16.2.app"
}

steps:
Expand Down
15 changes: 14 additions & 1 deletion include/avnd/common/aggregates.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
/* SPDX-License-Identifier: GPL-3.0-or-later */

#include <avnd/common/index_sequence.hpp>
#include <avnd/common/inline.hpp>
#include <avnd/common/tuple.hpp>

#include <string_view>
#include <tuple>

namespace avnd
{
Expand All @@ -15,9 +16,21 @@ struct typelist
};
}

// clang-format off
#if defined(__clang_major__) && (__clang_major__ < 21)
#define AVND_USE_BOOST_PFR 1
#endif

#if defined(_MSC_VER)
#define AVND_USE_BOOST_PFR 1
#endif

#if !defined(AVND_USE_BOOST_PFR)
#if (__cpp_structured_bindings < 202403L) || (__cpp_pack_indexing < 202311L) || (__cplusplus < 202400L)
#define AVND_USE_BOOST_PFR 1
#endif
#endif
// clang-format on

#if AVND_USE_BOOST_PFR
#include <boost/mp11/algorithm.hpp>
Expand Down
100 changes: 15 additions & 85 deletions include/avnd/common/aggregates.p1061.hpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
#if __has_include(<tuplet/tuple.hpp>)
#include <tuplet/tuple.hpp>
namespace tpl = tuplet;
#else
namespace tpl = std;
#endif

namespace avnd
{
namespace pfr
{
AVND_INLINE
constexpr auto structure_to_typelist(const auto& s) noexcept
{
const auto& [... elts] = s;
return typelist<std::decay_t<decltype(elts)>...>{};
}
AVND_INLINE
constexpr auto structure_to_tuple(const auto& s) noexcept
{
const auto& [... elts] = s;
Expand All @@ -23,106 +20,39 @@ namespace detail
{
namespace sequence_tuple = ::tpl;
template <typename S>
constexpr auto tie_as_tuple(S&& s) noexcept
AVND_INLINE constexpr auto tie_as_tuple(S&& s) noexcept
{
auto&& [... elts] = static_cast<S&&>(s);
return tpl::tie(elts...);
return tpl::tie(std::forward_like<S>(elts)...);
}
}

template <class T>
constexpr auto fields_count_impl(const T& t) noexcept
AVND_INLINE constexpr auto fields_count_impl(const T& t) noexcept
{
const auto& [... elts] = t;
return avnd::num<sizeof...(elts)>{};
}

template <typename T>
static constexpr const std::size_t tuple_size_v = fields_count<T>();
static constexpr const std::size_t tuple_size_v
= decltype(fields_count_impl<T>(std::declval<const T&>()))::value;

template <std::size_t I, typename S>
constexpr auto& get(S&& s) noexcept
AVND_INLINE constexpr auto&& get(S&& s) noexcept
{
if constexpr(I == 0)
{
auto&& [a, ... elts] = static_cast<S&&>(s);
return a;
}
else if constexpr(I == 1)
{
auto&& [a, b, ... elts] = static_cast<S&&>(s);
return b;
}
else if constexpr(I == 2)
{
auto&& [a, b, c, ... elts] = static_cast<S&&>(s);
return c;
}
else if constexpr(I == 3)
{
auto&& [a, b, c, d, ... elts] = static_cast<S&&>(s);
return d;
}
else if constexpr(I == 4)
{
auto&& [a, b, c, d, e, ... elts] = static_cast<S&&>(s);
return e;
}
else if constexpr(I == 5)
{
auto&& [a, b, c, d, e, f, ... elts] = static_cast<S&&>(s);
return f;
}
else if constexpr(I == 6)
{
auto&& [a, b, c, d, e, f, g, ... elts] = static_cast<S&&>(s);
return g;
}
else if constexpr(I == 7)
{
auto&& [a, b, c, d, e, f, g, h, ... elts] = static_cast<S&&>(s);
return h;
}
else if constexpr(I == 8)
{
auto&& [a, b, c, d, e, f, g, h, i, ... elts] = static_cast<S&&>(s);
return i;
}
else if constexpr(I == 9)
{
auto&& [a, b, c, d, e, f, g, h, i, j, ... elts] = static_cast<S&&>(s);
return j;
}
else if constexpr(I == 10)
{
auto&& [a, b, c, d, e, f, g, h, i, j, k, ... elts] = static_cast<S&&>(s);
return k;
}
else if constexpr(I == 11)
{
auto&& [a, b, c, d, e, f, g, h, i, j, k, l, ... elts] = static_cast<S&&>(s);
return l;
}
else if constexpr(I == 12)
{
auto&& [a, b, c, d, e, f, g, h, i, j, k, l, m, ... elts] = static_cast<S&&>(s);
return m;
}
else if constexpr(I > 12)
{
auto&& [... elts] = static_cast<S&&>(s);
return tpl::get<I>(tpl::tie(elts...));
}
auto&& [... elts] = static_cast<S&&>(s);
return std::forward_like<S>(elts...[I]);
}

template <std::size_t Index, typename T>
using tuple_element_t = std::decay_t<decltype(get<Index>(std::declval<const T&>()))>;

template <typename S>
constexpr auto for_each_field(S&& s, auto&& f) noexcept
template <typename S, typename F>
AVND_INLINE constexpr auto for_each_field(S&& s, F&& f) noexcept
{
auto&& [... elts] = std::forward<S>(s);
(f(elts), ...);
auto&& [... elts] = static_cast<S&&>(s);
((static_cast<F&&>(f)(std::forward_like<S>(elts))), ...);
}
}

Expand Down
13 changes: 7 additions & 6 deletions include/avnd/common/for_nth.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ constexpr void for_each_field_ref(T&& value, F&& func)
#if AVND_USE_BOOST_PFR
using namespace pfr;
using namespace pfr::detail;
constexpr std::size_t fields_count_val
AVND_STATIC_CONSTEXPR std::size_t fields_count_val
= boost::pfr::tuple_size_v<std::remove_reference_t<T>>;

auto t = boost::pfr::detail::tie_as_tuple(
Expand Down Expand Up @@ -74,7 +74,7 @@ constexpr void for_each_field_ref(T&& value, F&& func)
#if AVND_USE_BOOST_PFR
using namespace pfr;
using namespace pfr::detail;
constexpr std::size_t fields_count_val
AVND_STATIC_CONSTEXPR std::size_t fields_count_val
= avnd::pfr::tuple_size_v<std::remove_reference_t<T>>;

auto t = avnd::pfr::detail::tie_as_tuple(value);
Expand All @@ -97,9 +97,8 @@ constexpr void for_each_field_ref_n(T&& value, F&& func)
#if AVND_USE_BOOST_PFR
using namespace pfr;
using namespace pfr::detail;
constexpr std::size_t fields_count_val
AVND_STATIC_CONSTEXPR std::size_t fields_count_val
= avnd::pfr::tuple_size_v<std::remove_reference_t<T>>;

auto t = avnd::pfr::detail::tie_as_tuple(value);

[&]<std::size_t... I>(std::index_sequence<I...>)
Expand All @@ -109,7 +108,9 @@ constexpr void for_each_field_ref_n(T&& value, F&& func)
(std::make_index_sequence<fields_count_val>{});
#else
auto&& [... elts] = value;
(func(elts), ...);
const auto [... Is] = field_indices(std::make_index_sequence<sizeof...(elts)>{});

(func(elts, Is), ...);
#endif
}

Expand Down Expand Up @@ -147,7 +148,7 @@ constexpr void for_each_field_function_table(T&& value, R func)
#endif
using namespace avnd::pfr;
using namespace avnd::pfr::detail;
constexpr std::size_t fields_count_val
AVND_STATIC_CONSTEXPR std::size_t fields_count_val
= avnd::pfr::tuple_size_v<std::remove_reference_t<T>>;

auto t = avnd::pfr::detail::tie_as_tuple(value);
Expand Down
8 changes: 8 additions & 0 deletions include/avnd/common/index.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#pragma once
#include <avnd/common/tuple.hpp>

#include <array>
#include <utility>

Expand All @@ -23,6 +25,12 @@ struct field_reflection
static const constexpr auto index = Idx;
};

template <std::size_t... I>
static constexpr auto field_indices(std::index_sequence<I...>)
{
return tpl::tuple<avnd::field_index<I>...>{};
}

template <int N, typename T, std::size_t M>
constexpr int index_of_element(const std::array<T, M>& arr) noexcept
{
Expand Down
19 changes: 15 additions & 4 deletions include/avnd/common/inline.hpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
#pragma once

#if defined(__GNUC__)
#include <version>
#if __cpp_constexpr >= 202211L
#define AVND_STATIC_CONSTEXPR static constexpr
#else
#define AVND_STATIC_CONSTEXPR constexpr
#endif

#if defined(__clang__)
#define AVND_INLINE inline __attribute__((always_inline))
#elif defined(__clang__)
#elif defined(__GNUC__)
#define AVND_INLINE inline __attribute__((always_inline))
#elif defined(_MSC_VER)
#define AVND_INLINE inline __forceinline
#else
#define AVND_INLINE inline
#endif

#if defined(__clang__)
#if defined __has_attribute
#if __has_attribute(flatten)
#define AVND_INLINE_FLATTEN AVND_INLINE __attribute__((flatten))
#else
#endif
#endif

#if !defined(AVND_INLINE_FLATTEN)
#define AVND_INLINE_FLATTEN AVND_INLINE
#endif
Loading
Loading