25#include <libcamera/base/private.h>
31#define O_TMPFILE (020000000 | O_DIRECTORY)
40const char *
basename(
const char *path);
43std::string
dirname(
const std::string &path);
46std::vector<typename T::key_type>
map_keys(
const T &map)
48 std::vector<typename T::key_type> keys;
49 std::transform(map.begin(), map.end(), std::back_inserter(keys),
50 [](
const auto &value) { return value.first; });
54template<
class InputIt1,
class InputIt2>
56 InputIt2 first2, InputIt2 last2)
58 unsigned int count = 0;
60 while (first1 != last1 && first2 != last2) {
61 if (*first1 < *first2) {
64 if (!(*first2 < *first1))
73using clock = std::chrono::steady_clock;
74using duration = std::chrono::steady_clock::duration;
88constexpr unsigned int hex_width()
93std::basic_ostream<char, std::char_traits<char>> &
94operator<<(std::basic_ostream<
char, std::char_traits<char>> &stream,
const hex &h);
98template<
typename T, std::enable_if_t<std::is_
integral_v<T>> * =
nullptr>
99details::hex
hex(T value,
unsigned int width = details::hex_width<T>())
101 return {
static_cast<std::make_unsigned_t<T>
>(value), width };
104size_t strlcpy(
char *dst,
const char *src,
size_t size);
107template<
typename Container,
typename UnaryOp>
108std::string join(
const Container &items,
const std::string &sep, UnaryOp op)
110 std::ostringstream ss;
113 for (
typename Container::const_iterator it = std::begin(items);
114 it != std::end(items); ++it) {
126template<
typename Container>
127std::string
join(
const Container &items,
const std::string &sep)
129 std::ostringstream ss;
132 for (
typename Container::const_iterator it = std::begin(items);
133 it != std::end(items); ++it) {
145template<
typename Container,
typename UnaryOp>
146std::string
join(
const Container &items,
const std::string &sep, UnaryOp op =
nullptr);
154 StringSplitter(
const std::string &str,
const std::string &delim);
159 using difference_type = std::size_t;
160 using value_type = std::string;
161 using pointer = value_type *;
162 using reference = value_type &;
163 using iterator_category = std::input_iterator_tag;
165 iterator(
const StringSplitter *ss, std::string::size_type pos);
167 iterator &operator++();
170 bool operator==(
const iterator &other)
const
172 return pos_ == other.pos_;
175 bool operator!=(
const iterator &other)
const
177 return !(*
this == other);
181 const StringSplitter *ss_;
182 std::string::size_type pos_;
183 std::string::size_type next_;
186 iterator begin()
const
193 return {
this, std::string::npos };
203details::StringSplitter
split(
const std::string &str,
const std::string &delim);
205std::string
toAscii(
const std::string &str);
210constexpr unsigned int alignDown(
unsigned int value,
unsigned int alignment)
212 return value / alignment * alignment;
215constexpr unsigned int alignUp(
unsigned int value,
unsigned int alignment)
217 return (value + alignment - 1) / alignment * alignment;
223struct reverse_adapter {
228auto begin(reverse_adapter<T> r)
230 return std::rbegin(r.iterable);
234auto end(reverse_adapter<T> r)
236 return std::rend(r.iterable);
242details::reverse_adapter<T>
reverse(T &&iterable)
249template<
typename Base>
250class enumerate_iterator
253 using base_reference =
typename std::iterator_traits<Base>::reference;
256 using difference_type =
typename std::iterator_traits<Base>::difference_type;
257 using value_type = std::pair<const std::size_t, base_reference>;
258 using pointer = value_type *;
259 using reference = value_type &;
260 using iterator_category = std::input_iterator_tag;
262 explicit enumerate_iterator(Base iter)
263 : current_(iter), pos_(0)
267 enumerate_iterator &operator++()
274 bool operator!=(
const enumerate_iterator &other)
const
276 return current_ != other.current_;
279 value_type operator*()
const
281 return { pos_, *current_ };
289template<
typename Base>
290class enumerate_adapter
293 using iterator = enumerate_iterator<Base>;
295 enumerate_adapter(Base begin, Base end)
296 : begin_(begin), end_(end)
300 iterator begin()
const
302 return iterator{ begin_ };
307 return iterator{ end_ };
320 return details::enumerate_adapter{ std::begin(iterable), std::end(iterable) };
323class Duration :
public std::chrono::duration<double, std::nano>
325 using BaseDuration = std::chrono::duration<double, std::nano>;
328 Duration() =
default;
330 template<
typename Rep>
336 template<
typename Rep,
typename Period>
337 constexpr Duration(
const std::chrono::duration<Rep, Period> &d)
342 template<
typename Period>
345 auto const c = std::chrono::duration_cast<std::chrono::duration<double, Period>>(*this);
351 return BaseDuration::operator-();
354 explicit constexpr operator bool()
const
356 return *
this != BaseDuration::zero();
369double strtod(
const char *__restrict nptr,
char **__restrict endptr);
374 return static_cast<std::underlying_type_t<Enum>
>(e);
382 void operator+=(std::function<
void()> &&action);
386 std::vector<std::function<void()>> actions_;
394 template<
typename Fn,
395 std::enable_if_t<!std::is_same_v<std::remove_cv_t<std::remove_reference_t<Fn>>, scope_exit> &&
396 std::is_constructible_v<EF, Fn>> * =
nullptr>
397 explicit scope_exit(Fn &&fn)
398 : exitFunction_(std::forward<Fn>(fn))
400 static_assert(std::is_nothrow_constructible_v<EF, Fn>);
422scope_exit(EF) -> scope_exit<EF>;
429template<
class CharT,
class Traits>
430std::basic_ostream<CharT, Traits> &
operator<<(std::basic_ostream<CharT, Traits> &os,
Utilities to help constructing class interfaces.
#define LIBCAMERA_DISABLE_COPY_AND_MOVE(klass)
Disable copy and move construction and assignment of the klass.
Definition class.h:29
Helper class from std::chrono::duration that represents a time duration in nanoseconds with double pr...
Definition utils.h:324
constexpr Duration operator-() const
Negation operator to negate a Duration.
Definition utils.h:349
constexpr Duration(const Rep &r)
Construct a Duration with r ticks.
Definition utils.h:331
double get() const
Retrieve the tick count, converted to the timebase provided by the template argument Period of type s...
Definition utils.h:343
constexpr Duration(const std::chrono::duration< Rep, Period > &d)
Construct a Duration by converting an arbitrary std::chrono::duration.
Definition utils.h:337
An object that performs actions upon destruction.
Definition utils.h:378
void release()
Remove all exit actions.
Definition utils.cpp:653
void operator+=(std::function< void()> &&action)
Add an exit action.
Definition utils.cpp:641
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:55
constexpr unsigned int alignUp(unsigned int value, unsigned int alignment)
Align value up to alignment.
Definition utils.h:215
std::chrono::steady_clock::duration duration
The libcamera duration related to libcamera::utils::clock.
Definition utils.h:74
auto enumerate(T &iterable)
Wrap an iterable to enumerate index and value in a range-based loop.
Definition utils.h:318
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:309
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:75
std::string join(const Container &items, const std::string &sep, UnaryOp op=nullptr)
Join elements of a container in a string with a separator.
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:323
constexpr unsigned int alignDown(unsigned int value, unsigned int alignment)
Align value down to alignment.
Definition utils.h:210
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:73
details::reverse_adapter< T > reverse(T &&iterable)
Wrap an iterable to reverse iteration in a range-based loop.
Definition utils.h:242
decltype(auto) abs_diff(const T &a, const T &b)
Calculates the absolute value of the difference between two elements.
Definition utils.h:361
std::vector< typename T::key_type > map_keys(const T &map)
Retrieve the keys of a std::map<>.
Definition utils.h:46
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
constexpr std::underlying_type_t< Enum > to_underlying(Enum e) noexcept
Convert an enumeration to its underlygin type.
Definition utils.h:372
details::hex hex(T value, unsigned int width=details::hex_width< T >())
Write an hexadecimal value to an output string.
Definition utils.h:99
std::string dirname(const std::string &path)
Identify the dirname portion of a path.
Definition utils.cpp:83