Elfspy/elfspy/Fake.h

64 lines
1.3 KiB
C
Raw Permalink Normal View History

2016-09-16 17:45:35 +02:00
#ifndef ELFSPY_FAKE_H
#define ELFSPY_FAKE_H
#include "elfspy/Lambda.h"
2022-08-02 06:55:56 +02:00
#include <memory>
2022-08-02 07:37:27 +02:00
#include <vector>
2022-08-02 11:13:23 +02:00
#include <iostream>
2022-08-02 06:55:56 +02:00
2016-09-16 17:45:35 +02:00
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
*/
2022-08-02 07:37:27 +02:00
2016-09-16 17:45:35 +02:00
template <typename H, typename ReturnType, typename... ArgTypes>
2022-08-02 11:13:23 +02:00
class Fake
2016-09-16 17:45:35 +02:00
{
public:
Fake(H& hook, ReturnType (*func)(ArgTypes...));
~Fake();
private:
H& hook_;
ReturnType (*func_)(ArgTypes...);
};
template <typename H, typename ReturnType, typename... ArgTypes>
2022-08-02 11:13:23 +02:00
Fake<H, ReturnType, ArgTypes...>::
2016-09-16 17:45:35 +02:00
Fake(H& hook, ReturnType (*func)(ArgTypes...))
:hook_(hook)
{
func_ = hook_.patch(func);
}
2022-08-02 11:13:23 +02:00
/*
2016-09-16 17:45:35 +02:00
template <typename H, typename ReturnType, typename... ArgTypes>
2022-08-02 11:13:23 +02:00
Fake<H, ReturnType, ArgTypes...>::~Fake()
2016-09-16 17:45:35 +02:00
{
2022-08-02 11:13:23 +02:00
std::cout << "Fake" << std::endl;
2016-09-16 17:45:35 +02:00
hook_.patch(func_);
}
template <typename H, typename ReturnType, typename... ArgTypes>
2022-08-02 11:13:23 +02:00
Fake<H, ReturnType, ArgTypes...>* new_fake(H& hook, ReturnType (*patch)(ArgTypes...))
2016-09-16 17:45:35 +02:00
{
2022-08-02 11:13:23 +02:00
std::cout << "hola" << std::endl;
auto instance = new Fake<H, ReturnType, ArgTypes...>(hook, patch);
fake_list.push_back (instance);
return instance;
2016-09-16 17:45:35 +02:00
}
2022-08-02 11:13:23 +02:00
*/
2016-09-16 17:45:35 +02:00
} // namespace elfspy
#endif