BRICKS
Small, useful blocks of code, to build bigger things.
Loading...
Searching...
No Matches
bricks Namespace Reference

Classes

class  bad_result_access
 This is the type of the error thrown when accessing a bad result. More...
 
struct  deleter
 A deleter is a function that deletes a pointer. More...
 
struct  has_find
 Checks if a type has a find method taking a specific type. More...
 
struct  is_iterator
 Checks if a type is an iterator. More...
 
class  mutex
 A mutual exclusion primitive that can be used to protect shared data. More...
 
class  result
 A class to represent the result of an operation. More...
 
class  rw_lock
 A reader-writer lock. More...
 
class  timer
 A timer that can be started and aborted. More...
 

Typedefs

template<typename T , auto fn>
using handle = std::unique_ptr<T, deleter<T, fn>>
 A handle is a unique_ptr with a custom deleter.
 
template<typename T >
using ok = detail::value_container<T, detail::ok_tag>
 
template<typename E >
using err = detail::value_container<E, detail::err_tag>
 

Functions

template<class Container >
auto keys (const Container &input_map) -> std::vector< typename Container::key_type >
 Get the keys of an associative container.
 
template<class Container >
auto values (const Container &input_map) -> std::vector< typename Container::mapped_type >
 Get the values of an associative container.
 
template<typename F , typename... FrontArgs>
constexpr auto bind_front (F &&f, FrontArgs &&... front_args)
 Bind arguments to the front of a function.
 
template<class Container , class Value >
constexpr auto contains (const Container &container, const Value &value) noexcept -> bool
 Checks whether a container contains a specific value.
 
template<class Container , class UnaryPredicate >
constexpr auto contains_if (const Container &container, const UnaryPredicate &predicate) -> bool
 Checks if a container contains a value that satisfies a predicate.
 
template<class Container , class Value >
constexpr auto index_of (const Container &container, const Value &value) noexcept -> std::optional< size_t >
 Get the index of the first occurence of a value in a container.
 
template<class Container , class UnaryPredicate >
constexpr auto index_of_if (const Container &container, const UnaryPredicate &predicate) -> std::optional< size_t >
 Get the index of the first element in a container for which a predicate is true.
 
template<class T , class Rep , class Period >
auto is_ready_after (const std::future< T > &future, const std::chrono::duration< Rep, Period > &timeout) noexcept -> bool
 Check whether a future is ready after a timeout.
 
template<class T , class Clock , class Duration >
auto is_ready_at (const std::future< T > &future, const std::chrono::time_point< Clock, Duration > &timeout) noexcept -> bool
 Check whether a future is ready at a specific time.
 
template<typename T >
auto to_string (const T &value, std::size_t buffer_size=(std::numeric_limits< T >::digits10+2)) noexcept -> result< std::string, std::errc >
 Exceptionlessly convert a number to a string.
 
template<typename T >
auto from_string (const std::string_view &str) noexcept -> result< T, std::errc >
 Exceptionlessly convert a string to a number.
 
template<typename Range >
auto enumerate (Range &range)
 Enumerate a range.
 
template<typename Range , typename UnaryPredicate >
auto filter (Range &range, UnaryPredicate predicate) -> detail::filterer< Range, UnaryPredicate >
 Filter a range.
 
template<typename Range >
auto reverse (Range &range)
 Create a reverse iterator from a range.
 
template<typename... Ranges>
auto zip (Ranges &&... t)
 A zip iterator adapter.
 

Variables

template<class >
constexpr bool always_false_v = false
 Helper variable template for static assertions.
 
template<typename T >
constexpr bool is_iterator_v = is_iterator<T>::value
 Helper variable template to check if a type is an iterator.
 

Typedef Documentation

◆ err

template<typename E >
using bricks::err = detail::value_container<E, detail::err_tag>

◆ handle

template<typename T , auto fn>
using bricks::handle = std::unique_ptr<T, deleter<T, fn>>

A handle is a unique_ptr with a custom deleter.

Example:

auto f = file{fopen("test.txt", "w")};
// f is automatically closed when it goes out of scope
Template Parameters
TThe type of the handle.
fnThe deleter function.

◆ ok

template<typename T >
using bricks::ok = detail::value_container<T, detail::ok_tag>

Function Documentation

◆ bind_front()

template<typename F , typename... FrontArgs>
auto bricks::bind_front ( F && f,
FrontArgs &&... front_args )
constexpr

Bind arguments to the front of a function.

C++17 implementation of the C++20 std::bind_front(...) function.

Example:

auto add = [](int a, int b) { return a + b; };
auto add_1 = bricks::bind_front(add, 1);
INFO(add_1(2)); // prints 3
Parameters
fThe function to bind front arguments to.
front_argsThe front arguments to bind.

◆ contains()

template<class Container , class Value >
auto bricks::contains ( const Container & container,
const Value & value ) -> bool
constexprnoexcept

