Introduce Checkable#GetLastComment()

This commit is contained in:
Alexander A. Klimov 2021-07-29 12:10:42 +02:00
parent 9169c805a8
commit 2818245e01
2 changed files with 16 additions and 0 deletions

View File

@ -7,6 +7,7 @@
#include "base/timer.hpp" #include "base/timer.hpp"
#include "base/utility.hpp" #include "base/utility.hpp"
#include "base/logger.hpp" #include "base/logger.hpp"
#include <utility>
using namespace icinga; using namespace icinga;
@ -42,6 +43,20 @@ std::set<Comment::Ptr> Checkable::GetComments() const
return m_Comments; return m_Comments;
} }
Comment::Ptr Checkable::GetLastComment() const
{
std::unique_lock<std::mutex> lock (m_CommentMutex);
Comment::Ptr lastComment;
for (auto& comment : m_Comments) {
if (!lastComment || comment->GetEntryTime() > lastComment->GetEntryTime()) {
lastComment = comment;
}
}
return std::move(lastComment);
}
void Checkable::RegisterComment(const Comment::Ptr& comment) void Checkable::RegisterComment(const Comment::Ptr& comment)
{ {
std::unique_lock<std::mutex> lock(m_CommentMutex); std::unique_lock<std::mutex> lock(m_CommentMutex);

View File

@ -152,6 +152,7 @@ public:
void RemoveCommentsByType(int type, const String& removedBy = String()); void RemoveCommentsByType(int type, const String& removedBy = String());
std::set<Comment::Ptr> GetComments() const; std::set<Comment::Ptr> GetComments() const;
Comment::Ptr GetLastComment() const;
void RegisterComment(const Comment::Ptr& comment); void RegisterComment(const Comment::Ptr& comment);
void UnregisterComment(const Comment::Ptr& comment); void UnregisterComment(const Comment::Ptr& comment);