Skip to content

Commit

Permalink
Replace RageUtil_AutoPtr with std smart pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
nico-abram committed May 29, 2022
1 parent 10a0fa7 commit 3f4e235
Show file tree
Hide file tree
Showing 26 changed files with 121 additions and 215 deletions.
23 changes: 14 additions & 9 deletions src/Etterna/Actor/Base/Actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ GetMessageNameFromCommandName(const std::string& sCommandName,

Actor::Actor()
{
m_pLuaInstance = new LuaClass;
m_pLuaInstance = std::make_unique<LuaClass>();
auto* L = LUA->Get();
m_pLuaInstance->PushSelf(L);
lua_newtable(L);
Expand Down Expand Up @@ -176,17 +176,21 @@ Actor::Actor(const Actor& cpy)
{
/* Don't copy an Actor in the middle of rendering. */
ASSERT(cpy.m_pTempState == nullptr);
m_pTempState = nullptr;

#define CPY(x) x = cpy.x
*this = cpy;
}
Actor& Actor::operator=(const Actor& x)
{
#define CPY(membername) this->membername = x.membername
CPY(m_pTempState);
CPY(m_sName);
CPY(m_pParent);
CPY(m_FakeParent);
CPY(m_pLuaInstance);
m_pLuaInstance = std::make_unique<LuaClass>(*x.m_pLuaInstance);

m_WrapperStates.resize(cpy.m_WrapperStates.size());
m_WrapperStates.resize(x.m_WrapperStates.size());
for (size_t i = 0; i < m_WrapperStates.size(); ++i) {
auto* const cp = dynamic_cast<ActorFrame*>(cpy.m_WrapperStates[i]);
auto* const cp = dynamic_cast<ActorFrame*>(x.m_WrapperStates[i]);
ASSERT_M(cp != nullptr,
"Dynamic cast to ActorFrame copy failed at runtime.");
m_WrapperStates[i] = new ActorFrame(*cp);
Expand All @@ -201,15 +205,15 @@ Actor::Actor(const Actor& cpy)
CPY(m_size);
CPY(m_current);
CPY(m_start);
for (auto* m_Tween : cpy.m_Tweens)
for (auto* m_Tween : x.m_Tweens)
m_Tweens.push_back(new TweenStateAndInfo(*m_Tween));

CPY(m_fHorizAlign);
CPY(m_fVertAlign);
#if defined(SSC_FUTURES)
// I'm a bit worried about this -aj
for (unsigned i = 0; i < cpy.m_Effects.size(); ++i)
m_Effects.push_back((*cpy.m_Effects[i]));
for (unsigned i = 0; i < x.m_Effects.size(); ++i)
m_Effects.push_back((*x.m_Effects[i]));
#else
CPY(m_Effect);
#endif
Expand Down Expand Up @@ -247,6 +251,7 @@ Actor::Actor(const Actor& cpy)
CPY(m_mapNameToCommands);
CPY(m_tween_uses_effect_delta);
#undef CPY
return *this;
}

/* XXX: This calls InitCommand, which must happen after all other
Expand Down
4 changes: 2 additions & 2 deletions src/Etterna/Actor/Base/Actor.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include "Etterna/Models/Misc/EnumHelper.h"
#include "Etterna/Models/Lua/LuaReference.h"
#include "RageUtil/Misc/RageTypes.h"
#include "RageUtil/Utils/RageUtil_AutoPtr.h"
#include "Etterna/Singletons/MessageManager.h"
#include "Tween.h"

Expand Down Expand Up @@ -84,6 +83,7 @@ class Actor : public MessageSubscriber
* @param cpy the new Actor to use in place of this one. */
Actor(const Actor& cpy);
~Actor() override;
Actor& operator=(const Actor& x);
[[nodiscard]] virtual auto Copy() const -> Actor*;
virtual void InitState();
virtual void LoadFromNode(const XNode* pNode);
Expand Down Expand Up @@ -808,7 +808,7 @@ class Actor : public MessageSubscriber
virtual void SetUpdateRate(float /*unused*/) {}
virtual auto GetUpdateRate() -> float { return 1.0F; }

HiddenPtr<LuaClass> m_pLuaInstance;
std::unique_ptr<LuaClass> m_pLuaInstance;

protected:
/** @brief the name of the Actor. */
Expand Down
51 changes: 18 additions & 33 deletions src/Etterna/Actor/Menus/MusicWheel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,18 +176,6 @@ MusicWheel::MusicWheel()
FOREACH_ENUM(SortOrder, so) { m_WheelItemDatasStatus[so] = INVALID; }
}

MusicWheel::~MusicWheel()
{
FOREACH_ENUM(SortOrder, so)
{
auto i = m__UnFilteredWheelItemDatas[so].begin();
auto iEnd = m__UnFilteredWheelItemDatas[so].end();
for (; i != iEnd; ++i) {
delete *i;
}
}
}

// this is a trainwreck and i made it worse -mina
void
MusicWheel::ReloadSongList(bool searching, const std::string& findme)
Expand Down Expand Up @@ -770,7 +758,7 @@ MusicWheel::FilterBySkillsets(std::vector<Song*>& inv)

void
MusicWheel::BuildWheelItemDatas(
std::vector<MusicWheelItemData*>& arrayWheelItemDatas,
std::vector<std::unique_ptr<MusicWheelItemData>>& arrayWheelItemDatas,
SortOrder so,
bool searching,
const std::string& findme)
Expand All @@ -782,18 +770,18 @@ MusicWheel::BuildWheelItemDatas(
std::vector<std::string> vsNames;
split(MODE_MENU_CHOICE_NAMES, ",", vsNames);
for (auto i = 0; i < static_cast<int>(vsNames.size()); ++i) {
MusicWheelItemData wid(
auto wid = std::make_unique<MusicWheelItemData>(
WheelItemDataType_Sort, nullptr, "", SORT_MENU_COLOR, 0);
wid.m_pAction = HiddenPtr<GameCommand>(new GameCommand);
wid.m_pAction->m_sName = vsNames[i];
wid.m_pAction->Load(i, ParseCommands(CHOICE.GetValue(vsNames[i])));
wid.m_sLabel = WHEEL_TEXT(vsNames[i]);
wid->m_pAction = std::make_unique<GameCommand>();
wid->m_pAction->m_sName = vsNames[i];
wid->m_pAction->Load(i, ParseCommands(CHOICE.GetValue(vsNames[i])));
wid->m_sLabel = WHEEL_TEXT(vsNames[i]);

if (!wid.m_pAction->IsPlayable()) {
if (!wid->m_pAction->IsPlayable()) {
continue;
}

arrayWheelItemDatas.emplace_back(new MusicWheelItemData(wid));
arrayWheelItemDatas.emplace_back(std::move(wid));
}
} else {
// Make an array of Song*, then sort them
Expand Down Expand Up @@ -982,17 +970,15 @@ MusicWheel::BuildWheelItemDatas(
: SECTION_COLORS.GetValue(iSectionColorIndex);
iSectionColorIndex =
(iSectionColorIndex + 1) % NUM_SECTION_COLORS;
arrayWheelItemDatas.emplace_back(
new MusicWheelItemData(WheelItemDataType_Section,
arrayWheelItemDatas.push_back(std::make_unique<MusicWheelItemData>(WheelItemDataType_Section,
nullptr,
sThisSection,
colorSection,
iSectionCount));
sLastSection = sThisSection;
}
}
arrayWheelItemDatas.emplace_back(
new MusicWheelItemData(WheelItemDataType_Song,
arrayWheelItemDatas.emplace_back(std::make_unique<MusicWheelItemData>(WheelItemDataType_Song,
pSong,
sLastSection,
SONGMAN->GetSongColor(pSong),
Expand Down Expand Up @@ -1031,8 +1017,7 @@ MusicWheel::BuildWheelItemDatas(
auto colorSection = SONGMAN->GetSongGroupColor(gname);
iSectionColorIndex =
(iSectionColorIndex + 1) % NUM_SECTION_COLORS;
arrayWheelItemDatas.emplace_back(
new MusicWheelItemData(WheelItemDataType_Section,
arrayWheelItemDatas.emplace_back(std::make_unique<MusicWheelItemData>(WheelItemDataType_Section,
nullptr,
gname,
colorSection,
Expand All @@ -1042,8 +1027,7 @@ MusicWheel::BuildWheelItemDatas(
// the song is in the arraysongs set defined above -mina
for (auto& s : gsongs) {
if (hurp.count(s) != 0u) {
arrayWheelItemDatas.emplace_back(
new MusicWheelItemData(WheelItemDataType_Song,
arrayWheelItemDatas.emplace_back(std::make_unique<MusicWheelItemData>(WheelItemDataType_Song,
s,
gname,
SONGMAN->GetSongColor(s),
Expand Down Expand Up @@ -1108,7 +1092,7 @@ MusicWheel::readyWheelItemsData(SortOrder so,
}

void
MusicWheel::FilterWheelItemDatas(std::vector<MusicWheelItemData*>& aUnFilteredDatas,
MusicWheel::FilterWheelItemDatas(std::vector<std::unique_ptr<MusicWheelItemData>>& aUnFilteredDatas,
std::vector<MusicWheelItemData*>& aFilteredData,
SortOrder /*so*/) const
{
Expand Down Expand Up @@ -1169,7 +1153,7 @@ MusicWheel::FilterWheelItemDatas(std::vector<MusicWheelItemData*>& aUnFilteredDa
if (aiRemove[i]) {
continue;
}
aFilteredData.emplace_back(aUnFilteredDatas[i]);
aFilteredData.emplace_back(aUnFilteredDatas[i].get());
}

// Update the song count in each section header.
Expand Down Expand Up @@ -1206,10 +1190,11 @@ MusicWheel::FilterWheelItemDatas(std::vector<MusicWheelItemData*>& aUnFilteredDa
--filteredSize;
}

// If we've filtered all items, insert a dummy.
// If we've filtered all items, insert a static dummy.
if (aFilteredData.empty()) {
aFilteredData.emplace_back(new MusicWheelItemData(
WheelItemDataType_Section, nullptr, EMPTY_STRING, EMPTY_COLOR, 0));
static MusicWheelItemData EmptyDummy(
WheelItemDataType_Section, nullptr, EMPTY_STRING, EMPTY_COLOR, 0);
aFilteredData.emplace_back(&EmptyDummy);
}
}

Expand Down
11 changes: 7 additions & 4 deletions src/Etterna/Actor/Menus/MusicWheel.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class MusicWheel : public WheelBase

public:
MusicWheel();
~MusicWheel() override;
~MusicWheel() override = default;
void Load(const string& sType) override;
void BeginScreen();

Expand Down Expand Up @@ -133,14 +133,17 @@ class MusicWheel : public WheelBase
NEEDREFILTER,
VALID
} m_WheelItemDatasStatus[NUM_SortOrder]{};
// Stores pointers owned by m__UnFilteredWheelItemDatas
// This is fine because whenever m__UnFilteredWheelItemDatas is updated we
// then also update m__WheelItemDatas
std::vector<MusicWheelItemData*> m__WheelItemDatas[NUM_SortOrder];
std::vector<MusicWheelItemData*> m__UnFilteredWheelItemDatas[NUM_SortOrder];
std::vector<std::unique_ptr<MusicWheelItemData>> m__UnFilteredWheelItemDatas[NUM_SortOrder];

void BuildWheelItemDatas(std::vector<MusicWheelItemData*>& arrayWheelItemDatas,
void BuildWheelItemDatas(std::vector<std::unique_ptr<MusicWheelItemData>>& arrayWheelItemDatas,
SortOrder so,
bool searching,
const std::string& findme);
void FilterWheelItemDatas(std::vector<MusicWheelItemData*>& aUnFilteredDatas,
void FilterWheelItemDatas(std::vector<std::unique_ptr<MusicWheelItemData>>& aUnFilteredDatas,
std::vector<MusicWheelItemData*>& aFilteredData,
SortOrder so) const;
std::string prevSongTitle;
Expand Down
2 changes: 1 addition & 1 deletion src/Etterna/Actor/Menus/MusicWheelItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ struct MusicWheelItemData : WheelItemBaseData

// for TYPE_SORT
std::string m_sLabel;
HiddenPtr<GameCommand> m_pAction;
std::unique_ptr<GameCommand> m_pAction;
};

#endif
19 changes: 15 additions & 4 deletions src/Etterna/Models/HighScore/HighScore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1008,17 +1008,26 @@ HighScore::HasReplayData() -> bool
return DoesFileExist(basicpath);
}

REGISTER_CLASS_TRAITS(HighScoreImpl, new HighScoreImpl(*pCopy))

HighScore::HighScore()
{
m_Impl = new HighScoreImpl;
m_Impl = std::make_unique<HighScoreImpl>();
}

HighScore::HSImplUniquePtr::~HSImplUniquePtr() = default;
HighScore::HSImplUniquePtr::HSImplUniquePtr(std::unique_ptr<HighScoreImpl> ptr) :p(std::move(ptr)) {}
HighScore::HSImplUniquePtr::HSImplUniquePtr(): p(std::make_unique<HighScoreImpl>()) { }
HighScore::HSImplUniquePtr::HSImplUniquePtr(const HSImplUniquePtr& rhs) {
p = rhs.p ? std::make_unique<HighScoreImpl>(*rhs.p) : nullptr;
}
auto HighScore::HSImplUniquePtr::operator=(const HSImplUniquePtr& rhs) -> HSImplUniquePtr& {
p = rhs.p ? std::make_unique<HighScoreImpl>(*rhs.p) : nullptr;
return *this;
}

void
HighScore::Unset()
{
m_Impl = new HighScoreImpl;
m_Impl = std::make_unique<HighScoreImpl>();
}

auto
Expand Down Expand Up @@ -1631,6 +1640,8 @@ HighScore::operator!=(const HighScore& other) const -> bool
return !operator==(other);
}

auto HighScore::operator=(const HighScore &) -> HighScore& = default;

auto
HighScore::CreateEttNode() const -> XNode*
{
Expand Down
18 changes: 16 additions & 2 deletions src/Etterna/Models/HighScore/HighScore.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include "Etterna/Models/Misc/GameConstantsAndTypes.h"
#include "Etterna/Models/Misc/Grade.h"
#include "Etterna/Models/HighScore/ReplayConstantsAndTypes.h"
#include "RageUtil/Utils/RageUtil_AutoPtr.h"

class XNode;
struct RadarValues;
Expand Down Expand Up @@ -157,6 +156,7 @@ struct HighScore
auto operator>=(HighScore const& other) const -> bool;
auto operator==(HighScore const& other) const -> bool;
auto operator!=(HighScore const& other) const -> bool;
auto HighScore::operator=(const HighScore &) -> HighScore&;

[[nodiscard]] auto CreateNode() const -> XNode*;
[[nodiscard]] auto CreateEttNode() const -> XNode*;
Expand Down Expand Up @@ -204,7 +204,21 @@ struct HighScore
void PushSelf(lua_State* L);

private:
HiddenPtr<HighScoreImpl> m_Impl;
struct HSImplUniquePtr {
std::unique_ptr<HighScoreImpl> p;

HSImplUniquePtr();
~HSImplUniquePtr();
HSImplUniquePtr(const HSImplUniquePtr& rhs);
HSImplUniquePtr(std::unique_ptr<HighScoreImpl> ptr);
auto operator=(const HSImplUniquePtr& rhs) -> HSImplUniquePtr&;

HighScoreImpl *operator->() { return &*p; }
HighScoreImpl &operator*() { return *p; }
HighScoreImpl const *operator->() const { return &*p; }
HighScoreImpl const &operator*() const { return *p; }
};
HSImplUniquePtr m_Impl;
};

/** @brief the picture taken of the high score. */
Expand Down
3 changes: 0 additions & 3 deletions src/Etterna/Models/Lua/LuaBinding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,6 @@ LuaBinding::ApplyDerivedType(Lua* L, const std::string& sClassName, void* pSelf)
lua_setmetatable(L, iTable);
}

#include "RageUtil/Utils/RageUtil_AutoPtr.h"
REGISTER_CLASS_TRAITS(LuaClass, new LuaClass(*pCopy))

void*
LuaBinding::GetPointerFromStack(Lua* L, const std::string& sType, int iArg)
{
Expand Down
2 changes: 0 additions & 2 deletions src/Etterna/Models/Lua/LuaReference.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#include "Etterna/Globals/global.h"
#include "LuaReference.h"

REGISTER_CLASS_TRAITS(LuaReference, new LuaReference(*pCopy))

LuaReference::LuaReference()
{
m_iReference = LUA_NOREF;
Expand Down
1 change: 0 additions & 1 deletion src/Etterna/Models/Lua/LuaReference.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#define LUA_REFERENCE_H

#include "Etterna/Singletons/LuaManager.h"
#include "RageUtil/Utils/RageUtil_AutoPtr.h"

struct lua_State;
using Lua = lua_State;
Expand Down
2 changes: 0 additions & 2 deletions src/Etterna/Models/Misc/GameCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ static LocalizedString COULD_NOT_LAUNCH_BROWSER(
"GameCommand",
"Could not launch web browser.");

REGISTER_CLASS_TRAITS(GameCommand, new GameCommand(*pCopy));

void
GameCommand::Init()
{
Expand Down
3 changes: 0 additions & 3 deletions src/Etterna/Models/NoteData/NoteData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include "Etterna/Singletons/GameState.h" // blame radar calculations.
#include "NoteData.h"
#include "RageUtil/Utils/RageUtil.h"
#include "RageUtil/Utils/RageUtil_AutoPtr.h"
#include "Etterna/FileTypes/XmlFile.h"

#include <unordered_map>
Expand All @@ -11,8 +10,6 @@
using std::map;
using std::vector;

REGISTER_CLASS_TRAITS(NoteData, new NoteData(*pCopy))

void
NoteData::Init()
{
Expand Down
Loading

0 comments on commit 3f4e235

Please sign in to comment.