teslamotors / fixed-containers

C++ Fixed Containers
MIT License
352 stars 31 forks source link

C++ Fixed Containers

License Standard Ubuntu Ubuntu (clang) Windows CodeQL

Header-only C++20 library that provides containers with the following properties:

Features

Rich enum features

static_assert(fixed_containers::rich_enums::is_rich_enum<Color>);  // Type-trait `concept`
inline constexpr const Color& COLOR = Color::RED();                // Note the parens
static_assert("RED" == COLOR.to_string());                         // auto-provided member
static_assert(COLOR.is_primary());                                 // Custom member
static_assert(COLOR == Color::value_of("RED").value());            // auto-provided
static_assert(4 == Color::count());                                // auto-provided

More examples can be found here.

Examples

Integration

cmake

find_package(fixed_containers CONFIG REQUIRED)
target_link_libraries(<your_binary> fixed_containers::fixed_containers)

bazel

Use the following in your WORKSPACE file:

http_archive(
    name = "fixed_containers",
    urls = ["https://github.com/teslamotors/fixed-containers/archive/<commit>.tar.gz"],
    strip_prefix = "fixed-containers-<commit>",
)

load("@fixed_containers//:fixed_containers_deps.bzl", "fixed_containers_deps")
fixed_containers_deps()

Then use in your targets like this:

cc_test(
    name = "test",
    srcs = ["test.cpp"],
    deps = [
        "@fixed_containers//:fixed_vector",
        "@fixed_containers//:enum_map",
        "@fixed_containers//:enum_set",
    ],
    copts = ["-std=c++20"],
)

Alternative

Since this is a header-only library, you can also:

Running the tests

cmake

1) Build with the vcpkg toolchain file

mkdir build && cd build
cmake .. -DCMAKE_C_COMPILER=/bin/clang-13 -DCMAKE_CXX_COMPILER=/bin/clang++-13 -DCMAKE_TOOLCHAIN_FILE=/path/to/vcpkg/scripts/buildsystems/vcpkg.cmake
cmake --build .

2) Run tests

ctest -C Debug

bazel

clang

1) Build separately (optional)

CC=clang++-13 bazel build --config=clang ...

2) Run tests

CC=clang++-13 bazel test --config=clang :all_tests

gcc

1) Build separately (optional)

CC=g++-11 bazel build --config=gcc ...

2) Run tests

CC=g++-11 bazel test --config=gcc :all_tests

Tested Compilers

Licensed under the MIT License