Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/shimogawa/monkeyc
Monkey patching for C/C++ in a single header
https://github.com/shimogawa/monkeyc
Last synced: 1 day ago
JSON representation
Monkey patching for C/C++ in a single header
- Host: GitHub
- URL: https://github.com/shimogawa/monkeyc
- Owner: Shimogawa
- Created: 2024-06-11T02:59:48.000Z (5 months ago)
- Default Branch: master
- Last Pushed: 2024-06-13T04:40:19.000Z (5 months ago)
- Last Synced: 2024-06-13T10:49:12.996Z (5 months ago)
- Language: C++
- Homepage:
- Size: 31.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Monkey patching in C and C++
![example workflow](https://github.com/Shimogawa/monkeyc/actions/workflows/cmake.yml/badge.svg)
A single-header library that provides monkey patching in C and C++.
**THIS HEADER SHOULD ONLY BE USED FOR TESTING PURPOSES.**
## Usage
### C
```c
#include "monkey.h"
#include__attribute__((noinline)) int plus1(int i) {
return i + 1;
}__attribute__((noinline)) int plus2(int i) {
return i + 2;
}int main(void) {
printf("%d\n", plus1(3)); // 4
monkeyc_patched p = monkeyc_patch(plus1, plus2);
printf("%d\n", plus1(3)); // 5
monkeyc_unpatch(&p);
printf("%d\n", plus1(3)); // 4
return 0;
}
```### C++
```c++
#include "monkey.h"
#include__attribute__((noinline)) int plus1(int i) {
return i + 1;
}int main(void) {
std::cout << plus1(1) << std::endl;
{
auto g = monkeyc::patch(plus1, [](int i) { return i + 2; });
std::cout << plus1(1) << std::endl;
}
std::cout << plus1(1) << std::endl;
return 0;
}
```## References
- https://github.com/mehcode/guerrilla
- https://github.com/bouk/monkey