Skip to content

Commit

Permalink
refactor: replace foreach with generator
Browse files Browse the repository at this point in the history
  • Loading branch information
OEOTYAN committed Oct 21, 2024
1 parent 7f318d8 commit bdc0fcd
Show file tree
Hide file tree
Showing 12 changed files with 80 additions and 44 deletions.
5 changes: 2 additions & 3 deletions src-server/ll/api/service/PlayerInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,11 @@ class PlayerInfo::Impl {
}
Impl() {
std::lock_guard lock(mutex);
storage.iter([this](std::string_view key, std::string_view value) {
for (auto&& [key, value] : storage.iter()) {
auto uuid = mce::UUID(key);
auto nbt = CompoundTag::fromBinaryNbt(value).value();
addPlayer(uuid, nbt["xuid"], nbt["name"]);
return true;
});
}
listener =
event::EventBus::getInstance().emplaceListener<event::PlayerJoinEvent>([this](event::PlayerJoinEvent& ev) {
if (ev.self().isSimulated()) {
Expand Down
15 changes: 13 additions & 2 deletions src/ll/api/data/KeyValueDB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@

#include "ll/api/utils/StringUtils.h"

using namespace ll::data;

namespace ll::data {
class KeyValueDB::KeyValueDBImpl {
public:
std::unique_ptr<leveldb::DB> db;
Expand Down Expand Up @@ -108,3 +107,15 @@ void KeyValueDB::iter(std::function<bool(std::string_view, std::string_view)> co
}
}
}
coro::Generator<std::pair<std::string_view, std::string_view>> KeyValueDB::iter() const {
std::unique_ptr<leveldb::Iterator> it(impl->db->NewIterator(impl->readOptions));
for (it->SeekToFirst(); it->Valid(); it->Next()) {
auto k = it->key();
auto v = it->value();
co_yield {
{k.data(), k.size()},
{v.data(), v.size()}
};
}
}
} // namespace ll::data
5 changes: 4 additions & 1 deletion src/ll/api/data/KeyValueDB.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <vector>

#include "ll/api/base/Macro.h"
#include "ll/api/coro/Generator.h"

namespace ll::data {
class KeyValueDB {
Expand Down Expand Up @@ -47,7 +48,9 @@ class KeyValueDB {

LLAPI bool del(std::string_view key);

LLAPI void iter(std::function<bool(std::string_view, std::string_view)> const& fn) const;
[[deprecated]] LLAPI void iter(std::function<bool(std::string_view, std::string_view)> const& fn) const;

LLNDAPI coro::Generator<std::pair<std::string_view, std::string_view>> iter() const;
};

} // namespace ll::data
6 changes: 2 additions & 4 deletions src/ll/api/io/Logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,9 @@ size_t Logger::addSink(std::shared_ptr<SinkBase> sink) const {

std::shared_ptr<SinkBase> Logger::getSink(size_t index) const { return impl->sinks->at(index); }

void Logger::forEachSink(std::function<bool(SinkBase&)> const& fn) const {
coro::Generator<SinkBase&> Logger::sinks() const {
for (auto& sink : *impl->sinks) {
if (!fn(*sink)) {
break;
}
co_yield *sink;
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/ll/api/io/Logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "ll/api/base/Concepts.h" // IWYU pragma: keep
#include "ll/api/base/Macro.h"
#include "ll/api/coro/Generator.h"
#include "ll/api/io/LogLevel.h"
#include "ll/api/io/Sink.h"

Expand Down Expand Up @@ -121,6 +122,6 @@ class Logger {

LLAPI std::shared_ptr<SinkBase> getSink(size_t index) const;

LLAPI void forEachSink(std::function<bool(SinkBase&)> const& fn) const;
LLNDAPI coro::Generator<SinkBase&> sinks() const;
};
} // namespace ll::io
2 changes: 2 additions & 0 deletions src/ll/api/mod/Mod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ Manifest const& Mod::getManifest() const { return mImpl->manifest; }

std::string const& Mod::getName() const { return getManifest().name; }

std::string const& Mod::getType() const { return getManifest().type; }

std::filesystem::path const& Mod::getModDir() const { return mImpl->modDir; }

std::filesystem::path const& Mod::getDataDir() const { return mImpl->dataDir; }
Expand Down
2 changes: 2 additions & 0 deletions src/ll/api/mod/Mod.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class Mod {

LLNDAPI std::string const& getName() const;

LLNDAPI std::string const& getType() const;

LLNDAPI std::filesystem::path const& getModDir() const;

LLNDAPI std::filesystem::path const& getDataDir() const;
Expand Down
14 changes: 10 additions & 4 deletions src/ll/api/mod/ModManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ ModManager::ModManager(std::string_view type) : impl(std::make_unique<Impl>(std:

ModManager::~ModManager() = default;

std::lock_guard<std::recursive_mutex> ModManager::lock() { return std::lock_guard(impl->mutex); }
std::lock_guard<std::recursive_mutex> ModManager::lock() const { return std::lock_guard(impl->mutex); }

void ModManager::addMod(std::string_view name, std::shared_ptr<Mod> const& mod) {
auto l(lock());
Expand All @@ -39,20 +39,20 @@ Expected<> ModManager::disable(std::string_view name) {

[[nodiscard]] std::string const& ModManager::getType() const { return impl->type; }

[[nodiscard]] bool ModManager::hasMod(std::string_view name) {
[[nodiscard]] bool ModManager::hasMod(std::string_view name) const {
auto l(lock());
return impl->mods.contains(name);
}

[[nodiscard]] std::shared_ptr<Mod> ModManager::getMod(std::string_view name) {
[[nodiscard]] std::shared_ptr<Mod> ModManager::getMod(std::string_view name) const {
auto l(lock());
if (auto i = impl->mods.find(name); i != impl->mods.end()) {
return i->second;
}
return {};
}

[[nodiscard]] size_t ModManager::getModCount() {
[[nodiscard]] size_t ModManager::getModCount() const {
auto l(lock());
return impl->mods.size();
}
Expand All @@ -66,4 +66,10 @@ void ModManager::forEachMod(std::function<bool(std::string_view name, Mod&)> con
}
}

coro::Generator<Mod&> ModManager::mods() const {
auto l(lock());
for (auto& p : impl->mods) {
co_yield *p.second;
}
}
} // namespace ll::mod
13 changes: 8 additions & 5 deletions src/ll/api/mod/ModManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include "ll/api/Expected.h"
#include "ll/api/base/Macro.h"
#include "ll/api/coro/Generator.h"
#include "ll/api/mod/Manifest.h"
#include "ll/api/mod/Mod.h"

Expand All @@ -23,16 +24,18 @@ class ModManager {
public:
LLNDAPI std::string const& getType() const;

LLNDAPI bool hasMod(std::string_view name);
LLNDAPI bool hasMod(std::string_view name) const;

LLNDAPI std::shared_ptr<Mod> getMod(std::string_view name);
LLNDAPI std::shared_ptr<Mod> getMod(std::string_view name) const;

LLNDAPI size_t getModCount();
LLNDAPI size_t getModCount() const;

LLAPI void forEachMod(std::function<bool(std::string_view name, Mod&)> const& fn);
[[deprecated]] LLAPI void forEachMod(std::function<bool(std::string_view name, Mod&)> const& fn);

LLNDAPI coro::Generator<Mod&> mods() const;

protected:
LLNDAPI std::lock_guard<std::recursive_mutex> lock();
LLNDAPI std::lock_guard<std::recursive_mutex> lock() const;

LLNDAPI explicit ModManager(std::string_view type);

Expand Down
37 changes: 23 additions & 14 deletions src/ll/api/mod/ModManagerRegistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,25 @@ bool ModManagerRegistry::eraseManager(std::string_view type) {
return false;
}

void ModManagerRegistry::forEachManager(std::function<bool(std::string_view type, ModManager&)> const& fn) const {
coro::Generator<ModManager&> ModManagerRegistry::managers() const {
std::lock_guard lock(impl->modMtx);
for (auto& p : impl->managers) {
co_yield *p.second;
}
}

coro::Generator<Mod&> ModManagerRegistry::mods() const {
std::lock_guard lock(impl->modMtx);
for (auto& [type, manager] : impl->managers) {
if (!fn(type, *manager)) {
for (auto& manager : managers()) {
for (auto& mod : manager.mods()) {
co_yield mod;
}
}
}

void ModManagerRegistry::forEachManager(std::function<bool(std::string_view type, ModManager&)> const& fn) const {
for (auto& manager : managers()) {
if (!fn(manager.getType(), manager)) {
return;
}
}
Expand All @@ -139,17 +154,11 @@ void ModManagerRegistry::forEachManager(std::function<bool(std::string_view type
void ModManagerRegistry::forEachModWithType(
std::function<bool(std::string_view type, std::string_view name, Mod&)> const& fn
) const {
std::lock_guard lock(impl->modMtx);
bool interrupted = false;
forEachManager([&](std::string_view type, ModManager& manager) {
manager.forEachMod([&](std::string_view name, Mod& mod) {
if (!fn(type, name, mod)) {
interrupted = true;
}
return !interrupted;
});
return !interrupted;
});
for (auto& mod : mods()) {
if (!fn(mod.getType(), mod.getName(), mod)) {
return;
}
}
}

bool ModManagerRegistry::hasMod(std::string_view name) const {
Expand Down
11 changes: 8 additions & 3 deletions src/ll/api/mod/ModManagerRegistry.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "ll/api/Expected.h"
#include "ll/api/base/Macro.h"
#include "ll/api/coro/Generator.h"
#include "ll/api/data/DependencyGraph.h"
#include "ll/api/mod/ModManager.h"

Expand Down Expand Up @@ -47,10 +48,14 @@ class ModManagerRegistry {

LLNDAPI std::shared_ptr<ModManager> getManagerForMod(std::string_view name) const;

LLAPI void forEachManager(std::function<bool(std::string_view type, ModManager&)> const& fn) const;
[[deprecated]] LLAPI void forEachManager(std::function<bool(std::string_view type, ModManager&)> const& fn) const;

LLAPI void forEachModWithType(std::function<bool(std::string_view type, std::string_view name, Mod&)> const& fn
) const;
[[deprecated]] LLAPI void
forEachModWithType(std::function<bool(std::string_view type, std::string_view name, Mod&)> const& fn) const;

LLNDAPI coro::Generator<ModManager&> managers() const;

LLNDAPI coro::Generator<Mod&> mods() const;

LLNDAPI bool hasMod(std::string_view name) const;

Expand Down
11 changes: 4 additions & 7 deletions src/ll/core/command/ModManage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,10 @@ void registerModManageCommand() {
cmd.overload().text("list").execute([](CommandOrigin const&, CommandOutput& output) {
size_t amount = 0;
std::string mods;
ll::mod::ModManagerRegistry::getInstance().forEachModWithType(
[&amount, &mods](std::string_view, std::string_view name, mod::Mod&) {
++amount;
mods = mods.append(name).append(", ");
return true;
}
);
for (auto& mod : ll::mod::ModManagerRegistry::getInstance().mods()) {
++amount;
mods = mods.append(mod.getName()).append(", ");
}
if (!mods.empty()) {
mods.resize(mods.size() - 2);
}
Expand Down

0 comments on commit bdc0fcd

Please sign in to comment.