Checks whether a container contains a specific value.

If the container has a find method, it will be used to check if the container contains the value. Otherwise, the container will be searched using std::find. If std::find is used, the container must have a begin and end method. If the container has a find method, the container must have a key_type type alias. If std::find throws an exception, std::terminate will be called.

Example:

std::vector<int> vec = {1, 2, 3, 4, 5};
INFO(bricks::contains(vec, 3)); // prints true
INFO(bricks::contains(vec, 6)); // prints false
Template Parameters
ContainerThe type of the container.
ValueThe type of the value.
Parameters
containerThe container.
valueThe value to check for.
Returns
true If the container contains the value, false otherwise.

◆ contains_if()

template<class Container , class UnaryPredicate >
auto bricks::contains_if ( const Container & container,
const UnaryPredicate & predicate ) -> bool
constexpr

Checks if a container contains a value that satisfies a predicate.

Example:

std::vector<int> vec = {1, 2, 3, 4, 5};
INFO(bricks::contains_if(vec, [](int i) { return i % 2 == 0; })); // prints true
Template Parameters
ContainerThe type of the container.
UnaryPredicateThe type of the predicate.
Parameters
containerThe container.
predicateThe predicate.
Returns
true If the container contains a value that satisfies the predicate, false otherwise.

◆ enumerate()

template<typename Range >
auto bricks::enumerate ( Range & range)

Enumerate a range.

This function creates an iterator adapter that enumerates the values of a range. The range can be any type that has a begin and end function. Each element in the enumerator is a pair of the index and the value.

Example:

std::vector<int> v{1, 2, 3, 4, 5};
for (auto [i, e] : bricks::enumerate(v)) {
auto idx = i;
auto elem = e;
INFO(idx, ": ", elem); // prints 0: 1, 1: 2, 2: 3, 3: 4, 4: 5
}
Template Parameters
RangeThe type of range to enumerate.
Parameters
rangeThe range to enumerate.
Returns
auto An enumerator.

◆ filter()

template<typename Range , typename UnaryPredicate >
auto bricks::filter ( Range & range,
UnaryPredicate predicate ) -> detail::filterer<Range, UnaryPredicate>

Filter a range.

This function creates an iterator adapter that filters the values of a range. The range can be any type that has a begin and end function. Each element in the iterator adapter is a value that satisfies the predicate.

Example:

std::vector<int> v{1, 2, 3, 4, 5};
auto filtered = bricks::filter(v, [](int i) { return i % 2 == 0; });
int expected = 2;
for (auto i : filtered) {
CHECK(i == expected);
expected += 2;
}
Parameters
rangeThe range to filter.
predicateThe predicate to filter with.
Returns
auto The iterator adapter.

◆ from_string()

template<typename T >
auto bricks::from_string ( const std::string_view & str) -> result<T, std::errc>
noexcept

Exceptionlessly convert a string to a number.

This function is a wrapper around std::from_chars and will return the same error codes. It, however, will return std::errc::invalid_argument if the string contains characters that are not part of the number.

Example:

auto value = bricks::from_string<int>("42");
REQUIRE(value.is_value());
CHECK(value.unwrap() == 42);
Template Parameters
TThe type of number to convert.
Parameters
strThe string to convert.
Returns
result<T, std::errc> Result of the conversion. Will contain the error code if the conversion failed.

◆ index_of()

template<class Container , class Value >
auto bricks::index_of ( const Container & container,
const Value & value ) -> std::optional<size_t>
constexprnoexcept

Get the index of the first occurence of a value in a container.

If the container has a find method, it will be used to get the index of the value. Otherwise, the container will be searched using std::find. If std::find is used, the container must have a begin and end method. If the container has a find method, the container must have a key_type type alias. If std::find throws an exception, std::terminate will be called.

Example:

std::vector<int> vec = {1, 2, 3, 4, 5};
INFO(bricks::index_of(vec, 3)); // prints 2
Template Parameters
ContainerThe type of the container.
ValueThe type of value to search for.
Parameters
containerThe container.
valueThe value to get the index of.
Returns
constexpr std::optional<size_t> The index of the value, if it exists.

◆ index_of_if()

template<class Container , class UnaryPredicate >
auto bricks::index_of_if ( const Container & container,
const UnaryPredicate & predicate ) -> std::optional<size_t>
constexpr

Get the index of the first element in a container for which a predicate is true.

Example:

std::vector<int> vec = {1, 2, 3, 4, 5};
INFO(bricks::index_of_if(vec, [](int i) { return i % 2 == 0; })); // prints 1
Template Parameters
ContainerThe type of the container.
UnaryPredicateThe type of the predicate.
Parameters
containerThe container.
predicateThe predicate.
Returns
constexpr std::optional<size_t> The index of the value, if it exists.

◆ is_ready_after()

