diff --git a/README.md b/README.md index 43233b3..0c71285 100644 --- a/README.md +++ b/README.md @@ -48,8 +48,8 @@ class Rectangle { std::string PrintDrawableToString(pro::proxy p) { std::stringstream result; result << "shape = "; - p.Draw(result); - result << ", area = " << p.Area(); + p.Draw(result); // Polymorphic call + result << ", area = " << p.Area(); // Polymorphic call return std::move(result).str(); } @@ -62,7 +62,7 @@ pro::proxy CreateRectangleAsDrawable(int width, int height) { } ``` -Here is another demo showing how to define overloads in a dispatch. Note that `.invoke<>` can be ommitted when only 1 dispatch is defined in a facade: +Here is another demo showing how to define overloads in a dispatch: just to add any number of signatures in the definition of a dispatch. ```cpp // Specifications of abstraction @@ -75,11 +75,11 @@ PRO_DEF_FACADE(Logger, Log); // Client - Consumer void MyVerboseFunction(pro::proxy logger) { - logger("hello"); + logger.Log("hello"); try { throw std::runtime_error{"runtime error!"}; } catch (const std::exception& e) { - logger("world", e); + logger.Log("world", e); } } @@ -101,7 +101,7 @@ 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. +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. Note that `.Call` can be omitted when only 1 dispatch is defined in a facade: ```cpp // Specifications of abstraction @@ -139,7 +139,7 @@ int main() { } ``` -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/voEacxT76)). +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/4cK8PPTE1)). ## Minimum requirements for compilers diff --git a/tests/proxy_integration_tests.cpp b/tests/proxy_integration_tests.cpp index b289005..83fe950 100644 --- a/tests/proxy_integration_tests.cpp +++ b/tests/proxy_integration_tests.cpp @@ -23,10 +23,10 @@ PRO_DEF_FACADE(Drawable, PRO_MAKE_DISPATCH_PACK(Draw, Area)); class Rectangle { public: + explicit Rectangle(double width, double height) : width_(width), height_(height) {} + Rectangle(const Rectangle&) = default; void Draw(std::ostream& out) const { out << "{Rectangle: width = " << width_ << ", height = " << height_ << "}"; } - void SetWidth(double width) { width_ = width; } - void SetHeight(double height) { height_ = height; } double Area() const noexcept { return width_ * height_; } private: @@ -36,8 +36,9 @@ class Rectangle { class Circle { public: + explicit Circle(double radius) : radius_(radius) {} + Circle(const Circle&) = default; void Draw(std::ostream& out) const { out << "{Circle: radius = " << radius_ << "}"; } - void SetRadius(double radius) { radius_ = radius; } double Area() const noexcept { return std::numbers::pi * radius_ * radius_; } private: @@ -88,18 +89,11 @@ pro::proxy MakeDrawableFromCommand(const std::string& s) { if (parsed.size() == 3u) { static std::pmr::unsynchronized_pool_resource rectangle_memory_pool; std::pmr::polymorphic_allocator<> alloc{&rectangle_memory_pool}; - auto deleter = [alloc](Rectangle* ptr) mutable - { alloc.delete_object(ptr); }; - Rectangle* instance = alloc.new_object(); - std::unique_ptr p{instance, deleter}; - p->SetWidth(std::stod(parsed[1u])); - p->SetHeight(std::stod(parsed[2u])); - return p; + return pro::allocate_proxy(alloc, std::stod(parsed[1u]), std::stod(parsed[2u])); } } else if (parsed[0u] == "Circle") { if (parsed.size() == 2u) { - Circle circle; - circle.SetRadius(std::stod(parsed[1u])); + Circle circle{std::stod(parsed[1u])}; return pro::make_proxy(circle); } } else if (parsed[0u] == "Point") {