#ifndef ELFSPY_FAKE_H #define ELFSPY_FAKE_H #include "elfspy/Lambda.h" namespace spy { /** * @namespace spy * @class Fake * This replaces an existing function with another function, so that all calls * to the program are made to the other function. * The constructor installs the new function in place of the function, the * destructor uninstalls it */ template class Fake { public: Fake(H& hook, ReturnType (*func)(ArgTypes...)); ~Fake(); private: H& hook_; ReturnType (*func_)(ArgTypes...); }; template inline Fake:: Fake(H& hook, ReturnType (*func)(ArgTypes...)) :hook_(hook) { func_ = hook_.patch(func); } template inline Fake::~Fake() { hook_.patch(func_); } template inline auto fake(H& hook, ReturnType (*patch)(ArgTypes...)) -> Fake { return { hook, patch }; } template inline auto new_fake(H& hook, ReturnType (*patch)(ArgTypes...)) -> Fake* { return new Fake(hook, patch); } } // namespace elfspy #endif