24#include <libcamera/base/private.h>
30#define O_TMPFILE (020000000 | O_DIRECTORY)
39const char *
basename(
const char *path);
42std::string
dirname(
const std::string &path);
45std::vector<typename T::key_type>
map_keys(
const T &map)
47 std::vector<typename T::key_type> keys;
48 std::transform(map.begin(), map.end(), std::back_inserter(keys),
49 [](
const auto &value) { return value.first; });
53template<
class InputIt1,
class InputIt2>
55 InputIt2 first2, InputIt2 last2)
57 unsigned int count = 0;
59 while (first1 != last1 && first2 != last2) {
60 if (*first1 < *first2) {
63 if (!(*first2 < *first1))
72using clock = std::chrono::steady_clock;
73using duration = std::chrono::steady_clock::duration;
85std::basic_ostream<char, std::char_traits<char>> &
86operator<<(std::basic_ostream<
char, std::char_traits<char>> &stream,
const _hex &h);
90 std::enable_if_t<std::is_integral<T>::value> * =
nullptr>
91_hex
hex(T value,
unsigned int width = 0);
95inline _hex
hex<int8_t>(int8_t value,
unsigned int width)
97 return {
static_cast<uint64_t
>(value), width ? width : 2 };
101inline _hex hex<uint8_t>(uint8_t value,
unsigned int width)
103 return {
static_cast<uint64_t
>(value), width ? width : 2 };
107inline _hex hex<int16_t>(int16_t value,
unsigned int width)
109 return {
static_cast<uint64_t
>(value), width ? width : 4 };
115 return {
static_cast<uint64_t
>(value), width ? width : 4 };
119inline _hex
hex<int32_t>(int32_t value,
unsigned int width)
121 return {
static_cast<uint64_t
>(value), width ? width : 8 };
127 return {
static_cast<uint64_t
>(value), width ? width : 8 };
131inline _hex
hex<int64_t>(int64_t value,
unsigned int width)
133 return {
static_cast<uint64_t
>(value), width ? width : 16 };
139 return {
static_cast<uint64_t
>(value), width ? width : 16 };
143size_t strlcpy(
char *dst,
const char *src,
size_t size);
146template<
typename Container,
typename UnaryOp>
147std::string
join(
const Container &items,
const std::string &sep, UnaryOp op)
149 std::ostringstream ss;
152 for (
typename Container::const_iterator it = std::begin(items);
153 it != std::end(items); ++it) {
165template<
typename Container>
166std::string
join(
const Container &items,
const std::string &sep)
168 std::ostringstream ss;
171 for (
typename Container::const_iterator it = std::begin(items);
172 it != std::end(items); ++it) {
184template<
typename Container,
typename UnaryOp>
185std::string
join(
const Container &items,
const std::string &sep, UnaryOp op =
nullptr);
193 StringSplitter(
const std::string &str,
const std::string &delim);
198 using difference_type = std::size_t;
199 using value_type = std::string;
200 using pointer = value_type *;
201 using reference = value_type &;
202 using iterator_category = std::input_iterator_tag;
204 iterator(
const StringSplitter *ss, std::string::size_type pos);
206 iterator &operator++();
209 bool operator==(
const iterator &other)
const
211 return pos_ == other.pos_;
214 bool operator!=(
const iterator &other)
const
216 return !(*
this == other);
220 const StringSplitter *ss_;
221 std::string::size_type pos_;
222 std::string::size_type next_;
225 iterator begin()
const
232 return {
this, std::string::npos };
242details::StringSplitter
split(
const std::string &str,
const std::string &delim);
244std::string
toAscii(
const std::string &str);
249constexpr unsigned int alignDown(
unsigned int value,
unsigned int alignment)
251 return value / alignment * alignment;
254constexpr unsigned int alignUp(
unsigned int value,
unsigned int alignment)
256 return (value + alignment - 1) / alignment * alignment;
262struct reverse_adapter {
267auto begin(reverse_adapter<T> r)
269 return std::rbegin(r.iterable);
273auto end(reverse_adapter<T> r)
275 return std::rend(r.iterable);
281details::reverse_adapter<T>
reverse(T &&iterable)
288template<
typename Base>
289class enumerate_iterator
292 using base_reference =
typename std::iterator_traits<Base>::reference;
295 using difference_type =
typename std::iterator_traits<Base>::difference_type;
296 using value_type = std::pair<const std::size_t, base_reference>;
297 using pointer = value_type *;
298 using reference = value_type &;
299 using iterator_category = std::input_iterator_tag;
301 explicit enumerate_iterator(Base iter)
302 : current_(iter), pos_(0)
306 enumerate_iterator &operator++()
313 bool operator!=(
const enumerate_iterator &other)
const
315 return current_ != other.current_;
318 value_type operator*()
const
320 return { pos_, *current_ };
328template<
typename Base>
329class enumerate_adapter
332 using iterator = enumerate_iterator<Base>;
334 enumerate_adapter(Base begin, Base end)
335 : begin_(begin), end_(end)
339 iterator begin()
const
341 return iterator{ begin_ };
346 return iterator{ end_ };
357auto enumerate(T &iterable) -> details::enumerate_adapter<
decltype(iterable.begin())>
359 return { std::begin(iterable), std::end(iterable) };
363template<
typename T,
size_t N>
364auto enumerate(T (&iterable)[N]) -> details::enumerate_adapter<T *>
366 return { std::begin(iterable), std::end(iterable) };
370class Duration :
public std::chrono::duration<double, std::nano>
372 using BaseDuration = std::chrono::duration<double, std::nano>;
375 Duration() =
default;
377 template<
typename Rep>
383 template<
typename Rep,
typename Period>
384 constexpr Duration(
const std::chrono::duration<Rep, Period> &d)
389 template<
typename Period>
392 auto const c = std::chrono::duration_cast<std::chrono::duration<double, Period>>(*this);
396 explicit constexpr operator bool()
const
398 return *
this != BaseDuration::zero();
411double strtod(
const char *__restrict nptr,
char **__restrict endptr);
416 return static_cast<std::underlying_type_t<Enum>
>(e);
424 void operator+=(std::function<
void()> &&action);
428 std::vector<std::function<void()>> actions_;
434template<
class CharT,
class Traits>
435std::basic_ostream<CharT, Traits> &
operator<<(std::basic_ostream<CharT, Traits> &os,
Helper class from std::chrono::duration that represents a time duration in nanoseconds with double pr...
Definition utils.h:371
constexpr Duration(const Rep &r)
Construct a Duration with r ticks.
Definition utils.h:378
double get() const
Retrieve the tick count, converted to the timebase provided by the template argument Period of type s...
Definition utils.h:390
constexpr Duration(const std::chrono::duration< Rep, Period > &d)
Construct a Duration by converting an arbitrary std::chrono::duration.
Definition utils.h:384
An object that performs actions upon destruction.
Definition utils.h:420
void release()
Remove all exit actions.
Definition utils.cpp:646
void operator+=(std::function< void()> &&action)
Add an exit action.
Definition utils.cpp:634
Top-level libcamera namespace.
Definition backtrace.h:17
std::ostream & operator<<(std::ostream &out, const Point &p)
Insert a text representation of a Point into an output stream.
Definition geometry.cpp:91
Matrix< U, Rows, Cols > operator*(T d, const Matrix< U, Rows, Cols > &m)
Multiply the matrix by a scalar.
Definition matrix.h:133
unsigned int set_overlap(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2)
Count the number of elements in the intersection of two ranges.
Definition utils.h:54
constexpr unsigned int alignUp(unsigned int value, unsigned int alignment)
Align value up to alignment.
Definition utils.h:254
std::chrono::steady_clock::duration duration
The libcamera duration related to libcamera::utils::clock.
Definition utils.h:73
const char * basename(const char *path)
Strip the directory prefix from the path.
Definition utils.cpp:37
details::StringSplitter split(const std::string &str, const std::string &delim)
Split a string based on a delimiter.
Definition utils.cpp:308
std::string time_point_to_string(const time_point &time)
Convert a time point to a string representation.
Definition utils.cpp:175
std::chrono::steady_clock::time_point time_point
The libcamera time point related to libcamera::utils::clock.
Definition utils.h:74
std::string join(const Container &items, const std::string &sep, UnaryOp op=nullptr)
Join elements of a container in a string with a separator.
_hex hex(T value, unsigned int width=0)
Write an hexadecimal value to an output string.
std::string libcameraSourcePath()
Retrieve the path to the source directory.
Definition source_paths.cpp:114
std::string toAscii(const std::string &str)
Remove any non-ASCII characters from a string.
Definition utils.cpp:322
constexpr unsigned int alignDown(unsigned int value, unsigned int alignment)
Align value down to alignment.
Definition utils.h:249
size_t strlcpy(char *dst, const char *src, size_t size)
Copy a string with a size limit.
Definition utils.cpp:240
char * secure_getenv(const char *name)
Get an environment variable.
Definition utils.cpp:61
std::chrono::steady_clock clock
The libcamera clock (monotonic)
Definition utils.h:72
details::reverse_adapter< T > reverse(T &&iterable)
Wrap an iterable to reverse iteration in a range-based loop.
Definition utils.h:281
decltype(auto) abs_diff(const T &a, const T &b)
Calculates the absolute value of the difference between two elements.
Definition utils.h:403
std::vector< typename T::key_type > map_keys(const T &map)
Retrieve the keys of a std::map<>
Definition utils.h:45
std::string libcameraBuildPath()
Retrieve the path to the build directory.
Definition source_paths.cpp:74
struct timespec duration_to_timespec(const duration &value)
Convert a duration to a timespec.
Definition utils.cpp:161
auto enumerate(T &iterable) -> details::enumerate_adapter< decltype(iterable.begin())>
Wrap an iterable to enumerate index and value in a range-based loop.
Definition utils.h:357
constexpr std::underlying_type_t< Enum > to_underlying(Enum e) noexcept
Convert an enumeration to its underlygin type.
Definition utils.h:414
std::string dirname(const std::string &path)
Identify the dirname portion of a path.
Definition utils.cpp:83