Various useful bits of C++
This is header only C++ library for doing useful things. Such as printing human
readable type names, converting string_views to numbers. See the Currently
available targets section for detail.
All files are in individual directories under include.
Documentation can be found at the top of each file.
The easiest way to include libut in your CMake project is with
fetch_content.
Simply add the following to your CMakeLists.txt:
include(FetchContent)
# Set the directory to download dependencies to. Avoids putting them in $buildDir
set(FETCHCONTENT_BASE_DIR "${PROJECT_SOURCE_DIR}/_deps")
FetchContent_Declare(
libut
GIT_REPOSITORY https://github.com/dk949/libut/
GIT_TAG trunk # alternatively you can use a hash to pin exact venison to use
)
FetchContent_MakeAvailable(libut)
# assuming MY_TARGET is a valid target (executable or library)
target_link_libraries(MY_TARGET ut::target_name)ut::ptr_containers: containers for storing owning pointersut::check: type which prints when it is constructed/copied/moved/destructedut::pair: a betterstd::pairimplementationut::switchboard: composable and type-safe CLI parserut::err: convenient error type for use with std::expectedut::add_noexcept: add thenoexceptspecifier to a function typeut::change_observer: wrapper class that invokes callbacks when it's held value changesut::traced_error: an exception type that automatically stores a C++23 standard stacktraceut::sv_to_num: convertstd::string_viewto a numberut::copy_traits: copy cvref qualifiers from one type to anotherut::realloc_unique_ptr:reallocfunctionality forstd::uniqur_ptrut::assert: various assertion macrosut::resource: a more general version ofstd::unique_ptrthat operates on valuesut::demangle: convert types into human readable stringsut::constexpr_hash: a hashing function usable inconstexprcontextut::trim: trim characters from start and end of astd::string_viewut::pack_loops: loop over variadic template parameters (either values or types)ut::asis: Provides a more convenient way to work with different kinds of dynamically polymorphic values.ut::spawn: simple process spawning (currently Linux only)ut::static_string: compile time known string usable as a template parameterut::breakpoint: Portable(ish) breakpoint macrosut::mt_queue: thread safe FIFO queueut::curry: create a curried function out of a regular functionut::defer: a macro to defer execution until the scope endsut::print: macros for printing usingstd::formatin c++20ut::overload: creates a function object merging several other function objects as an overload set
A special target ut::ut is also available, combining all of the above.
If you prefer, you can install the library and import it into a project via
find_package like so:
git clone github.com/dk949/libut
cd libut
# use cmake --list-presets for a list of available presets
cmake --preset local -DCMAKE_INSTALL_PREFIX=./libut-install
cmake --install buildThen in your cmake file:
set(ENV{ut_DIR} /path/to/libut/libut-install)
find_package(ut REQUIRED)
target_link_libraries(MY_TARGET ut::ut_target_name)Note
When using find_package, all target names above (except ut::ut) are prefixed
withut_.
The prefixed targets are also available when using FetchContent, so for
a config that could use either, use the prefixed targets.
Test can be ran using Catch2
git clone https://github.com/Microsoft/vcpkg.git
./vcpkg/bootstrap-vcpkg.sh -disableMetrics
./vcpkg/vcpkg install catch2
cmake --preset local
cmake --build build
./build/tests/libut_tests
# or
ctest --output-on-failure --test-dir build/testsNOTE: you can use the local preset to build with
ninja, or default to build with the default
generator (e.g. "Unix Makefiles" on Linux)
The files included as gists, the license is included in the file (usually at the
bottom). For all other files, see LICENSE in the root of this repo.