![]() |
Arene Base
Fundamental Utilities For Safety Critical C++
|
Arene Base provides backports of facilities from <utility> in newer C++ versions, along with other similar low-level facilities.
The public header is
The Bazel target is
The utility package provides the following basic facilities:
arene::base::is_aligned: check if an integer or pointer is aligned to the specified power of 2.arene::base::align_floor: adjust an integer to the nearest multiple of the specified alignment that is less than or equal to the supplied valuearene::base::align_ceil: adjust an integer to the nearest multiple of the specified alignment that is greater than or equal to the supplied valuearene::base::alignment_offset: determine the number of bytes by which a pointer would need to be adjusted to be aligned to the specified valuearene::base::bit_mask: create a mask value where the specified number of low bits are all 1arene::base::as_const(foo) returns a const reference to foo. This is a back-port of std::as_constarene::base::forward_like provides a backport of C++23's std::forward_like. This is useful in implementing deducing this style helpers, where the const and r/l-value reference qualifications of the Self parameter need to be propagated to the type of other values.
arene::base::in_place, arene::base::in_place_index and arene::base::in_place_type provide tags to indicate that types such as arene::base::optional should construct their stored value from the supplied arguments "in place", rather than attempting a copy or conversion.arene::base::make_subrange creates a range object that can be used with range-based for loops from an iterator pair:arene::base::swap provides a constexpr version of std::swap, which is also implemented as a Customization Point Object. As specified in the linked paper, doing so allows a few of advantages:
using arene::base::swap; before an unqualified swap call: Invoking arene::base::swap(T&,T&) will find ADL-specializations of swap(T&, T&), or fall back to the default implementation if no customizations are found.arene::base::is_swappable_v<T> trait is checked up front, rather than a user seeing a potentially confusing failure if ADL lookup fails and the type is not default-swappable.swap to be passed directly to algorithms as a function object.arene::base::to_underlying is a backport of std::to_underlying, for casting an enumeration to its underlying type.Backports of the C++20 safe integer comparison facilities are provided. These perform integer comparisons without performing any implicit conversions on the arguments, thereby avoiding issues such as negative values implicitly wrapping around to large positive values. For example, using raw comparison operators 0 < 5u == true as expected, but -1 < 5u == false. Using the equivalent safe comparison instead, arene::base::cmp_less(0, 5u) == true and arene::base::cmp_less(-1, 5u) == true as one would expect.
The available functions are as follows:
| utility | corresponding operator | C++20 stdlib equivalent |
|---|---|---|
arene::base::cmp_equal | operator== | std::cmp_equal |
arene::base::cmp_not_equal | operator!= | std::cmp_not_equal |
arene::base::cmp_less | operator< | std::cmp_less |
arene::base::cmp_greater | operator> | std::cmp_greater |
arene::base::cmp_less_equal | operator<= | std::cmp_less_equal |
arene::base::cmp_greater_equal | operator>= | std::cmp_greater_equal |