add uv::Context
This commit is contained in:
parent
fb2745e37d
commit
ded037dada
@ -17,6 +17,7 @@ target_sources(nf7_core
|
||||
luajit/context.hh
|
||||
luajit/lambda.hh
|
||||
luajit/thread.hh
|
||||
uv/context.hh
|
||||
clock.hh
|
||||
logger.hh
|
||||
version.hh
|
||||
@ -30,6 +31,7 @@ target_sources(nf7_core_test
|
||||
luajit/lambda_test.cc
|
||||
luajit/thread_test.cc
|
||||
luajit/thread_test.hh
|
||||
uv/context_test.hh
|
||||
clock_test.cc
|
||||
)
|
||||
target_link_libraries(nf7_core_test
|
||||
|
65
core/uv/context.hh
Normal file
65
core/uv/context.hh
Normal file
@ -0,0 +1,65 @@
|
||||
// No copyright
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <thread>
|
||||
|
||||
#include <uvw.hpp>
|
||||
|
||||
#include "iface/common/exception.hh"
|
||||
#include "iface/subsys/interface.hh"
|
||||
#include "iface/env.hh"
|
||||
|
||||
namespace nf7::core::uv {
|
||||
|
||||
class Context : public subsys::Interface {
|
||||
public:
|
||||
template <typename T>
|
||||
std::shared_ptr<T> Make() const
|
||||
try {
|
||||
auto ptr = loop_->resource<T>();
|
||||
if (nullptr == ptr) {
|
||||
throw Exception {"failed to init libuv resource"};
|
||||
}
|
||||
return ptr;
|
||||
} catch (const std::bad_alloc&) {
|
||||
throw Exception {"failed to allocate libuv resource"};
|
||||
}
|
||||
|
||||
void Exit() noexcept {
|
||||
loop_->walk([](auto&& h) { h.close(); });
|
||||
loop_->stop();
|
||||
}
|
||||
|
||||
const std::shared_ptr<uvw::loop>& loop() const noexcept { return loop_; }
|
||||
|
||||
protected:
|
||||
Context(const char* name, Env&)
|
||||
: subsys::Interface(name), loop_(uvw::loop::create()) {
|
||||
if (nullptr == loop_) {
|
||||
throw Exception {"failed to create new uv loop"};
|
||||
}
|
||||
}
|
||||
|
||||
protected:
|
||||
void Run() noexcept {
|
||||
loop_->run(uvw::loop::run_mode::DEFAULT);
|
||||
}
|
||||
void RunOnce() noexcept {
|
||||
loop_->run(uvw::loop::run_mode::ONCE);
|
||||
}
|
||||
|
||||
private:
|
||||
std::shared_ptr<uvw::loop> loop_;
|
||||
};
|
||||
|
||||
class MainContext : public Context {
|
||||
public:
|
||||
explicit MainContext(Env& env)
|
||||
: Context("nf7::core::uv::MainContext", env) { }
|
||||
|
||||
using Context::Run;
|
||||
using Context::RunOnce;
|
||||
};
|
||||
|
||||
} // namespace nf7::core::uv
|
41
core/uv/context_test.hh
Normal file
41
core/uv/context_test.hh
Normal file
@ -0,0 +1,41 @@
|
||||
// No copyright
|
||||
#pragma once
|
||||
|
||||
#include "core/uv/context.hh"
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
|
||||
#include "iface/env.hh"
|
||||
|
||||
#include "core/clock.hh"
|
||||
|
||||
|
||||
namespace nf7::core::uv::test {
|
||||
|
||||
class ContextFixture : public ::testing::Test {
|
||||
protected:
|
||||
void SetUp() override {
|
||||
env_.emplace(SimpleEnv::FactoryMap {
|
||||
SimpleEnv::MakePair<Context, MainContext>(),
|
||||
{
|
||||
typeid(subsys::Clock),
|
||||
[](auto&) { return std::make_shared<core::Clock>();
|
||||
}},
|
||||
});
|
||||
ctx_ = std::dynamic_pointer_cast<MainContext>(env_->Get<Context>());
|
||||
}
|
||||
void TearDown() override {
|
||||
ctx_->Run();
|
||||
env_ = std::nullopt;
|
||||
ctx_ = nullptr;
|
||||
}
|
||||
|
||||
protected:
|
||||
std::optional<SimpleEnv> env_;
|
||||
std::shared_ptr<MainContext> ctx_;
|
||||
};
|
||||
|
||||
} // namespace nf7::core:uv::test
|
Loading…
x
Reference in New Issue
Block a user