Adding auto deletion.

This commit is contained in:
sergiotarxz 2022-08-02 06:32:28 +02:00
parent 1e4f54fe68
commit 05eabb3bc5
1 changed files with 13 additions and 6 deletions

19
Fake.h
View File

@ -16,7 +16,15 @@ namespace spy
*/
template <typename H, typename ReturnType, typename... ArgTypes>
extern std::vector<Fake *> fake_list;
extern std::vector<std::shared_ptr<Fake>> fake_list;
void delete_mocks (void) {
while (fake_list.size () != 0) {
auto index = fake_list.size() - 1;
auto element = fake_list[index];
fake_list.erase (&fake_list[index]);
delete element;
}
}
class Fake
{
public:
@ -34,7 +42,6 @@ Fake(H& hook, ReturnType (*func)(ArgTypes...))
:hook_(hook)
{
func_ = hook_.patch(func);
fake_list.push_back(&this);
}
template <typename H, typename ReturnType, typename... ArgTypes>
@ -44,12 +51,12 @@ inline Fake<H, ReturnType, ArgTypes...>::~Fake()
}
template <typename H, typename ReturnType, typename... ArgTypes>
inline auto fake(H& hook, ReturnType (*patch)(ArgTypes...))
-> Fake<H, ReturnType, ArgTypes...>
{
return { hook, patch };
void fake(H& hook, ReturnType (*patch)(ArgTypes...)) {
auto shared_ptr = std::make_shared<Fake<H, ReturnType, ArgTypes...>> (hook, patch);
fake_list.push_back (shared_ptr);
}
template <typename H, typename ReturnType, typename... ArgTypes>
inline auto new_fake(H& hook, ReturnType (*patch)(ArgTypes...))
-> Fake<H, ReturnType, ArgTypes...>*