libcamera  v0.0.0+100-debian/0_git20200629+e7aa92a-8-9-g77f5237c-dirty (2021-05-05T16:20:29+01:00)
Supporting cameras in Linux since 2019
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 #ifndef __LIBCAMERA_INTERNAL_UTILS_H__
8 #define __LIBCAMERA_INTERNAL_UTILS_H__
9 
10 #include <algorithm>
11 #include <chrono>
12 #include <memory>
13 #include <ostream>
14 #include <sstream>
15 #include <string>
16 #include <string.h>
17 #include <sys/time.h>
18 #include <vector>
19 
20 #ifndef __DOXYGEN__
21 
22 /* uClibc and uClibc-ng don't provide O_TMPFILE */
23 #ifndef O_TMPFILE
24 #define O_TMPFILE (020000000 | O_DIRECTORY)
25 #endif
26 
27 #endif
28 
29 namespace libcamera {
30 
31 namespace utils {
32 
33 const char *basename(const char *path);
34 
35 char *secure_getenv(const char *name);
36 std::string dirname(const std::string &path);
37 
38 template<typename T>
39 std::vector<typename T::key_type> map_keys(const T &map)
40 {
41  std::vector<typename T::key_type> keys;
42  std::transform(map.begin(), map.end(), std::back_inserter(keys),
43  [](const auto &value) { return value.first; });
44  return keys;
45 }
46 
47 template<class InputIt1, class InputIt2>
48 unsigned int set_overlap(InputIt1 first1, InputIt1 last1,
49  InputIt2 first2, InputIt2 last2)
50 {
51  unsigned int count = 0;
52 
53  while (first1 != last1 && first2 != last2) {
54  if (*first1 < *first2) {
55  ++first1;
56  } else {
57  if (!(*first2 < *first1))
58  count++;
59  ++first2;
60  }
61  }
62 
63  return count;
64 }
65 
66 using clock = std::chrono::steady_clock;
67 using duration = std::chrono::steady_clock::duration;
68 using time_point = std::chrono::steady_clock::time_point;
69 
70 struct timespec duration_to_timespec(const duration &value);
71 std::string time_point_to_string(const time_point &time);
72 
73 #ifndef __DOXYGEN__
74 struct _hex {
75  uint64_t v;
76  unsigned int w;
77 };
78 
79 std::basic_ostream<char, std::char_traits<char>> &
80 operator<<(std::basic_ostream<char, std::char_traits<char>> &stream, const _hex &h);
81 #endif
82 
83 template<typename T>
84 _hex hex(T value, unsigned int width = 0);
85 
86 #ifndef __DOXYGEN__
87 template<>
88 inline _hex hex<int32_t>(int32_t value, unsigned int width)
89 {
90  return { static_cast<uint64_t>(value), width ? width : 8 };
91 }
92 
93 template<>
94 inline _hex hex<uint32_t>(uint32_t value, unsigned int width)
95 {
96  return { static_cast<uint64_t>(value), width ? width : 8 };
97 }
98 
99 template<>
100 inline _hex hex<int64_t>(int64_t value, unsigned int width)
101 {
102  return { static_cast<uint64_t>(value), width ? width : 16 };
103 }
104 
105 template<>
106 inline _hex hex<uint64_t>(uint64_t value, unsigned int width)
107 {
108  return { static_cast<uint64_t>(value), width ? width : 16 };
109 }
110 #endif
111 
112 size_t strlcpy(char *dst, const char *src, size_t size);
113 
114 #ifndef __DOXYGEN__
115 template<typename Container, typename UnaryOp>
116 std::string join(const Container &items, const std::string &sep, UnaryOp op)
117 {
118  std::ostringstream ss;
119  bool first = true;
120 
121  for (typename Container::const_iterator it = std::begin(items);
122  it != std::end(items); ++it) {
123  if (!first)
124  ss << sep;
125  else
126  first = false;
127 
128  ss << op(*it);
129  }
130 
131  return ss.str();
132 }
133 
134 template<typename Container>
135 std::string join(const Container &items, const std::string &sep)
136 {
137  std::ostringstream ss;
138  bool first = true;
139 
140  for (typename Container::const_iterator it = std::begin(items);
141  it != std::end(items); ++it) {
142  if (!first)
143  ss << sep;
144  else
145  first = false;
146 
147  ss << *it;
148  }
149 
150  return ss.str();
151 }
152 #else
153 template<typename Container, typename UnaryOp>
154 std::string join(const Container &items, const std::string &sep, UnaryOp op = nullptr);
155 #endif
156 
157 namespace details {
158 
159 class StringSplitter
160 {
161 public:
162  StringSplitter(const std::string &str, const std::string &delim);
163 
164  class iterator
165  {
166  public:
167  iterator(const StringSplitter *ss, std::string::size_type pos);
168 
169  iterator &operator++();
170  std::string operator*() const;
171  bool operator!=(const iterator &other) const;
172 
173  private:
174  const StringSplitter *ss_;
175  std::string::size_type pos_;
176  std::string::size_type next_;
177  };
178 
179  iterator begin() const;
180  iterator end() const;
181 
182 private:
183  std::string str_;
184  std::string delim_;
185 };
186 
187 } /* namespace details */
188 
189 details::StringSplitter split(const std::string &str, const std::string &delim);
190 
191 std::string toAscii(const std::string &str);
192 
193 std::string libcameraBuildPath();
194 std::string libcameraSourcePath();
195 
196 constexpr unsigned int alignDown(unsigned int value, unsigned int alignment)
197 {
198  return value / alignment * alignment;
199 }
200 
201 constexpr unsigned int alignUp(unsigned int value, unsigned int alignment)
202 {
203  return (value + alignment - 1) / alignment * alignment;
204 }
205 
206 namespace details {
207 
208 template<typename T>
209 struct reverse_adapter {
210  T &iterable;
211 };
212 
213 template<typename T>
214 auto begin(reverse_adapter<T> r)
215 {
216  return std::rbegin(r.iterable);
217 }
218 
219 template<typename T>
220 auto end(reverse_adapter<T> r)
221 {
222  return std::rend(r.iterable);
223 }
224 
225 } /* namespace details */
226 
227 template<typename T>
228 details::reverse_adapter<T> reverse(T &&iterable)
229 {
230  return { iterable };
231 }
232 
233 } /* namespace utils */
234 
235 } /* namespace libcamera */
236 
237 #endif /* __LIBCAMERA_INTERNAL_UTILS_H__ */
std::string libcameraSourcePath()
Retrieve the path to the source directory.
Definition: utils.cpp:428
const char * basename(const char *path)
Strip the directory prefix from the path.
Definition: utils.cpp:45
struct timespec duration_to_timespec(const duration &value)
Convert a duration to a timespec.
Definition: utils.cpp:164
Transform operator*(Transform t0, Transform t1)
Compose two transforms together.
Definition: transform.cpp:207
Top-level libcamera namespace.
Definition: bound_method.h:15
std::chrono::steady_clock clock
The libcamera clock (monotonic)
Definition: utils.h:66
std::vector< typename T::key_type > map_keys(const T &map)
Retrieve the keys of a std::map<>
Definition: utils.h:39
std::string dirname(const std::string &path)
Identify the dirname portion of a path.
Definition: utils.cpp:86
_hex hex(T value, unsigned int width=0)
Write an hexadecimal value to an output string.
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:48
std::chrono::steady_clock::time_point time_point
The libcamera time point related to libcamera::utils::clock.
Definition: utils.h:68
std::string time_point_to_string(const time_point &time)
Convert a time point to a string representation.
Definition: utils.cpp:178
char * secure_getenv(const char *name)
Get an environment variable.
Definition: utils.cpp:65
std::string libcameraBuildPath()
Retrieve the path to the build directory.
Definition: utils.cpp:388
constexpr unsigned int alignDown(unsigned int value, unsigned int alignment)
Align value down to alignment.
Definition: utils.h:196
std::chrono::steady_clock::duration duration
The libcamera duration related to libcamera::utils::clock.
Definition: utils.h:67
details::StringSplitter split(const std::string &str, const std::string &delim)
Split a string based on a delimiter.
Definition: utils.cpp:326
size_t strlcpy(char *dst, const char *src, size_t size)
Copy a string with a size limit.
Definition: utils.cpp:243
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:201
std::string toAscii(const std::string &str)
Remove any non-ASCII characters from a string.
Definition: utils.cpp:340
details::reverse_adapter< T > reverse(T &&iterable)
Wrap an iterable to reverse iteration in a range-based loop.
Definition: utils.h:228