CheatMenuSA/CheatMenu/VehExtender.h

50 lines
1.0 KiB
C
Raw Normal View History

2021-02-27 15:40:51 -05:00
/*
VS Code extension doesn't work well with template classes
*/
#pragma once
#include <vector>
#include "CVehicle.h"
2021-03-02 14:18:37 -05:00
#include "Events.h"
2021-02-27 15:40:51 -05:00
template <class T>
class VehExtender
{
2021-03-02 14:18:37 -05:00
private:
2021-03-10 16:10:59 -05:00
inline static std::vector<std::pair<CVehicle*,T>> data{};
2021-03-02 14:18:37 -05:00
2021-02-27 15:40:51 -05:00
public:
2021-03-02 14:18:37 -05:00
static void RemoveVehEntry(CVehicle *pVeh)
{
2021-03-10 16:10:59 -05:00
for (auto it = data.begin(); it != data.end(); it++)
2021-03-02 14:18:37 -05:00
{
if (it->first == pVeh)
2021-03-10 16:10:59 -05:00
{
2021-03-02 14:18:37 -05:00
data.erase(it);
2021-03-10 16:10:59 -05:00
break;
}
2021-03-02 14:18:37 -05:00
}
}
VehExtender()
{
2021-03-10 16:10:59 -05:00
plugin::Events::vehicleDtorEvent.before += RemoveVehEntry;
2021-03-02 14:18:37 -05:00
}
~VehExtender()
{
2021-03-10 16:10:59 -05:00
plugin::Events::vehicleDtorEvent.before -= RemoveVehEntry;
2021-03-02 14:18:37 -05:00
}
2021-02-27 15:40:51 -05:00
VehExtender(const VehExtender&) = delete;
2021-03-02 14:18:37 -05:00
T& Get(CVehicle *pVeh)
2021-02-27 15:40:51 -05:00
{
for (auto it = data.begin(); it < data.end(); ++it)
{
2021-03-02 14:18:37 -05:00
if (it->first == pVeh)
2021-02-27 15:40:51 -05:00
return it->second;
}
2021-03-02 14:18:37 -05:00
data.push_back({pVeh, T(pVeh)});
2021-02-27 15:40:51 -05:00
return data.back().second;
}
};