Skip to content
Draft
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
1 change: 1 addition & 0 deletions docs/spec/.pages
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ nav:
- allocate_proxy_shared: allocate_proxy_shared.md
- allocate_proxy: allocate_proxy.md
- make_proxy_inplace: make_proxy_inplace.md
- make_proxy_observed: make_proxy_observed.md
- make_proxy_shared: make_proxy_shared.md
- make_proxy_view: make_proxy_view.md
- make_proxy: make_proxy.md
Expand Down
1 change: 1 addition & 0 deletions docs/spec/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ This document provides the API specifications for the C++ library Proxy (version
| [`allocate_proxy_shared`](allocate_proxy_shared.md) | Creates a `proxy` object with shared ownership using an allocator |
| [`allocate_proxy`](allocate_proxy.md) | Creates a `proxy` object with an allocator |
| [`make_proxy_inplace`](make_proxy_inplace.md) | Creates a `proxy` object with strong no-allocation guarantee |
| [`make_proxy_observed`](make_proxy_observed.md) | Creates a `proxy` object with no ownership |
| [`make_proxy_shared`](make_proxy_shared.md) | Creates a `proxy` object with shared ownership |
| [`make_proxy_view`](make_proxy_view.md) | Creates a `proxy_view` object |
| [`make_proxy`](make_proxy.md) | Creates a `proxy` object potentially with heap allocation |
Expand Down
50 changes: 50 additions & 0 deletions docs/spec/make_proxy_observed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Function template `make_proxy_observed`

> Header: `proxy.h`
> Module: `proxy`
> Namespace: `pro::inline v4`
> Since: 4.1.0

The definition of `make_proxy_observed` makes use of an exposition-only class template *observer-ptr*. `observer-ptr<T>` contains a raw pointer to an object of type `T`, and provides `operator*` for access with the same qualifiers.

```cpp
template <facade F, class T>
proxy<F> make_proxy_observed(T& value) noexcept;
```

Creates a `proxy<F>` object containing a value `p` of type `observer-ptr<T>`, where `*p` is direct-non-list-initialized with `std::addressof(value)`. If [`proxiable_target<T, F>`](proxiable_target.md) is `false`, the program is ill-formed and diagnostic messages are generated.

## Return Value

The constructed `proxy` object.

## Example

```cpp
#include <iostream>

#include <proxy/proxy.h>

struct Printable : pro::facade_builder //
::add_convention<pro::operator_dispatch<"<<", true>,
std::ostream&(std::ostream&) const> //
::build {};

int main() {
int val = 123;
pro::proxy<Printable> p = pro::make_proxy_observed<Printable>(val);

// Prints "123"
std::cout << *p << "\n";

val = 456;

// Prints "456"
std::cout << *p << "\n";
}
```

## See Also

- [concept `proxiable_target`](proxiable_target.md)
- [function template `make_proxy_view`](make_proxy_view.md)
4 changes: 1 addition & 3 deletions docs/spec/make_proxy_view.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@
> Namespace: `pro::inline v4`
> Since: 3.3.0

The definition of `make_proxy_view` makes use of an exposition-only class template *observer-ptr*. `observer-ptr<T>` contains a raw pointer to an object of type `T`, and provides `operator*` for access with the same qualifiers.

```cpp
template <facade F, class T>
proxy_view<F> make_proxy_view(T& value) noexcept;
```

Creates a `proxy_view<F>` object containing a value `p` of type `observer-ptr<T>`, where `*p` is direct-non-list-initialized with `&value`. If [`proxiable_target<T, F>`](proxiable_target.md) is `false`, the program is ill-formed and diagnostic messages are generated.
Equivalent to `return make_proxy_observed<observer_facade<F>>(value)`.

## Return Value

Expand Down
4 changes: 2 additions & 2 deletions docs/spec/proxiable_target.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

```cpp
template <class T, class F>
concept proxiable_target = proxiable<observer-ptr<T>, F>;
concept proxiable_target = proxiable<observer-ptr<T>, observer_facade<F>>;
```

See [`make_proxy_view`](make_proxy_view.md) for the definition of the exposition-only class template *observer-ptr*.
See [`make_proxy_observed`](make_proxy_observed.md) for the definition of the exposition-only class template *observer-ptr*.

## Example

Expand Down
2 changes: 1 addition & 1 deletion docs/spec/skills_as_view.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ using as_view = /* see below */;

The alias template `as_view` modifies a specialization of [`basic_facade_builder`](basic_facade_builder/README.md) to allow implicit conversion from [`proxy`](proxy/README.md)`<F>` to [`proxy_view`](proxy_view.md)`<F>`, where `F` is a built [facade](facade.md) type.

Let `p` be a value of type `proxy<F>`, `ptr` be the contained value of `p` (if any), the conversion from type `proxy<F>&` to type `proxy_view<F>` is equivalent to `return observer-ptr{std::addressof(*ptr)}` if `p` contains a value, or otherwise equivalent to `return nullptr`. `observer-ptr` is an exposition-only type that `*observer-ptr`, `*std::as_const(observer-ptr)`, `*std::move(observer-ptr)` and `*std::move(std::as_const(observer-ptr))` are equivalent to `*ptr`, `*std::as_const(ptr)`, `*std::move(ptr)` and `*std::move(std::as_const(ptr))`, respectively.
Let `p` be a value of type `proxy<F>`, `ptr` be the contained value of `p` (if any), the conversion from type `proxy<F>&` to type `proxy_view<F>` is equivalent to `return make_proxy_view<F>(*ptr)` if `p` contains a value, or otherwise equivalent to `return nullptr`.

## Notes

Expand Down
8 changes: 6 additions & 2 deletions include/proxy/v4/proxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -1818,10 +1818,14 @@ constexpr proxy<F> make_proxy_inplace(T&& value) noexcept(
std::in_place, std::forward<T>(value)};
}

