libcamera
v0.3.2+116-83c5ad0f
Supporting cameras in Linux since 2019
|
Miscellaneous utility functions. More...
#include <algorithm>
#include <chrono>
#include <functional>
#include <iterator>
#include <ostream>
#include <sstream>
#include <string.h>
#include <string>
#include <sys/time.h>
#include <type_traits>
#include <utility>
#include <vector>
#include <libcamera/base/private.h>
Go to the source code of this file.
Classes | |
class | libcamera::utils::Duration |
Helper class from std::chrono::duration that represents a time duration in nanoseconds with double precision. More... | |
class | libcamera::utils::ScopeExitActions |
An object that performs actions upon destruction. More... | |
Namespaces | |
libcamera | |
Top-level libcamera namespace. | |
Typedefs | |
using | libcamera::utils::clock = std::chrono::steady_clock |
The libcamera clock (monotonic) | |
using | libcamera::utils::duration = std::chrono::steady_clock::duration |
The libcamera duration related to libcamera::utils::clock. | |
using | libcamera::utils::time_point = std::chrono::steady_clock::time_point |
The libcamera time point related to libcamera::utils::clock. | |
Functions | |
const char * | libcamera::utils::basename (const char *path) |
Strip the directory prefix from the path. More... | |
char * | libcamera::utils::secure_getenv (const char *name) |
Get an environment variable. More... | |
std::string | libcamera::utils::dirname (const std::string &path) |
Identify the dirname portion of a path. More... | |
template<typename T > | |
std::vector< typename T::key_type > | libcamera::utils::map_keys (const T &map) |
Retrieve the keys of a std::map<> More... | |
template<class InputIt1 , class InputIt2 > | |
unsigned int | libcamera::utils::set_overlap (InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2) |
Count the number of elements in the intersection of two ranges. More... | |
struct timespec | libcamera::utils::duration_to_timespec (const duration &value) |
Convert a duration to a timespec. More... | |
std::string | libcamera::utils::time_point_to_string (const time_point &time) |
Convert a time point to a string representation. More... | |
template<typename T , std::enable_if_t< std::is_integral< T >::value > * = nullptr> | |
_hex | libcamera::utils::hex (T value, unsigned int width=0) |
Write an hexadecimal value to an output string. More... | |
size_t | libcamera::utils::strlcpy (char *dst, const char *src, size_t size) |
Copy a string with a size limit. More... | |
template<typename Container , typename UnaryOp > | |
std::string | libcamera::utils::join (const Container &items, const std::string &sep, UnaryOp op=nullptr) |
Join elements of a container in a string with a separator. More... | |
details::StringSplitter | libcamera::utils::split (const std::string &str, const std::string &delim) |
Split a string based on a delimiter. More... | |
std::string | libcamera::utils::toAscii (const std::string &str) |
Remove any non-ASCII characters from a string. More... | |
std::string | libcamera::utils::libcameraBuildPath () |
Retrieve the path to the build directory. More... | |
std::string | libcamera::utils::libcameraSourcePath () |
Retrieve the path to the source directory. More... | |
constexpr unsigned int | libcamera::utils::alignDown (unsigned int value, unsigned int alignment) |
Align value down to alignment. More... | |
constexpr unsigned int | libcamera::utils::alignUp (unsigned int value, unsigned int alignment) |
Align value up to alignment. More... | |
template<typename T > | |
details::reverse_adapter< T > | libcamera::utils::reverse (T &&iterable) |
Wrap an iterable to reverse iteration in a range-based loop. More... | |
template<typename T > | |
auto | libcamera::utils::enumerate (T &iterable) -> details::enumerate_adapter< decltype(iterable.begin())> |
Wrap an iterable to enumerate index and value in a range-based loop. More... | |
template<typename T > | |
decltype(auto) | libcamera::utils::abs_diff (const T &a, const T &b) |
Calculates the absolute value of the difference between two elements. More... | |
double | libcamera::utils::strtod (const char *__restrict nptr, char **__restrict endptr) |
Convert a string to a double independently of the current locale. More... | |
template<class Enum > | |
constexpr std::underlying_type_t< Enum > | libcamera::utils::to_underlying (Enum e) noexcept |
Convert an enumeration to its underlygin type. More... | |
Miscellaneous utility functions.
libcamera::utils::abs_diff | ( | const T & | a, |
const T & | b | ||
) |
Calculates the absolute value of the difference between two elements.
[in] | a | The first element |
[in] | b | The second element |
This function calculates the absolute value of the difference between two elements of the same type, in such a way that a negative value will never occur during the calculation.
This is inspired by the std::abs_diff() candidate proposed in N4318 (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4318.pdf).
libcamera::utils::alignDown | ( | unsigned int | value, |
unsigned int | alignment | ||
) |
Align value down to alignment.
[in] | value | The value to align |
[in] | alignment | The alignment |
libcamera::utils::alignUp | ( | unsigned int | value, |
unsigned int | alignment | ||
) |
Align value up to alignment.
[in] | value | The value to align |
[in] | alignment | The alignment |
const char * libcamera::utils::basename | ( | const char * | path | ) |
Strip the directory prefix from the path.
[in] | path | The path to process |
basename is implemented differently across different C libraries. This implementation matches the one provided by the GNU libc, and does not modify its input parameter.
std::string libcamera::utils::dirname | ( | const std::string & | path | ) |
Identify the dirname portion of a path.
[in] | path | The full path to parse |
This function conforms with the behaviour of the dirname() function as defined by POSIX.
struct timespec libcamera::utils::duration_to_timespec | ( | const duration & | value | ) |
Convert a duration to a timespec.
[in] | value | The duration |
libcamera::utils::enumerate | ( | T & | iterable | ) | -> details::enumerate_adapter<decltype(iterable.begin())> |
Wrap an iterable to enumerate index and value in a range-based loop.
[in] | iterable | The iterable |
Range-based for loops are handy and widely preferred in C++, but are limited in their ability to replace for loops that require access to a loop counter. The enumerate() function solves this problem by wrapping the iterable in an adapter that, when used as a range-expression, will provide iterators whose value_type is a pair of index and value reference.
The iterable must support std::begin() and std::end(). This includes all containers provided by the standard C++ library, as well as C-style arrays.
A typical usage pattern would use structured binding to store the index and value in two separate variables:
Note that the argument to enumerate() has to be an lvalue, as the lifetime of any rvalue would not be extended to the whole for loop. The compiler will complain if an rvalue is passed to the function, in which case it should be stored in a local variable before the loop.
libcamera::utils::hex | ( | T | value, |
unsigned int | width = 0 |
||
) |
Write an hexadecimal value to an output string.
value | The value |
width | The width |
Return an object of unspecified type such that, if os is the name of an output stream of type std::ostream, and T is an integer type, then the expression
will output the value to the stream in hexadecimal form with the base prefix and the filling character set to '0'. The field width is set to width if specified to a non-zero value, or to the native width of type T otherwise. The os stream configuration is not modified.
template< typename Container, typename UnaryOp > std::string libcamera::utils::join | ( | const Container & | items, |
const std::string & | sep, | ||
UnaryOp | op = nullptr |
||
) |
Join elements of a container in a string with a separator.
[in] | items | The container |
[in] | sep | The separator to add between elements |
[in] | op | A function that converts individual elements to strings |
This function joins all elements in the items container into a string and returns it. The sep separator is added between elements. If the container elements are not implicitly convertible to std::string, the op function shall be provided to perform conversion of elements to std::string.
std::string libcamera::utils::libcameraBuildPath | ( | ) |
Retrieve the path to the build directory.
During development, it is useful to run libcamera binaries directly from the build directory without installing them. This function helps components that need to locate resources in the build tree, such as IPA modules or IPA proxy workers, by providing them with the path to the root of the build directory. Callers can then use it to complement or override searches in system-wide directories.
If libcamera has been installed, the build directory path is not available and this function returns an empty string.
std::string libcamera::utils::libcameraSourcePath | ( | ) |
Retrieve the path to the source directory.
During development, it is useful to run libcamera binaries directly from the build directory without installing them. This function helps components that need to locate resources in the source tree, such as IPA configuration files, by providing them with the path to the root of the source directory. Callers can then use it to complement or override searches in system-wide directories.
If libcamera has been installed, the source directory path is not available and this function returns an empty string.
std::vector< typename T::key_type > libcamera::utils::map_keys | ( | const T & | map | ) |
Retrieve the keys of a std::map<>
[in] | map | The map whose keys to retrieve |
libcamera::utils::reverse | ( | T && | iterable | ) |
Wrap an iterable to reverse iteration in a range-based loop.
[in] | iterable | The iterable |
char * libcamera::utils::secure_getenv | ( | const char * | name | ) |
Get an environment variable.
[in] | name | The name of the variable to return |
The environment list is searched to find the variable 'name', and the corresponding string is returned.
If 'secure execution' is required then this function always returns NULL to avoid vulnerabilities that could occur if set-user-ID or set-group-ID programs accidentally trust the environment.
libcamera::utils::set_overlap | ( | InputIt1 | first1, |
InputIt1 | last1, | ||
InputIt2 | first2, | ||
InputIt2 | last2 | ||
) |
Count the number of elements in the intersection of two ranges.
Count the number of elements in the intersection of the sorted ranges [first1, last1) and [first1, last2). Elements are compared using operator< and the ranges must be sorted with respect to the same.
details::StringSplitter libcamera::utils::split | ( | const std::string & | str, |
const std::string & | delim | ||
) |
Split a string based on a delimiter.
[in] | str | The string to split |
[in] | delim | The delimiter string |
This function splits the string str into substrings based on the delimiter delim. It returns an object of unspecified type that can be used in a range-based for loop and yields the substrings in sequence.
size_t libcamera::utils::strlcpy | ( | char * | dst, |
const char * | src, | ||
size_t | size | ||
) |
Copy a string with a size limit.
[in] | dst | The destination string |
[in] | src | The source string |
[in] | size | The size of the destination string |
This function copies the null-terminated string src to dst with a limit of size - 1 characters, and null-terminates the result if size is larger than 0. If src is larger than size - 1, dst is truncated.
double libcamera::utils::strtod | ( | const char *__restrict | nptr, |
char **__restrict | endptr | ||
) |
Convert a string to a double independently of the current locale.
[in] | nptr | The string to convert |
[out] | endptr | Pointer to trailing portion of the string after conversion |
This function is a locale-independent version of the std::strtod() function. It behaves as the standard function, but uses the "C" locale instead of the current locale.
std::string libcamera::utils::time_point_to_string | ( | const time_point & | time | ) |
Convert a time point to a string representation.
[in] | time | The time point |
|
noexcept |
Convert an enumeration to its underlygin type.
[in] | e | Enumeration value to convert |
This function is equivalent to the C++23 std::to_underlying().
std::string libcamera::utils::toAscii | ( | const std::string & | str | ) |
Remove any non-ASCII characters from a string.
[in] | str | The string to strip |
Remove all non-ASCII characters from a string.