20#ifndef OPM_MESSAGELIMITER_HEADER_INCLUDED
21#define OPM_MESSAGELIMITER_HEADER_INCLUDED
26#include <unordered_map>
37 enum { NoLimit = -1 };
71 MessageLimiter(
const int tag_limit,
const std::map<std::int64_t, int>& category_limits)
72 : tag_limit_ { (tag_limit < 0) ? NoLimit : tag_limit }
73 , category_limits_ { category_limits }
90 const std::int64_t messageMask)
const
95 if (!tag.empty() && (this->tag_limit_ != NoLimit)) {
96 res = this->countBasedResponseTag(this->increaseTagCount(tag));
106 const auto count = this->increaseCategoryCount(messageMask);
108 if (
const auto limitPos = this->category_limits_.find(messageMask);
109 (limitPos != this->category_limits_.end()) &&
110 (limitPos->second != NoLimit))
114 res = this->countBasedResponseCategory(count, limitPos->second);
130 const auto countPos = this->category_counts_.find(category);
132 return (countPos != this->category_counts_.end())
133 ? countPos->second : 0;
140 int tag_limit_ {NoLimit};
147 std::map<std::int64_t, int> category_limits_{};
150 mutable std::unordered_map<std::string, int> tag_counts_{};
153 mutable std::map<std::int64_t, int> category_counts_{};
155 int increaseTagCount(
const std::string& tag)
const
157 return increaseCount(tag, this->tag_counts_);
160 int increaseCategoryCount(
const std::int64_t messageMask)
const
162 return increaseCount(messageMask, this->category_counts_);
165 Response countBasedResponseTag(
const int count)
const
167 return response(count, this->tag_limit_,
168 Response::JustOverTagLimit,
169 Response::OverTagLimit);
172 Response countBasedResponseCategory(
const int count,
173 const int category_limit)
const
175 return response(count, category_limit,
176 Response::JustOverCategoryLimit,
177 Response::OverCategoryLimit);
180 template <
typename Key,
class CountMap>
181 static int increaseCount(
const Key& key, CountMap& counts)
183 return ++counts.try_emplace(key, 0).first->second;
186 static Response response(
const int count,
188 const Response justOverLimit,
189 const Response overLimit)
191 if (count <= limit) {
192 return Response::PrintMessage;
193 }
else if (count == limit + 1) {
194 return justOverLimit;
Handles limiting the number of messages with the same tag.
Definition MessageLimiter.hpp:34
Response
Used for handleMessageLimits() return type (see that function).
Definition MessageLimiter.hpp:41
@ JustOverCategoryLimit
Message has reached the current limit for this category.
Definition MessageLimiter.hpp:49
@ JustOverTagLimit
Message has reached the current limit for this tag.
Definition MessageLimiter.hpp:46
@ OverTagLimit
Message is over the current limit for this tag.
Definition MessageLimiter.hpp:52
@ OverCategoryLimit
Message is over the current limit of this category.
Definition MessageLimiter.hpp:55
@ PrintMessage
Message should be printed. Not affected by any limit.
Definition MessageLimiter.hpp:43
MessageLimiter(const int tag_limit)
Construct with given limit to number of messages with the same tag.
Definition MessageLimiter.hpp:67
int tagMessageLimit() const
The tag message limit (same for all tags).
Definition MessageLimiter.hpp:77
int categoryMessageCount(const std::int64_t category) const
Retrieve message count for specific category.
Definition MessageLimiter.hpp:128
Response handleMessageLimits(const std::string &tag, const std::int64_t messageMask) const
If (tag count == tag limit + 1) for the passed tag, respond JustOverTagLimit.
Definition MessageLimiter.hpp:89
MessageLimiter()=default
Default constructor, no limit to the number of messages.
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition Exceptions.hpp:30