libcamera v0.3.2 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 <string.h>
17#include <string>
18#include <sys/time.h>
19#include <type_traits>
20#include <utility>
21#include <vector>
22
23#include <libcamera/base/private.h>
24
25#ifndef __DOXYGEN__
26
27/* uClibc and uClibc-ng don't provide O_TMPFILE */
28#ifndef O_TMPFILE
29#define O_TMPFILE (020000000 | O_DIRECTORY)
30#endif
31
32#endif
33
34 namespace libcamera {
35
36 namespace utils {
37
38 const char *basename(const char *path);
39
40 char *secure_getenv(const char *name);
41std::string dirname(const std::string &path);
42
43 template<typename T>
44std::vector<typename T::key_type> map_keys(const T &map)
45{
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; });
49 return keys;
50}
51
52 template<class InputIt1, class InputIt2>
53 unsigned int set_overlap(InputIt1 first1, InputIt1 last1,
54 InputIt2 first2, InputIt2 last2)
55{
56 unsigned int count = 0;
57
58 while (first1 != last1 && first2 != last2) {
59 if (*first1 < *first2) {
60 ++first1;
61 } else {
62 if (!(*first2 < *first1))
63 count++;
64 ++first2;
65 }
66 }
67
68 return count;
69}
70
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;
74
75 struct timespec duration_to_timespec(const duration &value);
76std::string time_point_to_string(const time_point &time);
77
78#ifndef __DOXYGEN__
79 struct _hex {
80 uint64_t v;
81 unsigned int w;
82};
83
84std::basic_ostream<char, std::char_traits<char>> &
85 operator<<(std::basic_ostream<char, std::char_traits<char>> &stream, const _hex &h);
86#endif
87
88 template<typename T,
89 std::enable_if_t<std::is_integral<T>::value> * = nullptr>
90_hex hex(T value, unsigned int width = 0);
91
92#ifndef __DOXYGEN__
93 template<>
94 inline _hex hex<int8_t>(int8_t value, unsigned int width)
95{
96 return { static_cast<uint64_t>(value), width ? width : 2 };
97}
98
99 template<>
100 inline _hex hex<uint8_t>(uint8_t value, unsigned int width)
101{
102 return { static_cast<uint64_t>(value), width ? width : 2 };
103}
104
105 template<>
106 inline _hex hex<int16_t>(int16_t value, unsigned int width)
107{
108 return { static_cast<uint64_t>(value), width ? width : 4 };
109}
110
111 template<>
112 inline _hex hex<uint16_t>(uint16_t value, unsigned int width)
113{
114 return { static_cast<uint64_t>(value), width ? width : 4 };
115}
116
117 template<>
118 inline _hex hex<int32_t>(int32_t value, unsigned int width)
119{
120 return { static_cast<uint64_t>(value), width ? width : 8 };
121}
122
123 template<>
124 inline _hex hex<uint32_t>(uint32_t value, unsigned int width)
125{
126 return { static_cast<uint64_t>(value), width ? width : 8 };
127}
128
129 template<>
130 inline _hex hex<int64_t>(int64_t value, unsigned int width)
131{
132 return { static_cast<uint64_t>(value), width ? width : 16 };
133}
134
135 template<>
136 inline _hex hex<uint64_t>(uint64_t value, unsigned int width)
137{
138 return { static_cast<uint64_t>(value), width ? width : 16 };
139}
140#endif
141
142 size_t strlcpy(char *dst, const char *src, size_t size);
143
144#ifndef __DOXYGEN__
145 template<typename Container, typename UnaryOp>
146std::string join(const Container &items, const std::string &sep, UnaryOp op)
147{
148 std::ostringstream ss;
149 bool first = true;
150
151 for (typename Container::const_iterator it = std::begin(items);
152 it != std::end(items); ++it) {
153 if (!first)
154 ss << sep;
155 else
156 first = false;
157
158 ss << op(*it);
159 }
160
161 return ss.str();
162}
163
164 template<typename Container>
165std::string join(const Container &items, const std::string &sep)
166{
167 std::ostringstream ss;
168 bool first = true;
169
170 for (typename Container::const_iterator it = std::begin(items);
171 it != std::end(items); ++it) {
172 if (!first)
173 ss << sep;
174 else
175 first = false;
176
177 ss << *it;
178 }
179
180 return ss.str();
181}
182#else
183 template<typename Container, typename UnaryOp>
184std::string join(const Container &items, const std::string &sep, UnaryOp op = nullptr);
185#endif
186
187 namespace details {
188
189 class StringSplitter
190{
191 public:
192 StringSplitter(const std::string &str, const std::string &delim);
193
194 class iterator
195 {
196 public:
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;
202
203 iterator(const StringSplitter *ss, std::string::size_type pos);
204
205 iterator &operator++();
206 std::string operator*() const;
207 bool operator!=(const iterator &other) const;
208
209 private:
210 const StringSplitter *ss_;
211 std::string::size_type pos_;
212 std::string::size_type next_;
213 };
214
215 iterator begin() const;
216 iterator end() const;
217
218 private:
219 std::string str_;
220 std::string delim_;
221};
222
223} /* namespace details */
224
225details::StringSplitter split(const std::string &str, const std::string &delim);
226
227std::string toAscii(const std::string &str);
228
229std::string libcameraBuildPath();
230std::string libcameraSourcePath();
231
232 constexpr unsigned int alignDown(unsigned int value, unsigned int alignment)
233{
234 return value / alignment * alignment;
235}
236
237 constexpr unsigned int alignUp(unsigned int value, unsigned int alignment)
238{
239 return (value + alignment - 1) / alignment * alignment;
240}
241
242 namespace details {
243
244 template<typename T>
245 struct reverse_adapter {
246 T &iterable;
247};
248
249 template<typename T>
250 auto begin(reverse_adapter<T> r)
251{
252 return std::rbegin(r.iterable);
253}
254
255 template<typename T>
256 auto end(reverse_adapter<T> r)
257{
258 return std::rend(r.iterable);
259}
260
261} /* namespace details */
262
263 template<typename T>
264details::reverse_adapter<T> reverse(T &&iterable)
265{
266 return { iterable };
267}
268
269 namespace details {
270
271 template<typename Base>
272 class enumerate_iterator
273{
274 private:
275 using base_reference = typename std::iterator_traits<Base>::reference;
276
277 public:
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;
283
284 explicit enumerate_iterator(Base iter)
285 : current_(iter), pos_(0)
286 {
287 }
288
289 enumerate_iterator &operator++()
290 {
291 ++current_;
292 ++pos_;
293 return *this;
294 }
295
296 bool operator!=(const enumerate_iterator &other) const
297 {
298 return current_ != other.current_;
299 }
300
301 value_type operator*() const
302 {
303 return { pos_, *current_ };
304 }
305
306 private:
307 Base current_;
308 std::size_t pos_;
309};
310
311 template<typename Base>
312 class enumerate_adapter
313{
314 public:
315 using iterator = enumerate_iterator<Base>;
316
317 enumerate_adapter(Base begin, Base end)
318 : begin_(begin), end_(end)
319 {
320 }
321
322 iterator begin() const
323 {
324 return iterator{ begin_ };
325 }
326
327 iterator end() const
328 {
329 return iterator{ end_ };
330 }
331
332 private:
333 const Base begin_;
334 const Base end_;
335};
336
337} /* namespace details */
338
339 template<typename T>
340 auto enumerate(T &iterable) -> details::enumerate_adapter<decltype(iterable.begin())>
341{
342 return { std::begin(iterable), std::end(iterable) };
343}
344
345#ifndef __DOXYGEN__
346 template<typename T, size_t N>
347 auto enumerate(T (&iterable)[N]) -> details::enumerate_adapter<T *>
348{
349 return { std::begin(iterable), std::end(iterable) };
350}
351#endif
352
353 class Duration : public std::chrono::duration<double, std::nano>
354{
355 using BaseDuration = std::chrono::duration<double, std::nano>;
356
357 public:
358 Duration() = default;
359
360 template<typename Rep>
361 constexpr explicit Duration(const Rep &r)
362 : BaseDuration(r)
363 {
364 }
365
366 template<typename Rep, typename Period>
367 constexpr Duration(const std::chrono::duration<Rep, Period> &d)
368 : BaseDuration(d)
369 {
370 }
371
372 template<typename Period>
373 double get() const
374 {
375 auto const c = std::chrono::duration_cast<std::chrono::duration<double, Period>>(*this);
376 return c.count();
377 }
378
379 explicit constexpr operator bool() const
380 {
381 return *this != BaseDuration::zero();
382 }
383};
384
385 template<typename T>
386 decltype(auto) abs_diff(const T &a, const T &b)
387{
388 if (a < b)
389 return b - a;
390 else
391 return a - b;
392}
393
394 double strtod(const char *__restrict nptr, char **__restrict endptr);
395
396 template<class Enum>
397 constexpr std::underlying_type_t<Enum> to_underlying(Enum e) noexcept
398{
399 return static_cast<std::underlying_type_t<Enum>>(e);
400}
401
402 class ScopeExitActions
403{
404 public:
405 ~ScopeExitActions();
406
407 void operator+=(std::function<void()> &&action);
408 void release();
409
410 private:
411 std::vector<std::function<void()>> actions_;
412};
413
414} /* namespace utils */
415
416#ifndef __DOXYGEN__
417 template<class CharT, class Traits>
418std::basic_ostream<CharT, Traits> &operator<<(std::basic_ostream<CharT, Traits> &os,
419 const utils::Duration &d);
420#endif
421
422} /* namespace libcamera */
libcamera::utils::Duration
Helper class from std::chrono::duration that represents a time duration in nanoseconds with double pr...
Definition utils.h:354
libcamera::utils::Duration::Duration
constexpr Duration(const Rep &r)
Construct a Duration with r ticks.
Definition utils.h:361
libcamera::utils::Duration::get
double get() const
Retrieve the tick count, converted to the timebase provided by the template argument Period of type s...
Definition utils.h:373
libcamera::utils::Duration::Duration
constexpr Duration(const std::chrono::duration< Rep, Period > &d)
Construct a Duration by converting an arbitrary std::chrono::duration.
Definition utils.h:367
libcamera::utils::ScopeExitActions
An object that performs actions upon destruction.
Definition utils.h:403
libcamera::utils::ScopeExitActions::release
void release()
Remove all exit actions.
Definition utils.cpp:661
libcamera::utils::ScopeExitActions::operator+=
void operator+=(std::function< void()> &&action)
Add an exit action.
Definition utils.cpp:649
libcamera::ipa::operator!=
bool operator!=(const Vector< T, Rows > &lhs, const Vector< T, Rows > &rhs)
Compare vectors for inequality.
Definition vector.h:172
libcamera
Top-level libcamera namespace.
Definition backtrace.h:17
libcamera::operator<<
std::ostream & operator<<(std::ostream &out, const Point &p)
Insert a text representation of a Point into an output stream.
Definition geometry.cpp:91
libcamera::operator*
Transform operator*(Transform t0, Transform t1)
Compose two transforms by applying t0 first then t1.
Definition transform.cpp:209
libcamera::utils::set_overlap
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
libcamera::utils::alignUp
constexpr unsigned int alignUp(unsigned int value, unsigned int alignment)
Align value up to alignment.
Definition utils.h:237
libcamera::utils::duration
std::chrono::steady_clock::duration duration
The libcamera duration related to libcamera::utils::clock.
Definition utils.h:72
libcamera::utils::basename
const char * basename(const char *path)
Strip the directory prefix from the path.
Definition utils.cpp:37
libcamera::utils::split
details::StringSplitter split(const std::string &str, const std::string &delim)
Split a string based on a delimiter.
Definition utils.cpp:323
libcamera::utils::time_point_to_string
std::string time_point_to_string(const time_point &time)
Convert a time point to a string representation.
Definition utils.cpp:175
libcamera::utils::time_point
std::chrono::steady_clock::time_point time_point
The libcamera time point related to libcamera::utils::clock.
Definition utils.h:73
libcamera::utils::join
std::string join(const Container &items, const std::string &sep, UnaryOp op=nullptr)
Join elements of a container in a string with a separator.
libcamera::utils::hex
_hex hex(T value, unsigned int width=0)
Write an hexadecimal value to an output string.
libcamera::utils::libcameraSourcePath
std::string libcameraSourcePath()
Retrieve the path to the source directory.
Definition source_paths.cpp:114
libcamera::utils::toAscii
std::string toAscii(const std::string &str)
Remove any non-ASCII characters from a string.
Definition utils.cpp:337
libcamera::utils::alignDown
constexpr unsigned int alignDown(unsigned int value, unsigned int alignment)
Align value down to alignment.
Definition utils.h:232
libcamera::utils::strlcpy
size_t strlcpy(char *dst, const char *src, size_t size)
Copy a string with a size limit.
Definition utils.cpp:240
libcamera::utils::secure_getenv
char * secure_getenv(const char *name)
Get an environment variable.
Definition utils.cpp:61
libcamera::utils::clock
std::chrono::steady_clock clock
The libcamera clock (monotonic)
Definition utils.h:71
libcamera::utils::reverse
details::reverse_adapter< T > reverse(T &&iterable)
Wrap an iterable to reverse iteration in a range-based loop.
Definition utils.h:264
libcamera::utils::abs_diff
decltype(auto) abs_diff(const T &a, const T &b)
Calculates the absolute value of the difference between two elements.
Definition utils.h:386
libcamera::utils::map_keys
std::vector< typename T::key_type > map_keys(const T &map)
Retrieve the keys of a std::map<>
Definition utils.h:44
libcamera::utils::libcameraBuildPath
std::string libcameraBuildPath()
Retrieve the path to the build directory.
Definition source_paths.cpp:74
libcamera::utils::duration_to_timespec
struct timespec duration_to_timespec(const duration &value)
Convert a duration to a timespec.
Definition utils.cpp:161
libcamera::utils::enumerate
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
libcamera::utils::to_underlying
constexpr std::underlying_type_t< Enum > to_underlying(Enum e) noexcept
Convert an enumeration to its underlygin type.
Definition utils.h:397
libcamera::utils::dirname
std::string dirname(const std::string &path)
Identify the dirname portion of a path.
Definition utils.cpp:83