From 82c40b10d2d76114be405aaf5033810ce54598b8 Mon Sep 17 00:00:00 2001 From: falsycat Date: Sat, 28 Aug 2021 13:41:20 +0900 Subject: [PATCH] Allows UniqPtr to do upcast. --- src/Game.cc | 2 +- src/GlyphElementFactory.h | 2 +- src/InputWindowElementFactory.h | 4 ++-- src/LoadScene.cc | 2 +- src/Lua.cc | 2 +- src/MusicElementFactory.h | 2 +- src/PlayScene.cc | 2 +- src/ResultScene.cc | 2 +- src/TitleScene.cc | 2 +- src/iAllocator.h | 11 +++++------ 10 files changed, 15 insertions(+), 16 deletions(-) diff --git a/src/Game.cc b/src/Game.cc index 69b95c2..68c2abe 100644 --- a/src/Game.cc +++ b/src/Game.cc @@ -14,5 +14,5 @@ gj::Game::Game(gj::Game::Param&& p) : param.alloc = alloc_; param.clock = &clock_; param.audio = p.audio; - scene_ = alloc_->MakeUniq(param); + scene_ = alloc_->MakeUniq(param); } \ No newline at end of file diff --git a/src/GlyphElementFactory.h b/src/GlyphElementFactory.h index 4abf8f3..aeda0de 100644 --- a/src/GlyphElementFactory.h +++ b/src/GlyphElementFactory.h @@ -32,7 +32,7 @@ class GlyphElementFactory : public iElementFactory { auto& font = FindOrCreateFont(name); auto tex = std::move(font.RenderGlyphs(ConvertStrToWstr(text), size)); /* TODO */ - return alloc_->MakeUniq( + return alloc_->MakeUniq( param.period, std::move(tex), std::move(param.driver)); } diff --git a/src/InputWindowElementFactory.h b/src/InputWindowElementFactory.h index 93ab970..d5a58de 100644 --- a/src/InputWindowElementFactory.h +++ b/src/InputWindowElementFactory.h @@ -32,10 +32,10 @@ class InputWindowElementFactory : public iElementFactory { p.period = param.period; p.scoreboard = sb_; p.driver = std::move(param.driver); - p.matcher = alloc_->MakeUniq(ConvertStrToWstr(kana)); + p.matcher = alloc_->MakeUniq(ConvertStrToWstr(kana)); p.text = ConvertStrToWstr(text); - return alloc_->MakeUniq(std::move(p)); + return alloc_->MakeUniq(std::move(p)); } private: diff --git a/src/LoadScene.cc b/src/LoadScene.cc index d9b9be1..bf2009e 100644 --- a/src/LoadScene.cc +++ b/src/LoadScene.cc @@ -12,7 +12,7 @@ gj::LoadScene::LoadScene(Param&& p) : gj::UniqPtr gj::LoadScene::Update(Frame& frame) { if (prod_->HasPrepared() && !(orphan_ && orphan_->IsBusy())) { prod_->Start(); - return UniqPtr(prod_.release(), iAllocator::Deleter(alloc_)); + return std::move(prod_); } const uint64_t now = clock_->now(); diff --git a/src/Lua.cc b/src/Lua.cc index dad16ab..54e2c40 100644 --- a/src/Lua.cc +++ b/src/Lua.cc @@ -118,7 +118,7 @@ static int CallFactory_(lua_State* L) { gj::iElementFactory::Param param; param.period = gj::Period(st, ed); - param.driver = alloc->MakeUniq(L, n); + param.driver = alloc->MakeUniq(L, n); for (int i = 3; i < n; ++i) { gj::iElementFactory::Param::CustomValue v; diff --git a/src/MusicElementFactory.h b/src/MusicElementFactory.h index 67a667e..b247f23 100644 --- a/src/MusicElementFactory.h +++ b/src/MusicElementFactory.h @@ -33,7 +33,7 @@ class MusicElementFactory : public iElementFactory { p.path = path; p.offset = offset; p.driver = std::move(param.driver); - return alloc_->MakeUniq(std::move(p)); + return alloc_->MakeUniq(std::move(p)); } private: diff --git a/src/PlayScene.cc b/src/PlayScene.cc index ddd04c0..274e102 100644 --- a/src/PlayScene.cc +++ b/src/PlayScene.cc @@ -9,7 +9,7 @@ gj::UniqPtr gj::PlayScene::Update(Frame& f) { if (store_.IsEmpty()) { - return param_.alloc->MakeUniq(param_, sb_); + return param_.alloc->MakeUniq(param_, sb_); } store_.Update(f, clock_.now()); diff --git a/src/ResultScene.cc b/src/ResultScene.cc index fca5a6e..f44d47d 100644 --- a/src/ResultScene.cc +++ b/src/ResultScene.cc @@ -54,7 +54,7 @@ gj::UniqPtr gj::ResultScene::Update(Frame& f) { f.Add(&guide_); if (f.input.find(' ') != std::string::npos) { - return param_.alloc->MakeUniq(param_); + return param_.alloc->MakeUniq(param_); } return nullptr; diff --git a/src/TitleScene.cc b/src/TitleScene.cc index 4727f86..dae3bae 100644 --- a/src/TitleScene.cc +++ b/src/TitleScene.cc @@ -63,7 +63,7 @@ gj::UniqPtr gj::TitleScene::Update(Frame& frame) { param.path = s.path; param.title = s.title; param.orphan = std::move(music_); - return param_.alloc->MakeUniq(std::move(param)); + return param_.alloc->MakeUniq(std::move(param)); } case 'h': SelectScore_(select_index_? select_index_-1: list_.size()-1); diff --git a/src/iAllocator.h b/src/iAllocator.h index 88927c4..1c17996 100644 --- a/src/iAllocator.h +++ b/src/iAllocator.h @@ -19,7 +19,11 @@ public: Deleter& operator=(Deleter&&) = default; Deleter& operator=(const Deleter&) = delete; - Deleter(iAllocator* alloc) : alloc_(alloc) { + template + Deleter(Deleter&& src) { + alloc_ = src.alloc_; + } + explicit Deleter(iAllocator* alloc) : alloc_(alloc) { } void operator()(T* ptr) { @@ -57,11 +61,6 @@ public: T* ptr = Alloc(); return std::unique_ptr>(new(ptr) T(std::forward(args)...), Deleter(this)); } - template - UniqPtr MakeUniq(Args&&... args) { - T* ptr = Alloc(); - return std::unique_ptr>(new(ptr) T(std::forward(args)...), Deleter(this)); - } template UniqPtr MakeUniqArray(size_t n) { T* ptr = Alloc(n);