enum_set/0.1.0-a.1

[brief]

A library of type safe sets over fixed size collections of types or values, including methods for accessing, modifying, visiting and iterating over those.

Build status Code coverage GitHub release License

A library of type safe sets over fixed size collections of types or values, including methods for accessing, modifying, visiting and iterating over those.

Rationale

Enums are great, as long as you use them to represent individual flags or options. If you want to represent a set of options using an enumeration, one would typically let the values of the enumeration take on powers of two and then "or" them together

enum class option
{
    A = (1 << 0),
    B = (1 << 1),
    C = (1 << 2)
};

int operator|(option lhs, option rhs)
{
    return static_cast<int>(lhs) | static_cast<int>(rhs);
}

The problem with this is obvious: when using an integer representation for a set of options, we lose type information. Instead, one would like to do something like this

#include <enum_set/enum_set.hpp>

enum class option
{
    A,
    B,
    C
};

using option_set =
    enum_set::make_enum_set<
        option,
        option::C
    >;

option_set operator|(option lhs, option rhs)
{
    return {lhs, rhs};
}

where a type alias option_set is introduced to represent a set of values from the option enumeration.

This library provides means of doing this for various types of enumerated collections.

Requirements

enum_set is a zero dependency (except from standard library) header only library.

Requires at least C++14, and C++17 if you want to use it together with magic enum.

Installation

There are two ways of installing this library

  1. Poor mans installation: Just copy the contents of the include folder to your installation path.
  2. Using CMake: see BUILDING for detailed installation instructions.

Usage

The library provides a type_set class for holding a set of types and value_set class for holding a set of values.

If you roll with strong enums (that is, struct with class tags), use the type_set class (defined in <enum_set/type_set.hpp>).

If you want to model a set over a fixed collection of values, use the value_set class (defined in <enum_set/value_set.hpp>). The value_set is a refinement of the type_set class that replaces type like semantics with value like. Thus, if type_set provides a template method that accepts a type, value_set provides the a method with the same name accepting a value (of the given type) instead. In addition to the capabilities provided by type_set, value_set also provides support for iteration (there is support for visitation which is more safe and also works for type_set).

