Add message storage to GameCache

This store will be used for the messages added as part of work on #421
and #528.
This commit is contained in:
Oliver Hamlet
2016-01-20 19:01:18 +00:00
parent 93953940c1
commit ef8e8b6ab8
3 changed files with 59 additions and 1 deletions
+18 -1
View File
@@ -44,7 +44,8 @@ namespace loot {
masterlist(cache.masterlist),
userlist(cache.userlist),
conditionCache(cache.conditionCache),
plugins(cache.plugins) {}
plugins(cache.plugins),
messages(cache.messages) {}
GameCache& GameCache::operator=(const GameCache& cache) {
if (&cache != this) {
@@ -52,6 +53,7 @@ namespace loot {
userlist = cache.userlist;
conditionCache = cache.conditionCache;
plugins = cache.plugins;
messages = cache.messages;
}
return *this;
@@ -108,6 +110,16 @@ namespace loot {
pair.first->second = plugin;
}
std::vector<Message> GameCache::GetMessages() const {
return messages;
}
void GameCache::AppendMessage(const Message& message) {
std::lock_guard<std::mutex> guard(mutex);
messages.push_back(message);
}
void GameCache::ClearCachedConditions() {
std::lock_guard<std::mutex> guard(mutex);
@@ -119,4 +131,9 @@ namespace loot {
plugins.clear();
}
void GameCache::ClearMessages() {
std::lock_guard<std::mutex> guard(mutex);
messages.clear();
}
}
+5
View File
@@ -52,13 +52,18 @@ namespace loot {
const Plugin& GetPlugin(const std::string& pluginName) const;
void AddPlugin(const Plugin&& plugin);
std::vector<Message> GetMessages() const;
void AppendMessage(const Message& message);
void ClearCachedConditions();
void ClearCachedPlugins();
void ClearMessages();
private:
Masterlist masterlist;
MetadataList userlist;
std::unordered_map<std::string, bool> conditionCache;
std::unordered_map<std::string, Plugin> plugins;
std::vector<Message> messages;
mutable std::mutex mutex;
};
+36
View File
@@ -43,10 +43,14 @@ namespace loot {
cache.CacheCondition("True Condition", true);
cache.AddPlugin(loot::Plugin(game, "Blank.esm", true));
Message expectedMessage(Message::say, "1");
cache.AppendMessage(expectedMessage);
loot::GameCache otherCache(cache);
EXPECT_EQ(std::make_pair(true, true), otherCache.GetCachedCondition("true Condition"));
EXPECT_EQ("Blank.esm", otherCache.GetPlugin("Blank.esm").Name());
ASSERT_EQ(1, otherCache.GetMessages().size());
EXPECT_EQ(expectedMessage, otherCache.GetMessages()[0]);
}
TEST_F(GameCache, assignmentOperatorShouldCopyCachedData) {
@@ -56,10 +60,14 @@ namespace loot {
cache.CacheCondition("True Condition", true);
cache.AddPlugin(loot::Plugin(game, "Blank.esm", true));
Message expectedMessage(Message::say, "1");
cache.AppendMessage(expectedMessage);
loot::GameCache otherCache = cache;
EXPECT_EQ(std::make_pair(true, true), otherCache.GetCachedCondition("true Condition"));
EXPECT_EQ("Blank.esm", otherCache.GetPlugin("Blank.esm").Name());
ASSERT_EQ(1, otherCache.GetMessages().size());
EXPECT_EQ(expectedMessage, otherCache.GetMessages()[0]);
}
TEST_F(GameCache, gettingATrueConditionShouldReturnATrueTruePair) {
@@ -156,6 +164,34 @@ namespace loot {
EXPECT_TRUE(cache.GetPlugins().empty());
}
TEST_F(GameCache, noMessagesShouldBeStoredByDefault) {
EXPECT_TRUE(cache.GetMessages().empty());
}
TEST_F(GameCache, appendingMessagesShouldStoreThemInTheGivenOrder) {
std::vector<Message> messages({
Message(Message::say, "1"),
Message(Message::error, "2"),
});
for (const auto& message : messages)
cache.AppendMessage(message);
EXPECT_EQ(messages, cache.GetMessages());
}
TEST_F(GameCache, clearingMessagesShouldRemoveAllStoredMessages) {
std::vector<Message> messages({
Message(Message::say, "1"),
Message(Message::error, "2"),
});
for (const auto& message : messages)
cache.AppendMessage(message);
cache.ClearMessages();
EXPECT_TRUE(cache.GetMessages().empty());
}
}
}