Skip to content

Commit

Permalink
make less exception
Browse files Browse the repository at this point in the history
Took 4 minutes
  • Loading branch information
kagurazaka-ayano committed Feb 21, 2024
1 parent 5786e0b commit 1c0d440
Show file tree
Hide file tree
Showing 15 changed files with 13 additions and 27 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added .cache/clangd/index/Queue.h.D8336189E388D174.idx
Binary file not shown.
Binary file not shown.
Binary file added .cache/clangd/index/Topic.h.3F497A38C8507516.idx
Binary file not shown.
4 changes: 1 addition & 3 deletions include/KawaiiMQ/MessageQueueManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,13 @@ namespace KawaiiMQ {
* relate a queue to a topic
* @param topic topic you want to relate
* @param queue queue you want to relate
* @exception TopicException if the topic is already related to the queue
*/
void relate(const Topic& topic, std::shared_ptr<Queue> queue);

/**
* unrelate a queue from a topic
* @param topic topic you want to unrelate
* @param queue queue you want to unrelate
* @exception TopicException if the topic is not related to the queue
* @remark this function will wait until the queue is empty
*/
void unrelate(const Topic& topic, std::shared_ptr<Queue> queue);
Expand All @@ -54,7 +52,7 @@ namespace KawaiiMQ {
* get all queues related to the topic
* @param topic given topic
* @return all queues related to the topic
* @exception TopicException if the topic is not related to any queue
* @remark this function will return a empty vector when there is nothing related to this topic
*/
std::vector<std::shared_ptr<Queue>> getAllRelatedQueue(const Topic& topic) const;

Expand Down
36 changes: 12 additions & 24 deletions src/MessageQueueManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,13 @@ namespace KawaiiMQ {

void MessageQueueManager::relate(const Topic &topic, std::shared_ptr<Queue> queue) {
std::lock_guard lock(mtx);
bool find = false;
for (const auto& q : topic_map[topic]) {
if (q->getName() == queue->getName()) {
find = true;
break;
return;
}
}
if (!find) {
topic_map[topic].push_back(queue);
} else {
throw QueueException("Attempted to relate duplicated queue in same topic: " + topic.getName());
}
topic_map[topic].push_back(queue);

}


Expand All @@ -36,22 +31,15 @@ namespace KawaiiMQ {
bool find = false;
for (const auto& q : topic_map[topic]) {
if (q->getName() == queue->getName()) {
find = true;
break;
auto &queues = topic_map[topic];
auto it = std::find_if(
queues.begin(), queues.end(),
[&queue](const std::shared_ptr<Queue> &q) {
return queue->getName() == q->getName();
});
queues.erase(it);
}
}
if (find) {
auto &queues = topic_map[topic];
auto it = std::find_if(
queues.begin(), queues.end(),
[&queue](const std::shared_ptr<Queue> &q) {
return queue->getName() == q->getName();
});
queues.erase(it);
}
else{
throw QueueException("Attempted to unrelate nonexistent queue from topic: " + topic.getName());
}
}

std::vector<std::shared_ptr<Queue>> MessageQueueManager::getAllRelatedQueue(const Topic& topic) const {
Expand All @@ -60,9 +48,9 @@ namespace KawaiiMQ {
return topic_map.at(topic);
}
catch (std::out_of_range& e) {
throw TopicException("Topic " + topic.getName() + " is not related to any queue");
std::cerr << "Topic " + topic.getName() + " is not related to any queue";
return std::vector<std::shared_ptr<Queue>>();
}

}

bool MessageQueueManager::isRelated(const Topic& topic, std::shared_ptr<Queue> queue) {
Expand Down

0 comments on commit 1c0d440

Please sign in to comment.