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

Revise the semantics of facade #48

Merged
merged 6 commits into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
47 changes: 42 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Proxy: Polymorphism in C++ Redefined
# Proxy: Next Generation Polymorphism in C++

[![Proxy-CI](https://github.com/microsoft/proxy/actions/workflows/pipeline-ci.yml/badge.svg)](https://github.com/microsoft/proxy/actions/workflows/pipeline-ci.yml)

Expand All @@ -24,8 +24,8 @@ The majority of the library is defined in namespace `pro`. Some macros are provi
// Abstraction (poly is short for polymorphism)
namespace poly {

PRO_DEF_MEMBER_DISPATCH(Draw, Draw, void(std::ostream&));
PRO_DEF_MEMBER_DISPATCH(Area, Area, double());
PRO_DEF_MEMBER_DISPATCH(Draw, void(std::ostream&));
PRO_DEF_MEMBER_DISPATCH(Area, double());
PRO_DEF_FACADE(Drawable, PRO_MAKE_DISPATCH_PACK(Draw, Area));

} // namespace poly
Expand Down Expand Up @@ -68,8 +68,7 @@ Here is another demo showing how to define overloads in a dispatch. Note that `.
// Abstraction (poly is short for polymorphism)
namespace poly {

PRO_DEF_MEMBER_DISPATCH(Log, Log,
void(const char*), void(const char*, const std::exception&));
PRO_DEF_MEMBER_DISPATCH(Log, void(const char*), void(const char*, const std::exception&));
PRO_DEF_FACADE(Logger, Log);

} // namespace poly
Expand Down Expand Up @@ -102,6 +101,44 @@ int main() {
}
```

By design, the body of a dispatch could be any code. While member function is one useful pattern supported by macro `PRO_DEF_MEMBER_DISPATCH`, free function is also supported with another macro `PRO_DEF_FREE_DISPATCH`. The following example uses `PRO_DEF_FREE_DISPATCH` and `std::invoke` to implement similar function wrapper as `std::function` and `std::move_only_function` and supports multiple overloads.

```cpp
// Abstraction (poly is short for polymorphism)
namespace poly {

template <class... Overloads>
PRO_DEF_FREE_DISPATCH(Call, std::invoke, Overloads...);
template <class... Overloads>
PRO_DEF_FACADE(MovableCallable, Call<Overloads...>);
template <class... Overloads>
PRO_DEF_FACADE(CopyableCallable, Call<Overloads...>, pro::copyable_pointer_constraints);

} // namespace poly

// MyFunction has similar functionality as std::function but supports multiple overloads
// MyMoveOnlyFunction has similar functionality as std::move_only_function but supports multiple overloads
template <class... Overloads>
using MyFunction = pro::proxy<poly::MovableCallable<Overloads...>>;
template <class... Overloads>
using MyMoveOnlyFunction = pro::proxy<poly::CopyableCallable<Overloads...>>;

int main() {
auto f = [](auto&&... v) {
printf("f() called. Args: ");
((std::cout << v << ":" << typeid(decltype(v)).name() << ", "), ...);
puts("");
mingxwa marked this conversation as resolved.
Show resolved Hide resolved
};
MyFunction<void(int)> p0{&f};
p0(123); // Prints "f() called. Args: 123:i," (assuming GCC)
MyMoveOnlyFunction<void(), void(int), void(double)> p1{&f};
p1(); // Prints "f() called. Args:"
p1(456); // Prints "f() called. Args: 456:i,"
p1(1.2); // Prints "f() called. Args: 1.2:d,"
return 0;
}
```

Please find more details and discussions in the spec. The complete version of the "drawable" demo could be found in [tests/proxy_integration_tests.cpp](tests/proxy_integration_tests.cpp) (also available on [Compiler Explorer](https://godbolt.org/z/5a3jeE1M8)).

## Minimum requirements for compilers
Expand Down
6 changes: 3 additions & 3 deletions proxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -601,15 +601,15 @@ struct facade_prototype {

} // namespace pro

#define PRO_DEF_MEMBER_DISPATCH(__NAME, __FUNC, ...) \
#define PRO_DEF_MEMBER_DISPATCH(__NAME, ...) \
struct __NAME { \
using overload_types = std::tuple<__VA_ARGS__>;\
template <class __T, class... __Args> \
decltype(auto) operator()(__T&& __self, __Args&&... __args) \
requires(requires{ std::forward<__T>(__self) \
.__FUNC(std::forward<__Args>(__args)...); }) { \
.__NAME(std::forward<__Args>(__args)...); }) { \
return std::forward<__T>(__self) \
.__FUNC(std::forward<__Args>(__args)...); \
.__NAME(std::forward<__Args>(__args)...); \
} \
}
#define PRO_DEF_FREE_DISPATCH(__NAME, __FUNC, ...) \
Expand Down
4 changes: 2 additions & 2 deletions samples/resource_dictionary/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

namespace poly {

PRO_DEF_MEMBER_DISPATCH(At, at, std::string(int));
PRO_DEF_FACADE(Dictionary, At);
PRO_DEF_MEMBER_DISPATCH(at, std::string(int));
PRO_DEF_FACADE(Dictionary, at);

} // namespace poly

Expand Down
4 changes: 2 additions & 2 deletions tests/proxy_integration_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ namespace {

namespace poly {

PRO_DEF_MEMBER_DISPATCH(Draw, Draw, void(std::ostream&));
PRO_DEF_MEMBER_DISPATCH(Area, Area, double());
PRO_DEF_MEMBER_DISPATCH(Draw, void(std::ostream&));
PRO_DEF_MEMBER_DISPATCH(Area, double());
PRO_DEF_FACADE(Drawable, PRO_MAKE_DISPATCH_PACK(Draw, Area));

} // namespace poly
Expand Down
2 changes: 1 addition & 1 deletion tests/proxy_invocation_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace {
namespace poly {

template <class... Os>
PRO_DEF_MEMBER_DISPATCH(Call, operator(), Os...);
PRO_DEF_FREE_DISPATCH(Call, std::invoke, Os...);
template <class... Os>
PRO_DEF_FACADE(Callable, Call<Os...>, pro::copyable_pointer_constraints);

Expand Down