Skip to content

Commit

Permalink
Fix readme and refactor sample code (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
mingxwa authored May 9, 2024
1 parent dc44c4a commit 51af4a8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 19 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ class Rectangle {
std::string PrintDrawableToString(pro::proxy<spec::Drawable> 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();
}

Expand All @@ -62,7 +62,7 @@ pro::proxy<spec::Drawable> 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
Expand All @@ -75,11 +75,11 @@ PRO_DEF_FACADE(Logger, Log);
// Client - Consumer
void MyVerboseFunction(pro::proxy<spec::Logger> 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);
}
}
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
18 changes: 6 additions & 12 deletions tests/proxy_integration_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -88,18 +89,11 @@ pro::proxy<spec::Drawable> 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<Rectangle>(ptr); };
Rectangle* instance = alloc.new_object<Rectangle>();
std::unique_ptr<Rectangle, decltype(deleter)> p{instance, deleter};
p->SetWidth(std::stod(parsed[1u]));
p->SetHeight(std::stod(parsed[2u]));
return p;
return pro::allocate_proxy<spec::Drawable, Rectangle>(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<spec::Drawable>(circle);
}
} else if (parsed[0u] == "Point") {
Expand Down

0 comments on commit 51af4a8

Please sign in to comment.