This a tiny set of C++11 utility headers that bring minimal
implementations of useful concepts useful for general C++
development, developed as needed at Starship. Some of these
have analogues in later C++ standards, but most are modelled on
the more functional Rust equivalents, like common::result
and
common::optional
.
These are designed to be low to zero dependency, and minimal in implementation size, while enabling sane error handling sans exceptions.
Requires a C++11 conformant compiler and STL implementation. Currently uses some posix APIs directly, and is primarily tested on linux.
Tests (in the tests/
directory) require
Google Test.
result<size_t, unix_err> extract_key_values(const char* filename, std::function<void (string_view key, string_view value)>&& dispatch_key_value)
{
file_handle file;
std::string content;
return file
.open(filename)
.and_then([&] (ok) {
return file.read_all(content);
})
.map([&] (ok) {
size_t count{0};
string_view{content}.split_fn('\n', [&] (string_view line)
{
string_view key, value;
line.split_args('=', key, value);
dispatch_key_value(key, value);
count += 1;
});
return count;
});
}
optional<T>
: aT
ornone
, include reference support andmap
/and_then
result<T, Error>
: aT
orError
includingmap
/and_then
array_view<T>
: a non-owning view to a contiguous block of 0..NT
string_view<T>
: a non-owning view with string helper methods for splitting and in-place formattingunix_err
: a trivial wrapper arounderrno
file_handle
: a very-trivial RAII wrapper aroundFILE*
with a few convenience functionstimestamp
: a {seconds, nanoseconds} timestamp