From 05eabb3bc50b4975f189fcf097a3aabafedd34d3 Mon Sep 17 00:00:00 2001 From: sergiotarxz Date: Tue, 2 Aug 2022 06:32:28 +0200 Subject: [PATCH] Adding auto deletion. --- Fake.h | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/Fake.h b/Fake.h index 3cd2e40..4272b70 100644 --- a/Fake.h +++ b/Fake.h @@ -16,7 +16,15 @@ namespace spy */ template -extern std::vector fake_list; +extern std::vector> 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 @@ -44,12 +51,12 @@ inline Fake::~Fake() } template -inline auto fake(H& hook, ReturnType (*patch)(ArgTypes...)) - -> Fake -{ - return { hook, patch }; +void fake(H& hook, ReturnType (*patch)(ArgTypes...)) { + auto shared_ptr = std::make_shared> (hook, patch); + fake_list.push_back (shared_ptr); } + template inline auto new_fake(H& hook, ReturnType (*patch)(ArgTypes...)) -> Fake*