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

Add ability to retrieve field names for structs without default constructor #81

Merged
merged 2 commits into from
Nov 22, 2024
Merged
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: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 0.2.35

* Added ability to retrieve field names for structs without default constructor (e.g. due to reference fields).

# 0.2.34

* Added `mbo::types::Required` which is similar to `RefWrap` but stores the actual type (and unlike `std::optional` cannot be reset).
Expand Down
25 changes: 18 additions & 7 deletions mbo/types/internal/struct_names_clang.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@ inline constexpr std::size_t kMaxFieldCount = 50;

template<typename T>
concept SupportsFieldNames =
std::is_class_v<T> && std::is_default_constructible_v<T> && !std::is_array_v<T> // Minimum requirement
std::is_class_v<T> && !std::is_array_v<T> // Minimum requirement
&& std::is_destructible_v<T> // Type T must have a constexpr destructor. But `std::is_literal_type` is deprecated,
&& !::mbo::types::HasUnionMember<T>;
&& !::mbo::types::HasUnionMember<T>; // Unions are problematic as they do not use the same fields (and thus names).

template<typename T>
concept SupportsFieldNamesConstexpr = // Constexpr capability is more restrictive.
SupportsFieldNames<T> // All general requirements, plus:
&& __is_literal_type(T); // so the internal version has to be used.
concept SupportsFieldNamesConstexpr = // Constexpr capability is more restrictive.
SupportsFieldNames<T> // All general requirements, plus:
&& std::is_default_constructible_v<T> // Warning will use actual uninitialized memory when retrieving field names.
&& __is_literal_type(T); // so the internal version has to be used.

template<typename T, bool, bool>
class StructMeta;
Expand All @@ -61,13 +62,23 @@ class StructMetaBase {
Uninitialized& operator=(const Uninitialized&) = delete;
Uninitialized(Uninitialized&&) = delete;
Uninitialized& operator=(Uninitialized&&) = delete;

unsigned temp{};
T value;
};

public:
constexpr Storage() noexcept { std::construct_at(&storage_.value); }
constexpr Storage() noexcept {
if constexpr (std::is_default_constructible_v<T>) {
std::construct_at(&storage_.value);
}
}

constexpr ~Storage() noexcept { std::destroy_at(&storage_.value); }
constexpr ~Storage() noexcept {
if constexpr (std::is_default_constructible_v<T>) {
std::destroy_at(&storage_.value);
}
}

Storage(const Storage&) = delete;
Storage& operator=(const Storage&) = delete;
Expand Down
24 changes: 22 additions & 2 deletions mbo/types/internal/struct_names_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,17 @@
#include <type_traits>
#include <utility>

#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "mbo/types/tstring.h"

namespace mbo::types::types_internal {
namespace {

using ::testing::ElementsAre;

struct StructNamesTest : ::testing::Test {};

template<typename T, std::size_t N, std::array<const char*, N> Names, typename Indices>
struct GetFieldNamesAreImpl;

Expand Down Expand Up @@ -58,11 +64,25 @@ struct Two {
static_assert(SupportsFieldNames<Two>);
static_assert(TestGetFieldNames<Two, "first"_ts, "second"_ts>);

struct DefaultConstructorDeleted {
DefaultConstructorDeleted() = delete;
};

static_assert(!std::is_default_constructible_v<DefaultConstructorDeleted>);
static_assert(SupportsFieldNames<DefaultConstructorDeleted>);

struct NoDefaultConstructor {
NoDefaultConstructor() = delete;
int& ref;
int val;
};

static_assert(!SupportsFieldNames<NoDefaultConstructor>);
static_assert(!std::is_default_constructible_v<NoDefaultConstructor>);
static_assert(SupportsFieldNames<NoDefaultConstructor>);

TEST_F(StructNamesTest, StructWithoutDefaultConstructor) {
// This cannot be done at compile time.
EXPECT_THAT(GetFieldNames<NoDefaultConstructor>(), ElementsAre("ref", "val"));
}

struct NoDestructor { // NOLINT(*-special-member-functions)
constexpr NoDestructor() = default;
Expand Down
Loading