Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

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