The library provides type aliases integer_set (defined in <enum_set/integer_set.hpp>) and index_set (defined in <enum_set<index_set.hpp>) for a value_set of integers or indices (a.k.a. std::size_t) respectively. Meta functions make_integer_set and make_index_set for creating sets over contiguous ranges of integers or indices are also provided (mimicks the style used by STL's std::make_integer_sequence and std::make_index_sequence).

Finally, if you use enums (drumroll...), you can use the make_enum_set meta function (defined in <enum_set/enum_set.hpp>). This assumes that you do not set the enumeration values manually (e.g. as powers of two), Also, as seen in the rationale example in the beginning, note that you have to explicitly provide the last enumeration value (until C++ have better built in reflection capabilities for enums). If you can use C++17, are willing to sacrifice standardness and to introduce another dependency, integration with magic enum is provided and you can create a value_set conveniently using the make_magic_enum_set meta function (defined in <enum_set/magic/magic_enum_set.hpp>) as follows

#include <enum_set/magic/magic_enum_set.hpp>

enum class option
{
    A,
    B,
    C
};

using option_set =
    enum_set::make_magic_enum_set<
        option
    >;

See this example for a tutorial on available methods and operators.

See this example for an illustration of the visitor pattern with type_set.

Reference documentation can be found here.

Contributing

Feel free to grab an issue from the issue list. Make a fork and submit a PR. We currently don't have any dedicated forum for discussion, if you have any ideas on how to improve the library and there is no issue for it, feel free to submit an issue yourself and we can discuss the idea there.

See HACKING for detailed instructions on setting up the developer environment.

See the Dockerfile for a minimal reproducible environment. Running the instructions given by the other documents inside the built container should work. The Dockerfile should, but is not guaranteed to, reflect the steps used by the CI. To see the exact steps used by the CI, see the GitHub workflow config file.

version 0.1.0-a.1
license MITMIT License
repository https://pkg.cppget.org/1/alpha
download enum_set-0.1.0-a.1.tar.gz
sha256 ae8f27819e04c14b9f37411863eaec366f629ef58df4618c27235d1d331a6562
project enum_set
url github.com/cdeln/cpp_enum_set
src-url github.com/cdeln/cpp_enum_set
topics enum setstype setstype safetyfixed size containers

Depends (2)

doctest ^2.3.6For testing
magic_enum ^0.7.2For magic enum integration

Requires (1)

c++ >= 17

Reviews

fail 0
pass 1

Builds

toolchain public-0.17.0
target x86_64-linux-gnu
tgt config linux_debian_12-clang_17_libc++
timestamp 2025-08-23 18:23:37 UTC (26:19 minutes ago)
result success | log | rebuild
toolchain public-0.17.0
target x86_64-linux-gnu
tgt config linux_debian_12-clang_17
timestamp 2025-08-23 18:16:11 UTC (33:45 minutes ago)
result success | log | rebuild
toolchain public-0.17.0
target x86_64-linux-gnu
tgt config linux_debian_12-gcc_13.1
timestamp 2025-08-23 18:15:54 UTC (34:01 minutes ago)
result success | log | rebuild
toolchain public-0.17.0
target x86_64-freebsd14.1
tgt config freebsd_14-clang_18
timestamp 2025-08-23 18:14:09 UTC (35:46 minutes ago)
result success | log | rebuild
toolchain public-0.17.0
target aarch64-linux-gnu
tgt config linux_debian_12-gcc_13
timestamp 2025-08-23 13:00:53 UTC (05:49:03 hours ago)
result success | log | rebuild
toolchain public-0.17.0
target aarch64-linux-gnu
tgt config linux_debian_12-clang_17
timestamp 2025-08-23 13:00:08 UTC (05:49:48 hours ago)
result success | log | rebuild
toolchain public-0.17.0
target aarch64-linux-gnu
tgt config linux_debian_12-clang_17_libc++
timestamp 2025-08-23 12:56:49 UTC (05:53:07 hours ago)
result success | log | rebuild
toolchain public-0.17.0
target aarch64-linux-gnu
tgt config linux_debian_12-gcc_14-static_O3
timestamp 2025-08-22 16:24:04 UTC (01 02:25:52 days ago)
result success | log | rebuild
toolchain public-0.17.0
target aarch64-linux-gnu
tgt config linux_debian_12-gcc_14-ndebug_O3
timestamp 2025-08-22 16:20:40 UTC (01 02:29:15 days ago)
result success | log | rebuild
toolchain public-0.17.0
target aarch64-linux-gnu
tgt config linux_debian_12-gcc_14-O3
timestamp 2025-08-22 16:19:28 UTC (01 02:30:27 days ago)
result success | log | rebuild
toolchain public-0.17.0
target aarch64-linux-gnu
tgt config linux_debian_12-gcc_14
timestamp 2025-08-22 16:16:39 UTC (01 02:33:17 days ago)
result success | log | rebuild
toolchain public-0.17.0
target x86_64-microsoft-win32-msvc14.3
tgt config windows_10-clang_18_llvm_msvc_17.10-O2
timestamp 2025-08-21 11:25:43 UTC (02 07:24:13 days ago)
result error (update) | log | rebuild
toolchain public-0.17.0
target x86_64-microsoft-win32-msvc14.3
tgt config windows_10-clang_18_llvm_msvc_17.10
timestamp 2025-08-21 11:23:27 UTC (02 07:26:29 days ago)
result error (update) | log | rebuild
toolchain public-0.17.0
target x86_64-microsoft-win32-msvc14.3
tgt config windows_10-clang_18_llvm_msvc_17.10-static_O2
timestamp 2025-08-21 11:20:56 UTC (02 07:29:00 days ago)
result error (update) | log | rebuild
toolchain public-0.17.0
target x86_64-microsoft-win32-msvc14.3
tgt config windows_10-clang_17_msvc_msvc_17.10
timestamp 2025-08-21 11:08:33 UTC (02 07:41:23 days ago)
result error (update) | log | rebuild
toolchain public-0.17.0
target x86_64-linux-gnu
tgt config linux_debian_12-gcc_14-static_O3
timestamp 2025-08-21 05:52:29 UTC (02 12:57:27 days ago)
result success | log | rebuild
toolchain public-0.17.0
target x86_64-linux-gnu
tgt config linux_debian_12-gcc_14-O3
timestamp 2025-08-21 05:40:06 UTC (02 13:09:50 days ago)
result success | log | rebuild
toolchain public-0.17.0
target x86_64-linux-gnu
tgt config linux_debian_12-gcc_14-ndebug_O3
timestamp 2025-08-21 05:37:03 UTC (02 13:12:52 days ago)
result success | log | rebuild
toolchain public-0.17.0
target x86_64-linux-gnu
tgt config linux_debian_12-gcc_14
timestamp 2025-08-21 05:36:12 UTC (02 13:13:43 days ago)
result success | log | rebuild
toolchain public-0.17.0
target x86_64-linux-gnu
tgt config linux_fedora_40-gcc_14-bindist
timestamp 2025-08-21 04:29:57 UTC (02 14:19:58 days ago)
result success | log | rebuild
toolchain public-0.17.0
target x86_64-w64-mingw32
tgt config windows_10-gcc_13.2_mingw_w64-static_O2
timestamp 2025-08-21 01:48:33 UTC (02 17:01:22 days ago)
result success | log | rebuild
toolchain public-0.17.0
target x86_64-w64-mingw32
tgt config windows_10-gcc_13.2_mingw_w64-O2
timestamp 2025-08-21 01:47:27 UTC (02 17:02:29 days ago)
result success | log | rebuild
toolchain public-0.17.0
target x86_64-w64-mingw32
tgt config windows_10-gcc_13.2_mingw_w64
timestamp 2025-08-21 01:46:14 UTC (02 17:03:42 days ago)
result success | log | rebuild
toolchain public-0.17.0
target x86_64-linux-gnu
tgt config linux_debian_12-gcc_12-bindist
timestamp 2025-08-21 01:31:25 UTC (02 17:18:30 days ago)
result success | log | rebuild
toolchain public-0.17.0
target x86_64-freebsd13.3
tgt config freebsd_13-clang_17
timestamp 2025-08-21 01:27:01 UTC (02 17:22:54 days ago)
result success | log | rebuild
toolchain public-0.17.0
target x86_64-linux-gnu
tgt config linux_ubuntu_24.04-gcc_13-bindist
timestamp 2025-08-21 01:08:25 UTC (02 17:41:30 days ago)
result success | log | rebuild
toolchain public-0.17.0
target x86_64-freebsd14.1
tgt config freebsd_14-clang_18-static_O3
timestamp 2025-08-21 01:03:10 UTC (02 17:46:45 days ago)
result success | log | rebuild
toolchain public-0.17.0
target x86_64-freebsd14.1
tgt config freebsd_14-clang_18-O3
timestamp 2025-08-21 00:58:12 UTC (02 17:51:44 days ago)
result success | log | rebuild
toolchain public-0.17.0
target aarch64-linux-gnu
tgt config linux_debian_12-clang_18_libc++-static_O3
timestamp 2025-08-20 22:27:39 UTC (02 20:22:17 days ago)
result success | log | rebuild
toolchain public-0.17.0
target aarch64-linux-gnu
tgt config linux_debian_12-clang_18_libc++-O3
timestamp 2025-08-20 22:23:47 UTC (02 20:26:09 days ago)
result success | log | rebuild
toolchain public-0.17.0
target aarch64-linux-gnu
tgt config linux_debian_12-clang_18_libc++
timestamp 2025-08-20 22:20:19 UTC (02 20:29:37 days ago)
result success | log | rebuild
toolchain public-0.17.0
target aarch64-linux-gnu
tgt config linux_debian_12-clang_18-O3
timestamp 2025-08-20 22:20:11 UTC (02 20:29:44 days ago)
result success | log | rebuild
toolchain public-0.17.0
target aarch64-linux-gnu
tgt config linux_debian_12-clang_18
timestamp 2025-08-20 22:19:39 UTC (02 20:30:16 days ago)
result success | log | rebuild
toolchain public-0.17.0
target aarch64-linux-gnu
tgt config linux_debian_12-clang_18-static_O3
timestamp 2025-08-20 22:19:26 UTC (02 20:30:29 days ago)
result success | log | rebuild
toolchain public-0.17.0
target x86_64-linux-gnu
tgt config linux_debian_12-clang_18
result unbuilt
toolchain public-0.17.0
target x86_64-linux-gnu
tgt config linux_debian_12-clang_18-O3
result unbuilt
toolchain public-0.17.0
target x86_64-linux-gnu
tgt config linux_debian_12-clang_18-static_O3
result unbuilt
toolchain public-0.17.0
target x86_64-linux-gnu
tgt config linux_debian_12-clang_18_libc++
result unbuilt
toolchain public-0.17.0
target x86_64-linux-gnu
tgt config linux_debian_12-clang_18_libc++-O3
result unbuilt
toolchain public-0.17.0
target x86_64-linux-gnu
tgt config linux_debian_12-clang_18_libc++-static_O3
result unbuilt
toolchain public-0.17.0
target x86_64-linux-gnu
tgt config linux_fedora_39-gcc_13-bindist
result unbuilt
toolchain public-0.17.0
target x86_64-apple-darwin22.5.0
tgt config macos_13-clang_15.0
result unbuilt
toolchain public-0.17.0
target x86_64-apple-darwin23.5.0
tgt config macos_14-clang_15.0
result unbuilt
toolchain public-0.17.0
target x86_64-apple-darwin23.5.0
tgt config macos_14-clang_15.0-O3
result unbuilt
toolchain public-0.17.0
target x86_64-apple-darwin23.5.0
tgt config macos_14-clang_15.0-static_O3
result unbuilt
toolchain public-0.17.0
target x86_64-apple-darwin23.5.0
tgt config macos_14-gcc_14_homebrew
result unbuilt
toolchain public-0.17.0
target x86_64-apple-darwin23.5.0
tgt config macos_14-gcc_14_homebrew-O3
result unbuilt
toolchain public-0.17.0
target x86_64-apple-darwin23.5.0
tgt config macos_14-gcc_14_homebrew-static_O3
result unbuilt
target x86_64-microsoft-win32-msvc14.3
tgt config windows_10-msvc_17.8
result excluded
target x86_64-microsoft-win32-msvc14.3
tgt config windows_10-msvc_17.8-O2
result excluded
target x86_64-microsoft-win32-msvc14.3
tgt config windows_10-msvc_17.8-static_O2
result excluded
target x86_64-microsoft-win32-msvc14.3
tgt config windows_10-msvc_17.10
result excluded
target x86_64-microsoft-win32-msvc14.3
tgt config windows_10-msvc_17.10-O2
result excluded
target x86_64-microsoft-win32-msvc14.3
tgt config windows_10-msvc_17.10-static_O2
result excluded