Allows UniqPtr to do upcast.

This commit is contained in:
falsycat 2021-08-28 13:41:20 +09:00
parent 7fa5227f42
commit 82c40b10d2
10 changed files with 15 additions and 16 deletions

View File

@ -14,5 +14,5 @@ gj::Game::Game(gj::Game::Param&& p) :
param.alloc = alloc_; param.alloc = alloc_;
param.clock = &clock_; param.clock = &clock_;
param.audio = p.audio; param.audio = p.audio;
scene_ = alloc_->MakeUniq<gj::iScene, gj::TitleScene>(param); scene_ = alloc_->MakeUniq<gj::TitleScene>(param);
} }

View File

@ -32,7 +32,7 @@ class GlyphElementFactory : public iElementFactory {
auto& font = FindOrCreateFont(name); auto& font = FindOrCreateFont(name);
auto tex = std::move(font.RenderGlyphs(ConvertStrToWstr(text), size)); /* TODO */ auto tex = std::move(font.RenderGlyphs(ConvertStrToWstr(text), size)); /* TODO */
return alloc_->MakeUniq<iElement, TextureElement>( return alloc_->MakeUniq<TextureElement>(
param.period, std::move(tex), std::move(param.driver)); param.period, std::move(tex), std::move(param.driver));
} }

View File

@ -32,10 +32,10 @@ class InputWindowElementFactory : public iElementFactory {
p.period = param.period; p.period = param.period;
p.scoreboard = sb_; p.scoreboard = sb_;
p.driver = std::move(param.driver); p.driver = std::move(param.driver);
p.matcher = alloc_->MakeUniq<iInputMatcher, HiraganaMatcher>(ConvertStrToWstr(kana)); p.matcher = alloc_->MakeUniq<HiraganaMatcher>(ConvertStrToWstr(kana));
p.text = ConvertStrToWstr(text); p.text = ConvertStrToWstr(text);
return alloc_->MakeUniq<iElement, InputWindowElement>(std::move(p)); return alloc_->MakeUniq<InputWindowElement>(std::move(p));
} }
private: private:

View File

@ -12,7 +12,7 @@ gj::LoadScene::LoadScene(Param&& p) :
gj::UniqPtr<gj::iScene> gj::LoadScene::Update(Frame& frame) { gj::UniqPtr<gj::iScene> gj::LoadScene::Update(Frame& frame) {
if (prod_->HasPrepared() && !(orphan_ && orphan_->IsBusy())) { if (prod_->HasPrepared() && !(orphan_ && orphan_->IsBusy())) {
prod_->Start(); prod_->Start();
return UniqPtr<iScene>(prod_.release(), iAllocator::Deleter<iScene>(alloc_)); return std::move(prod_);
} }
const uint64_t now = clock_->now(); const uint64_t now = clock_->now();

View File

@ -118,7 +118,7 @@ static int CallFactory_(lua_State* L) {
gj::iElementFactory::Param param; gj::iElementFactory::Param param;
param.period = gj::Period(st, ed); param.period = gj::Period(st, ed);
param.driver = alloc->MakeUniq<gj::iElementDriver, LuaFunc>(L, n); param.driver = alloc->MakeUniq<LuaFunc>(L, n);
for (int i = 3; i < n; ++i) { for (int i = 3; i < n; ++i) {
gj::iElementFactory::Param::CustomValue v; gj::iElementFactory::Param::CustomValue v;

View File

@ -33,7 +33,7 @@ class MusicElementFactory : public iElementFactory {
p.path = path; p.path = path;
p.offset = offset; p.offset = offset;
p.driver = std::move(param.driver); p.driver = std::move(param.driver);
return alloc_->MakeUniq<iElement, MusicElement>(std::move(p)); return alloc_->MakeUniq<MusicElement>(std::move(p));
} }
private: private:

View File

@ -9,7 +9,7 @@
gj::UniqPtr<gj::iScene> gj::PlayScene::Update(Frame& f) { gj::UniqPtr<gj::iScene> gj::PlayScene::Update(Frame& f) {
if (store_.IsEmpty()) { if (store_.IsEmpty()) {
return param_.alloc->MakeUniq<iScene, ResultScene>(param_, sb_); return param_.alloc->MakeUniq<ResultScene>(param_, sb_);
} }
store_.Update(f, clock_.now()); store_.Update(f, clock_.now());

View File

@ -54,7 +54,7 @@ gj::UniqPtr<gj::iScene> gj::ResultScene::Update(Frame& f) {
f.Add(&guide_); f.Add(&guide_);
if (f.input.find(' ') != std::string::npos) { if (f.input.find(' ') != std::string::npos) {
return param_.alloc->MakeUniq<iScene, TitleScene>(param_); return param_.alloc->MakeUniq<TitleScene>(param_);
} }
return nullptr; return nullptr;

View File

@ -63,7 +63,7 @@ gj::UniqPtr<gj::iScene> gj::TitleScene::Update(Frame& frame) {
param.path = s.path; param.path = s.path;
param.title = s.title; param.title = s.title;
param.orphan = std::move(music_); param.orphan = std::move(music_);
return param_.alloc->MakeUniq<iScene, LoadScene>(std::move(param)); return param_.alloc->MakeUniq<LoadScene>(std::move(param));
} }
case 'h': case 'h':
SelectScore_(select_index_? select_index_-1: list_.size()-1); SelectScore_(select_index_? select_index_-1: list_.size()-1);

View File

@ -19,7 +19,11 @@ public:
Deleter& operator=(Deleter&&) = default; Deleter& operator=(Deleter&&) = default;
Deleter& operator=(const Deleter&) = delete; Deleter& operator=(const Deleter&) = delete;
Deleter(iAllocator* alloc) : alloc_(alloc) { template <typename I>
Deleter(Deleter<I>&& src) {
alloc_ = src.alloc_;
}
explicit Deleter(iAllocator* alloc) : alloc_(alloc) {
} }
void operator()(T* ptr) { void operator()(T* ptr) {
@ -57,11 +61,6 @@ public:
T* ptr = Alloc<T>(); T* ptr = Alloc<T>();
return std::unique_ptr<T, Deleter<T>>(new(ptr) T(std::forward<Args>(args)...), Deleter<T>(this)); return std::unique_ptr<T, Deleter<T>>(new(ptr) T(std::forward<Args>(args)...), Deleter<T>(this));
} }
template <typename I, typename T, typename... Args>
UniqPtr<I> MakeUniq(Args&&... args) {
T* ptr = Alloc<T>();
return std::unique_ptr<I, Deleter<I>>(new(ptr) T(std::forward<Args>(args)...), Deleter<I>(this));
}
template <typename T> template <typename T>
UniqPtr<T> MakeUniqArray(size_t n) { UniqPtr<T> MakeUniqArray(size_t n) {
T* ptr = Alloc<T>(n); T* ptr = Alloc<T>(n);