镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content
This repository was archived by the owner on Jan 29, 2026. It is now read-only.
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ Note: If you prefer the library to be consumed as a (C++20) module, refer to [C+

### Hello World (Stream Version)

In the previous "Hello Word" example, we demonstrated how `proxy` could manage different types of objects and be formatted with `std::format`. While `std::format` is not the only option to print objects in C++, can we simply make `proxy` work with `std::cout`? The answer is "yes". The previous example is equivalent to the following implementation ([run](https://godbolt.org/z/q1b14WGff)):
In the previous "Hello World" example, we demonstrated how `proxy` could manage different types of objects and be formatted with `std::format`. While `std::format` is not the only option to print objects in C++, can we simply make `proxy` work with `std::cout`? The answer is "yes". The previous example is equivalent to the following implementation ([run](https://godbolt.org/z/q1b14WGff)):

```cpp
#include <iomanip>
Expand Down
3 changes: 3 additions & 0 deletions cmake/msft_proxy4ModuleTargets.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@ target_sources(msft_proxy4_module PUBLIC
${msft_proxy4_INCLUDE_DIR}/proxy/v4/proxy.ixx
)
target_compile_features(msft_proxy4_module PUBLIC cxx_std_20)
target_compile_options(msft_proxy4_module PRIVATE
$<$<CXX_COMPILER_ID:MSVC>:/utf-8>
)
target_link_libraries(msft_proxy4_module PUBLIC msft_proxy4::proxy)
117 changes: 75 additions & 42 deletions include/proxy/v4/proxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ consteval bool diagnose_proxiable_required_convention_not_implemented() {

template <class F, bool IsDirect, class D, class O>
struct invocation_meta {
constexpr invocation_meta() noexcept : dispatcher(nullptr) {}
constexpr invocation_meta() = default;
Comment thread
mingxwa marked this conversation as resolved.
Resolved
template <class P>
constexpr explicit invocation_meta(std::in_place_type_t<P>) noexcept
: dispatcher(
Expand Down Expand Up @@ -795,7 +795,7 @@ using ptr_prototype = void* [2];

template <class M>
struct meta_ptr_indirect_impl {
constexpr meta_ptr_indirect_impl() noexcept : ptr_(nullptr) {};
constexpr meta_ptr_indirect_impl() = default;
template <class P>
constexpr explicit meta_ptr_indirect_impl(std::in_place_type_t<P>) noexcept
: ptr_(&storage<P>) {}
Expand Down Expand Up @@ -948,7 +948,7 @@ class proxy : public details::facade_traits<F>::direct_accessor,
public:
using facade_type = F;

proxy() noexcept { ___PRO4_DEBUG(std::ignore = &_symbol_guard;) }
proxy() noexcept { initialize(); }
proxy(std::nullptr_t) noexcept : proxy() {}
proxy(const proxy&) noexcept
requires(F::constraints.copyability == constraint_level::trivial)
Expand All @@ -957,47 +957,31 @@ class proxy : public details::facade_traits<F>::direct_accessor,
constraint_level::nothrow)
requires(F::constraints.copyability == constraint_level::nontrivial ||
F::constraints.copyability == constraint_level::nothrow)
: details::inplace_ptr<proxy_indirect_accessor<F>>() { // Make GCC happy
if (rhs.meta_.has_value()) {
proxy_invoke<true, details::copy_dispatch,
void(proxy&) const noexcept(F::constraints.copyability ==
constraint_level::nothrow)>(
rhs, *this);
}
: details::inplace_ptr<
proxy_indirect_accessor<F>>() /* Make GCC happy */ {
initialize(rhs);
}
proxy(proxy&& rhs) noexcept(F::constraints.relocatability ==
constraint_level::nothrow)
requires(F::constraints.relocatability >= constraint_level::nontrivial &&
F::constraints.copyability != constraint_level::trivial)
{
if (rhs.meta_.has_value()) {
if constexpr (F::constraints.relocatability ==
constraint_level::trivial) {
std::ranges::uninitialized_copy(rhs.ptr_, ptr_);
meta_ = rhs.meta_;
rhs.meta_.reset();
} else {
proxy_invoke<true, details::copy_dispatch,
void(proxy&) && noexcept(F::constraints.relocatability ==
constraint_level::nothrow)>(
std::move(rhs), *this);
}
}
initialize(std::move(rhs));
}
template <class P>
constexpr proxy(P&& ptr) noexcept(
std::is_nothrow_constructible_v<std::decay_t<P>, P>)
requires(details::ptr_traits<std::decay_t<P>>::applicable &&
std::is_constructible_v<std::decay_t<P>, P>)
: proxy() {
{
initialize<std::decay_t<P>>(std::forward<P>(ptr));
}
template <class P, class... Args>
constexpr explicit proxy(std::in_place_type_t<P>, Args&&... args) noexcept(
std::is_nothrow_constructible_v<P, Args...>)
requires(details::ptr_traits<P>::applicable &&
std::is_constructible_v<P, Args...>)
: proxy() {
{
initialize<P>(std::forward<Args>(args)...);
}
template <class P, class U, class... Args>
Expand All @@ -1008,7 +992,7 @@ class proxy : public details::facade_traits<F>::direct_accessor,
P, std::initializer_list<U>&, Args...>)
requires(details::ptr_traits<P>::applicable &&
std::is_constructible_v<P, std::initializer_list<U>&, Args...>)
: proxy() {
{
initialize<P>(il, std::forward<Args>(args)...);
}
proxy& operator=(std::nullptr_t) noexcept(F::constraints.destructibility >=
Expand All @@ -1031,8 +1015,8 @@ class proxy : public details::facade_traits<F>::direct_accessor,
{
if (this != std::addressof(rhs)) [[likely]] {
if constexpr (F::constraints.copyability == constraint_level::nothrow) {
std::destroy_at(this);
std::construct_at(this, rhs);
destroy();
initialize(rhs);
} else {
*this = proxy{rhs};
}
Expand All @@ -1049,7 +1033,7 @@ class proxy : public details::facade_traits<F>::direct_accessor,
{
if (this != std::addressof(rhs)) [[likely]] {
reset();
std::construct_at(this, std::move(rhs));
initialize(std::move(rhs));
}
return *this;
}
Expand All @@ -1062,7 +1046,7 @@ class proxy : public details::facade_traits<F>::direct_accessor,
F::constraints.destructibility >= constraint_level::nontrivial)
{
if constexpr (std::is_nothrow_constructible_v<std::decay_t<P>, P>) {
std::destroy_at(this);
destroy();
initialize<std::decay_t<P>>(std::forward<P>(ptr));
} else {
*this = proxy{std::forward<P>(ptr)};
Expand All @@ -1076,11 +1060,7 @@ class proxy : public details::facade_traits<F>::direct_accessor,
requires(F::constraints.destructibility == constraint_level::nontrivial ||
F::constraints.destructibility == constraint_level::nothrow)
{
if (meta_.has_value()) {
proxy_invoke<true, details::destroy_dispatch,
void() noexcept(F::constraints.destructibility ==
constraint_level::nothrow)>(*this);
}
destroy();
}

bool has_value() const noexcept { return meta_.has_value(); }
Expand All @@ -1089,8 +1069,8 @@ class proxy : public details::facade_traits<F>::direct_accessor,
constraint_level::nothrow)
requires(F::constraints.destructibility >= constraint_level::nontrivial)
{
std::destroy_at(this);
meta_.reset();
destroy();
initialize();
}
void swap(proxy& rhs) noexcept(F::constraints.relocatability >=
constraint_level::nothrow ||
Expand All @@ -1107,13 +1087,13 @@ class proxy : public details::facade_traits<F>::direct_accessor,
if (meta_.has_value()) {
if (rhs.meta_.has_value()) {
proxy temp = std::move(*this);
std::construct_at(this, std::move(rhs));
std::construct_at(std::addressof(rhs), std::move(temp));
initialize(std::move(rhs));
rhs.initialize(std::move(temp));
} else {
std::construct_at(std::addressof(rhs), std::move(*this));
rhs.initialize(std::move(*this));
}
} else if (rhs.meta_.has_value()) {
std::construct_at(this, std::move(rhs));
initialize(std::move(rhs));
}
}
}
Expand Down Expand Up @@ -1148,8 +1128,51 @@ class proxy : public details::facade_traits<F>::direct_accessor,
}

private:
void initialize() {
___PRO4_DEBUG(std::ignore = &_symbol_guard;)
meta_.reset();
}
void initialize(const proxy& rhs)
requires(F::constraints.copyability != constraint_level::none)
{
___PRO4_DEBUG(std::ignore = &_symbol_guard;)
if (rhs.meta_.has_value()) {
if constexpr (F::constraints.copyability == constraint_level::trivial) {
std::ranges::uninitialized_copy(rhs.ptr_, ptr_);
meta_ = rhs.meta_;
} else {
proxy_invoke<true, details::copy_dispatch,
void(proxy&) const noexcept(F::constraints.copyability ==
constraint_level::nothrow)>(
rhs, *this);
}
} else {
meta_.reset();
}
}
void initialize(proxy&& rhs)
requires(F::constraints.relocatability != constraint_level::none)
{
___PRO4_DEBUG(std::ignore = &_symbol_guard;)
if (rhs.meta_.has_value()) {
if constexpr (F::constraints.relocatability ==
constraint_level::trivial) {
std::ranges::uninitialized_copy(rhs.ptr_, ptr_);
meta_ = rhs.meta_;
rhs.meta_.reset();
} else {
proxy_invoke<true, details::copy_dispatch,
void(proxy&) && noexcept(F::constraints.relocatability ==
constraint_level::nothrow)>(
std::move(rhs), *this);
}
} else {
meta_.reset();
}
}
template <class P, class... Args>
constexpr P& initialize(Args&&... args) {
___PRO4_DEBUG(std::ignore = &_symbol_guard;)
P& result = *std::construct_at(reinterpret_cast<P*>(ptr_),
std::forward<Args>(args)...);
if constexpr (proxiable<P, F>) {
Expand All @@ -1159,7 +1182,17 @@ class proxy : public details::facade_traits<F>::direct_accessor,
}
return result;
}

void destroy()
requires(F::constraints.destructibility != constraint_level::none)
{
if constexpr (F::constraints.destructibility != constraint_level::trivial) {
if (meta_.has_value()) {
proxy_invoke<true, details::destroy_dispatch,
void() noexcept(F::constraints.destructibility ==
constraint_level::nothrow)>(*this);
}
}
}
___PRO4_DEBUG(static inline void _symbol_guard(proxy& self,
const proxy& cself) noexcept {
self.operator->();
Expand Down