libcamera: include/libcamera/base/utils.h Source File

libcamera v0.1.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 * utils.h - Miscellaneous utility functions
6 */
7
8#pragma once
9
10#include <algorithm>
11#include <chrono>
12#include <iterator>
13#include <memory>
14#include <ostream>
15#include <sstream>
16#include <string>
17#include <string.h>
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
34namespace libcamera {
35
36namespace utils {
37
38const char *basename(const char *path);
39
40char *secure_getenv(const char *name);
41std::string dirname(const std::string &path);
42
43template<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
52template<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
75struct timespec duration_to_timespec(const duration &value);
76std::string time_point_to_string(const time_point &time);
77
78#ifndef __DOXYGEN__
79struct _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
88template<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__
93template<>
94inline _hex hex<int32_t>(int32_t value, unsigned int width)
95{
96 return { static_cast<uint64_t>(value), width ? width : 8 };
97}
98
99template<>
100inline _hex hex<uint32_t>(uint32_t value, unsigned int width)
101{
102 return { static_cast<uint64_t>(value), width ? width : 8 };
103}
104
105template<>
106inline _hex hex<int64_t>(int64_t value, unsigned int width)
107{
108 return { static_cast<uint64_t>(value), width ? width : 16 };
109}
110
111template<>
112inline _hex hex<uint64_t>(uint64_t value, unsigned int width)
113{
114 return { static_cast<uint64_t>(value), width ? width : 16 };
115}
116#endif
117
118size_t strlcpy(char *dst, const char *src, size_t size);
119
120#ifndef __DOXYGEN__
121template<typename Container, typename UnaryOp>
122std::string join(const Container &items, const std::string &sep, UnaryOp op)
123{
124 std::ostringstream ss;
125 bool first = true;
126
127 for (typename Container::const_iterator it = std::begin(items);
128 it != std::end(items); ++it) {
129 if (!first)
130 ss << sep;
131 else
132 first = false;
133
134 ss << op(*it);
135 }
136
137 return ss.str();
138}
139
140template<typename Container>
141std::string join(const Container &items, const std::string &sep)
142{
143 std::ostringstream ss;
144 bool first = true;
145
146 for (typename Container::const_iterator it = std::begin(items);
147 it != std::end(items); ++it) {
148 if (!first)
149 ss << sep;
150 else
151 first = false;
152
153 ss << *it;
154 }
155
156 return ss.str();
157}
158#else
159template<typename Container, typename UnaryOp>
160std::string join(const Container &items, const std::string &sep, UnaryOp op = nullptr);
161#endif
162
163namespace details {
164
165class StringSplitter
166{
167public:
168 StringSplitter(const std::string &str, const std::string &delim);
169
170 class iterator
171 {
172 public:
173 using difference_type = std::size_t;
174 using value_type = std::string;
175 using pointer = value_type *;
176 using reference = value_type &;
177 using iterator_category = std::input_iterator_tag;
178
179 iterator(const StringSplitter *ss, std::string::size_type pos);
180
181 iterator &operator++();
182 std::string operator*() const;
183 bool operator!=(const iterator &other) const;
184
185 private:
186 const StringSplitter *ss_;
187 std::string::size_type pos_;
188 std::string::size_type next_;
189 };
190
191 iterator begin() const;
192 iterator end() const;
193
194private:
195 std::string str_;
196 std::string delim_;
197};
198
199} /* namespace details */
200
201details::StringSplitter split(const std::string &str, const std::string &delim);
202
203std::string toAscii(const std::string &str);
204
205std::string libcameraBuildPath();
206std::string libcameraSourcePath();
207
208 constexpr unsigned int alignDown(unsigned int value, unsigned int alignment)
209{
210 return value / alignment * alignment;
211}
212
213 constexpr unsigned int alignUp(unsigned int value, unsigned int alignment)
214{
215 return (value + alignment - 1) / alignment * alignment;
216}
217
218namespace details {
219
220template<typename T>
221struct reverse_adapter {
222 T &iterable;
223};
224
225template<typename T>
226auto begin(reverse_adapter<T> r)
227{
228 return std::rbegin(r.iterable);
229}
230
231template<typename T>
232auto end(reverse_adapter<T> r)
233{
234 return std::rend(r.iterable);
235}
236
237} /* namespace details */
238
239template<typename T>
240details::reverse_adapter<T> reverse(T &&iterable)
241{
242 return { iterable };
243}
244
245namespace details {
246
247template<typename Base>
248class enumerate_iterator
249{
250private:
251 using base_reference = typename std::iterator_traits<Base>::reference;
252
253public:
254 using difference_type = typename std::iterator_traits<Base>::difference_type;
255 using value_type = std::pair<const std::size_t, base_reference>;
256 using pointer = value_type *;
257 using reference = value_type &;
258 using iterator_category = std::input_iterator_tag;
259
260 explicit enumerate_iterator(Base iter)
261 : current_(iter), pos_(0)
262 {
263 }
264
265 enumerate_iterator &operator++()
266 {
267 ++current_;
268 ++pos_;
269 return *this;
270 }
271
272 bool operator!=(const enumerate_iterator &other) const
273 {
274 return current_ != other.current_;
275 }
276
277 value_type operator*() const
278 {
279 return { pos_, *current_ };
280 }
281
282private:
283 Base current_;
284 std::size_t pos_;
285};
286
287template<typename Base>
288class enumerate_adapter
289{
290public:
291 using iterator = enumerate_iterator<Base>;
292
293 enumerate_adapter(Base begin, Base end)
294 : begin_(begin), end_(end)
295 {
296 }
297
298 iterator begin() const
299 {
300 return iterator{ begin_ };
301 }
302
303 iterator end() const
304 {
305 return iterator{ end_ };
306 }
307
308private:
309 const Base begin_;
310 const Base end_;
311};
312
313} /* namespace details */
314
315template<typename T>
316 auto enumerate(T &iterable) -> details::enumerate_adapter<decltype(iterable.begin())>
317{
318 return { std::begin(iterable), std::end(iterable) };
319}
320
321#ifndef __DOXYGEN__
322template<typename T, size_t N>
323auto enumerate(T (&iterable)[N]) -> details::enumerate_adapter<T *>
324{
325 return { std::begin(iterable), std::end(iterable) };
326}
327#endif
328
329 class Duration : public std::chrono::duration<double, std::nano>
330{
331 using BaseDuration = std::chrono::duration<double, std::nano>;
332
333public:
334 Duration() = default;
335
336 template<typename Rep>
337 constexpr explicit Duration(const Rep &r)
338 : BaseDuration(r)
339 {
340 }
341
342 template<typename Rep, typename Period>
343 constexpr Duration(const std::chrono::duration<Rep, Period> &d)
344 : BaseDuration(d)
345 {
346 }
347
348 template<typename Period>
349 double get() const
350 {
351 auto const c = std::chrono::duration_cast<std::chrono::duration<double, Period>>(*this);
352 return c.count();
353 }
354
355 explicit constexpr operator bool() const
356 {
357 return *this != BaseDuration::zero();
358 }
359};
360
361template<typename T>
362 decltype(auto) abs_diff(const T &a, const T &b)
363{
364 if (a < b)
365 return b - a;
366 else
367 return a - b;
368}
369
370double strtod(const char *__restrict nptr, char **__restrict endptr);
371
372} /* namespace utils */
373
374#ifndef __DOXYGEN__
375template<class CharT, class Traits>
376std::basic_ostream<CharT, Traits> &operator<<(std::basic_ostream<CharT, Traits> &os,
377 const utils::Duration &d);
378#endif
379
380} /* namespace libcamera */
libcamera::utils::Duration
Helper class from std::chrono::duration that represents a time duration in nanoseconds with double pr...
Definition utils.h:330
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:349
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:343
libcamera::utils::Duration::Duration
constexpr Duration(const Rep &r)
Construct a Duration with r ticks.
Definition utils.h:337
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 together.
Definition transform.cpp:207
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::alignDown
constexpr unsigned int alignDown(unsigned int value, unsigned int alignment)
Align value down to alignment.
Definition utils.h:208
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::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::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::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::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::alignUp
constexpr unsigned int alignUp(unsigned int value, unsigned int alignment)
Align value up to alignment.
Definition utils.h:213
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:316
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:362
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::duration
std::chrono::steady_clock::duration duration
The libcamera duration related to libcamera::utils::clock.
Definition utils.h:72
libcamera::utils::reverse
details::reverse_adapter< T > reverse(T &&iterable)
Wrap an iterable to reverse iteration in a range-based loop.
Definition utils.h:240
libcamera::utils::hex
_hex hex(T value, unsigned int width=0)
Write an hexadecimal value to an output string.
libcamera::utils::dirname
std::string dirname(const std::string &path)
Identify the dirname portion of a path.
Definition utils.cpp:83