beman.copyable_function
beman.copyable_function: A Beman Library Implementation of copyable_function
beman.copyable_function is a type-erased function wrapper that can represent any copyable callable matching the function signature R(Args…). The library conforms to the The Beman Standard.
Implements: copyable_function (P2548)
Usage
The following code snippet illustrates copyable_function:
#include <beman/copyable_function/copyable_function.hpp>
// a Callable object
struct Callable {
int operator()() { return 42; }
int operator()() const noexcept { return 43; }
};
int main()
{
beman::copyable_function<int()> f(Callable{});
int x = f();
return 0;
}
Full runnable examples can be found in examples/.
Dependencies
Build Environment
This project requires at least the following to build:
-
A C compiler that conforms to the C17 standard or greater
-
CMake 3.30 or later
-
(Test Only) GoogleTest
You can disable building tests by setting CMake option BEMAN_COPYABLE_FUNCTION_BUILD_TESTS to OFF when configuring the project.
Development
See the Contributing Guidelines.
Integrate beman.copyable_function into your project
Build
You can build copyable_function using a CMake workflow preset:
cmake --workflow --preset gcc-release
To list available workflow presets, you can invoke:
cmake --list-presets=workflow
For details on building beman.copyable_function without using a CMake preset, refer to the Contributing Guidelines.
Installation
Vcpkg
The preferred way to install copyable_function is via vcpkg. To do so, after installing vcpkg itself, you need to add support for the Beman project’s vcpkg registry by configuring a vcpkg-configuration.json file (which copyable_function provides).
Then, simply run vcpkg install beman-copyable-function.
Manual
To install beman.copyable_function globally after building with the gcc-release preset, you can run:
sudo cmake --install build/gcc-release
Alternatively, to install to a prefix, for example /opt/beman, you can run:
sudo cmake --install build/gcc-release --prefix /opt/beman
This will generate the following directory structure:
/opt/beman
├── include
│ └── beman
│ └── copyable_function
│ ├── copyable_function.hpp
│ └── ...
└── lib
└── cmake
└── beman.copyable_function
├── beman.copyable_function-config-version.cmake
├── beman.copyable_function-config.cmake
└── beman.copyable_function-targets.cmake
CMake Configuration
If you installed beman.copyable_function to a prefix, you can specify that prefix to your CMake project using CMAKE_PREFIX_PATH; for example, -DCMAKE_PREFIX_PATH=/opt/beman.
You need to bring in the beman.copyable_function package to define the beman::copyable_function CMake target:
find_package(beman.copyable_function REQUIRED)
You will then need to add beman::copyable_function to the link libraries of any libraries or executables that include beman.copyable_function headers.
target_link_libraries(yourlib PUBLIC beman::copyable_function)
Contributing
Please do! You encourage you to checkout our contributor’s guide. Issues and pull requests are appreciated.