CheatMenuSA/src/VehExtender.h

47 lines
979 B
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:
inline static std::vector<std::pair<CVehicle*,T>> data;
2021-02-27 15:40:51 -05:00
public:
2021-03-02 14:18:37 -05:00
static void RemoveVehEntry(CVehicle *pVeh)
{
for (auto it = data.begin(); it < data.end(); ++it)
{
if (it->first == pVeh)
data.erase(it);
}
}
VehExtender()
{
plugin::Events::vehicleCtorEvent.after += RemoveVehEntry;
}
~VehExtender()
{
plugin::Events::vehicleCtorEvent.after -= RemoveVehEntry;
}
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;
}
};