template<class T , class Rep , class Period >
auto bricks::is_ready_after ( const std::future< T > & future,
const std::chrono::duration< Rep, Period > & timeout ) -> bool
noexcept

Check whether a future is ready after a timeout.

Example:

auto future = std::async(std::launch::async, []() {
std::this_thread::sleep_for(std::chrono::milliseconds(5));
return 42;
});
// Will print "Future is ready!" after 10 milliseconds.
if (bricks::is_ready_after(future, std::chrono::seconds(10))) {
INFO("Future is ready!");
}
Template Parameters
TThe type of the future.
Parameters
futureThe future.
timeoutThe timeout.
Returns
true If the future is ready, false otherwise.

◆ is_ready_at()

template<class T , class Clock , class Duration >
auto bricks::is_ready_at ( const std::future< T > & future,
const std::chrono::time_point< Clock, Duration > & timeout ) -> bool
noexcept

Check whether a future is ready at a specific time.

Example:

auto future = std::async(std::launch::async, []() {
std::this_thread::sleep_for(std::chrono::milliseconds(5));
return 42;
});
// Will print "Future is ready!" after 10 milliseconds.
if (bricks::is_ready_at(future, std::chrono::steady_clock::now() + std::chrono::seconds(10))) {
INFO("Future is ready!");
}
Template Parameters
TThe type of the future.
Parameters
futureThe future.
timeoutThe timeout.
Returns
true If the future is ready, false otherwise.

◆ keys()

template<class Container >
auto bricks::keys ( const Container & input_map) -> std::vector<typename Container::key_type>

Get the keys of an associative container.

Example:

std::map<std::string, int> map = {{"a", 1}, {"b", 2}, {"c", 3}};
auto keys = bricks::keys(map);
for (auto key : keys) {
INFO(key); // prints "a", "b", "c"
}
Template Parameters
ContainerThe type of the container.
Parameters
input_mapThe container.
Returns
std::vector<typename Container::key_type> The keys.

◆ reverse()

template<typename Range >
auto bricks::reverse ( Range & range)

Create a reverse iterator from a range.

This function creates a reverse iterator from a range. The range can be any type that has a rbegin and rend function.

Example:

std::vector<int> v{1, 2, 3, 4, 5};
for (auto e : bricks::reverse(v)) {
INFO(e); // prints 5, 4, 3, 2, 1
}
Template Parameters
RangeThe type of range to reverse.
Parameters
rangeThe range to reverse.
Returns
auto A reverse iterator.

◆ to_string()

template<typename T >
auto bricks::to_string ( const T & value,
std::size_t buffer_size = (std::numeric_limits<T>::digits10 + 2) ) -> result<std::string, std::errc>
noexcept

Exceptionlessly convert a number to a string.

This function is a wrapper around std::to_chars and will return the same error codes.

Example:

auto output = bricks::to_string(42);
REQUIRE(output.is_value());
CHECK(output.unwrap() == "42");
Template Parameters
TThe type of number to convert.
Parameters
valueThe number to convert.
buffer_sizeThe size of the buffer to use for the conversion. If the buffer is too small the conversion will fail with std::errc::value_too_large. Can be omitted if the type is an integral type.
Returns
result<std::string, std::errc> Result of the conversion. Will contain the error code if the conversion failed.

◆ values()

template<class Container >
auto bricks::values ( const Container & input_map) -> std::vector<typename Container::mapped_type>

Get the values of an associative container.

Example:

std::map<std::string, int> map = {{"a", 1}, {"b", 2}, {"c", 3}};
auto values = bricks::values(map);
for (auto value : values) {
INFO(value); // prints 1, 2, 3
}
Template Parameters
ContainerThe type of the container.
Parameters
input_mapThe container.
Returns
std::vector<typename Container::mapped_type> The values.

◆ zip()

template<typename... Ranges>
auto bricks::zip ( Ranges &&... t)

A zip iterator adapter.

This iterator allows to iterate over multiple containers at the same time. It is similar to the zip function in python. The iterator will iterate over the shortest container. The iterator will dereference to a tuple of references to the elements of the containers.

Example:

std::vector<int> a{1, 2, 3};
std::vector<int> b{4, 5, 6};
for (auto [x, y] : bricks::zip(a, b)) {
auto sum = x + y;
INFO(sum); // prints 5, 7, 9
}

Variable Documentation

◆ always_false_v

template<class >
bool bricks::always_false_v = false
inlineconstexpr

Helper variable template for static assertions.

◆ is_iterator_v

template<typename T >
bool bricks::is_iterator_v = is_iterator<T>::value
inlineconstexpr

Helper variable template to check if a type is an iterator.

Example:

static_assert(bricks::is_iterator_v<std::vector<int>::iterator>);
static_assert(bricks::is_iterator_v<std::vector<int>::const_iterator>);
constexpr bool is_iterator_v
Helper variable template to check if a type is an iterator.
Definition type_traits.hpp:80
Template Parameters
T