template <facade F, class T>
constexpr proxy<F> make_proxy_observed(T& value) noexcept {
return proxy<F>{details::observer_ptr<T&, const T&, T&&, const T&&>{value}};
}

template <facade F, class T>
constexpr proxy_view<F> make_proxy_view(T& value) noexcept {
return proxy_view<F>{
details::observer_ptr<T&, const T&, T&&, const T&&>{value}};
return make_proxy_observed<observer_facade<F>>(value);
}

#if __STDC_HOSTED__
Expand Down
1 change: 1 addition & 0 deletions include/proxy/v4/proxy.ixx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ using v4::is_bitwise_trivially_relocatable;
using v4::is_bitwise_trivially_relocatable_v;
using v4::make_proxy;
using v4::make_proxy_inplace;
using v4::make_proxy_observed;
using v4::make_proxy_shared;
using v4::make_proxy_view;
using v4::not_implemented;
Expand Down
26 changes: 26 additions & 0 deletions tests/proxy_creation_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ struct TestWeakSharedStringable : pro::facade_builder //

static_assert(pro::proxiable<int*, TestSharedStringable>);
static_assert(!pro::proxiable<int*, TestWeakSharedStringable>);
static_assert(pro::proxiable_target<int, TestWeakSharedStringable>);

} // namespace proxy_creation_tests_details

Expand Down Expand Up @@ -1222,3 +1223,28 @@ TEST(ProxyCreationTests, TestMakeProxyView) {
p = pro::make_proxy_view<TestFacade>(test_callable);
ASSERT_EQ((*std::move(std::as_const(p)))(), 3);
}

TEST(ProxyCreationTests, TestMakeProxyObserved) {
struct TestFacade
: pro::facade_builder //
::add_convention<pro::operator_dispatch<"()">, int() &, int() const&,
int() && noexcept, int() const&&> //
::build {};

struct {
int operator()() & noexcept { return 0; }
int operator()() const& noexcept { return 1; }
int operator()() && noexcept { return 2; }
int operator()() const&& noexcept { return 3; }
} test_callable;

pro::proxy<TestFacade> p =
pro::make_proxy_observed<TestFacade>(test_callable);
static_assert(!noexcept((*p)()));
static_assert(noexcept((*std::move(p))()));
ASSERT_EQ((*p)(), 0);
ASSERT_EQ((*std::as_const(p))(), 1);
ASSERT_EQ((*std::move(p))(), 2);
p = pro::make_proxy_observed<TestFacade>(test_callable);
ASSERT_EQ((*std::move(std::as_const(p)))(), 3);
}
Loading