Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/scenent/cpp-bind-any-methods
It's the way you can bind any methods to map and call it dynamically. Implemented with TMP of C++.
https://github.com/scenent/cpp-bind-any-methods
cpp cpp17 tmp
Last synced: 5 days ago
JSON representation
It's the way you can bind any methods to map and call it dynamically. Implemented with TMP of C++.
- Host: GitHub
- URL: https://github.com/scenent/cpp-bind-any-methods
- Owner: scenent
- License: cc0-1.0
- Created: 2024-02-11T20:25:59.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2024-02-11T21:00:18.000Z (11 months ago)
- Last Synced: 2024-11-15T02:29:22.964Z (2 months ago)
- Topics: cpp, cpp17, tmp
- Language: C++
- Homepage:
- Size: 11.7 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# cpp-bind-any-methods
The way you can bind any methods(even class's member functions) to map and call it dynamically.## Usage
### Code
```cpp
#include "my_function_map.h"
std::string add_str(std::string i, std::string j) {
return i + " " + j;
}int main() {
MyFunctionMap myFunctionMap;
myFunctionMap.bindFunction("add_str", &add_str);
std::string value = std::any_cast(myFunctionMap.getFunction("add_str")->call({ std::string("Hello"), std::string("World") }));
std::cout << value << "\n";
}
```
### Console Output
```
Hello World
```## How does It works?
1. The `Function` class does not have any template factors.
2. Only `CallableFunction` and `CallableClassFunction`, which inherit the `Function` class, have template factors.
3. The `MyFunctionMap` class has a pointer of the `Function` class as information of the map.
4. The `call` function of the `Function` class is declared as a virtual function.
5. In the `call` function, the internal binded function is called through the C++'s `variadic template` and `std::apply`.## Where do I use it?
You can use this technique to your own dynamic language interpreter.## Warning
- Only C++ 17+ compiler can compile this source.
- When you call `Function::call(const std::vector& args)` function, You should pass explicit value. If type of given parameters are not the same with info of binded function, It will throw std::bad_any_cast.
- The binded function must uses `decltype(nullptr)` instead of void at return type('Cause we can't covert void type into std::any) and return nullptr.