From 2afb3344846a3d419dff293bb518e2f1a88b6f1b Mon Sep 17 00:00:00 2001 From: Anthony Wang Date: Wed, 28 Apr 2021 07:41:35 -0500 Subject: Rewrite using operator overloading --- channels/channels.cpp | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) (limited to 'channels') diff --git a/channels/channels.cpp b/channels/channels.cpp index f1964db..8d4b6cd 100644 --- a/channels/channels.cpp +++ b/channels/channels.cpp @@ -6,23 +6,24 @@ class channel { private: std::any val; public: - void write(auto v) { + void operator<<(auto & v) { val = v; } - auto read() { + void operator>>(auto & v) { while (!val.has_value()); - auto ret = val; + v = std::any_cast(val); val.reset(); - return ret; } }; int main() { channel c; int x = 1; - c.write(x); - std::cout << std::any_cast(c.read()) << std::endl; + c << x; std::string y = "Hello world"; - c.write(y); - std::cout << std::any_cast(c.read()) << std::endl; + c >> x; + c << y; + std::cout << x << std::endl; + c >> y; + std::cout << y << std::endl; } -- cgit v1.2.3-70-g09d2