19 #include <type_traits> 23 #include <libcamera/base/private.h> 29 #define O_TMPFILE (020000000 | O_DIRECTORY) 38 const char *
basename(
const char *path);
41 std::string
dirname(
const std::string &path);
44 std::vector<typename T::key_type>
map_keys(
const T &map)
46 std::vector<typename T::key_type> keys;
47 std::transform(map.begin(), map.end(), std::back_inserter(keys),
48 [](
const auto &value) {
return value.first; });
52 template<
class InputIt1,
class InputIt2>
54 InputIt2 first2, InputIt2 last2)
56 unsigned int count = 0;
58 while (first1 != last1 && first2 != last2) {
59 if (*first1 < *first2) {
62 if (!(*first2 < *first1))
71 using clock = std::chrono::steady_clock;
72 using duration = std::chrono::steady_clock::duration;
73 using time_point = std::chrono::steady_clock::time_point;
84 std::basic_ostream<char, std::char_traits<char>> &
85 operator<<(std::basic_ostream<char, std::char_traits<char>> &stream,
const _hex &h);
89 std::enable_if_t<std::is_integral<T>::value> * =
nullptr>
90 _hex
hex(T value,
unsigned int width = 0);
94 inline _hex hex<int8_t>(int8_t value,
unsigned int width)
96 return {
static_cast<uint64_t
>(value), width ? width : 2 };
100 inline _hex hex<uint8_t>(uint8_t value,
unsigned int width)
102 return {
static_cast<uint64_t
>(value), width ? width : 2 };
106 inline _hex hex<int16_t>(int16_t value,
unsigned int width)
108 return {
static_cast<uint64_t
>(value), width ? width : 4 };
112 inline _hex hex<uint16_t>(uint16_t value,
unsigned int width)
114 return {
static_cast<uint64_t
>(value), width ? width : 4 };
118 inline _hex hex<int32_t>(int32_t value,
unsigned int width)
120 return {
static_cast<uint64_t
>(value), width ? width : 8 };
124 inline _hex hex<uint32_t>(uint32_t value,
unsigned int width)
126 return {
static_cast<uint64_t
>(value), width ? width : 8 };
130 inline _hex hex<int64_t>(int64_t value,
unsigned int width)
132 return {
static_cast<uint64_t
>(value), width ? width : 16 };
136 inline _hex hex<uint64_t>(uint64_t value,
unsigned int width)
138 return {
static_cast<uint64_t
>(value), width ? width : 16 };
142 size_t strlcpy(
char *dst,
const char *src,
size_t size);
145 template<
typename Container,
typename UnaryOp>
146 std::string
join(
const Container &items,
const std::string &sep, UnaryOp op)
148 std::ostringstream ss;
151 for (
typename Container::const_iterator it = std::begin(items);
152 it != std::end(items); ++it) {
164 template<
typename Container>
165 std::string
join(
const Container &items,
const std::string &sep)
167 std::ostringstream ss;
170 for (
typename Container::const_iterator it = std::begin(items);
171 it != std::end(items); ++it) {
183 template<
typename Container,
typename UnaryOp>
184 std::string
join(
const Container &items,
const std::string &sep, UnaryOp op =
nullptr);
192 StringSplitter(
const std::string &str,
const std::string &delim);
197 using difference_type = std::size_t;
198 using value_type = std::string;
199 using pointer = value_type *;
200 using reference = value_type &;
201 using iterator_category = std::input_iterator_tag;
203 iterator(
const StringSplitter *ss, std::string::size_type pos);
205 iterator &operator++();
207 bool operator!=(
const iterator &other)
const;
210 const StringSplitter *ss_;
211 std::string::size_type pos_;
212 std::string::size_type next_;
215 iterator begin()
const;
216 iterator end()
const;
225 details::StringSplitter
split(
const std::string &str,
const std::string &delim);
227 std::string
toAscii(
const std::string &str);
232 constexpr
unsigned int alignDown(
unsigned int value,
unsigned int alignment)
234 return value / alignment * alignment;
237 constexpr
unsigned int alignUp(
unsigned int value,
unsigned int alignment)
239 return (value + alignment - 1) / alignment * alignment;
245 struct reverse_adapter {
250 auto begin(reverse_adapter<T> r)
252 return std::rbegin(r.iterable);
256 auto end(reverse_adapter<T> r)
258 return std::rend(r.iterable);
264 details::reverse_adapter<T>
reverse(T &&iterable)
271 template<
typename Base>
272 class enumerate_iterator
275 using base_reference =
typename std::iterator_traits<Base>::reference;
278 using difference_type =
typename std::iterator_traits<Base>::difference_type;
279 using value_type = std::pair<const std::size_t, base_reference>;
280 using pointer = value_type *;
281 using reference = value_type &;
282 using iterator_category = std::input_iterator_tag;
284 explicit enumerate_iterator(Base iter)
285 : current_(iter), pos_(0)
289 enumerate_iterator &operator++()
296 bool operator!=(
const enumerate_iterator &other)
const 298 return current_ != other.current_;
303 return { pos_, *current_ };
311 template<
typename Base>
312 class enumerate_adapter
315 using iterator = enumerate_iterator<Base>;
317 enumerate_adapter(Base begin, Base end)
318 : begin_(begin), end_(end)
322 iterator begin()
const 324 return iterator{ begin_ };
329 return iterator{ end_ };
340 auto enumerate(T &iterable) -> details::enumerate_adapter<decltype(iterable.begin())>
342 return { std::begin(iterable), std::end(iterable) };
346 template<
typename T,
size_t N>
347 auto enumerate(T (&iterable)[N]) -> details::enumerate_adapter<T *>
349 return { std::begin(iterable), std::end(iterable) };
353 class Duration :
public std::chrono::duration<double, std::nano>
355 using BaseDuration = std::chrono::duration<double, std::nano>;
360 template<
typename Rep>
366 template<
typename Rep,
typename Period>
367 constexpr
Duration(
const std::chrono::duration<Rep, Period> &d)
372 template<
typename Period>
375 auto const c = std::chrono::duration_cast<std::chrono::duration<double, Period>>(*this);
379 explicit constexpr
operator bool()
const 381 return *
this != BaseDuration::zero();
386 decltype(
auto) abs_diff(const T &a, const T &b)
394 double strtod(
const char *__restrict nptr,
char **__restrict endptr);
399 return static_cast<std::underlying_type_t<Enum>
>(e);
407 void operator+=(std::function<
void()> &&action);
411 std::vector<std::function<void()>> actions_;
417 template<
class CharT,
class Traits>
418 std::basic_ostream<CharT, Traits> &operator<<(std::basic_ostream<CharT, Traits> &os,
std::string libcameraSourcePath()
Retrieve the path to the source directory.
Definition: source_paths.cpp:114
const char * basename(const char *path)
Strip the directory prefix from the path.
Definition: utils.cpp:37
struct timespec duration_to_timespec(const duration &value)
Convert a duration to a timespec.
Definition: utils.cpp:161
Top-level libcamera namespace.
Definition: backtrace.h:17
std::chrono::steady_clock clock
The libcamera clock (monotonic)
Definition: utils.h:71
An object that performs actions upon destruction.
Definition: utils.h:402
std::vector< typename T::key_type > map_keys(const T &map)
Retrieve the keys of a std::map<>
Definition: utils.h:44
std::string dirname(const std::string &path)
Identify the dirname portion of a path.
Definition: utils.cpp:83
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:53
std::chrono::steady_clock::time_point time_point
The libcamera time point related to libcamera::utils::clock.
Definition: utils.h:73
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:340
std::string time_point_to_string(const time_point &time)
Convert a time point to a string representation.
Definition: utils.cpp:175
double strtod(const char *__restrict nptr, char **__restrict endptr)
Convert a string to a double independently of the current locale.
Definition: utils.cpp:511
char * secure_getenv(const char *name)
Get an environment variable.
Definition: utils.cpp:61
Transform operator*(Transform t0, Transform t1)
Compose two transforms by applying t0 first then t1.
Definition: transform.cpp:209
std::string libcameraBuildPath()
Retrieve the path to the build directory.
Definition: source_paths.cpp:74
constexpr Duration(const Rep &r)
Construct a Duration with r ticks.
Definition: utils.h:361
constexpr unsigned int alignDown(unsigned int value, unsigned int alignment)
Align value down to alignment.
Definition: utils.h:232
std::chrono::steady_clock::duration duration
The libcamera duration related to libcamera::utils::clock.
Definition: utils.h:72
details::StringSplitter split(const std::string &str, const std::string &delim)
Split a string based on a delimiter.
Definition: utils.cpp:323
_hex hex(T value, unsigned int width=0)
Write an hexadecimal value to an output string.
Helper class from std::chrono::duration that represents a time duration in nanoseconds with double pr...
Definition: utils.h:353
size_t strlcpy(char *dst, const char *src, size_t size)
Copy a string with a size limit.
Definition: utils.cpp:240
constexpr std::underlying_type_t< Enum > to_underlying(Enum e) noexcept
Convert an enumeration to its underlygin type.
Definition: utils.h:397
std::string join(const Container &items, const std::string &sep, UnaryOp op=nullptr)
Join elements of a container in a string with a separator.
constexpr unsigned int alignUp(unsigned int value, unsigned int alignment)
Align value up to alignment.
Definition: utils.h:237
std::string toAscii(const std::string &str)
Remove any non-ASCII characters from a string.
Definition: utils.cpp:337
details::reverse_adapter< T > reverse(T &&iterable)
Wrap an iterable to reverse iteration in a range-based loop.
Definition: utils.h:264
constexpr Duration(const std::chrono::duration< Rep, Period > &d)
Construct a Duration by converting an arbitrary std::chrono::duration.
Definition: utils.h:367