https://github.com/htfy96/policycb
Policy-based C++ Callback implementation
https://github.com/htfy96/policycb
Last synced: about 1 month ago
JSON representation
Policy-based C++ Callback implementation
- Host: GitHub
- URL: https://github.com/htfy96/policycb
- Owner: htfy96
- License: apache-2.0
- Created: 2024-02-19T23:10:01.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2024-02-20T01:08:43.000Z (over 2 years ago)
- Last Synced: 2025-01-13T18:52:21.562Z (over 1 year ago)
- Language: C++
- Size: 15.6 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# PolicyCB
PolicyCB is a C++20 library that provides a flexbible Callback implementation. This is a highly experimental implementation and shouldn't be use in any production environment. View the associated [Blog post](https://intmainreturn0.com/notes/callback-design.html) for more details. Users may configure the behavior of the class via the below template parameters:
```cpp
// The policy on allowed callable and Callback itself
// Note that when none of them is DYNAMIC, Callback<> could
// utilize flattened function pointer to save a virtual call
// @{
enum class MovePolicy
{
// Allows non-trivially movable object
DYNAMIC = 0,
// Only allows trivially movable object
TRIVIAL_ONLY = 1,
// Forbids any move on Callback
NOMOVE = 2,
};
enum class CopyPolicy
{
// Allows non-trivially copyable object
DYNAMIC = 0,
// Only allows trivially copyable object
TRIVIAL_ONLY = 1,
// Forbids any copy on Callback
NOCOPY = 2,
};
enum class DestroyPolicy
{
// Allows non-trivially destructable object
DYNAMIC = 0,
// Only allows trivially destructable object
TRIVIAL_ONLY = 1,
};
// @}
// Policy on the small-buffer-optimization storage
enum class SBOPolicy
{
// Allows the Callback to store arbitrary-sized object
// The storage takes InitialBufferSize + 8 bytes
DYNAMIC_GROWTH = 0,
// Only allows the Callback to store a object with specified
// maximum size. Causes an compilation error if the object is
// too large.
FIXED_SIZE = 1,
// This disables storage of the original function,
// essentially makes the Callback a function pointer
NO_STORAGE = 2,
};
template
class Callback;
```
This is a header-only library. Drop in `include/PolicyCB.hpp` into your project to use it.
## License
Apache 2.0