首次提交

This commit is contained in:
2026-08-07 16:21:44 +08:00
parent 396311246c
commit 1e903dfe1e
33 changed files with 5960 additions and 1 deletions
@@ -0,0 +1,125 @@
#pragma once
#include <concepts>
#include <functional>
#include <type_traits>
#include <utility>
namespace structive {
template <class>
struct Member_Pointer_Traits;
template <class Object, class Value>
struct Member_Pointer_Traits<Value Object::*> {
using object_type = Object;
using value_type = Value;
};
template <class>
struct Member_Function_Traits;
template <class Object, class Return, class... Args>
struct Member_Function_Traits<Return (Object::*)(Args...)> {
using object_type = Object;
using return_type = Return;
};
template <class Object, class Return, class... Args>
struct Member_Function_Traits<Return (Object::*)(Args...) const> {
using object_type = Object;
using return_type = Return;
};
template <class Object, class Return, class... Args>
struct Member_Function_Traits<Return (Object::*)(Args...) noexcept> : Member_Function_Traits<Return (Object::*)(Args...)> {};
template <class Object, class Return, class... Args>
struct Member_Function_Traits<Return (Object::*)(Args...) const noexcept> : Member_Function_Traits<Return (Object::*)(Args...) const> {};
template <auto Member>
struct Member_Storage_Identity {};
template <auto Getter>
concept Trusted_Getter_Function = requires {
typename Member_Function_Traits<decltype(Getter)>::object_type;
} && requires(const typename Member_Function_Traits<decltype(Getter)>::object_type& object) {
std::invoke(Getter, object);
} && (!std::is_void_v<std::invoke_result_t<decltype(Getter), const typename Member_Function_Traits<decltype(Getter)>::object_type&>>) && (!std::is_rvalue_reference_v<std::invoke_result_t<decltype(Getter), const typename Member_Function_Traits<decltype(Getter)>::object_type&>>) && (!(std::is_lvalue_reference_v<std::invoke_result_t<decltype(Getter), const typename Member_Function_Traits<decltype(Getter)>::object_type&>> && !std::is_const_v<std::remove_reference_t<std::invoke_result_t<decltype(Getter), const typename Member_Function_Traits<decltype(Getter)>::object_type&>>>));
template <auto Getter>
using trusted_getter_object_t = typename Member_Function_Traits<decltype(Getter)>::object_type;
template <auto Getter>
using trusted_getter_value_t = std::remove_cvref_t<std::invoke_result_t<decltype(Getter), const trusted_getter_object_t<Getter>&>>;
template <auto Setter, class Object, class Value>
concept Trusted_Setter_Function_For = requires {
typename Member_Function_Traits<decltype(Setter)>::object_type;
} && std::same_as<typename Member_Function_Traits<decltype(Setter)>::object_type, Object> && requires(Object& object, Value value) {
{ std::invoke(Setter, object, std::move(value)) } -> std::same_as<void>;
};
template <auto Member>
struct Member_Accessor {
using traits = Member_Pointer_Traits<decltype(Member)>;
using object_type = typename traits::object_type;
using value_type = typename traits::value_type;
using storage_identity = Member_Storage_Identity<Member>;
static constexpr bool readable = true;
static constexpr bool writable = !std::is_const_v<value_type>;
static constexpr bool synchronized_view_read = false;
static constexpr bool trusted_object_access = false;
static constexpr auto member = Member;
constexpr const value_type& read(const object_type& object) const {
return object.*Member;
}
constexpr value_type& read(object_type& object) const {
return object.*Member;
}
template <class Value>
constexpr void write(object_type& object, Value&& value) const requires writable && std::assignable_from<value_type&, Value> {
object.*Member = std::forward<Value>(value);
}
};
template <class Object, class Value, class Function>
struct Synchronized_Computed_Accessor {
using object_type = Object;
using value_type = Value;
using storage_identity = void;
static constexpr bool readable = true;
static constexpr bool writable = false;
static constexpr bool synchronized_view_read = true;
static constexpr bool trusted_object_access = false;
[[no_unique_address]] Function function;
template <class View>
constexpr value_type read(const View& view) const requires std::invocable<const Function&, const View&> && std::constructible_from<value_type, std::invoke_result_t<const Function&, const View&>> {
return value_type(std::invoke(function, view));
}
};
template <auto Getter> requires Trusted_Getter_Function<Getter>
struct Trusted_Computed_Accessor {
using object_type = trusted_getter_object_t<Getter>;
using value_type = trusted_getter_value_t<Getter>;
using storage_identity = void;
static constexpr bool readable = true;
static constexpr bool writable = false;
static constexpr bool synchronized_view_read = false;
static constexpr bool trusted_object_access = true;
constexpr decltype(auto) read(const object_type& object) const noexcept(std::is_nothrow_invocable_v<decltype(Getter), const object_type&>) {
return std::invoke(Getter, object);
}
};
template <auto Getter, auto Setter> requires Trusted_Getter_Function<Getter> && Trusted_Setter_Function_For<Setter, trusted_getter_object_t<Getter>, trusted_getter_value_t<Getter>>
struct Trusted_Getter_Setter_Accessor {
using object_type = trusted_getter_object_t<Getter>;
using value_type = trusted_getter_value_t<Getter>;
using storage_identity = void;
static constexpr bool readable = true;
static constexpr bool writable = true;
static constexpr bool synchronized_view_read = false;
static constexpr bool trusted_object_access = true;
constexpr decltype(auto) read(const object_type& object) const noexcept(std::is_nothrow_invocable_v<decltype(Getter), const object_type&>) {
return std::invoke(Getter, object);
}
template <class Value>
constexpr void write(object_type& object, Value&& value) const requires std::invocable<decltype(Setter), object_type&, Value> && std::same_as<std::invoke_result_t<decltype(Setter), object_type&, Value>, void> {
std::invoke(Setter, object, std::forward<Value>(value));
}
};
template <class Accessor>
concept Property_Accessor = requires {
typename Accessor::object_type;
typename Accessor::value_type;
typename Accessor::storage_identity;
{ Accessor::readable } -> std::convertible_to<bool>;
{ Accessor::writable } -> std::convertible_to<bool>;
{ Accessor::synchronized_view_read } -> std::convertible_to<bool>;
{ Accessor::trusted_object_access } -> std::convertible_to<bool>;
};
}