containers#

namespace heph
namespace containers#
template<UnsignedEnum Enum>
class BitFlag#
#include “hephaestus/containers/bit_flag.h”

This class allows to use enum classes as bit flags. Enum classes need to satisfy three properties:

  • Underline type is unsigned (checked by template concept)

  • All values are power of 2 (checked by static assert)

  • No duplicated values (not checked, and impossible to until we get reflection) Usage: enum class Flag : uint8_t { A = 1u << 1u, B = 1u << 2u, C = 1u << 3u }; BitFlag<Flag> flag{ Flag::A }; flag.set(Flag::B).set(Flag::C); flag.has(Flag::B); // true flag.unset(Flag::A); Variable containing multiple flags can be created as constexpr: constexpr auto D = BitFlag<Flag>{ Flag::B }.set(Flag::C); // == Flag::B | Flag::C flag.hasAnyOf(D); // true

Note: This class uses magic_enum, which by default only supports enum values in the range -127..128. If your enum contains larger values, then ‘magic_enum::customize::enum_range<EnumT>’ needs to be implemented for that type. See Neargye/magic_enum for more details.

Public Types

using EnumT = Enum#
using T = std::underlying_type_t<EnumT>#

Public Functions

inline constexpr BitFlag()#
inline constexpr BitFlag(EnumT value)#
inline explicit constexpr BitFlag(T underlying_value)#

Constructs a BitFlag with the given underlying value.

An ‘Panic’ is thrown if there are bits set in ‘underlying_value’ which do not correspond to a valid enum value.

bool=default operator== (const BitFlag &) const
inline BitFlag &reset()#
inline BitFlag &set(BitFlag flag)#

Set the input flag(s)

inline BitFlag &unset(BitFlag flag)#

Unset the given flag(s)

inline BitFlag &set(BitFlag flag, bool value)#
inline bool has(BitFlag flag) const#

Returns true if the input flag is set. In case of multiple input flags, returns true if all input flags are set. has logically behaves as hasAll.

inline bool hasExactly(BitFlag flag) const#

Returns true if any of the input flags are set. In case of a single input flag, hasAny is equivalent to has.

inline bool hasAnyOf(BitFlag flag) const#

Returns true if any of the input flags are set.

inline T getUnderlyingValue() const#

Return the underlying type of the enum. This is to be used only for serialization.

namespace random#

Functions

template<containers::IsBitFlag T>
T random(std::mt19937_64 &mt)#
namespace heph
namespace containers
namespace concepts#
template<class T>
class BlockingQueue#
#include “hephaestus/containers/blocking_queue.h”

Queue that allows the consumer to block until new data is available, and immediately resume execution when new data is written in the queue.

Public Functions

inline explicit BlockingQueue(std::optional<std::size_t> max_size)#

Create a queue

Parameters:

max_size – Max number of concurrent element in the queue. If not specified, a queue with infinite length is created.

template<concepts::SimilarTo<T> U>
inline bool tryPush(U &&obj)#

Attempt to enqueue the data if there is space in the queue.

Note

This is safe to call from multiple threads.

Returns:

true if the new data is added to the queue, false otherwise.

template<concepts::SimilarTo<T> U>
inline std::optional<T> forcePush(U &&obj)#

Write the data to the queue. If no space is left in the queue, the oldest element is dropped.

Note

This is safe to call from multiple threads.

Parameters:

obj – The value to push into the queue.

Returns:

If the queue was full, the element dropped to make space for the new one.

template<concepts::SimilarTo<T> U>
inline void waitAndPush(U &&obj)#

Write the data to the queue. If no space is left in the queue, the function blocks until either space is freed or stop is called.

Note

This is safe to call from multiple threads.

template<typename ...Args>
inline bool tryEmplace(Args&&... args)#

Attempt to enqueue the data if there is space in the queue. Support constructing a new element in-place.

Note

This is safe to call from multiple threads.

Returns:

true if the new data is added to the queue, false otherwise.

template<typename ...Args>
inline std::optional<T> forceEmplace(Args&&... args)#

Write the data to the queue. If no space is left in the queue, the oldest element is dropped. Support constructing a new element in-place.

Note

This is safe to call from multiple threads.

Returns:

If the queue was full, the element dropped to make space for the new one.

template<typename ...Args>
inline void waitAndEmplace(Args&&... args)#

Write the data to the queue. If no space is left in the queue, the function blocks until either space is freed or stop is called. Support constructing a new element in-place.

Note

This is safe to call from multiple threads.

inline std::optional<T> waitAndPop()#

Pop data from the queue, if data is present the function returns immediately, otherwise it blocks waiting for new data, or the stop signal is set.

Note

This is safe to call from multiple threads.

Returns:

The first element from the queue if the queue contains data, std::nullopt otherwise.

inline std::deque<T> waitAndPopAll()#
inline std::optional<T> tryPop()#

Tries to pop data from the queue, if no data is present it returns nothing.

Note

This is safe to call from multiple threads.

Returns:

The first element from the queue if the queue contains data, std::nullopt otherwise.

inline void stop()#

Stop the queue, waking up all blocked consumers.

Note

This is safe to call from multiple threads.

inline void restart()#
inline std::size_t size() const#
inline bool empty() const#
namespace heph
namespace containers
template<IntrusiveFifoQueueElement T>
class IntrusiveFifoQueue#

Public Functions

inline bool empty() const#
inline std::size_t size() const#
inline auto enqueue(T *t)#
inline T *dequeue()#
inline auto erase(T *t)#
struct IntrusiveFifoQueueAccess#

Public Static Functions

template<typename T>
static inline T *&next(T *t)#
template<typename T>
static inline T *&next(T *t)
template<typename T>
static inline T *&prev(T *t)#
template<typename T>
static inline T *&prev(T *t)