libcamera v0.6.0
Supporting cameras in Linux since 2019
Loading...
Searching...
No Matches
utils.h
Go to the documentation of this file.
1/* SPDX-License-Identifier: LGPL-2.1-or-later */
2/*
3 * Copyright (C) 2018, Google Inc.
4 *
5 * Miscellaneous utility functions
6 */
7
8#pragma once
9
10#include <algorithm>
11#include <chrono>
12#include <functional>
13#include <iterator>
14#include <ostream>
15#include <sstream>
16#include <stdint.h>
17#include <string.h>
18#include <string>
19#include <sys/time.h>
20#include <type_traits>
21#include <utility>
22#include <vector>
23
25#include <libcamera/base/private.h>
26
27#ifndef __DOXYGEN__
28
29/* uClibc and uClibc-ng don't provide O_TMPFILE */
30#ifndef O_TMPFILE
31#define O_TMPFILE (020000000 | O_DIRECTORY)
32#endif
33
34#endif
35
36namespace libcamera {
37
38namespace utils {
39
40const char *basename(const char *path);
41
42char *secure_getenv(const char *name);
43std::string dirname(const std::string &path);
44
45template<typename T>
46std::vector<typename T::key_type> map_keys(const T &map)
47{
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; });
51 return keys;
52}
53
54template<class InputIt1, class InputIt2>
55unsigned int set_overlap(InputIt1 first1, InputIt1 last1,
56 InputIt2 first2, InputIt2 last2)
57{
58 unsigned int count = 0;
59
60 while (first1 != last1 && first2 != last2) {
61 if (*first1 < *first2) {
62 ++first1;
63 } else {
64 if (!(*first2 < *first1))
65 count++;
66 ++first2;
67 }
68 }
69
70 return count;
71}
72
73using clock = std::chrono::steady_clock;
74using duration = std::chrono::steady_clock::duration;
75using time_point = std::chrono::steady_clock::time_point;
76
77struct timespec duration_to_timespec(const duration &value);
78std::string time_point_to_string(const time_point &time);
79
80namespace details {
81
82struct hex {
83 uint64_t v;
84 unsigned int w;
85};
86
87template<typename T>
88constexpr unsigned int hex_width()
89{
90 return sizeof(T) * 2;
91}
92
93std::basic_ostream<char, std::char_traits<char>> &
94operator<<(std::basic_ostream<char, std::char_traits<char>> &stream, const hex &h);
95
96} /* namespace details */
97
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>())
100{
101 return { static_cast<std::make_unsigned_t<T>>(value), width };
102}
103
104size_t strlcpy(char *dst, const char *src, size_t size);
105
106#ifndef __DOXYGEN__
107template<typename Container, typename UnaryOp>
108std::string join(const Container &items, const std::string &sep, UnaryOp op)
109{
110 std::ostringstream ss;
111 bool first = true;
112
113 for (typename Container::const_iterator it = std::begin(items);
114 it != std::end(items); ++it) {
115 if (!first)
116 ss << sep;
117 else
118 first = false;
119
120 ss << op(*it);
121 }
122
123 return ss.str();
124}
125
126template<typename Container>
127std::string join(const Container &items, const std::string &sep)
128{
129 std::ostringstream ss;
130 bool first = true;
131
132 for (typename Container::const_iterator it = std::begin(items);
133 it != std::end(items); ++it) {
134 if (!first)
135 ss << sep;
136 else
137 first = false;
138
139 ss << *it;
140 }
141
142 return ss.str();
143}
144#else
145template<typename Container, typename UnaryOp>
146std::string join(const Container &items, const std::string &sep, UnaryOp op = nullptr);
147#endif
148
149namespace details {
150
151class StringSplitter
152{
153public:
154 StringSplitter(const std::string &str, const std::string &delim);
155
156 class iterator
157 {
158 public:
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;
164
165 iterator(const StringSplitter *ss, std::string::size_type pos);
166
167 iterator &operator++();
168 std::string operator*() const;
169
170 bool operator==(const iterator &other) const
171 {
172 return pos_ == other.pos_;
173 }
174
175 bool operator!=(const iterator &other) const
176 {
177 return !(*this == other);
178 }
179
180 private:
181 const StringSplitter *ss_;
182 std::string::size_type pos_;
183 std::string::size_type next_;
184 };
185
186 iterator begin() const
187 {
188 return { this, 0 };
189 }
190
191 iterator end() const
192 {
193 return { this, std::string::npos };
194 }
195
196private:
197 std::string str_;
198 std::string delim_;
199};
200
201} /* namespace details */
202
203details::StringSplitter split(const std::string &str, const std::string &delim);
204
205std::string toAscii(const std::string &str);
206
207std::string libcameraBuildPath();
208std::string libcameraSourcePath();
209
210constexpr unsigned int alignDown(unsigned int value, unsigned int alignment)
211{
212 return value / alignment * alignment;
213}
214
215constexpr unsigned int alignUp(unsigned int value, unsigned int alignment)
216{
217 return (value + alignment - 1) / alignment * alignment;
218}
219
220namespace details {
221
222template<typename T>
223struct reverse_adapter {
224 T &iterable;
225};
226
227template<typename T>
228auto begin(reverse_adapter<T> r)
229{
230 return std::rbegin(r.iterable);
231}
232
233template<typename T>
234auto end(reverse_adapter<T> r)
235{
236 return std::rend(r.iterable);
237}
238
239} /* namespace details */
240
241template<typename T>
242details::reverse_adapter<T> reverse(T &&iterable)
243{
244 return { iterable };
245}
246
247namespace details {
248
249template<typename Base>
250class enumerate_iterator
251{
252private:
253 using base_reference = typename std::iterator_traits<Base>::reference;
254
255public:
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;
261
262 explicit enumerate_iterator(Base iter)
263 : current_(iter), pos_(0)
264 {
265 }
266
267 enumerate_iterator &operator++()
268 {
269 ++current_;
270 ++pos_;
271 return *this;
272 }
273
274 bool operator!=(const enumerate_iterator &other) const
275 {
276 return current_ != other.current_;
277 }
278
279 value_type operator*() const
280 {
281 return { pos_, *current_ };
282 }
283
284private:
285 Base current_;
286 std::size_t pos_;
287};
288
289template<typename Base>
290class enumerate_adapter
291{
292public:
293 using iterator = enumerate_iterator<Base>;
294
295 enumerate_adapter(Base begin, Base end)
296 : begin_(begin), end_(end)
297 {
298 }
299
300 iterator begin() const
301 {
302 return iterator{ begin_ };
303 }
304
305 iterator end() const
306 {
307 return iterator{ end_ };
308 }
309
310private:
311 const Base begin_;
312 const Base end_;
313};
314
315} /* namespace details */
316
317template<typename T>
318auto enumerate(T &iterable)
319{
320 return details::enumerate_adapter{ std::begin(iterable), std::end(iterable) };
321}
322
323class Duration : public std::chrono::duration<double, std::nano>
324{
325 using BaseDuration = std::chrono::duration<double, std::nano>;
326
327public:
328 Duration() = default;
329
330 template<typename Rep>
331 constexpr explicit Duration(const Rep &r)
332 : BaseDuration(r)
333 {
334 }
335
336 template<typename Rep, typename Period>
337 constexpr Duration(const std::chrono::duration<Rep, Period> &d)
338 : BaseDuration(d)
339 {
340 }
341
342 template<typename Period>
343 double get() const
344 {
345 auto const c = std::chrono::duration_cast<std::chrono::duration<double, Period>>(*this);
346 return c.count();
347 }
348
349 constexpr Duration operator-() const
350 {
351 return BaseDuration::operator-();
352 }
353
354 explicit constexpr operator bool() const
355 {
356 return *this != BaseDuration::zero();
357 }
358};
359
360template<typename T>
361decltype(auto) abs_diff(const T &a, const T &b)
362{
363 if (a < b)
364 return b - a;
365 else
366 return a - b;
367}
368
369double strtod(const char *__restrict nptr, char **__restrict endptr);
370
371template<class Enum>
372constexpr std::underlying_type_t<Enum> to_underlying(Enum e) noexcept
373{
374 return static_cast<std::underlying_type_t<Enum>>(e);
375}
376
378{
379public:
381
382 void operator+=(std::function<void()> &&action);
383 void release();
384
385private:
386 std::vector<std::function<void()>> actions_;
387};
388
389#ifndef __DOXYGEN__
390template<typename EF>
391class scope_exit
392{
393public:
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))
399 {
400 static_assert(std::is_nothrow_constructible_v<EF, Fn>);
401 }
402
403 ~scope_exit()
404 {
405 if (active_)
406 exitFunction_();
407 }
408
409 void release()
410 {
411 active_ = false;
412 }
413
414private:
416
417 EF exitFunction_;
418 bool active_ = true;
419};
420
421template<typename EF>
422scope_exit(EF) -> scope_exit<EF>;
423
424#endif /* __DOXYGEN__ */
425
426} /* namespace utils */
427
428#ifndef __DOXYGEN__
429template<class CharT, class Traits>
430std::basic_ostream<CharT, Traits> &operator<<(std::basic_ostream<CharT, Traits> &os,
431 const utils::Duration &d);
432#endif
433
434} /* namespace libcamera */
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