Added isTopicEmpty function (#1808)

Co-authored-by: TheEnigmist <lthenigmistl@gmail.com>
This commit is contained in:
TheEnigmist 2023-10-10 05:03:27 +02:00 committed by GitHub
parent 1efe89a719
commit d9afdf279a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -243,6 +243,31 @@ class PubSubService : public trantor::NonCopyable
topicMap_.erase(topicName);
}
/**
* @brief Check if a topic is empty.
*
* @param topicName The topic name.
* @return true means there are no subscribers.
* @return false means there are subscribers in the topic.
*/
bool isTopicEmpty(const std::string &topicName) const
{
std::shared_ptr<Topic<MessageType>> topicPtr;
{
std::shared_lock<SharedMutex> lock(mutex_);
auto iter = topicMap_.find(topicName);
if (iter != topicMap_.end())
{
topicPtr = iter->second;
}
else
{
return true;
}
}
return topicPtr->empty();
}
private:
std::unordered_map<std::string, std::shared_ptr<Topic<MessageType>>>
topicMap_;