very simple single header-only c++ library for simple reading or writing flags on the youngest unused bits of pointer.
#include <string>
#include <tagged_ptr/tagged_ptr.hpp>
int main(int argc, char** argv) {
using namespace eggman79;
enum class Color {Red, Green, Blue};
auto str = make_tagged_ptr<std::string, true, tag<Color, 2>, tag<bool, 1>>("string");
str.set_tag<0>(Color::Blue);
str.set_tag<1>(true);
if (*str == "string" && str.get_tag<0>() == Color::Blue && str.get_tag<1>()) {
std::cout << "ok" << std::endl;
}
}
git clone https://github.com/eggman79/tagged_ptr
cd tagged_ptr
git submodule update --init
mkdir build
cd build
cmake .. -DTAGGED_PTR_BUILD_TESTS
make
make test
tagged_ptr.hpp
is the single required file. You need to add
#include <tagged_ptr/tagged_ptr.hpp>
using namespace eggman79;
You can also use the tagged_ptr::tagged_ptr
interface target in CMake. This target populates the appropriate usage requirements for INTERFACE_INCLUDE_DIRECTORIES
to point to the appropriate include directories and INTERFACE_COMPILE_FEATURES
for the necessary C++20 flags.
Build and install library:
git clone https://github.com/eggman79/tagged_ptr
cd tagged_ptr
mkdir build
cd build
cmake ..
make
make install # if you are not su then: sudo make install
To use this library from a CMake project, you can locate it directly with find_package()
and use the namespaced imported target from the generated package configuration:
# CMakeLists.txt
find_package(tagged_ptr REQUIRED)
...
add_library(foo ...)
...
target_link_libraries(foo PRIVATE tagged_ptr::tagged_ptr)
To embed the library directly into an existing CMake project:
mkdir thirdparty # or another arbitrary folder name
git submodule add https://github.com/eggman79/tagged_ptr thirdparty/tagged_ptr
Call add_subdirectory() and target_link_libraries() in your CMakeLists.txt file:
add_subdirectory(thirdparty/tagged_ptr)
...
add_library(foo ...)
...
target_link_libraries(foo PRIVATE tagged_ptr::tagged_ptr)