Elfspy/elfspy/Fake.h

64 lines
1.3 KiB
C++

#ifndef ELFSPY_FAKE_H
#define ELFSPY_FAKE_H
#include "elfspy/Lambda.h"
#include <memory>
#include <vector>
#include <iostream>
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 <typename H, typename ReturnType, typename... ArgTypes>
class Fake
{
public:
Fake(H& hook, ReturnType (*func)(ArgTypes...));
~Fake();
private:
H& hook_;
ReturnType (*func_)(ArgTypes...);
};
template <typename H, typename ReturnType, typename... ArgTypes>
Fake<H, ReturnType, ArgTypes...>::
Fake(H& hook, ReturnType (*func)(ArgTypes...))
:hook_(hook)
{
func_ = hook_.patch(func);
}
/*
template <typename H, typename ReturnType, typename... ArgTypes>
Fake<H, ReturnType, ArgTypes...>::~Fake()
{
std::cout << "Fake" << std::endl;
hook_.patch(func_);
}
template <typename H, typename ReturnType, typename... ArgTypes>
Fake<H, ReturnType, ArgTypes...>* new_fake(H& hook, ReturnType (*patch)(ArgTypes...))
{
std::cout << "hola" << std::endl;
auto instance = new Fake<H, ReturnType, ArgTypes...>(hook, patch);
fake_list.push_back (instance);
return instance;
}
*/
} // namespace elfspy
#endif