Skip to content

Commit

Permalink
Code optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
Nevermore1994 authored and rolf.tan committed Dec 7, 2023
1 parent 4e623e8 commit 8b81351
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 18 deletions.
24 changes: 14 additions & 10 deletions Channel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,13 +258,17 @@ class Sender final {
std::shared_ptr<Channel<T>> channel_;
};

constexpr std::string_view kClosedMessage = "can't send message to closed channel.";
namespace _detail {
auto closedHandler = []{
throw std::runtime_error("can't send message to closed channel.");
};
}

template <typename T, typename U>
auto operator<<(const SenderPtr<U>& sender, T&& message) -> const SenderPtr<U>&
requires std::movable<T> && std::is_convertible_v<T, U> {
if (!sender->send(std::forward<T>(message))) {
throw std::runtime_error(kClosedMessage.data());
_detail::closedHandler();
}
return sender;
}
Expand All @@ -273,7 +277,7 @@ template <typename T, typename U>
auto operator<<(const SenderPtr<U>& sender, const T& message) -> const SenderPtr<U>&
requires std::copyable<T> && std::is_convertible_v<T, U> {
if (!sender->send(message)) {
throw std::runtime_error(kClosedMessage.data());
_detail::closedHandler();
}
return sender;
}
Expand All @@ -282,7 +286,7 @@ template <typename I, typename U>
auto operator<<(const SenderPtr<U>& sender, const I& box) -> const SenderPtr<U>&
requires IsRange<I> && std::is_convertible_v<typename decltype(box.begin())::value_type, U> {
if (!sender->send(box)) {
throw std::runtime_error(kClosedMessage.data());
_detail::closedHandler();
}
return sender;
}
Expand All @@ -291,7 +295,7 @@ template <typename I, typename U>
auto operator<<(const SenderPtr<U>& sender, I&& box) -> const SenderPtr<U>&
requires IsRange<I> && std::is_convertible_v<typename decltype(box.begin())::value_type, U> {
if (!sender->send(std::forward<I>(box))) {
throw std::runtime_error(kClosedMessage.data());
_detail::closedHandler();
}
return sender;
}
Expand All @@ -300,7 +304,7 @@ template <typename T, typename U>
auto operator<<(const SenderRefPtr<U>& sender, T&& message) -> const SenderRefPtr<U>&
requires std::movable<T> && std::is_convertible_v<T, U> {
if (!sender->send(std::forward<T>(message))) {
throw std::runtime_error(kClosedMessage.data());
_detail::closedHandler();
}
return sender;
}
Expand All @@ -309,7 +313,7 @@ template <typename T, typename U>
auto operator<<(const SenderRefPtr<U>& sender, const T& message) -> const SenderRefPtr<U>&
requires std::copyable<U> && std::is_convertible_v<T, U> {
if (!sender->send(message)) {
throw std::runtime_error(kClosedMessage.data());
_detail::closedHandler();
}
return sender;
}
Expand All @@ -318,7 +322,7 @@ template <typename I, typename U>
auto operator<<(const SenderRefPtr<U>& sender, const I& box) -> const SenderRefPtr<U>&
requires IsRange<I> && std::is_convertible_v<typename decltype(box.begin())::value_type, U> {
if (!sender->send(box)) {
throw std::runtime_error(kClosedMessage.data());
_detail::closedHandler();
}
return sender;
}
Expand All @@ -327,7 +331,7 @@ template <typename I, typename U>
auto operator<<(const SenderRefPtr<U>& sender, I&& box) -> const SenderRefPtr<U>&
requires IsRange<I> && std::is_convertible_v<typename decltype(box.begin())::value_type, U> {
if (!sender->send(std::forward<I>(box))) {
throw std::runtime_error(kClosedMessage.data());
_detail::closedHandler();
}
return sender;
}
Expand Down Expand Up @@ -507,7 +511,7 @@ class ReceiverIterator {
}

template <typename = std::enable_if_t<std::is_copy_assignable_v<T>>>
ReceiverIterator& operator=(const ReceiverIterator& src) noexcept {
auto operator=(const ReceiverIterator& src) noexcept -> ReceiverIterator& {
receiverImpl_ = src.receiverImpl_;
value_ = src.value_;
return *this;
Expand Down
6 changes: 3 additions & 3 deletions example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ struct People {
People() = default;
};


int main() {
using type = std::unordered_map<int, People>;
using namespace std::chrono_literals;
auto [sp, rp] = Channel<People>::create();
std::thread t([rp = std::move(rp)] {
// //can use for range
Expand Down Expand Up @@ -73,7 +73,7 @@ int main() {
//send single message
sp->send(People{timestamp(), randomAge(), randomId()});
sp << People{timestamp(), randomAge(), randomId()} << (People{timestamp(), randomAge(), randomId()});

std::this_thread::sleep_for(1s);
//send multi message
std::vector<People> peoples;
peoples.reserve(10);
Expand All @@ -86,7 +86,7 @@ int main() {
for(int i = 0; i < 10; i++) {
peoples.emplace_back(timestamp(), randomAge(), randomId());
}

std::this_thread::sleep_for(1s);
//can use ranges
sp << (peoples | std::views::take(3)); // << higher priority than |
for (bool p : peoples | std::views::drop(4) | SenderView(sp)){
Expand Down
14 changes: 9 additions & 5 deletions tests/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,18 +230,22 @@ struct B {

TEST(ChannelTest, ImplicitConversion) {
using type = A;
auto [sp, rp] = Channel<type>::create();
auto [sp, rp] = Channel<type*>::create();
std::thread t1([rp = std::move(rp)]{
int value = 0;
for(auto res : *rp) {
EXPECT_EQ(res.value, value);
auto k = static_cast<TestValue*>(res);
EXPECT_EQ(k->value, value);
EXPECT_EQ(k->key, std::to_string(value));
value++;
delete res;
}
});
std::vector<TestValue> values;
std::vector<TestValue*> values;
for (int i = 0; i < 10; i++) {
TestValue value;
value.value = i;
auto value = new TestValue();
value->value = i;
value->key = std::to_string(i);
values.emplace_back(value);
}
sp << values;
Expand Down

0 comments on commit 8b81351

Please sign in to comment.